jQuery(function(){
		   

	//--- Enable bottom thumbnail carousel
	/* if(jQuery("div.productthumbnailbg").length>0){ 
		jQuery(".slider").jCarouselLite({
			btnNext: ".next",
			btnPrev: ".prev",
        	visible: 7,
            afterEnd: function(a) {
                //-- this afterEnd should be called for rollover effect in vis() function of this pluggin.
            },
            circular: false
		});
	}

	$("#recentlyviewed").click(function(){
        jQuery("#show").slideToggle("slow");		
    }); */
	
	
	

    /********** Print option **********************/	
	$(".wishlist").find("a[href=#print]").click(function(){ 
		jQuery(this).css("outline", "none");
		var pid = $(this).closest(".productShare").attr("id");
		pid = pid.split("-");
		pid = pid[1];
		url = "printwishlist.php?wid="+jQuery("#wid").val()+"&pid="+pid;
		popupWindow(url, 10, 10, 680, 520 );		
		return false;
	});
	
     /********** Print option **********************/	
	jQuery("#askInfo,a[href=#ask]").click(function(){
		jQuery(this).css("outline", "none");
		
		var url = "ask.php?wid="+jQuery("#wid").val()+"&pid="+jQuery("#pid").val();
				
		$.ajax({
			url: url,
			dataType:"html",
			type:"post",
			success:function(data) {
				$(".popup").show().popUp(data);
			},
			error:function() {
				alert('error');	
			}
		});
		return false;
	});   
	
	
	/********** Adding a product to Wishlist  **********************/	
	jQuery("#addtoWishlist").click(function(){ 
		jQuery(this).css("outline", "none");
		addWishlist( jQuery("#hproductid").val() ) ;		
		
	});
	
	/********** Removing a product from Wishlist  **********************/
	jQuery("#removefromWishlist,a[href=#removefromWishlist]").click(function(){  
		var $this = $(this);
		/*
		if( confirm("Are you sure want to remove from wish list?") ){
			//var wishlistID = $("#wid").val();
			var wishlistID = $this.closest("tr").attr("id");
			wishlistID = wishlistID.split("-");
			wishlistID = wishlistID[1];
  		  window.location.href = "wishlist.php?rwid="+wishlistID;
		}else{
			// $(this).attr("checked", false);
			return false;
		}
		*/
		$(".popup").popUpConfirm("Are you sure you want to remove this item from your wish list?",function() {
			var wishlistID = $this.closest("tr").attr("id");
			wishlistID = wishlistID.split("-");
			wishlistID = wishlistID[1];
  		  	window.location.href = "wishlist.php?rwid="+wishlistID;
		});
	});
    
    /********** Removing a product from Wishlist  **********************/
	jQuery("#musthaveit").click(function(){  
          $("#keymode").val("update_musthaveit");
          document.myfrm.submit();
	});
    		
/*	jQuery("#removefromWishlist").click(function(){  
	   var checkbox = $(this);
		if( confirm("Are you sure want to delete?") ){
    		 jQuery.post( "ajaxhandler.php", {optype:"removefromwishlist", wid:$("#hwid").val() },
    			function(responseText){
    				if(responseText==1){
    					window.location.href="wishlist.php?msg=Product removed successfully.";
    				}else{
    					alert(responseText);
                        checkbox.attr("checked", false);	
        				}
        			}//end function
         	);//end post     
		}else{
			checkbox.attr("checked", false);	
		}
	});
	
*/	
	
	
/********** E-MAIL WISHLIST  **********************/
	jQuery("#emailInfo,#emailInfoAll").css("outline", "none").click(function(){
		var selobj = $(this);
		var selectedId = selobj.attr("id");
		//console.log(selectedId);
		
        $("#recipientName").focus();
		$("#wishlitMailtype").val(selectedId);
		//clearFields();		
		
		if(selectedId=="emailInfo"){
			$("#wheading").html("E-MAIL WISH LIST ITEM");
		}else{ $("#wheading").html("E-MAIL ENTIRE WISH LIST"); }
		
	  	// jQuery("#descriptionContainer").fadeOut(1, function(){ jQuery("#sendmailBox").show() });
		
		//var popupText = $("#sendmailBox").html();
		//$(".popup").popUp(popupText);	
		$("#sendmailBox").addClass("popup").css({"marginTop":"0px","top":"0px"}).popUp();	
    });
	

	/* jQuery("#closeEmail").css("outline", "none").click(function(){										  
		$(this).css("outline", "none");
	  	 toggleContainers(); clearFields();	 
		 $("#wheading").html("E-MAIL WISH LIST ITEM");
    }); */
	
	
	/* $("#submitEmail").click(function(){
		//var ajaxMessage = jQuery("#ajaxmsg");
		//ajaxMessage.html("Sending Wish List mail...");
		
		var wishlistID = $(this).closest("tr").attr("id");
		wishlistID = wishlistID.split("-");
		wishlistID = wishlistID[1];
		
		var pid = $(this).closest("div.productShare").attr("id");
		pid = pid.split("-");
		pid = pid[1];
			
		var params = {
			optype:$("#wishlitMailtype").val(),
			product_id: pid,
			wishlist_id: wishlistID,
			recipientName:$("#recipientName").val(),
			recipientMail:$("#recipientMail").val(), 
			subject:$("#subject").val() 
		};
		
		$.ajax({
			url: "ajaxhandler.php",
			data: params,
			success: function (data,textStatus) {
				if(textStatus==1){
					//clearFields();
					//ajaxMessage.html("Email sent successfully.");						
					//ajaxMessage.html("Email sent successfully.").fadeIn(400).fadeOut(400).fadeIn(500).fadeOut(400).fadeIn(500);
					$(".popup").popUp("<h1>Sent</h1><p>Email sent successfully.</p>");
				}else{
					//ajaxMessage.html(responseText);
					$(".popup").popUp("<h1>Error</h1><p>"+textStatus+"</p>");	
				}
			}//end function
		});//end post	
		
		return false;
											  
	});//end jQuery("#submitEmail")	*/
	
	//-- updating Wishlist comments
	$("textarea[name=wcomments]").blur(function(){
		var $this = $(this);
		
		//var ajaxMessage = jQuery("#ajaxmsg2");
		
		if ($this.val() == "") {
			// do nothing
		}
					
		$this.addClass("saving");
		
		var wishlistID = $this.closest("tr").attr("id");
		wishlistID = wishlistID.split("-");
		wishlistID = wishlistID[1];
		
		var wishcomments = $this.val();
		params = {optype:"updatewcontent" , wishlist_id: wishlistID, wishcomments: wishcomments  };
					
		jQuery.post("ajaxhandler.php", params, 
				function(responseText,status,response){
					if(responseText==1){
						$this.removeClass("saving");
					}else{
						$this.removeClass("saving");
						$(".popup").popUp("<h1>Error</h1><p>Could not save comments. Please try again.</p>",true);
					}
										
				}//end function
		);//end post
			
	});//end updating Wishlist comments	
	
 	//-- Ajax Error handler
	// timeout:500,
	$.ajaxSetup({ 	
		error: function( XMLHttpRequest, textStatus, errorThrown ){
		            alert("Unable to reach server ("+errorThrown+"). Please try again... ");
			   }
	});	
	
	
});//end jQuery dom ready function


