// JavaScript Document

// SCRIPT FOR footer newsletter signup.
					
var isSubmitted = false;
	
	function focusField(field){
		var theField = document.getElementById("email");
		if (theField.value == "Enter Your Email Address"){
			theField.value="";
		}
	}
	
	function blurField(field){
		var theField = document.getElementById("email");
		if (theField.value == ""){
			theField.value="Enter Your Email Address";
		} else if (theField.value == "Thank You! We'll keep you posted."){
			theField.value="Thank You! We'll keep you posted.";
		}
	}
					
// email validation script 
function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Please enter a valid email address.")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Please enter a valid email address.")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Please enter a valid email address.")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Please enter a valid email address.")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Please enter a valid email address.")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Please enter a valid email address.")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.getElementById('email');
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	submitFancyEmail();
 }
 
 
function submitFancyEmail() {
	var $submitURL = 'http://www.butterfliesandbikinis.com/thankyou.php?email=';
	var ef = document.getElementById('email').value;
	var eb = document.getElementById('newsletterSubmit');
	eb.href= $submitURL+ef;
    $("#newsletterSubmit").trigger('click');
	
}

// SCRIPT FOR header search field newsletter signup.

function focusSearchField(field){
	var theField = document.getElementById("searchField");
	if (theField.value == "Search..."){
		theField.value="";
	}
	theField.className="searchFocus";
}

function blurSearchField(field){
	var theField = document.getElementById("searchField");
	if (theField.value == ""){
		theField.value="Search...";
	}
	theField.className="searchField";
}

