var slideIdx = 0;
var totalSlides = 0;
var slideShowTimer;
// begin init
$(document).ready(function(){  
 
 $('#open-popup').click(function() {
   showSendToAFriend();
   return false;
  }); 
 
 $('#close-popup').click(function() {
   hideSendToAFriend();
   return false;
  });  
  
  $('#closelnk1').click(function() {
	$('.sign-up-popup').css('display','none');	 
	return false;
  });
  
  $('#closelnk2').click(function() {
	$('.sign-up-popup').css('display','none');			        	
	return false;
  }); 
 
  // binds submit event to the send to a friend form
  $('#emailFriendForm').ajaxForm( { beforeSubmit: validateSendToAFriend, url: '/promos/Spanish/_sendToAFriend.cfm', type: 'post', clearForm: true, success: completeSendToAFriend } );

  // binds submit event to the email signup form
  $('#emailSignUpForm').ajaxForm( { beforeSubmit: validateEmailSignUp, url: '/actions/_emailSignUp.cfm', type: 'post', clearForm: true, success: completeEmailSignUp } );
  
  // binds prev slide function
  $('#slide-prev').click(function() {
    prevSlide();
	return false;
  });
  // binds next slide function
  $('#slide-next').click(function() {
    nextSlide();
	return false;				        	
  });  
 
  // binds the play/pause togggle
  $('#slide-go').toggle(
    function () {
     stopSlideShow();			 
    },
    function () {
   	 startSlideShow(); 		
    }
  );  
  
  // sets the total slides found for the slideshow
  totalSlides = $('.item').length;
  
  // Display first image in the slide show when the page loads
  $('.item').get(0).style.visibility='visible';						
  
  // starts the slide show
  startSlideShow();
	
});
// end init

// General functions used by both forms
function isValidEmail(inputObj) {
 if(inputObj.value.indexOf('@') == -1 || inputObj.value.indexOf('.') == -1) {
  return false;
 }
 else {
  var tmp1 = inputObj.value.split("@");
  var tmp2 = tmp1[1].split(".");   
  if(tmp1[0].length < 1 || tmp2[0].length < 2 || tmp2[1].length < 2) {
   return false;
  }   
 }
 return true;
}
// End General functions used by both forms

// Email Sign up functions
function validateEmailSignUp() {
 clearErrors();
 if(!isValidEmail(document.getElementById('emailAddr'))) {
  displayErrors();  
  return false;
 }      
 return true; 
}

function completeEmailSignUp(str) {  
 //document.write(str); 
 $('.sign-up-popup').css('display','block');
 $('#emailAddr').val('Enter Your Email Address'); 
}

function clearErrors() {
 document.getElementById('errorContainer2').style.display="none";
}

function displayErrors() {
 document.getElementById('errorContainer2').style.display="block";
}
// End Email Sign up functions


// Send To A Friend Functions
function validateSendToAFriend() {
 clearSendToAFriendErrors()	
 var isValid = true;
 if($('#toName').val() == "") {	
  $('#toName').css("border","1px solid red");
  isValid = false;
 }
 if($('#fromName').val() == "") {
  $('#fromName').css("border","1px solid red");
  isValid = false;
 }
 if(!isValidEmail(document.getElementById('fromEmail'))) {
  $('#fromEmail').css("border","1px solid red");
  isValid = false;	 
 } 
 if(!isValidEmail(document.getElementById('toEmail'))) {
  $('#toEmail').css("border","1px solid red");
  isValid = false;
 }
 return isValid; 	
}

function completeSendToAFriend(str) {
 $('#popup-shadow').css('display','none');
 $('#popup-friend').css('display','none');
 document.getElementById('emailFriendForm').reset();
}

function clearSendToAFriendErrors() {
 $('#toName').css("border","1px solid #9b9f97");
 $('#fromName').css("border","1px solid #9b9f97");
 $('#fromEmail').css("border","1px solid #9b9f97");
 $('#toEmail').css("border","1px solid #9b9f97");
}

function hideSendToAFriend() {
 $('#popup-shadow').css('display','none');
 $('#popup-friend').css('display','none');
 document.getElementById('emailFriendForm').reset();	
}

function showSendToAFriend() { 
 document.getElementById("popup-shadow").style.height = document.getElementById("main").offsetHeight + "px";	
 $('#popup-shadow').css('display','block');
 $('#popup-friend').css('display','block');
 document.getElementById('emailFriendForm').reset();	
}

function completeSendToAFriend() {
 hideSendToAFriend();
 alert("Your email has been sent.");
}
// End Send To A Friend Functions

// Start slide show functions
function prevSlide() {
 if(slideIdx == 0) {
  slideIdx = totalSlides-1;
 }
 else {
  slideIdx--;
 } 
 $('.item').css('visibility','hidden');
 $('.item').get(slideIdx).style.visibility='visible';
}

function nextSlide() {
 if(slideIdx == (totalSlides-1)) {
  slideIdx = 0;
 }
 else {
  slideIdx++;
 }	
 $('.item').css('visibility','hidden');
 $('.item').get(slideIdx).style.visibility='visible'; 
}

function startSlideShow() {
 slideShowTimer = setInterval("nextSlide()",2000);	
}

function stopSlideShow() {
 slideShowTimer = clearTimeout(slideShowTimer);	
}
// End slide show functions

