var $3d_browser = false;
var $ie8 = false;
var stats_timer;
var stats_array = new Array();
var stats_active = 0;
var stats_activated = false;
var $current = 0;
var $url = window.location.href;
if($url.substr($url.length-1,1) != '/'){$url = $url+'/';}

$(function(){
	$3d_browser  	= 	$('body').supports_3D();
	$ie8			=	$('body').is_ie8();
	
	news_interaction();
	
	/*
	array: object , steps in ms , width in elements , interval , animation-width speed , animation-width kind
	*/
	
	stats_array.push(new Array($('#stats_people p.stats_top'),1,'count',50,300,'easeOutQuad'));
	stats_array.push(new Array($('#stats_msg p.stats_top'),1331,'last',10,1000,'easeOutQuad'));
	stats_array.push(new Array($('#stats_push p.stats_top'),81,'first',10,500,'easeOutQuad'));
	stats_array.push(new Array($('#stats_calls p.stats_top'),1331,'all',10,1000,'linear'));
	stats_array.push(new Array($('#stats_flights p.stats_top'),131,'first',10,500,'easeOutQuad'));
	
	$(window).scroll(function () { 
		if(!stats_activated && $('#about_section').position().top-$(window).scrollTop()-$(window).height()+400 < 0){
			stats_activated = true;
			animate_stats(0);
		}
	})
	
	if($3d_browser){
		$.getScript('inc/js/libs/jquery.transit.min.js', function(){canvas_interaction();});
	}else{
		var $fix_content = '<aside id="circle_travel" class="circle_fixed"><figure class="back"></figure><figure class="front"></figure></aside><aside id="circle_cc" class="circle_fixed"><figure class="back"></figure><figure class="front"></figure></aside><aside id="circle_media" class="circle_fixed"><figure class="back"></figure><figure class="front"></figure></aside><section id="slogan"></section>';
		$('#main_canvas *').hide();
		$('#main_canvas').html($fix_content);
		
		if($ie8){canvas_interaction_fixed_ie8();}
		else{canvas_interaction_fixed_other();};
	}
});

function news_interaction(){
	
	if ($("#news_holder > section.news_package").size() == 1){
		switch_state_interactin_news_btn('next','off',true);
		return false;
	}
	
		
	$('#news_holder section.news_package article').each(function(){
		$(this).click(function(){
			window.location = $(this).data('url');
		})
	})
	$('#news_holder').css({width: $("#news_holder > section.news_package").size() * 970 });

	$('#btn_news_next')
	.hover(
		function(){
			if($(this).data('status') != 'active'){return false;}
			$(this).find('.arrow').stop().animate({left: '0px'},200,'easeInOutCubic');
		},
		function(){
			if($(this).data('status') != 'active'){return false;}
			$(this).find('.arrow').stop().animate({left: '13px'},200,'easeInOutCubic');
		}
	)
	.click(
		function(){
			if($(this).data('status') != 'active'){return false;}
			$current = parseFloat($('#news_holder').data('current'));
			$current++;
			
			if ($current >= $("#news_holder > section.news_package").size()){return false;}
			if($('#btn_news_prev').data('status') == 'inactive'){switch_state_interactin_news_btn('prev','on',true)}
			$('#news_holder').data({current: $current})
			$_this = $(this);
			$_this.data({status:'animating'});
			$current_new = $current;
			var $left = (935*-$current);
			$('#news_holder').stop().animate({left: $left},600,'easeInOutCubic',function(){
				if ($current_new+1 < $("#news_holder > section.news_package").size()){
					switch_state_interactin_news_btn('next','on',false);
				}else{
					switch_state_interactin_news_btn('next','off',false);
				}
			});

		}
	)
	
	$('#btn_news_prev')
	.hover(
		function(){
			if($(this).data('status') != 'active'){return false;}
			$(this).find('.arrow').stop().animate({left: '0px'},200,'easeInOutCubic');
		},
		function(){
			if($(this).data('status') != 'active'){return false;}
			$(this).find('.arrow').stop().animate({left: '-13px'},200,'easeInOutCubic');
		}
	)
	.click(
		function(){
			if($(this).data('status') != 'active'){return false;}
			$current = parseFloat($('#news_holder').data('current'));
			$current--;
			
			if ($current < 0){return false;}
			if($('#btn_news_next').data('status') == 'inactive'){switch_state_interactin_news_btn('next','on',true)}
			$('#news_holder').data({'current': $current})
			$_this = $(this);
			$_this.data({status:'animating'});
			$current_new = $current;
			var $left = (935*-$current);
			$('#news_holder').stop().animate({left: $left},600,'easeInOutCubic',function(){
				if ($current_new-1 < 0){
					switch_state_interactin_news_btn('prev','off',false);
				}else{
					switch_state_interactin_news_btn('prev','on',false);
				}
			});

		}
	)
}


