jQuery(document).ready(function(){

	var newContactUs = '<div id="contacUsNew"><a id="customer-care-telephone-number" ><img src="http://www.conceiveeasy.com/wp-content/themes/fertibellax/new_image/customer-care-telephone-number.png"></a>'+
	'</div>'+
	'<div id="shopBasketItemsCon">'+
		'<b><p><a style=\"color: #5B6571;\">Easy</a><a style=\"color: #5B6571;\"> Returns</a></p><span>Cart:<a>&nbsp;</a>&nbsp;Items</span></b>'+
	'</a></div>';
	jQuery("#header_contactus > div").hide();
	jQuery("#header_contactus").append(newContactUs );
	jQuery("#header_contactus").css('padding-top',"3px");
	
	jQuery("#magazine_2").css("top","11px");
	var the_x = ((jQuery("#header_2").width()/2) - (jQuery("#magazine_2").width()/2))+41;
	jQuery("#magazine_2").css("left",the_x);
	
	var mainMenuLeft = jQuery(window).width()/2 - jQuery("#header").width()/2 + 1;
	jQuery("#mainMenu").css("left",mainMenuLeft);

	
	jQuery("#scs_body_1").find("div").find("img").css("height","99px");
	
	
	
	var scs_x = jQuery(window).width()/2 - 928/2 +10;
	jQuery("#shoppingCartSummay").css('right',scs_x);
	
	jQuery("#shopBasketItemsCon").live("click", function(){
		 var flagger = jQuery(this).find("b").find("span").find("a").text();
		
		 if(flagger !=0){
			 pre_show_scs();
		 }	
	});
	jQuery("#free-returns-div").live("click", function(){
		 var flagger = jQuery(this).find("b").find("span").find("a").text();
		
		 if(flagger !=0){
			 pre_show_scs();
		 }	
	});
	//jQuery("#addToCartButton").live("click",addToCart);
	jQuery("#scs_close").live("click",pre_close);
		
	jQuery("#scs_blanket").live("click",function(){
		jQuery("#scs_close").trigger("click");
	});
	
	jQuery("#scs_continueShopping").live("click",function(){
		jQuery("#scs_close").trigger("click");
		location.reload;
	});
	
	jQuery("#free3dayLink").live("click",show3Day);
	jQuery("#close_3day").live("click",hide3Day);
	
	setDivPosition("#Shipping_3day");
		
	jQuery("#zoom").live("click", zooom);
	jQuery("#estimateDelivery").live("click",estimateDelivery);
	
	 jQuery(".lnkFreeReturn").hover(function(e){
		var the_x = e.pageX+5;
		var the_y = e.pageY+5;
		
		jQuery(".freeReturn").css("left",the_x);
		jQuery(".freeReturn").css("top",the_y);
		jQuery(".freeReturn").show(300);
	 },function(e){
		jQuery(".freeReturn").hide(300);
	 });
	 
	 jQuery(".lnkFreeReturn").live("mousemove",function(e){
		var the_x = e.pageX+5;
		var the_y = e.pageY+5;
		
		jQuery(".freeReturn").css("left",the_x);
		jQuery(".freeReturn").css("top",the_y);
	 });
	
	
	jQuery(".lnkShopConfidence").hover(function(e){
		var the_x = e.pageX+5;
		var the_y = e.pageY+5;
		
		jQuery(".shopConfidence").css("left",the_x);
		jQuery(".shopConfidence").css("top",the_y);
		jQuery(".shopConfidence").show(300);
	},function(e){
		jQuery(".shopConfidence").hide(300);
	});
	
	jQuery(".lnkShopConfidence").live("mousemove",function(e){
		var the_x = e.pageX+5;
		var the_y = e.pageY+5;
		
		jQuery(".shopConfidence").css("left",the_x);
		jQuery(".shopConfidence").css("top",the_y);
	});
	
	
	
	
	getShopBasketItems();
	updateCartTotals();
	
	jQuery(".updateQty").live("change",editCartValues);
	getNewestComments();
	jQuery(".rateYes").live("click",rateTestimonyYes);
	jQuery(".rateNo").live("click",rateTestimonyNo);
	jQuery("#guaranteeCon").live("click",pop_trial2);
	jQuery("#close_pop_trial2").live("click",hide_pop_trial2);
	jQuery("#get_pending_reviews").live("click",get_pending_reviews);
	jQuery(".approveComment").live("click",approveComment);
	jQuery(".editComment").live("click",editComment);
	
		  
	jQuery("#newestComments").live("click",getNewestComments);
	jQuery("#mostHelpful").live("click",getMostHelpfulComments);
	
	jQuery("#positiveComments").live("click",getPositiveComments);
	jQuery("#negativeComments").live("click",getNegativeComments);
	
	jQuery("#updateTestimony").live("click",updateTestimony);
	jQuery("#cancelUpdateTestimony").live("click",function(){
		jQuery("#formEditTestimonials").remove();
	});
	jQuery(".rateYes2").live("click",function(){
		alert('You have successfully rated this review');
	});
	jQuery(".rateNo2").live("click",function(){
		alert('You have successfully rated this review');
	});
	jQuery(".proceedCheckout").live("click",submitShopCart);


	
	jQuery("#mcafee").live("click",pop_mcafee);
   jQuery(".mcafee").live("click",pop_mcafee);
   jQuery("#verisign").live("click",pop_verisign);
 
	jQuery('.scs_freeGift').live("click",function(){
	
		
		var the_val = jQuery(this).val();
		
		if(the_val=="20/P"){
			if(jQuery('[name=freeGift][value="20/P"]').length() > 0){
			  jQuery('[name=freeGift][value="20/P"]').attr("checked","checked");
			}
		}else if(the_val=="20/O"){
			if(jQuery('[name=freeGift][value="20/O"]').length() > 0){
			  jQuery(' [name=freeGift][value="20/O"]').attr("checked","checked");
			}
		}else if(the_val=="10/10"){
			if(jQuery('[name=freeGift][value="10/10"]').length() > 0){
			  jQuery('[name=freeGift][value="10/10"]').attr("checked","checked");
			}
		}
		setSessionFreeGift(the_val);
	});
	
	jQuery(".scs_freeGift").attr("disabled","disabled");
	
	jQuery(".scs_freeGift").each(function(){
		if(jQuery(this).attr("checked")=="checked"){
			jQuery(this).removeAttr("disabled");
		}
	})
	
});

