// JavaScript Document
var s = 0;
var ec = 0;
var fcat = 0;
var fcol = 0;
var ffin = 0;
var fprice = 0;

$(function(){


//removing the default text from the input box
	$(".search_text").focus(function(){
		x = $(".search_text");
		//console.log(x.val());
		
		//if the default text is in the box, remove it and change the text color
		if( s == 0 ){
			s = 1;
			x.css({'color': '#333'});
			x.val("");
		}
	});
	
	$(".search_text").blur(function(){
		//if the text box is left blank, replace the default text and reset the default color
		if( x.val() == ""){
			s = 0;
			x.css({'color': '#ccc'});
			x.val("begin your search here..");
		}							
	});
	
	$("#ecouponText").focus(function(){
		x = $("#ecouponText");
		//console.log(x.val());
		
		//if the default text is in the box, remove it and change the text color
		if( ec == 0 ){
			ec = 1;
			x.css({'color': '#333'});
			x.val("");
		}
	});
	
	$("#ecouponText").blur(function(){
		//if the text box is left blank, replace the default text and reset the default color
		if( x.val() == ""){
			ec = 0;
			x.css({'color': '#ccc'});
			x.val("email");
		}							
	});

//why shop at lighting new york jquery code
	$("#free_shipping, #trade_discounts, #top_rated_service, #low_price, #shopping_assistance, #largest_selection").click(function(e){
		e.preventDefault();
		$("#why li a").css({"background-color" : "white"});
		$(this).css({"background-color" : "#eee"});	
	});
	$("#free_shipping").click(function(){	
		$("#description").html("<strong>Lighting New York</strong> provides free shipping for all products shipped UPS Ground!");
	});
	$("#trade_discounts").click(function(){	
		$("#description").html("<strong>Lighting New York</strong> even offers special deals for those professionals that are looking for a good deal on trade pricing!");
	});
	$("#top_rated_service").click(function(){		
		$("#description").html("Customer Service is a top priority here at <strong>Lighting New York</strong>. Expect phone operators seven days a week and guaranteed satisfaction at the end of your shopping experience!");
	});
	$("#low_price").click(function(){	
		$("#description").html("We promise to provide you with the lowest prices at <strong>Lighting New York</strong>.");
	});
	$("#shopping_assistance").click(function(){	
		$("#description").html("<strong>Lighting New York</strong> makes sure you have the best shopping experience. One way we accomplish this is by providing you with free live chat assistance to help you get the most out of our web site!");
	});
	$("#largest_selection").click(function(){	
		$("#description").html("You won't find a larger selection of widely known brands and categories than we have at <strong>Lighting New York</strong>! Truth!");
	});

//dropdown menu initialization and mouseOver delay code		   
	$('li.headlink').hoverIntent({ 
		sensitivity: 3, // sensitivity of the mouseOver
		interval: 50,  // milliseconds until browser checks for mouseOver event
		timeout: 50,   // milliseconds until mouseOut event occurs
		over: showDrop,
		out: function(){
			$('ul', this).hide();
		}
	
	}); 

//dropdown hover color
	$("li.headlink").hover(function(){
		$(this, 'li.headlink a').css({
			'background-image' : 'url(/grfx/search_button_bg2.png)'	
		});
	},
	function(){
		$(this, 'li.headlink a').css({
			'background-image' : "url('')"	
		});
	});

/*
//give the productDetails image a shadow border	
	$("#storeImage").dcCreate({
	   imgPrefix: "images/corners/image-",
	   fileType:  ".gif",
	   expand: 30,
	   position: "outside",
	   resize: "img"
	});
*/	
	
	//send ecoupon signup email
	$("#ecoupon #ecouponButton").click(function(){
		ecSignup();
	});
		
	//CLOSING ecPOPUP
	//Click out event!
	$("#backgroundPopup").click(function(){
			disablePopup();
	}); 

});

//checkBlanks
function checkBlanks(form){
	if(	$.trim(document.srch.q.value) == "" || $.trim(document.srch.q.value) == "begin your search here.." ){
		alert('Please enter the text you would like to search.');
		return false;
	}
	else
		return true;
}

function checkBlanks2(form){
	if(	$.trim(document.srch2.q.value) == "" || $.trim(document.srch2.q.value) == "begin your search here.." ){
		alert('Please enter the text you would like to search.');
		return false;
	}
	else
		return true;
}

//showDrop | display the dropdown menu, if the menu is longer than 200px, give it a scroll bar!
function showDrop(){
	$('ul', this).show();
	$('ul', this).css({
		'display': 'block',
		'z-index': '10'
	}); 
}

//perform a post to add coupon to the cart
function addCoupon(){
	x = $("[name=ca_coupon]");
	//console.log(x.val());
	
	$.post("post/couponApp.cfm", {
		
		code: x.val(),
		c: "a"
	},
	function(data) {
		if( $.trim(data) != "" ){
			$("#popup").html(data);
			$("#popup").height("auto");
			//load the popup div
			loadPopup();
			//center popup
			centerPopup();
			
		}
		else{
			window.location.href = 'viewCart.cfm';
		}
	}, "html"
	); //close $.post(..

	
}
	
//perform ecoupon checks for email submission*********************************************

//0 means disabled; 1 means enabled;  
var popupStatus = 0;

function ecSignup(){
	
	x = $("#ecouponText");
	//console.log(x.val());
	
	y = validateEmail(x.val());
			
	$.post("post/ecSignup.cfm", {
			
		email: x.val(),
		invalid: y
	},
	function(data) {
		$("#popup").html(data);
		$("#popup").height("auto");
		
		//load the popup div
		loadPopup();
		//center popup
		centerPopup();

	}, "html"
	); //close $.post(..
}

function disablePopup(){  
	//disables popup only if it is enabled  
	if(popupStatus==1){  
		$("#backgroundPopup").fadeOut("slow");  
		$("#popup").fadeOut("slow"); 
		popupStatus = 0;
		$("#ecouponText").focus();
		$("#ecouponText").val("");
		
	}  
}

//centering popup  
function centerPopup(){  
	//request data for centering  
	var windowWidth = document.documentElement.clientWidth;  
	var windowHeight = document.documentElement.clientHeight;  
	var popupHeight = $("#popup").height();  
	var popupWidth = $("#popup").width();  
	//centering  
	$("#popup").css({  
		"position": "absolute",  
		"top": windowHeight/2-popupHeight/2,  
		"left": windowWidth/2-popupWidth/2  
	});  
	//only need force for IE6  
 
	$("#backgroundPopup").css({  
		"height": windowHeight  
	});  
  
}  

//load popup
function loadPopup(){
	//loads popup only if it is disabled  
	if(popupStatus==0){  
		$("#backgroundPopup").css({  
			"opacity": "0.7"  
		});  
		$("#backgroundPopup").fadeIn("slow");  
		$("#popup").fadeIn("slow");  
		popupStatus = 1; 
		
/*		//add divcorners
		$("#popup").dcCreate({
			imgPrefix: "images/corners/popup-",
			fileType:  ".png",
			expand: 22,
			position: "outside"		 
		});
*/
		
	}
}

//validate ecoupon email
function validateEmail(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if (str.indexOf(at)==-1){
		   return 0;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return 0;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return 0;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		    return 0;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return 0;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		    return 0;
	}
		
	if (str.indexOf(" ")!=-1){
		    return 0;
	}

 		 return 1;					
}


//login functions
var show = 0;

function showLogin(){
	if( show == 0 ){
		document.getElementById('login_box').style.display='block';
		show = 1;
	}
	else{
		document.getElementById('login_box').style.display='none';
		show = 0;
	}
}