function switch_state_interactin_news_btn($btn, $state, $start){
	var $left = 1;
	if ($btn == 'next'){$btn = $('#btn_news_next');}
	else {$btn = $('#btn_news_prev'); $left = -1;}
	if ($state == 'on'){
		$btn.data({status:'active'});
		if ($start){$btn.find('.arrow').stop().animate({left: $left*13+'px'},200,'easeInOutCubic');}
	}else{
		$btn.data({status:'inactive'});
		$btn.find('.arrow').stop().animate({left: $left*30+'px'},200,'easeInOutCubic');
	}
}

function animate_stats($num){
	$obj = stats_array[$num][0];
	$steps = stats_array[$num][1];
	stats_timer = setInterval("count_stats($obj,$steps)",stats_array[$num][3]);
	$length = stats_array[$num][2];
	
	if ($length == 'first'){
		$length = stats_array[$num][0].width();
		stats_array[$num][0].css({width: 'auto'});
		$length_new = stats_array[$num][0].width()-2;
		stats_array[$num][0].css({width: $length});
		stats_array[$num][0].stop().animate({width: $length_new},stats_array[$num][4],stats_array[$num][5]);
	}
	else if ($length == 'last'){
		$length = $obj.parent().find('p.stats_bottom').width()+5;
		stats_array[$num][0].stop().animate({width: $length},stats_array[$num][4],stats_array[$num][5]);
	}
	else if ($length == 'all'){
		if ($ie8){$length = 750;}else{$length = 890;}
		stats_array[$num][0].stop().animate({width: $length},stats_array[$num][4],stats_array[$num][5]);
	}
}



function count_stats($obj,$steps){
	$final = parseFloat($obj.find('em').data('final'));
	$current = $obj.find('em').html().replace('.','');
	$current = $current.replace('.','');
	$current = parseFloat($current);
	if ($current+$steps <= $final){
		$current += $steps;
		$obj.find('em').html(addPeriodsToNumber($current.toString()));
	}else{
		clearInterval(stats_timer);
		stats_active++;
		if (stats_active < stats_array.length){animate_stats(stats_active);}
	}
}



function addPeriodsToNumber(nStr){
  nStr += '';
  x = nStr.split('.');
  x1 = x[0];
  x2 = x.length > 1 ? '.' + x[1] : '';
  var rgx = /(\d+)(\d{3})/;
  while (rgx.test(x1)) {
    x1 = x1.replace(rgx, '$1' + '.' + '$2');
  }
  return x1 + x2;
}