function setSessionFreeGift(free_gift){
	var data={
	  freeGift:free_gift,
	  action:'set_session_var'
	}
	
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		/* alert(ret); */
	});
}


function temp_save_sb_data(){
	//alert('test1');
	if(jQuery("#fblead").length > 0){
		//alert('test2');
			var sfname = jQuery("input[name=sfname]").val();
		var slname = jQuery("input[name=slname]").val();
		var phone = jQuery("input[name=phone]").val();
		var saddress = jQuery("input[name=saddress]").val();
		var scity = jQuery("input[name=scity]").val();
		var sstate = jQuery("select option:selected").val();
		var szip = jQuery("input[name=szip]").val();
		var email = jQuery("input[name=email]").val();
		
		var data = {
			sfname:sfname,
			slname:slname,
			phone:phone,
			saddress:saddress,
			scity:scity,
			sstate:sstate,
			szip:szip,
			email:email,
			action:'set_session_order_data'
		}

		jQuery.post(MyAjax.ajaxurl,data,function(ret){
			alert(ret);
		});
	}
}

function pop_verisign(){
		var xurl=jQuery(this).attr("href");
		window.open(xurl,"Window2","scrollbars=0,resizable=no,menubar=no,width=518,height=425,toolbar=no");
		return false;
	}
	
	function pop_mcafee(){
		var xurl=jQuery(this).attr("href");
		window.open(xurl,"Window1","scrollbars=0,resizable=no,menubar=no,width=550,height=460,toolbar=no");
		return false;
	}

function submitShopCart(){
	var shopCartTotal = jQuery("#finTotal").text();
	var shopCartSavings = jQuery("#finSavings").text();
	var shopCartDiscount = jQuery("#finDisc").text();
	var shopCartShipDiscount = jQuery("#shipDisc").text();
	var shopCartShipping = jQuery("#shipPrice").text();
	var shopCartSubTotal = jQuery("#finSubTot").text();
	
	//alert(shopCartSubTotal);
	
	jQuery("#shopCartTotal").attr("value", shopCartTotal);
	jQuery("#shopCartSavings").attr("value",shopCartSavings);
	jQuery("#shopCartDiscount").attr("value",shopCartDiscount);
	jQuery("#shopCartShipDiscount").attr("value",shopCartShipDiscount);
	jQuery("#shopCartShipping").attr("value",shopCartShipping);
	jQuery("#shopCartSubTotal").attr("value",shopCartSubTotal);
	
	jQuery("#shopBasketForm").submit();
	
}

function updateTestimony(){
	jQuery.post(MyAjax.ajaxurl,jQuery("#formEditTestimonials").serialize(),function(ret){
		if (ret=="Success"){
			location.reload();
		}
	});
}
function editComment(){
jQuery("#formEditTestimonials").remove();
 var comment_id = jQuery(this).attr("param");
 var theObj = jQuery(this);
	var data = { comment_id:comment_id,
				 action:'edit_testimony'	
				}
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		var newHtml = ret;
		theObj.parent().parent().parent().append(ret);
	});			
}

