var speed_menu = 300, js_speed = 200;
var chart, chart2, pos;
var current_tweet = 0;
var tweets = new Array();

function ask(message) {
	if (message==undefined) 
		message = "sicuro?"; 

	var agree=confirm(message); 
	if (agree) return true; else return false;
}

if ($('#chart1').length > 0) {
	Highcharts.setOptions({
	   global: {
		  useUTC: false
	   }
	});
}

function gotoAnchor(elementAnchor) {
	if (elementAnchor==undefined) elementAnchor="sTop";
	if ($('#'+elementAnchor).length > 0) {
		var destination = $("#"+elementAnchor).offset().top; 
		$("html:not(:animated),body:not(:animated)").animate({ 
			scrollTop:destination-150
		}, 1000 );
	}
}
function inside() {
	$('#inside_content').slideToggle(500, function() {
		
	});
}


$(document).ready(function(){

	setTimeout(function(){
		$('.err, .msg').slideUp(js_speed);
	}, 5000);
	
	
		
	if ($('#slider_list').length > 0) {
		$("#slider_list").sortable({
			opacity:0.5,
			axis: "y",
			update:  function(event,ui){
				var  itemOrder = $('#slider_list').sortable('serialize');
				$.post(URL_ADMIN + "order-slider",  itemOrder, function() {
				
				});
			}
		});
	}

	if ($('#portfolio_list').length > 0) {
		$("#portfolio_list").sortable({
			opacity:0.5,
			update:  function(event,ui){
				var  itemOrder = $('#portfolio_list').sortable('serialize');
				$.post(URL_ADMIN + "order-portfolio",  itemOrder, function(){
					var i = 0;
					$("#portfolio_list").find('li').each(function(k,v){
						$(v).attr('id', 'item_'+i);
						i++;
					});
				});
			}
		});
	}
		
	//Twitter ticker
	var obj_twitter = $('#last_twit').find('span');
	setInterval(function(){
        obj_twitter.fadeOut(js_speed,function(){
			current_tweet++;
			if (current_tweet == tweets.length) current_tweet = 0;
			$(this).html(tweets[current_tweet]).fadeIn(js_speed);
        });
    }, 5000);
	
	// slider
	if ($('#slides').length > 0) {
		$('#slides').slides({
			preload: true,
			preloadImage: 'img/loading.gif',
			play: 5000,
			pause: 1500,
			hoverPause: true,
			generateNextPrev: false
		});
	}
	
	// bottoni
	$('.button').click(function(){
		$('.button_selected').slideUp(speed_menu,function(){
			$('.button_selected').removeClass('button_selected').slideDown(speed_menu);
		});
		$(this).slideUp(speed_menu,function(){
			$('#parent').remove();
			$(this).find('span').before('<span id="parent">'+$(this).attr('parent')+'</span>');
			$(this).addClass('button_selected').slideDown(speed_menu, function() { 
				gotoAnchor($(this).attr('anchor'));
			});
		});
	});
	
	// apertura home
	if ($('#inside').length > 0) {
		$('#inside').slideDown(500, function(){
				$('#menu_home').fadeIn(500, function(){
				
				});
				$('#services').slideDown(500);
				$('.box_1').fadeIn(500, function(){
					$('.box_2').fadeIn(500, function(){
						$('.box_3').fadeIn(800, function() {

							$('.box_11').fadeIn(200, function() {
							$('.box_12').fadeIn(200, function() {
							$('.box_13').fadeIn(200, function() {

							$('.box_14').fadeIn(200, function() {
							$('.box_15').fadeIn(200, function() {
							$('.box_16').fadeIn(200, function() {
							});});});});});});

							$('#stats').slideDown(500, function(){
							});
						
						});
					});
				});

				
		});
	}
	
	if ($('#chart1').length > 0)  {
		// Chart 1
		chart = new Highcharts.Chart({
			chart: {
				renderTo: 'chart1',
				defaultSeriesType: 'areaspline'
			},
			title: { text: 'Visite ai progetti della Surrealist' },
			legend: {
				layout: 'vertical',
				align: 'left',
				verticalAlign: 'top',
				x: 150,
				y: 100,
				floating: true,
				borderWidth: 1,
				backgroundColor: '#FFFFFF'
			},
			xAxis: {
				title: { text: 'ultimi 10 giorni' },
				categories: [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
				
			},
			yAxis: {
				title: { text: 'visite' }
			},
			plotOptions: {
				areaspline: {
					fillOpacity: 0.1
				}
			},
			tooltip: {
				formatter: function() {
					return 'visite in questo giorno';
				}
			},
			series: [{
				name: 'surrealist.it',
				data: access_surrealist
			}, {
				name: 'nonconvenzionale.com',
				data: access_nc
			}]
		});

		// chart 2
		chart = new Highcharts.Chart({
			chart: {
				renderTo: 'chart2',
				defaultSeriesType: 'pie'
			},
			title: { text: 'Skills' },
			tooltip: {
				formatter: function() {
					return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
				}
			},
			series: [{
				name: 'Skills',
				data: [
					['adv',   skills[0]],
					['mkt',   skills[1]],
					['web',   skills[2]],
					['unc',   skills[3]],
					['scm',   skills[4]],
					['grp',   skills[5]]
				]
			}]
		});

	}
	
});
