// jQuery written for LoveFineArt (c)

// append the prodQuery menu item with a var indicating searchWithin if the checkBox has been checked
$(document).ready(function() {
if( $("#mainPromo").length != 0 ){
// ////////////////////////////////////////////////////////////////////////////
//             SIMPLE SLIDE SHOW BOX 
// /////////////////////////////////////////////////////////////////////////////
/*How it Works
The plugin provides a method called cycle which is invoked on a container element. Each child element of the container becomes a "slide". Options control how and when the slides are transitioned. */

   $('#rotator').before('<div id="slide-nav">').cycle({ pager: '#slide-nav' });
   
   // This little didy id for detecting which link to use. It is needed bc the normal linking is not working
   // due to some absolute positioning.
     $('#mainPromo').click(function(){
         var href = "";
         $('ul#rotator li').each(function(index){
        // alert( index +": "+$(this).css("opacity")+" child: "+ $(this).children(":first-child").attr("href")   ); 
               if(   $(this).css("opacity") >= .5  ){ 
               						href = $(this).children(":first-child").attr("href");  
               						
               	}
               window.location= href ;
          });
        
    });
   

	
}// end if( $("#mainPromo").length != 0 )
// ////////////////////////////////////////////////////////////////////////////
//             FULLTEXT SEARCH
// ///////////////////////////////////////////////////////////////////////////

$("#sr-results-per-page").change(function() {
        var resultsPerPage = this.value;
       $.post( 
					"/_ajax/search_results_per_page.php",
					{ results_per_page: resultsPerPage },
					function(data){	
                        location.reload();
					});
		// repopulate the search box
		//$("#search-text-input").val(query_text);
		return false;
	}); 
// ////////////////////////////////////////////////////////////////// 
// //////////////////////////////////////////////////////////////////


// See user forgot Pasword section below for functions
//$("#request-new-password-submit").click( function(){ requestNewPaswordFormValidation(); });

$("a.tool-tip").hover( function(){  $(this).find("div").show(); } , 
				                 function(){  $(this).find("div").hide();  }
							   );

// //////////////////////////////////////////////////////////////////////////////
//  TOOL TIP: PRODUCT DETAIL PAGE    
// ////////////////////////////////////////////////////////////////////////////// 
    var numOfRows = $("#num_of_prod_type_rows").html();
    for (row =0; row<=numOfRows; row++) {
			//iterate thru each product type row on the product details page
			
			   $(".prod-info-holder #what-is-this-"+row).tooltip({ 
			   // Slide Effect
			     effect: 'fade',
				// each trashcan image works as a trigger 
				tip: '#tooltip-'+row, 
				// custom positioning 
				position: 'top left', 
				// move tooltip to optimum position
				offset: [-185,-170], 
				// do not initialize tooltips until they are used 
				lazy: true,  
				// there is no delay when the mouse is moved away from the trigger 
				delay: 300 
				});
				
    }
 // /////////////////////////////////////////////////////////////////////      
 //      TABS     
 // /////////////////////////////////////////////////////////////////////  
 	// This will append the current tabs id with "-current"
			$("li.tab").click(function(){
			// NOTE: images must be named the same as the id
			//             images for active =  'tab_'+id+'_active.jpg'
			//             rest of images = 'tab_'+id+'_inactive.jpg'
				// TAB (list item) Clicked	
				    $("li.tab").each(function(index) {
                            $(this).css('background-image', 'url(/_images/tab_'+this.id+'_inactive.jpg)');
                            $(this).removeClass('active-tab');// turn off all tabs
                         });
				    $('.tab-pane').hide();// turn off all tab-panes
				    $(this).addClass('active-tab');// make this tab active
				    $(this).css('background-image', 'url(/_images/tab_'+this.id+'_active.jpg)');
                    $( '#'+this.id+'-pane').show();// turn on this specific pane
			});
							
 // /////////////////////////////////////////////////////////////////////    
 // ////////////////////////////////////////////////////////////////////   

// //////////////////////////////////////////////////////////////////////////////////
// //////////////// START:          SEARCH WITHIN     ///////////////////////////
// //////////////////////////////////////////////////////////////////////////////////
// Upon the click of any of the search menu items....
$("#secondary-column ul li a").click(function() {
		// ...check to see if the "seach-within" check box has been checked
		var seachWithin = $("#search-within input").attr("checked");// true or false
		if ( seachWithin == true){
				//... append the GET vars with "&searchWithin=TRUE";
				var origHref = $(this).attr("href");// true or false
				var newHref = origHref+"&searchWithin=TRUE";
				$(this).attr("href", newHref);
				var refNow = $(this).attr("href");
				
			}//if
		});	
// /////////////////////////////////////////////////////////////////////////////////
// ////////////////  END:           SEARCH WITHIN        ////////////////////////
// /////////////////////////////////////////////////////////////////////////////////

		
// ///////////////////////////////////////////////////////////////////////
// START:  CUSTOMER LOG-IN or REGISTER Link Response  //////
// //////////////////////////////////////////////////////////////////////
// Upon the click of the "login"  link ( id="custLoginLink"),
// this function pops open the "attention-div" box and put the a login 
// form inside it as an AJAX call from the "_ajax/customerLogin.php" file.
//---------+---------+---------+---------+---------+---------+---------+---------+
// NOTE: There are functions in _templates/attentionBox_logIn.tmp.php
// It must reside there because as an AJAX call, $(document).ready
// wont work on it from here.
//---------+---------+---------+---------+---------+---------+---------+---------+
$("#custLoginLink").click(
							function() {
							   var destination = $("#custLoginLink").attr("href");// true or false
								attentionBox_logIn(destination);
								return false;
							}//function
				);
 // ///////////////////////////////////////////////////////////////////////// 
//    END:  CUSTOMER LOG-IN or REGISTER Link Response  
 /////////////////////////////////////////////////////////////////////////// 
 // //////////////////////////////////////////////////////////////////////// 
//   START:  CUSTOMER LOG-OUT                                   
 // //////////////////////////////////////////////////////////////////////// 
// Upon the click of the "log out"  link ( id="custLogOutLink"),
// this function ...
// .
$("#custLogOutLink").click(
				function() {	
							$.post( 
										// beginning slash needed due to modReWrite
										"/_ajax/customerAuthentication.php", 
										{ status: "logout" },
										function(data){ 
												$("#ui_div").html(data);
												}
						);// $post		
					showAttentionBox();
					return false;
				}//function
		);
// //////////////////////////////////////////////////////////////////////
//    END:  CUSTOMER LOG-IN or REGISTER Link Response  ////// 
/////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////
//   START:  					ADD To CART      ///////
// //////////////////////////////////////////////////////////////////////
$(".add2cart-1").click(function() {	
						var info = this.id.split( "-" );
						var prod_id 	= info[0];
						var cart_row =	 info[1];
						var cpi_id 	=	 info[2];
						var action 	=	 info[3];
						$.post( 
									"/index.php?pg=shopping-cart",
									{ prod_id: prod_id },
									function(data){	
										// remove from wish list after adding to cart
										updateWishlistRow(cart_row,cpi_id,prod_id,action);
										var url = "/shopping-cart";    
										$(location).attr('href',url);
									});
						return false;
	});//$(".add2cart-1").click

$(".add2cart-results").click(function() {	
						var prod_id = this.id;	
						$.post( 
									"/index.php?pg=shopping-cart",
									{ prod_id: prod_id },
									function(data){	
										var url = "/shopping-cart";    
										$(location).attr('href',url);
									});
						return false;
	});//$(".add2cart-1").click
// //////////////////////////////////////////////////////////////////////////////
//    	END:  					ADD TO CART  			  ////// 
////////////////////////////////////////////////////////////////////////////////
// /////////////////////////////////////////////////////////////////////////////////
// //////////////// START:      WISHLIST    ////////////////////////
// /////////////////////////////////////////////////////////////////////////////////

// sr stands for search result(s)
$(".add2wishlist-sr").click(function() {	
						var prod_id = this.id;	
						$.post( 
									"/wishlist",
									{ prod_id: prod_id },
									function(data){	
										var url = "/wishlist";    
										$(location).attr('href',url);
									});
       return false;
	});//$(".add2wishlist").click
	
$(".add2wishlist").click(function() {	
						var prod_id = this.id;	
						$.post( 
									"/wishlist",
									{ prod_id: prod_id },
									function(data){	
										var url = "/wishlist";    
										$(location).attr('href',url);
									});
       return false;
	});//$(".add2cart-1").click
	
$(".cart2wishlist").click(function() {	
						var info = this.id.split( "-" );
						var prod_id 	 = info[0];
						var cart_row =	 info[1];
						var cpi_id 	 =	 info[2];
						var action 	 =	 info[3];	// action = "remove"
						$.post( 
									"/wishlist",
									{ prod_id: prod_id },
									function(data){	
										var url = "/wishlist";    
										//Remove from cart after saving to wish list
										updateCartRow(cart_row,cpi_id,prod_id,action);
										var url = "/shopping-cart";    
										$(location).attr('href',url);
									});
	       return false;
	});//$(".cart2wishlist").click
	
	$(".wishlist2cart").click(function(){	
	                    var info = this.id.split( "-" );
						var prod_id 	 =  info[0];
						var cart_row =	 info[1];
						var cpi_id 	 =	 info[2];
						var action 	 =	 info[3];	// action = "remove"

						$.post( 
									"/_wishlist/_ajax/add2cart_from_wishlist.php",
									{ prod_id: prod_id },
									function(data){	 
										//Remove from cart after saving to wish list
										//$('.cartRowContainer').html(data);
										updateWishlistRow(cart_row, cpi_id, prod_id, action);
										var url = "/shopping-cart";    
										$(location).attr('href',url);
									});
						return false;// <-- Keeps page from going to link on its own
									
	});//$(".cart2wishlist").click
	
	
// NOTE: Not sure if this is being used
// no time to check right now so clean this up when you have time
$("#wishListLink").click( function() {
								var isUserLoggedIn	= $("#isUserLoggedIn").attr("value");// true or false
								var destination			= $("#wishListLink").attr("href");// true or false
								if ( isUserLoggedIn == "false" ){
											attentionBox_logIn(destination);
											return false;// <-- Keeps page from going to link on its own
											}// if
											
							}//function			
				);	

// NOTE: Not sure if this is being used
// no time to check right now so clean this up when you have time
$(".add2wishlist-det").click(function() {	
						var prod_id = this.id;	
						alert("ID: "+ prod_id );
						$.post( 
									"/wishlist",
									{ prod_id: prod_id },
									function(data){	
										var url = "/wishlist";    
										$(location).attr('href',url);
									});
						return false;
	});//$(".add2wishlist").click
	

// ////////////////////////////////////////////////////////////////////////////
//                  NEWSLETTER SIGN UP
// ///////////////////////////////////////////////////////////////////////////// 
$("#btn-newsletter").click(function() {
// on click  validate the email
var email = $("#newsletter-email").val();
//var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
var emailExp = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if(email.match(emailExp)){
	            $.post( 
						   "/_ajax/newletter_signups.php",
							{ email: email },
							function(data){
								$("#response").html(data);
								$("#newsletter-email").val('');
								}// function
							);// post
			  //error message.
			  $("#response").html('<p class="message2 strong">Thank You</p>');		
			  return false;
	}else{ 
			   //error message.
			   $("#response").html('<p class="message2 strong">Error: Please type a valid email.</p>');
			   return false;
	}
	
});

// ////////////////////////////////////////////////////////////////////////////
//                  CONTACT US
// ///////////////////////////////////////////////////////////////////////////// 
$("a#contactus-submit").click(function(){
		var nameF = $("#contactus-name-first");
		var nameL = $("#contactus-name-last");
		var email  = $("#contactus-email");
		var comment  = $("#contactus-comment");
		var valid  = true;
		
		if( ! validateProspName('contactus-name-first') ){
			$("#contactus-name-first-msg").html('&nbsp;&nbsp;Please enter valid name');
			valid  = false;
			}else{   $("#contactus-name-first-msg").val('&nbsp;&nbsp;*'); }
		if( ! validateProspName('contactus-name-last') ){
			$("#contactus-name-last-msg").html('&nbsp;&nbsp;Please enter valid name');
			valid  = false;
			}else{   $("#contactus-name-last-msg").val('&nbsp;&nbsp;*'); }
		if( ! validateProspName('contactus-email') ){
			$("#contactus-email-msg").html('&nbsp;&nbsp;Please enter valid email');
			valid  = false;
			}else{   $("#contactus-email-msg").val('&nbsp;&nbsp;*'); }
		if( valid ) {
		$("#response").html('<h4> IS VALID </h4>');
		// submit
		$.post( 
				      "/_ajax/contact-us-process.php",
					  { email: email, nameF: nameF, nameL: nameL, comment: comment },
					  function(data){
					    $("#response").html(data);
					   }// function
					);// post		
		}
	return false;
});

// MAKE SIMILAR MASTER (featured)
$(".make-similar-master").click(function() {
    var master  = this.id;
	$.post( 
				 "/_ajax/make_similar_master.php",
				{ master: master },
				function(data){
						$("#similars-pane").html(data);
						$("#similars-pane").css('display', 'block');
						}// function
			  );// post
});
// ADD SIMILAR
$(".add-similar").click(function() {
    var similar  = this.id;
	$.post( 
				 "/_ajax/add_similar.php",
				{ similar: similar },
				function(data){
						$("#similars-pane").html(data);
						$("#similars-pane").css('display', 'block');
						}// function
			  );// post
});
// ADD Matched Set
$(".add-matched-set").click(function() {
    var matchedSet  = this.id;
	$.post( 
				 "/_ajax/add_similar.php",
				{ matchedSet: matchedSet },
				function(data){
						$("#similars-pane").html(data);
						$("#similars-pane").css('display', 'block');
						}// function
			  );// post
});


});	// END: $(document).ready(function()

function attentionBox_logIn( destination ){
// Put the log in code into the Attention Box
	$.post( 
				// beginning slash needed due to modReWrite
					"/_templates/attentionBox_LogIn.tmp.php", 
					{ destination: destination },
					function(data){ 
							$("#ui_div").html(data);
							}
	);// $post					
	showAttentionBox();
}//




 // ////////////////////////////////////////////////////////////////////////////
//                                ATTENTION BOX 
// ///////////////////////////////////////////////////////////////////////////// 
function showAttentionBox(){
	$("#ui_div").addClass("attention-box"); // make attention box visible
	$("#root").addClass("subdue"); // fade the background stuff
}//function

function hideAttentionBox(){ 
// NOTE: This also empties the code from the attention-box
	$("#ui_div").removeClass("attention-box"); // remove - make attention box visible
	$("#root").removeClass("subdue"); // un-fade the background stuff
	$("#ui_div").html('');// Important: Fill the Div with emptyness once you're done.
}//function

$(".attention-box input").focus(function() {
		$(this).addClass("input-focus");
	});
$(".attention-box input").blur(function() {
		$(this).removeClass("input-focus");
	});


// ////////////////////////////////////////////////////////////////// 
// //////////////////////////////////////////////////////////////////









