var extapi = "";
var c_pos_ant = 0;

jQuery(document).ready(function() {
	
	/* external link */		
	jQuery("a[rel='blank']").click(function(){
		window.open(jQuery(this).attr('href'));
		return false;
	});

	

	jQuery(window).load(function () {

		//numero items
	    //var itemCount= jQuery(".bigBanner .scrollable .item").length;
		var itemCount= jQuery(".bigBanner .scrollable .items").children("div[class*='bg_color_']").length;
		
		
		
		//inserimento div  per il  bg	
		var divsToAppend="";
		
		for(var i=0; i<itemCount;i++){
		  
			
			//var sliderItem = jQuery(".bigBanner .scrollable .item")[i];
			var sliderItem = jQuery(".bigBanner .scrollable .items").children("div[class*='bg_color_']")[i];
			
			
			
			
			//recupero  colore di  sfondo  da nome classe
			var sliderItemClass=jQuery(sliderItem).attr('class');
			var sliceIndex=sliderItemClass.indexOf("bg_color_")+9;
			var tempSubstring = sliderItemClass.slice(sliceIndex);
			var bgColor=tempSubstring.slice(0,6);
			

			
			var display = i==0?"block":"none";
			var zindex= -1-i; 
			zindex = zindex.toString();
			var counterName= i<10?"0"+i:i.toString(); 
			divsToAppend+= '<div class="bg_slide" id="bg_slide'+counterName+'" style="display:'+display+';background-color:#'+bgColor+'; z-index:'+zindex+'" ></div>';
		}
		jQuery('.main .std').prepend(divsToAppend);
		
		
	
		
		var root = jQuery("#slide").scrollable({next: '.next_slide', prev: '.prev_slide', speed: 2500, circular: true, touch: true,
		onBeforeSeek: function(event, i) {
			
			
			
			
			if (i==-1){ 
				c_pos=2;
			} else if (i==itemCount){ 
				c_pos=0;
			} else {
				c_pos=i;
			}
			
			if (c_pos_ant != c_pos)
				jQuery(".bg_slide").fadeOut(3000);
			
			jQuery("#bg_slide0"+c_pos).fadeIn(3000);
			//jQuery("body.cms-home").fadeIn(3000);
			//jQuery("body.cms-home").addClass("bg_slide0"+c_pos);
			//jQuery("body").removeClass("cms-home");
			
			c_pos_ant = c_pos;
			
		}}).autoscroll({ autoplay: false, interval: 8000 });

		var api = root.data("scrollable");
		extapi = root.data("scrollable");

		api.play();		
	
		window.check_next=0;
		if(jQuery(".outfit").length>0){
			if(jQuery(".outfit .items .item").length>2) {
			var horizontal = jQuery(".outfit").scrollable({circular: false, next: '.next_outfit', prev: '.prev_outfit', speed: 2500,
				onSeek: function() {
				if(window.check_next==1){
				//	this.prev(2500);
					window.check_next=0;
				}
				
				if(window.check_next==2){
				//	this.next(2500);
					window.check_next=0;
				}
			//	alert("aa");
			}
			
			}).autoscroll({ interval: 3000 });
			//var horizontal = jQuery(".outfit").scrollable({circular: false, next: '.next_outfit', prev: '.prev_outfit', speed: 2500});
			
			var api2 = horizontal.data("scrollable");
			api2.stop();
			}
			else {
				jQuery(".outfit .next_outfit,.outfit .prev_outfit").hide();
			}
		
		}
		
		jQuery("a.prev_outfit").click(function(){	
			window.check_next=1;
		});
		
		jQuery("a.next_outfit").click(function(){	
			window.check_next=2;
		});
		

		jQuery(".view").click(function(){
			api.stop();	
			jQuery("a.next_slide").animate({left: '200px'}, 500);
			//jQuery(this).parent('.figure').fadeOut("slow");
			jQuery(this).hide();
			jQuery(this).parent('.figure').animate({opacity:0});
			jQuery('.outfit').fadeIn('slow');		
			//api2.play();	
			return false;		
		});

		jQuery("a.prev_slide,a.next_slide").click(function(){	
			api.stop();		
			jQuery("a.next_slide").animate({left: '51px'}, 500);
			//jQuery('#slide .figure').fadeIn("slow");
			jQuery('#slide .figure').animate({opacity:1});
			jQuery('#slide .figure a').show();
			jQuery('.outfit').fadeOut('slow');	
			return false;
		});
		jQuery(".back").click(function(){	
		
			jQuery("a.next_slide").animate({left: '51px'}, 500);
			//jQuery('#slide .figure').fadeIn("slow");
			jQuery('#slide .figure').animate({opacity:1});
			jQuery('#slide .figure a').show();
			jQuery('.outfit').fadeOut('slow');	
			api.play();		
			return false;
		});
		jQuery("a.next_outfit,a.prev_outfit").click(function(){	
		//	api2.stop();	
			return false;	
		});
			
			
			
		jQuery("a.share").click(function(){			
			jQuery('.overlay ul.share').toggle(100);
			return false;
		});

		jQuery(".overlay").overlay({
			top: 58,
			fixed: false,
			mask: {

				color: '#000000',
				loadSpeed: 200,
				opacity: 0.7
			},
			api: true,
			onLoad: function() {
				api.stop();	
				jQuery('#scrollwrap').tinyscrollbar({ axis: 'x'});	
			},
			
			onClose: function() {
				jQuery('#player').html('<iframe class="iframe" id="iframe" src="" frameborder="0" allowfullscreen></iframe>');
			}
			
		});

		jQuery(".video").click(function() {
			verVideo();
			jQuery('#iframe').attr('src',jQuery(this).attr('title'));
			return false;
		});	

		
		jQuery("a.shop").hover(
		  function () {

			 jQuery(this).fadeTo('fast', 0, function() {
				jQuery(this).addClass('shop_on').fadeTo(1000, 1);
			});

		  }, 
		  function () {
			 jQuery(this).fadeTo('fast', 0, function() {
				jQuery(this).removeClass('shop_on').fadeTo(1000, 1);
			});
		});


				


	});
	


		

	function verVideo() {
		jQuery(".overlay").overlay().load();
		return false;
	}


	

});

