// Lighting New York - common scripts
var s = 0;
var ec = 0;
var fcat = 0;
var fcol = 0;
var ffin = 0;
var fprice = 0;

function checkBlanks(){
	q = document.srchFrm.q.value;
	if( $.trim(q) == '' || $.trim(q) == 'begin your search here..' ){
	 	document.srchFrm.q.focus();
		return false;		
	}
	else
		return true;
}

$(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" : "#EEEEEE"});	
	});
	$("#free_shipping").click(function(){	
		$("#description").html("<strong>Lighting New York</strong> provides free shipping for all products shipped UPS Ground with orders over $49!");
	});
	$("#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>. Sales from 8am to Midnight 7 days/week. Customer Service 9am to 5:30pm Monday - Friday [Eastern Time Zone]. And guaranteed satisfaction at the end of your shopping experience!");
	});
	$("#low_price").click(function(){	
		$("#description").html("At <strong>Lighting New York</strong>, if you find a lower advertised price (including shipping, handling and taxes if applicable) on the same item, simply let us know and we will gladly meet that price and beat it by an additional 10% of the difference! <a href='customer_service.cfm?cs=6'>see more details</a>");
	});
	$("#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' : 'none'	
		});
	});

/*
//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();
	}); 

});

//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';
		document.login.cu_loginEmail.focus();
		show = 1;
	}
	else{
		document.getElementById('login_box').style.display='none';
		show = 0;
	}
}

//track email function
function trackEmail2(email,source){	
	if( validateEmail(email) != 0 ){
		$.post("post/trackEmail.cfm", {
			email: email,
			source: source
		}, function(data){}, "html"); //close $.post(..
	}
}

//special offers signup function
function trackEmail(email,source){
	//console.log(validateEmail(document.signup.email.value));
	if( validateEmail(email) == 0 ){
		alert('Please enter a valid email address before submitting.');
		document.signup.email.focus();
	}
	else{
		$("#signup_tbl").fadeOut(300,function(){
			$.post("/post/trackEmail.cfm", {
				email: email,
				source: source
			}, function(data){
				$("#signup_tbl").html("<img src='/grfx/checkmark.png' align='left'>&nbsp;&nbsp;Thanks for signing up!");
				$("#signup_tbl").css('padding','7px');
				$("#signup_tbl").fadeIn(300);
			}, "html"); //close $.post(..
		
		});
	}
}

//if focusing: clear default text and change color darker. blurring: if blank return default text
//x = textbox, y = focus(1)/blur(2)
function signup_focus(x,y){
	if( y == 1 ){
		if( x.value == 'Enter Email Address' ){
			x.value = '';
			x.style.color = '#333';
		}
	}
	if( y == 2 ){
		if( x.value == '' ){
			x.value = 'Enter Email Address';
			x.style.color = '#ccc';
		}
	}
}

//close news banner
function closeNews(){
	$("#news_banner").slideUp(400);	
	$.post("/post/closeNews.cfm"); //close $.post(..
}