function canvas_interaction(){

	$('#slogan').delay(500).animate({bottom: 0, opacity: 1},500,'easeInOutCubic');

	/* vars */
	var $active_circle = '';
	var $active_animation = false;
	var $mouse = 0;
	
	$(function(){	
		$('section.circle .back').css({opacity: 0, display: 'none'});
		$('section.circle').css({opacity: 0, display: 'block'}).stop().animate({opacity: 1},500,'easeInOutCubic',function(){
			$(this).find('.back').css({opacity: 1, display: 'block'});
			
			/* mouse events */
		
			$('#circle1')
			.hover(function(){if (!$active_animation){hover_circle($(this),'in');}},function(){
				//if(!$active_animation){hover_circle($(this),'out');}
			})
			.click(function(){
			})
			
			$('#circle2')
			.hover(function(){if (!$active_animation){hover_circle($(this),'in');}},function(){
				//if(!$active_animation){hover_circle($(this),'out');}
			})
			.click(function(){
			})
			
			$('#circle3')
			.hover(function(){if (!$active_animation){hover_circle($(this),'in');}},function(){
				//if(!$active_animation){hover_circle($(this),'out');}
			})
			.click(function(){
			})
		});
	})
	
	function body_mousemove(e){
		if($active_circle == '' || $active_animation){return false;}
		
		$mouse = e.pageX - ($active_circle.offset().left + ($active_circle.width()/2));
		$mouseY = e.pageY - ($active_circle.offset().top + ($active_circle.height()/2));
		$activeY = get_yRotation($active_circle);
		$angle = ($mouse < 0) ? -180 : 180;
		
		if($mouse <= 0 && $activeY < 90 && $activeY > -90 ){
			$active_animation = true;
			rotate_and_open_circle($active_circle,$angle,800,'out',false);
		}else if($mouse > 0 && $activeY < 90 && $activeY > -90 ){
			$active_animation = true;
			rotate_and_open_circle($active_circle,$angle,800,'out',false);
		}
		
		if($mouse <= 0 && $activeY < 270 && $activeY > 90){
			rotate_circle($active_circle,180+($mouse/8),100,'out',false);
		}else if($mouse <= 0 && $activeY > -270 && $activeY < -90){
			rotate_circle($active_circle,-180+($mouse/8),100,'out',false);
		}else if($mouse > 0 && $activeY < 270 && $activeY > 90){
			rotate_circle($active_circle,180+($mouse/8),100,'out',false);
		}else if($mouse > 0 && $activeY > -270 && $activeY < -90){
			rotate_circle($active_circle,-180+($mouse/8),100,'out',false);
		}
		
		if($mouse < -145 || $mouse > 145 || $mouseY < -145 || $mouseY > 145){
			hover_circle($active_circle, 'out');
		}
	}
	
	
	
	/* rotation functions */
	
	function rotate_and_open_circle($obj,$deg,$speed,$animation, $active){
		$obj.stop().transition({
	  		rotateY: $deg+'deg'
		},$speed, $animation,function(){
			$active_animation = $active;
			
		});
		
		if($obj.attr('id') == 'circle1'){open_circle_1();}
		else if($obj.attr('id') == 'circle2'){open_circle_2();}
		else if($obj.attr('id') == 'circle3'){open_circle_3();}
	}
	
	function rotate_and_close_circle($obj){
		if($obj.attr('id') == 'circle1'){close_circle_1();}
		else if($obj.attr('id') == 'circle2'){close_circle_2();}
		else if($obj.attr('id') == 'circle3'){close_circle_3();}
	}
	
	
	/* animation functions */
	
	function open_circle_1(){
		$('#carkey').stop().animate({top: '-14px'},600,'easeOutQuart');
		$('#carkey').css({'-webkit-transform': 'translateZ(20px) rotateZ(27deg)'});
		$('#c1_iphone').stop().delay(150).animate({left:'45px'},400,'easeOutQuart');
		$('#c1_key').stop().delay(150).animate({top:'5px', left:'80px', width:'125px', height:'196px'},400,'easeOutQuart');
	}
	
	function open_circle_2(){
		$('#c2_rooster').css({'-webkit-transform': 'translateZ(50px) rotateY(-30deg) skewY(10deg)'});
		$('#c2_dots').css({'-webkit-transform': 'translateZ(20px)'});
		$('#c2_b1').css({'-webkit-transform': 'translateZ(55px)', left: '-43px', opacity: '0'});
		$('#c2_b2').css({'-webkit-transform': 'translateZ(60px)', left: '-15px', opacity: '0'});
		$('#c2_b3').css({'-webkit-transform': 'translateZ(70px)', left: '-65px', opacity: '0'});
		$('#c2_b4').css({'-webkit-transform': 'translateZ(70px)', left: '35px', opacity: '0'});
		$('#c2_b5').css({'-webkit-transform': 'translateZ(50px)', left: '-22px', opacity: '0'});
		$('#c2_b6').css({'-webkit-transform': 'translateZ(60px)', left: '10px', opacity: '0'});
		$('#c2_b7').css({'-webkit-transform': 'translateZ(30px)', left: '100px', top: '70px', opacity: '0'});
		$('#c2_b8').css({'-webkit-transform': 'translateZ(30px)', left: '110px', top: '115px', opacity: '0'});
		$('#c2_b9').css({'-webkit-transform': 'translateZ(30px)', left: '100px', top: '140px', opacity: '0'});
		$('#c2_dots').css({width: '0px'});
			
		
		
		$('#c2_b1').stop().delay(400).animate({left: '7px', opacity: '1'},290,'easeOutQuart');
		$('#c2_b2').stop().delay(420).animate({left: '35px', opacity: '1'},320,'easeOutQuart');
		$('#c2_b3').stop().delay(500).animate({left: '-15px', opacity: '1'},310,'easeOutQuart');
		$('#c2_b4').stop().delay(490).animate({left: '85px', opacity: '1'},290,'easeOutQuart');
		$('#c2_b_shadow').delay(580).animate({opacity: '1'},200);
		$('#c2_b5').stop().delay(500).animate({left: '28px', opacity: '1'},310,'easeOutQuart');
		$('#c2_b6').stop().delay(420).animate({left: '60px', opacity: '1'},300,'easeOutQuart');
		$('#c2_dots').stop().delay(650).animate({width: '88px'},400,'easeOutQuart');
		$('#c2_b7').stop().delay(650).animate({left: '210px', top: '40px', opacity: '1'},400,'easeOutQuart');
		$('#c2_b8').stop().delay(650).animate({left: '170px', top: '100px', opacity: '1'},300,'easeOutQuart');
		$('#c2_b9').stop().delay(650).animate({left: '230px', top: '120px', opacity: '1'},400,'easeOutQuart');
	}
	
	function open_circle_3(){
		$('#c3_arrow2_1').css({width:'0px'});
		$('#c3_arrow2_2').css({width:'0px'});
		$('#c3_arrow2_3').css({width:'0px'});
		$('#c3_remote').css({'-webkit-transform': 'translateZ(30px)'});
		$('#c3_remote').stop().delay(100).animate({top:'12px', left: '60px'},300,'easeOutQuart');
		$('#c3_arrow1').stop().delay(400).animate({width:'166px'},600,'easeOutQuart');
		$('#c3_arrow2_1').stop().delay(400).animate({width:'110px'},200,'easeOutQuart',function(){
			$('#c3_arrow2_2').stop().animate({width:'64px'},200,'easeOutQuart',function(){
				$('#c3_arrow2_3').stop().animate({width:'142px'},300,'easeOutQuart');
				$('#c3_arrow2_1').css({width:'110px'});
			$('#c3_arrow2_2').css({width:'64px'});
			});
		});
		$('#c3_arrow3').stop().delay(400).animate({width:'76px'},800,'easeOutQuart');
	}
	
	
	
	
	function close_circle_1(){
		$('#c1_iphone').stop().delay(150).animate({left:'85px'},400,'easeOutQuart');
		$('#carkey').stop().animate({top: '14px'},600,'easeOutQuart');
		$('#carkey').css({'-webkit-transform': 'translateZ(0px) rotateZ(27deg)'});
		$('#c1_key').stop().animate({top:'30px', left:'80px', width:'100px', height:'156px'},600,'easeOutQuart');
		
		rotate_circle($('#circle1'),0,800,'out',false);
	}
	
	function close_circle_2(){
		$('#c2_rooster').css({'-webkit-transform': 'translateZ(20px) rotateY(-30deg) skewY(10deg)'});
		$('#c2_dots').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b1').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b2').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b3').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b4').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b5').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b6').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b7').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b8').css({'-webkit-transform': 'translateZ(0px)'});
		$('#c2_b9').css({'-webkit-transform': 'translateZ(0px)'});
		
		$('#c2_b1').stop().delay(100).animate({left: '-43px', opacity: '0'},290,'easeOutQuart');
		$('#c2_b2').stop().delay(100).animate({left: '-15px', opacity: '0'},320,'easeOutQuart');
		$('#c2_b3').stop().delay(100).animate({left: '-65px', opacity: '0'},310,'easeOutQuart');
		$('#c2_b4').stop().delay(100).animate({left: '35px', opacity: '0'},290,'easeOutQuart');
		$('#c2_b_shadow').delay(50).animate({opacity: '0'},50);
		$('#c2_b5').stop().delay(100).animate({left: '-22px', opacity: '0'},310,'easeOutQuart');
		$('#c2_b6').stop().delay(100).animate({left: '10px', opacity: '0'},300,'easeOutQuart');
		$('#c2_dots').stop().delay(0).animate({width: '0px'},400,'easeOutQuart');
		$('#c2_b7').stop().delay(0).animate({left: '100px', top: '70px', opacity: '0'},400,'easeOutQuart');
		$('#c2_b8').stop().delay(0).animate({left: '110px', top: '115px', opacity: '0'},400,'easeOutQuart');
		$('#c2_b9').stop().delay(0).animate({left: '100px', top: '140px', opacity: '0'},400,'easeOutQuart');
		
		rotate_circle($('#circle2'),0,600,'out',false);
	}
	
	function close_circle_3(){
		$('#c3_arrow1').stop().animate({width:'0px'},700,'easeOutQuart');
		$('#c3_remote').css({'-webkit-transform': 'translateZ(10px)'});
		$('#c3_remote').stop().animate({top:'-20px', left: '30px'},600,'easeOutQuart',function(){;});
		$('#c3_arrow2_3').stop().animate({width:'0px'},200,'easeOutQuart',function(){
			$('#c3_arrow2_2').stop().animate({width:'0px'},0,'easeOutQuart',function(){
				$('#c3_arrow2_1').stop().animate({width:'0px'},0,'easeOutQuart');
			});
		});
		$('#c3_arrow3').stop().animate({width:'0px'},800,'easeOutQuart');
		
		rotate_circle($('#circle3'),0,600,'out',false);
	}
	
	
	/* global functions */
	
	function get_yRotation($obj){
		return parseFloat($obj.css('rotateY'));
		
	}
	
	function hover_circle($obj,$event){
		if ($event == 'in' && !$active_animation){
			$active_circle = $obj;
			$('body').bind('mousemove', body_mousemove);
		}else if ($event == 'out' && !$active_animation){
			$active_circle = '';
			$('body').unbind('mousemove', body_mousemove);
			rotate_and_close_circle($obj);
		}
	}
	
	function rotate_circle($obj,$deg,$speed,$animation,$active){
		$obj.stop().transition({
	  		rotateY: $deg+'deg'
		},$speed, $animation,function(){
			$active_animation = $active;
		});
	}
	
	$('#circle1').click(function(){
		window.location = $url+'travel';
	})
	
	$('#circle2').click(function(){
		window.location = $url+'customer_care';
	})
	
	$('#circle3').click(function(){
		window.location = $url+'media';
	})
}