function addWishlist(productid){
	jQuery.post( "ajaxhandler.php", {optype:"savetowishlist", pid:productid},
				function(responseText){
					if(responseText==1){
						//$("#wishCount").load( "ajaxhandler.php", { optype:"wishcount" } );
						//alert("THIS ITEM HAS SUCCESSFULLY BEEN ADDED TO YOUR WISH LIST.");
						//window.location.href="product.php?wishlist&pid="+productid;
						$(".popup").popUp("<p>This product has been added to your wishlist.</p>",true);
						//---- update counter in header
						var wishlistCounter = $("span.wishlistCounter").text();
						wishlistCounter = Number(wishlistCounter);
						wishlistCounter += 1;
						$("span.wishlistCounter").text(wishlistCounter);
						return false;
                       // window.location.href="wishlist.php?pid="+productid;
					}else{
						$(".popup").popUp("<p>"+responseText+"</p>",true);
					}
					
				}
	);//end post
	
}//end addWishlist function





//function toggleContainers(){
//	jQuery("#descriptionContainer, #sendmailBox").toggle();		
//}


function clearFields(){
	 jQuery("#recipientName, #recipientMail, #subject").val("");
	 jQuery("#ajaxmsg,#ajaxmsg2").html("");
}


/*
Opens a new browser window, 
given a URL and the left, top, width, and height of the window.
*/
var popupWin=0;
function popupWindow(URLStr, left, top, width, height){
  if(popupWin){
    if(!popupWin.closed) popupWin.close();
  }
  popupWin = open(URLStr, 'popUpWin', 'toolbar=1,location=no,directories=no,status=no,menubar=1,scrollbars=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}