function getNewestComments(){
	getSortedComment("get_newest_testimonies");
}
function getMostHelpfulComments(){
	getSortedComment("get_most_helpful_testimonies");
}

function getPositiveComments(){
	getSortedComment("get_positive_testimonies");
}

function getNegativeComments(){
	getSortedComment("get_negative_testimonies");
}

function getSortedComment(action){
//alert(action);
var data ={	
		action:action
	}
	
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		
		if(ret ==-1 || ret == 0){}else{
		var newHtml = ret;
		jQuery("#all_testimony_con").empty();
		jQuery("#all_testimony_con").append(newHtml);
		}
	});
	
}

function approveComment(){
	var comment_id = jQuery(this).attr("param");
	var data = { comment_id:comment_id,
				 action:'approve_testimony'	
				}
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		alert(ret);
		get_pending_reviews();
	});			
}

function get_pending_reviews(){
	var data ={	
		action:'get_pending_reviews'
	}
	
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		if(ret ==-1 || ret == 0){}else{
		var newHtml = ret;
		jQuery("#all_testimony_con").empty();
		jQuery("#all_testimony_con").append(newHtml);
		}
	});
}

function hide_pop_trial2(){
	jQuery("#pop_trial2").hide();
}
function pop_trial2(){
	var the_left = jQuery(window).width()/2 + 300;
	jQuery("#pop_trial2").css("left",the_left);
	jQuery("#pop_trial2").css("top","450px");
	jQuery("#pop_trial2").show();
	
}


function editCartValues(){
	var multiplier = parseFloat(jQuery(this).attr('value'));
	var targ = jQuery(this).attr('param');
	
	var newOrigPrice = parseFloat(jQuery("#"+targ+"_origPrice > p").attr('param')) * multiplier;
	  jQuery("#"+targ+"_origPrice > p").text("$"+newOrigPrice.toFixed(2));
	  jQuery("#"+targ+"_origPrice > p").attr("title",newOrigPrice.toFixed(2));
	var newDiscPrice = parseFloat(jQuery("#"+targ+"_savePrice > a > p").attr('param')) * multiplier;
	  jQuery("#"+targ+"_savePrice > a > p").text("$"+ newDiscPrice.toFixed(2));
	  jQuery("#"+targ+"_savePrice > a > p").attr("title",newDiscPrice.toFixed(2));
	var newSubFinPrice = parseFloat(jQuery("#"+targ+"_subFinPrice > a > p").attr('param')) * multiplier;
	  jQuery("#"+targ+"_subFinPrice > a > p").text("$"+newSubFinPrice.toFixed(2));
	  jQuery("#"+targ+"_subFinPrice > a > p").attr("title",newSubFinPrice.toFixed(2));
	  
	 updateCartTotals();
	
}

function updateCartTotals(){
	var subTots = 0;
	 var subSaves =0;
	 var subOrigPrice = 0;
	 jQuery(".subTots").each(function(){
		subTots = parseFloat(jQuery(this).attr("title")) + parseFloat(subTots);
	});  
		//subTots = subTots-9.95;
	  jQuery('#finTotal').text('$'+subTots.toFixed(2));
	
	jQuery(".subSaves").each(function(){
		subSaves = parseFloat(jQuery(this).attr("title")) + parseFloat(subSaves);
	});  
	  jQuery('#finDisc').text('$'+subSaves.toFixed(2));
	  
	jQuery(".subOrigPrice").each(function(){
		subOrigPrice = parseFloat(jQuery(this).attr("title")) + parseFloat(subOrigPrice);
	});  
	  jQuery('#finSubTot').text('$'+subOrigPrice.toFixed(2));
	  subSaves=subSaves+9.95;
	  jQuery('#finSavings').text('$'+subSaves.toFixed(2));
	  
}

function estimateDelivery(){
	if (jQuery("#zip").val()!=''){
		var zipCheck = jQuery("#zip").val().substr(0,1);
		
		jQuery("#estimated_delivery").hide();
		now = new Date();
		
		
		if (zipCheck==9 || zipCheck == 8){
			daysfromnow = new Date(now.getTime() + (2 * 24 * 60 * 60 * 1000));
		}else{
			daysfromnow = new Date(now.getTime() + (3 * 24 * 60 * 60 * 1000));
		}
		
		months = new Array("January ","February ","March ","April ","May ","June ","July ","August ","September ","October ","November ","December "); 
		var theDate = months[daysfromnow.getMonth()] +" " + daysfromnow.getDate()+", "+ daysfromnow.getFullYear();
		jQuery("#estimated_delivery > b").text(theDate);
		setTimeout('jQuery("#estimated_delivery").show()',300);
	}else{
		alert("No zip entered");
	}
	
}