function canvas_interaction_fixed_ie8(){
	$.getScript('inc/js/libs/jquery-css-transform.js', function(){
		$.getScript('inc/js/libs/jquery-animate-css-rotate-scale.js', function(){
			$('#slogan').delay(500).animate({bottom: 0, opacity: 1},500,'easeInOutCubic');
			
			$('.circle_fixed').each(function(){
				$(this).stop().animate({opacity: 1},500,'easeInOutCubic')
			})
			
			$('aside.circle_fixed figure.back').css({opacity: '0', left: -20});
			$('aside.circle_fixed').hover(
				function(){
					$(this).find('figure.back').stop().animate({opacity: 1}, 400, 'easeOutQuart');
					$(this).find('figure.front').stop().animate({opacity: 0}, 400, 'easeOutQuart');
				},
				function(){
					$(this).find('figure.back').stop().animate({opacity: 0}, 300, 'easeOutQuart');
					$(this).find('figure.front').stop().animate({opacity: 1}, 300, 'easeOutQuart');
				}
			)	
			
			circle_links_fixed();
		});
	});
}

function canvas_interaction_fixed_other(){
	$.getScript('inc/js/libs/jquery-css-transform.js', function(){
		$.getScript('inc/js/libs/jquery-animate-css-rotate-scale.js', function(){
		
			$('.circle_fixed').each(function(){
				$(this).stop().animate({opacity: 1},500,'easeInOutCubic')
			})
			
			$('#slogan').stop().animate({bottom: 0, opacity: 1},500,'easeInOutCubic');
			$('aside.circle_fixed figure.back').stop().animate({scale: 0.5},0).css({opacity: '0'});
						
			$('aside.circle_fixed').hover(
				function(){
					$(this).find('figure.back').stop().animate({left: -20, scale: 1, opacity: 1}, 150, 'easeOutCubic');
					$(this).find('figure.front').stop().animate({scale: 1.2, opacity: 0}, 200, 'easeOutCubic');
				},
				function(){
					$(this).find('figure.back').stop().animate({left: -20, scale: 0.5, opacity: 0}, 200, 'easeOutCubic');
					$(this).find('figure.front').stop().animate({scale: 1, opacity: 1}, 200, 'easeOutCubic');
				}
			)
			
			circle_links_fixed();
		});
	});
}

function circle_links_fixed(){
	$('#circle_travel').click(function(){
		window.location = $url+'travel';
	})
	
	$('#circle_cc').click(function(){
		window.location = $url+'customer_care';
	})
	
	$('#circle_media').click(function(){
		window.location = $url+'media';
	})
			
}