$(document).ready(function() {	
	$('#banner_container2').cycle({ 
		    fx: 'fade',
		    timeout:6000,			speed:1000
		    /*timeoutFn:function(currSlideElement, nextSlideElement, options, forwardFlag) {
			 	set_idx_circle(this.currSlide);
			 	return 8000;
			}*/
	});
	
	//$('#banner_container2').cycle();
	
	$('.circlesBtn img').click(function(obj) {
		var circle_idx = $(this).attr('idx');
		set_idx_circle(circle_idx);
		$('#banner_container').cycle(parseInt(circle_idx));
		return false; 
	});
	
	$('#bannerShortLeft').cycle({ 
		    fx: 'fade',
		    timeout:2000,
			timeoutFn:function(currSlideElement, nextSlideElement, options, forwardFlag) {
			return 2000;
			}
	});
	
	$('#bannerShortRight').cycle({ 
		    fx: 'fade',
			delay: 500,
		    timeout:2000,
		    timeoutFn:function(currSlideElement, nextSlideElement, options, forwardFlag) {
				return 2000;
			}
	});
	
	$('div#featuredItems').cycle({
    fx:     'scrollHorz',
    speed:  'fast',
    timeout: 0,
    next:   'a#next',
    prev:   'a#prev'
	});
	
	$("a#enlarge_image").colorbox({
	maxWidth: "100%",
	maxHeight: "100%",
	scalePhotos: true							  
	});
	
	$("a.enlarge").colorbox({
	maxWidth: "100%",
	maxHeight: "100%",
	scalePhotos: true							  
	}); 
	
	$("a.popup").colorbox({
	maxWidth: "100%",
	maxHeight: "100%",
	scalePhotos: true							  
	}); 
	
	$("a.fittingRoom").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true
	});
	
	$("div#mysizeBtns a").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true,
		onClosed: function() {
			location.reload();
		}
	});
	
	$("li .products a#mysize").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true,
		onClosed: function() {
			location.reload();
		}
	}); 
	
	$("#rack a.mysize").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true,
		onClosed: function() {
			location.reload();
		}
	});
	
	$("#rack a.mySize").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true,
		onClosed: function() {
			window.location = "http://www.butterfliesandbikinis.com/bikinis_rack/?t=&b=";
		}
	});
	
	$("#banner_container a#mysize").colorbox({
		width:"1024", 
		height:"680", 
		iframe:true,
		onClosed: function() {
			location.reload();
		}
	}); 
	
	/*
	$("a.quickview").fancybox({
		'frameHeight': 430, 
		'frameWidth': 600, 
		'hideOnContentClick': false 
	});
	*/
	$('a.quickview').colorbox({
		width:'950px',
		height:'640px',
		iframe: true							  
	});
	
	$("a#newsletterSubmit").fancybox({ 
		'frameHeight': 250, 
		'frameWidth': 416, 
		'hideOnContentClick': false 
	});
	
	$('#searchField').smartSuggest({
		src: 'http://www.butterfliesandbikinis.com/Scripts/assets/php/json.php',
		minChars: 1
	});
	
	$("div.itemDiv ul#colors li").hover( function () {
		var colorToSwap = $(this).attr("class");
		var origImg = $(this).attr("id");								   
		var colorImg = origImg + "-" + colorToSwap + ".jpg";
		var imgToSwap = "#" + $(this).attr("rel");
		$(imgToSwap).attr("src", colorImg);
   	});
	
	$("div#thumbs img").hover( function () {
		var imgToSwap = $(this).attr("rel");								   
		$("img#bikini_main_image").attr("src", imgToSwap);										   
   	});
	
	$("ul#colors li").hover( function () {
		var colorToSwap = $(this).attr("class");
		var origImg = $("img#bikini_main_image").attr("rel");								   
		var colorImg = origImg + "-" + colorToSwap + ".jpg";		
		$("img#bikini_main_image").attr("src", colorImg);							   
		var colorName = $(this).attr("title");	
		$("p#colorShown span").html(colorName);	
		
		var selectValue = "Color: " + $(this).attr("id");
		//alert(selectValue);
		//var colorOptionId = "form#set select#Color #" + colorToSwap;
		
		$("form#set select#Color").val(selectValue);
		//$("form#bottom select#Color").val(selectValue);
		
		if($("a#enlarge_image").attr("rel") == "bikinis_lg") {							   
			var largeImg = origImg + "_xlarge-" + colorToSwap + ".jpg";	
			$("a#enlarge_image").attr("href", largeImg);
		}
   	});
	
	$("form#set select#Color.change").change( function () {
		
		var colorVal = $(this).attr("value");	
		$("p#colorShown span").html(colorVal);	
		$("form#top select#Color").val(colorVal);
		$("form#bottom select#Color").val(colorVal);		
		
		var colorToSwap = $(this).children("option:selected").attr("rel");
		origImg = $("img#bikini_main_image").attr("rel");							   
		
		var colorImg = origImg + "-" + colorToSwap + ".jpg";
		$("img#bikini_main_image").attr("src", colorImg);		
		
		if( $("a#enlarge_image").attr("rel") == "bikinis_lg") {							   
			var largeImg = origImg + "_xlarge-" + colorToSwap + ".jpg";	
			
			$("a#enlarge_image").attr("href", largeImg);
		}		
		
		var topColor = 'form#top select#Color option#' + colorToSwap;
		$(topColor).setAttribute('selected', true);
		var bottomColor = 'form#top select#Color option#' + colorToSwap;
		$(bottomColor).setAttribute('selected', true);
    });
	
	
	$("div.imageDiv img#bikini_main_image").hover( function () {
		var OGImg = $("div.imageDiv img#bikini_main_image").attr("src");
		$("img#bikini_main_image").attr("src", OGImg); 											
	});
	
	$('div#options').cycle({
	  	timeout: 0,	
		speed: 600,
		cleartype: true, 
		cleartypeNoBg: true,
      	fx: 'scrollUp' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
	
	$("ul#optionTabs li#set").click( function() {
    	$('div#options').cycle(0);
	});
	
	$("ul#optionTabs li#top").click( function() {
    	$('div#options').cycle(1);
	});
	
	$("ul#optionTabs li#bottom").click( function() {
    	$('div#options').cycle(2);
	});
	$("select#BottomSize").change( function () {
		var sizeNote = $(this).children("option:selected").attr("rel");
		if (sizeNote != "") { 
			$("form#set #msg").html(sizeNote);
			//alert (sizeNote); 
		}
	});
	
	$("select#TopSize").change( function () {
		var sizeNote = $(this).children("option:selected").attr("rel");
		if (sizeNote != "") { 
			$("form#set #msg").html(sizeNote);
			//alert (sizeNote); 
		}
	});
	
	$("form#set select#Cut").change( function () {
		var originalPrice = Number( $("form#set #originalPrice").attr("value") );
		var priceChange = Number( $(this).children("option:selected").attr("alt") );
		var newPrice = originalPrice + priceChange;
		newPrice = newPrice.toFixed(2);
		$("form#set span#thePrice").html(newPrice);
		$("form#set #formPrice").val(newPrice);
		
		var cutNote = $(this).children("option:selected").attr("rel");
		if (cutNote != "") { 
			$("form#set #msg").html(cutNote);
			//alert (cutNote); 
		}
	});
	
	$("form#set select#TopCut").change( function () {
		var originalPrice = Number( $("form#set #originalPrice").attr("value") );
		var priceChange = Number( $(this).children("option:selected").attr("alt") );
		var newPrice = originalPrice + priceChange;
		newPrice = newPrice.toFixed(2);
		$("form#set span#thePrice").html(newPrice);
		$("form#set #formPrice").val(newPrice);
		
		var cutNote = $(this).children("option:selected").attr("rel");
		if (cutNote != "") { 
			$("form#set #msg").html(cutNote);
			//alert (cutNote); 
		}
	});
	
	$("form#top select#TopCut").change( function () {
		var originalPrice = Number( $("form#top #originalPrice").attr("value") );
		var priceChange = Number( $(this).children("option:selected").attr("alt") );
		var newPrice = originalPrice + priceChange;
		newPrice = newPrice.toFixed(2);
		$("form#top span#thePrice").html(newPrice);
		$("form#top #formPrice").val(newPrice);
	});
	
	$("form#bottom select#Cut").change( function () {
		var originalPrice = Number( $("form#bottom #originalPrice").attr("value") );
		var priceChange = Number( $(this).children("option:selected").attr("alt") );
		var newPrice = originalPrice + priceChange;
		newPrice = newPrice.toFixed(2);
		$("form#bottom span#thePrice").html(newPrice);
		$("form#bottom #formPrice").val(newPrice);
	});
	
	$('div#product_info').cycle({
	  	timeout: 0,	
		speed: 600,
		cleartype: true, 
		cleartypeNoBg: true,
      	fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
	$('ul#product_info_tabs li a').click(function() {
		$(this).addClass('active');
		$(this).parent('li').siblings('li').children('a').removeClass('active');
	});
	$('ul#product_info_tabs li a#related').click(function() {
		$('div#product_info').cycle(0);
	});
	$('ul#product_info_tabs li a#details').click(function() {
		$('div#product_info').cycle(1);
	});
	$('ul#product_info_tabs li a#comments').click(function() {
		$('div#product_info').cycle(2);
	});	
	
	$('p a#details').click(function() {
		$('html,body').animate({scrollTop: ($("#details").offset().top)+360},'slow');
		$('div#product_info').cycle(1);
	});
	
	// catalog more expansion
	$("#catalogHeading #expand").click( function() {
    	$('#catalogHeading #more').slideToggle();
	});
	
	//$("div#alert").append("<div id='promoMsg'>Spring Fling! Free domestic ground shipping on all orders over $100!</div>");	
	
	$("div#rack .itemDiv").hover(
		function (event) {
			//var height = $(this).children('div.drop').height();
			//$(this).children('div.subnav').stop(true, true).animate({height: 'toggle'}, 'fast' );
			$(this).children('form').show();
		}, function() {
			
			//$(this).children('div.subnav').stop(true, true).animate({height: 'toggle'}, 'medium' );			
			$(this).children('form').hide();
		});
		
    //select all the a tag with name equal to modal
    $("div#rack .itemDiv a.moreInfo").click(function(e) {
        //Cancel the link behavior
        e.preventDefault();
        //Get the A tag
        var id = $(this).attr('href');
     
        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        //Set height and width to mask to fill up the whole screen
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        //transition effect    
        $('#mask').fadeIn();   
        $('#mask').fadeTo(0.8); 
     
        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();
               
        //Set the popup window to center
        $(id).css('top',  winH/2-$(id).height()/2);
        $(id).css('left', winW/2-$(id).width()/2);
     
        //transition effect
        $(id).fadeIn(2000);
    });
     
    //if close button is clicked
    $('.window .close').click(function (e) {
        //Cancel the link behavior
        e.preventDefault();
        $('#mask, .window').hide();
    });    
     
    //if mask is clicked
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });  
	// SALE BANNER
    $('div#alert').html('<div id="msg">EXPEDITED SHIPPING ON ALL INTERNATIONAL ORDERS... FREE! <a href="/shipping/">[ SEE DETAILS ]</</div>');
});

	function set_idx_circle(idx) {
		var empty = 'bikinis_images/banner_dot.png';
		var full = 'bikinis_images/banner_dot_selected.png';
		$('.circlesBtn img').attr('src',empty).attr('selected','false');
		$('.circlesBtn img[idx='+idx+']').attr('src',full).attr('selected','true');
	}

var colorbox_width = "80%";
var colorbox_height = "80%";
