var home			=	{
	init						:	function(){
		home.createSlides();
	},
	loaded					:	new Array(),
	enablebuttons		:	false,
	slidesLinks			:	function(){
		jQuery('.launches .row').click(function(event){
			event.preventDefault();
			var destination = jQuery(this).find('.launch .btn:first').attr('href');
			all.routeTo(destination);
		});
	},
	initAnimations		:	function(){
		jQuery('.launches .row1 .launch .launch-content').animate({
			left	:	'0%'
		}, 1000, all.alternative_easing,function(){
			jQuery('.launches .row2 .launch .launch-content').animate({
				left	:	'0%'
			}, 1000, all.alternative_easing,function(){
				//abilito anim button ENTRA solo dopo fine animazione
				//di entrata del blocco padre
				home.enablebuttons = true;
			});
		});
		var anim = null;
		if(jQuery.browser.webkit){
			anim = {
				left	:	'50%',
				'margin-left': '-'+(parseInt(jQuery('.launches .row .launch .slides img').width())/2)+'px'
			};
		}
		else{
			anim = {
				left	:	'0'
			};
		}
		window.setTimeout(function(){
			jQuery('.launches .row1 .launch .slides').animate(anim, 1000, all.alternative_easing,function(){
				jQuery('.launches .row2 .launch .slides').animate(anim, 1000, all.alternative_easing,function(){
					//animazioni dopo entrata del lancio
				});
			});
		}, 200);
	},
	createSlides		:	function(){
		jQuery('.launches .row .launch .slides').each(function(){
			var elem = this;
			if(jQuery(elem).attr('data-images') != ''){
				var str = jQuery(elem).attr('data-images');
				while (str != (str = str.replace("'", '"')));
				var jso = jQuery.parseJSON(str);
				if( home.loaded.length <= jso.length ){
					jQuery(elem).parent().append('<div class="spin">');
				}
				var i = 1;
				jQuery.each(jso, function(k,v){
					var nimg = jQuery('<img src="'+v+'" height="100%" />');
					jQuery(nimg).bind('load',function(){
						if(i >= jso.length && jQuery(elem).parent().hasClass('launch4')){
							jQuery('.launches .row .launch .spin').remove();
							home.startSlideShow();
							home.slidesLinks();
							window.setTimeout(home.initAnimations, 1000);
						}
						i++;
					});

					jQuery(elem).append(nimg);
				});
			}
		});
	},
	startSlideShow	: function(){


		var opt = null;

		if(jQuery.browser.webkit){
			opt = {
				cleartype:     !jQuery.support.opacity,  // true if clearType corrections should be applied (for IE)
				cleartypeNoBg: true, // set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
				fx:      'custom',
				sync: 1,
				cssBefore: {
					left: '-800px',
					display: 'block'
				},
				animIn:  {
					left: 0
				},
				animOut: {
					left: '800px'
				},
				delay: 0,
				easing: all.alternative_easing,
				timeout: (all.isOtherDevice()) ? 3500 : 2700
			};
		}
		else if(jQuery.browser.msie){
			opt = {
				cleartype:     !jQuery.support.opacity,  // true if clearType corrections should be applied (for IE)
				cleartypeNoBg: true, // set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
				fx:      'custom',
				sync: 1,
				cssFirst:  {
					'left': '50%',
					'margin-left': '-'+(parseInt(jQuery('.launches .row .launch .slides img').width())/2)+'px',
					'display': 'block'
				},
				cssBefore: {
					'left': '-1200px',
					'margin-left': '0',
					'display': 'block'
				},
				cssAfter: {
					'left': '1200px',
					'margin-left': '0'
				},
				animIn:  {
					'left': '50%',
					'margin-left': '-'+(parseInt(jQuery('.launches .row .launch .slides img').width())/2)+'px'
				},
				animOut: {
					'left': '1200px',
					'margin-left': '0'
				},
				delay: -1000,
				easing: all.alternative_easing,
				timeout: 2700
			};
		}
		else{
			opt = {
				cleartype:     !jQuery.support.opacity,  // true if clearType corrections should be applied (for IE)
				cleartypeNoBg: true, // set to true to disable extra cleartype fixing (leave false to force background color setting on slides)
				fx:      'custom',
				sync: 1,
				cssFirst:  {
					'left': '50%',
					'margin-left': '-'+(parseInt(jQuery('.launches .row .launch .slides img').width())/2)+'px',
					'display': 'block'
				},
				cssBefore: {
					'left': '-1200px',
					'display': 'block',
					'margin-left': '0'
				},
				cssAfter: {
					'left': '1200px',
					'display': 'block',
					'margin-left': '0'
				},
				animIn:  {
					'left': '50%',
					'display': 'block',
					'margin-left': '-'+(parseInt(jQuery('.launches .row .launch .slides img').width())/2)+'px'
				},
				animOut: {
					'left': '1200px',
					'display': 'block',
					'margin-left': '0'
				},
				delay: -1000,
				easing: all.alternative_easing,
				timeout: 2700
			};
		}
		jQuery('.launches .row .launch .btn-wrapper').css({
			opacity	:	1,
			display	:	'none'
		});

		if(all.isOtherDevice()){
			jQuery('.launches .row1 .launch .slides').cycle(opt).cycle('resume');
			window.setTimeout(function(){
				jQuery('.launches .row2 .launch .slides').cycle(opt).cycle('resume');
			}, 1000);
		}
		else jQuery('.launches .row .launch .slides').cycle(opt).cycle('pause');

		jQuery('.launches .row').mouseenter(function(){
			if(!all.isOtherDevice()) jQuery(this).find('.launch .slides').cycle('resume');//.cycle('next');
			if(home.enablebuttons){
				jQuery(this).find('.launch .btn-wrapper').css('display','block')
					.animate({
						opacity	:	1,
						marginTop	:	-5
					}, 200, all.alternative_easing);
				jQuery(this).find('.launch .link')
					.animate({paddingTop	:	40}, 200, all.alternative_easing);
				jQuery(this).find('.launch .info')
					.animate({top	:	-10}, 200, all.alternative_easing);
				jQuery(this).find('.launch .launch-content')
					.animate({marginTop	:	'-55px'}, 200, all.alternative_easing);
			}
		}).mouseleave(function(){
			if(home.enablebuttons){
				jQuery(this).find('.launch .btn-wrapper')
					.stop()
					.animate({
						opacity	:	0,
						marginTop	:	0
					}, 200,function(){
						jQuery(this).css({display	:	'none',opacity:1});
					});
				jQuery(this).find('.launch .link')
					.stop()
					.animate({paddingTop	:	20}, 200, all.alternative_easing);
				jQuery(this).find('.launch .info')
					.stop()
					.animate({top	:	0}, 200, all.alternative_easing);
				jQuery(this).find('.launch .launch-content')
					.stop()
					.animate({marginTop	:	'-45px'}, 200, all.alternative_easing);
			}
			if(!all.isOtherDevice()) jQuery(this).find('.launch .slides').cycle('pause');
		});
	}
};
jQuery(window).load(function(){
	home.init();
});