function getShopBasketItems(){
	var data= {
		action:"getShopBasketItems"
	}
	
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		var cartItems = jQuery("#cartItemCount",ret).text();
		var itemName = jQuery("#itemName",ret).text();
		var outOf = '1 out of '+cartItems+' items';
		if (cartItems == '' || cartItems == 0){
			cartItems = 0;
			outOf = '0 out of 0 items';
		}
		var cart_subtotal = jQuery("#cartSubTotal",ret).text();
		jQuery("#shopBasketItemsCon").find("span").find("a").text(" "+cartItems);
		if (cart_subtotal=='' || cart_subtotal=='$FREE'){
			cart_subtotal=0;
		}
		//for shopping cart summary
		jQuery('#scs_body_3 > span > label > a').text(cartItems);
		jQuery("#scs_body_3 > span >p").text('$' + cart_subtotal);
		jQuery("#outOfItems").text(outOf);
		jQuery("#csc_prodName").text(itemName);
	})
}
function zooom(){
	var zoomLimit = parseInt(jQuery("#prodImg").attr("param"));
	jQuery("#prodImg").attr("param",zoomLimit+1);
	var curr_width = jQuery("#prodImg > img").width() ;
	
	if (zoomLimit<5){		
		jQuery("#prodImg > img").width(curr_width + 100);
		var curr_margin = jQuery("#prodImg > img").css('marginTop',0) ;
		if (curr_margin >0){
			jQuery("#prodImg > img").css("marginTop",curr_margin - 100);
		};
	}else{
		jQuery("#prodImg").attr("param",0);
		jQuery("#prodImg > img").width(curr_width - (100 * zoomLimit));
	}
	
}

function setDivPosition(elem){
	var widh = parseInt(jQuery(elem).width());
	var posX = jQuery(window).width()/2 - 50;
	jQuery(elem).css("left",posX);
	return true;
}

function hide3Day(){
var target=jQuery(this).attr("param");
show_hide_div("#"+target,"hide");
}

function show3Day(){
var target=jQuery(this).attr("param");
show_hide_div("#"+target,"show");
}

function show_hide_div(elem,action){
	if (action=="show"){
		jQuery(elem).show(300);
	}else{
		jQuery(elem).hide(300);
	}
}

function locationReplace(){
document.location.replace(main_shop_link);
}

function addToCart(){

	var itemId = jQuery('.pageTitleCon').attr("param");
	var itemName = jQuery('.pageTitleCon').attr("title");
	var itemImage = jQuery('#prodImg >img').attr('src');
	/* alert(itemImage); */
	
	var data = { 
				itemID:itemId,
				itemPrice:39.95,
				itemName:itemName,
				itemImage:itemImage,
				action:'addItemToCart'
			}
		
		jQuery.post(MyAjax.ajaxurl,data,function(ret){
			getShopBasketItems();
			pre_show_scs();
		})	
		
}

function pre_show_scs(){
	var tot = jQuery("#scs_merchandiseTotal >p").text();

	/*if(tot!="" && tot!='$0'){
		jQuery("#scs_merchandiseTotal >p").text('$9.95');
	}*/
	var blanketDiv ='<div id="scs_blanket"></div>';
	var blanket_height = jQuery(window).height();
	var blanket_width = jQuery(window).width();
	jQuery("body").append(blanketDiv);
	jQuery("#scs_blanket").css("display","block");
	jQuery("#scs_blanket").css("overflow","hidden");
	jQuery("#scs_blanket").css("position","absolute");
	jQuery("#scs_blanket").css("top","absolute");
	
	jQuery("#scs_blanket").css("height",blanket_height);
	jQuery("#scs_blanket").css("width",blanket_width);
	shower_closer("#shoppingCartSummay",'open');
	
	
}

function shower_closer(elem,action){
	if (action=='open'){
		jQuery(elem).animate({top:'-5px'},300);
				
	}else if(action=='close'){
		jQuery(elem).animate({top:'-392px'},300);
	}
	
}

function pre_close(){
jQuery('#scs_blanket').remove();
var elemName = jQuery(this).attr('param');

shower_closer('#'+elemName,'close');
}
 
function rateTestimonyYes(){
	var testiId = jQuery(this).attr("param");
	var rateStat = rateTestimony(testiId,1);
	jQuery(this).removeAttr("class");
	
}

function rateTestimonyNo(){
	var testiId = jQuery(this).attr("param");
	var rateStat = rateTestimony(testiId,0);
	jQuery(this).removeAttr("class");
}

function rateTestimony(testiId,rate){
	var data = { testimony_id:testiId,
				rating:rate,
				action:'testimony_rate'
	}
	
	jQuery.post(MyAjax.ajaxurl,data,function(ret){
		
		if (ret=='Success'){
			alert('You have successfully rated this review');
			return true;
		}
	});
}
