/*
* Author: Antonio J. Redondo:  esMeta
*/

// Speed of the automatic slideshow
var slideshowSpeed = 6000;

// Variable to store the images we need to set as background
// which also includes some text and url's.
var photos = [ {
		"image" : "image1.jpg"
	}, {
		
		"image" : "image2.jpg"
	}, {
		
		"image" : "image3.jpg"
	}, {
		"image" : "image4.jpg"
	}
];

jQuery.preloadImages = function(){
  for(var i = 0; i<arguments.length; i++){
    jQuery("<img>").attr("src", arguments[i]);
  }
}
// Para utilizar el script y cargar tus imágenes:
$.preloadImages("r_media/r_gallery/image1.jpg", "r_media/r_gallery/image2.jpg", "r_media/r_gallery/image3.jpg", "r_media/r_gallery/image4.jpg");





$(document).ready(function() {
		
	// Backwards navigation
	$(function()
			{
				$('.scroll-pane').jScrollPane(
					{
						verticalDragMinHeight: 20,
						verticalDragMaxHeight: 20,
						horizontalDragMinWidth: 20,
						horizontalDragMaxWidth: 20
					}
				);
			});
	var modal=0;
	
	$(".numbers").find("img").stop().css({opacity: 0.4});
	
	$("#menu ul li a img").mouseenter(function() {  
	if(!$(this).hasClass("active")){
        imgName=$(this).attr("src").replace("ff.png","n.png");
        $(this).attr("src",imgName);
		} 
    });
	$("#menu ul li a img").mouseout(function() {
		if(!$(this).hasClass("active")){
			imgName=$(this).attr("src").replace("n.png","ff.png");
			$(this).attr("src",imgName);
		}
	});
	
	
	
	$("#explain").hide();
	$("#explain2").hide();
	$("#explain3").hide();
	$("#data").hide();	
	
	
	$(".submit a").click(function(event){	
		$("#data").fadeIn("slow");
	});
	$("#go-zip").click(function(event){	
		$("#data").fadeIn("slow");
	});
	
	$("#state").click(function(event){
		$("#data").fadeOut("slow");
	});
	
	
	
	$("#act").click(function(event){		 
		  event.preventDefault();
		  resetStoreLocator();
		  resetContactForm();
		  deactivelink();
		  modal=1;
		  desactivarMenu();
		  $(this).find("img").addClass("active");
		  imgName=$(this).find("img").attr("src").replace("ff.png","n.png");
	      $(this).find("img").attr("src",imgName);
		  $("#explain2").fadeOut("fast");
		  $("#explain3").fadeOut("fast");
		  $("#explain").delay(1000).fadeIn("slow");	
		 	navigate(1);
			numberout();
			numberin(1);
		
   });
   
   
   $("#close").click(function(event){
    event.preventDefault();
	 deactivelink();		
	 modal=0;
	$("#explain").fadeOut("slow");
   });
	
	
	$("#act2").click(function(event){
		event.preventDefault();	
		resetStoreLocator();
		resetContactForm();
		 deactivelink();
		 modal=1;
		desactivarMenu();
		  $(this).find("img").addClass("active");
		imgName=$(this).find("img").attr("src").replace("ff.png","n.png");
	    $(this).find("img").attr("src",imgName);
		$("#explain").fadeOut("fast");
		$("#explain3").fadeOut("fast");
		$("#explain2").delay(1000).fadeIn("slow");
			navigate(2);	
			numberout();
			numberin(2);
		
   });
   
   $("#close2").click(function(event){
    event.preventDefault();	
	 deactivelink();
	 modal=0;
	$("#explain2").fadeOut("slow");	
	
	
   });
   
   
   
   
   
   $("#act3").click(function(event){
	 event.preventDefault();
	 resetStoreLocator();
	 resetContactForm();
	  $("#explain3").fadeOut("fast");
	  $("#option1").removeAttr("selected");
	  $("#option0").attr("selected", "selected");
	  deactivelink();
	 desactivarMenu();
	  modal=1;
	  $(this).find("img").addClass("active");
	  imgName=$(this).find("img").attr("src").replace("ff.png","n.png");
      $(this).find("img").attr("src",imgName);
	 	$("#explain").fadeOut("fast");
	 	$("#explain2").fadeOut("fast");
		$("#explain3").delay(1000).fadeIn("slow");
		navigate(4);
		numberout();
		numberin(4);
   });
   
   $("#close3").click(function(event){
    event.preventDefault();
	 deactivelink();
	  modal=0;
	$("#explain3").fadeOut("slow");
   });
   
   
   $("#act4").click(function(event){
		 event.preventDefault();
		 resetStoreLocator();
		 $("#txt_YourName").attr("value", "Your Name");
		 $("#txt_YourEmail").attr("value", "Your Email");
		 $("#txtarea_Message").attr("value", "");
		 $("#btn").html("<input type='button' onclick='Contact.validateFormContact();' value='Send'>");
//		 resetContactForm();
		  $("#explain3").fadeOut("fast");
		  deactivelink();
		 desactivarMenu();
		  modal=1;
		  $(this).find("img").addClass("active");
		  imgName=$(this).find("img").attr("src").replace("ff.png","n.png");
	      $(this).find("img").attr("src",imgName);
		 	$("#explain").fadeOut("fast");
		 	$("#explain2").fadeOut("fast");
			$("#explain3").delay(1000).fadeIn("slow");
			navigate(3);
			numberout();
			numberin(3);
//			$("#sel_selectcontact").html("<option value='lookbook@annecole.com'>lookbook@annecole.com</option>");
			$("#option1").attr("selected", "selected");
	   });
	   
	   $("#close4").click(function(event){
	    event.preventDefault();
		 modal=0;
		 deactivelink();
		$("#explain3").fadeOut("slow");
	   });
	
	
	
	
	
	$(".contact-info #more").live('click', function(event){
				
				$('.contact-form').fadeOut("slow");
				$('.contact-form').css({"top" : "800"});
				
				setTimeout (function () {				
					$('#info').load("contactUs.php", function(){
					$("#info .fff").scrollbar();
					}).animate({ opacity: 1});
					
				}, 1000);
			
				//$('.contact-form').hide();
				
		});
			
	
		
		$(".numbers #img1 a").bind("click", function(event){
			
		event.preventDefault();
		numberout();
  		numberin(1);
		navigate(1);
		
		});
		
		
	$(".numbers #img2 a").bind("click", function(event){
		event.preventDefault();
		numberout();
  		numberin(2);
		navigate(2);
	
		});
		
		
		$(".numbers #img3 a").bind("click", function(event){
		event.preventDefault();
		numberout();
  		numberin(3);
		navigate(3);
		
		});
		
		$(".numbers #img4 a").bind("click", function(event){
		event.preventDefault();		
		numberout();
  		numberin(4);
		navigate(4);
		
		});
		
		var numberin = function(num) {
			
		$("#img" + num  +" img").animate({opacity:1}, "slow");
			
		}
		
		var numberout = function() {
			
		$("#img" + "1"  +" img").animate({opacity:0.4}, "slow");
		$("#img" + "2"  +" img").animate({opacity:0.4}, "slow");
		$("#img" + "3"  +" img").animate({opacity:0.4}, "slow");
		$("#img" + "4"  +" img").animate({opacity:0.4}, "slow");
		
			
			}
		
		
		
		
			
	var activeContainer = 1;	
	var currentImg = 0;
	var prev;
	var animating = false;
		
	var navigate = function(direction) {
		// Check if no animation is running. If it is, prevent the action
		if(animating) {
			return;
		}
		
		// Check which current image we need to show
		currentImg = direction;	
		
		// Check which container we need to use
		var currentContainer = activeContainer;
		if(activeContainer == 1) {
			activeContainer = 2;
		} else {
			activeContainer = 1;
		}
		
		showImage(photos[currentImg - 1], currentContainer, activeContainer);
		
	};
	
	var currentZindex = -1;
	var showImage = function(photoObject, currentContainer, activeContainer) {
		animating = true;
		
		// Make sure the new container is always on the background
		currentZindex--;
		
		// Set the background image of the new active container
		$("#headerimg" + activeContainer).css({
			"background-image" : "url(r_media/r_gallery/" + photoObject.image + ")",
			"display" : "block",
			"z-index" : currentZindex
		});
		
		
		// Fade out the current container
		// and display the header text when animation is complete
		$("#headerimg" + currentContainer).fadeOut(function() {
			setTimeout(function() {
				$("#headertxt").css({"display" : "block"});
				animating = false;
			}, 500);
		});
	};
	
	
	// We should statically set the first image and number
	navigate(1);
	$("#img" + "1" +" img").animate({opacity:1}, "slow");
	
	function desactivarMenu(){		
		imgName=$("#act img").attr("src").replace("n.png","ff.png");
		$("#act img").attr("src",imgName);
		
		imgName=$("#act2 img").attr("src").replace("n.png","ff.png");
		$("#act2 img").attr("src",imgName);
		
		imgName=$("#act3 img").attr("src").replace("n.png","ff.png");
		$("#act3 img").attr("src",imgName);
		
	}

		
	function deactivelink(){
		imgName1=$("#act img").attr("src").replace("n.png","ff.png");
		imgName2=$("#act2 img").attr("src").replace("n.png","ff.png");
		imgName3=$("#act3 img").attr("src").replace("n.png","ff.png");
		imgName4=$("#act4 img").attr("src").replace("n.png","ff.png");		
		
      	$("#act").find("img").attr("src",imgName1);
		$("#act2").find("img").attr("src",imgName2);
		$("#act3").find("img").attr("src",imgName3);
		$("#act4").find("img").attr("src",imgName4);
		
		$("#act").find("img").removeClass("active");
		$("#act2").find("img").removeClass("active");
		$("#act3").find("img").removeClass("active");
		$("#act4").find("img").removeClass("active");
		
		}
	
	function resetStoreLocator(){
		setTimeout (function () {	

			$("#option0_state").attr("selected", "selected");
			$("#option0_city").attr("selected", "selected");
			$("#data").hide();
			$("#zip").attr("value", "ZIP CODE");
				
		}, 2000);
	}
	
	function resetContactForm(){
		setTimeout (function () {	
			  
			$("#txt_YourName").attr("value", "Your Name");
			$("#txt_YourEmail").attr("value", "Your Email");
			$("#option0").attr("selected", "selected");
			$("#txtarea_Message").attr("value", "");
			$("#btn").html("<input type='button' onclick='Contact.validateFormContact();' value='Send'>");
				
		}, 1500);			
	}
	
});
