var genericLoadingText = '<div align="center"><br><span class="t0">Loading. . .<br><br></span><img src="<?=site_url;?>images/loading/1.gif"></div>'; // generic loading text for pages
var timeout;
var currentURI;
var currentCategoryDivID = null;

$(document).ready(function() { // works like onLoad
//  PrepareSearchForm(); // get the search form ready
/*
 if(document.location.hash != "") { // if refreshed with the hash URI
  var page_id = ((document.location.hash).replace("#", "")).replace(".html", "");  
  LoadPageContent(page_id);

   var hashURI = /#/;
   if (formFlag.test(document.location.hash)) { // if a match found for <...>

   } // end of if a match found for <...>

  } // end of if refreshed with the hash URI
*/
  TimerStart();
}); // end of document ready      


function PrepareSearchForm() {
  var options = { 
        target:        '#pageBody',   // target element(s) to be updated with server response 
        url:           '_action_search.php',         // override for form's 'action' attribute 		
        beforeSubmit:  beforeSearchSubmit,  // pre-submit callback 
        success:       afterSearchSubmit  // post-submit callback 
  }; 
   // bind form using 'ajaxForm' 
 $('#searchForm').ajaxForm(options); 

 function beforeSearchSubmit(formData, jqForm, options) { 
  var queryString = $.param(formData); 
  //alert(queryString);
  return true; 
 } // end of beforeSearchSubmit
 
   // post-submit callback 
 function afterSearchSubmit(responseText, statusText)  { 
  //alert(responseText + " --- " + statusText);
 } // end of afterSearchSubmit

} // end of PrepSearchForm



function TimerStart() {
 currentURI = document.location.href;	
 timeout = setTimeout('TimeoutTrigger()', 250);
} // end of TimerStart



function TimerStop() {
 clearTimeout(timeout);
} // end of TimerStop




function TimeoutTrigger() {
 if(window.location != currentURI) {	// if browser URL is not the saved URL, means navigation button pressed
  //alert("WILL CHANGE TO " + window.location + " FROM " + currentURI);
  //window.location = window.location;
//  location.replace(window.location);

  LoadHashURL();
 } // end of if browser URL is not the saved URL, means navigation button pressed
 TimerStart(); // start timout again
} // end of TimeoutTrigger

function LoadHashURL() {
 var page_id = ((document.location.hash).replace("#", "")).replace(".html", "");  
 LoadPageContent(page_id);
} // end of LoadHashURL



function DisplayBreadCrumbs() {
 var divID = "#breadCrumbs"
 $.ajax({
   type: "POST",
   url: "libBody.php",
   data: "echo=1&functionName=BreadCrumbs",
   success: function(msg){
   $(divID).html(''); // clear the content
    $(divID).html(msg);
   }
 });
} // end of DisplayBreadCrumbs



function LoadPageContent(page_id) { 
 var contentURL = "_content_" + page_id + ".php"; 
 /*
 $.ajax({
   type: "POST",
   url: contentURL,
   //data: "echo=1&functionName=PageHeader&functionParameters[page_id]=" + page_id,
   success: function(msg){
	 LoadPageHeader(page_id);
	 LoadPageMenu(page_id);
	 $("#pageContent").html(msg);
     history.previous(contentURL);  
//    window.location.replace(contentURL);
   }
   */
 });
 
 
} // end of LoadPageHeader


function LoadPageHeader(page_id) { 
 $.ajax({
   type: "POST",
   url: "libHeader.php",
   data: "echo=1&functionName=PageHeader&functionParameters[page_id]=" + page_id,
   success: function(msg){
	 $("#pageHeader").html(msg);
   }
 });
} // end of LoadPageHeader



function LoadPageMenu(page_id) {
 $.ajax({
   type: "POST",
   url: "libHeader.php",
   data: "echo=1&functionName=PageHeader&functionParameters[page_id]=" + page_id + "",
   success: function(msg){
	 $("#pageHeader").html(msg);
   }
 });
} // end of LoadPageMenu



function LoadStoreImageThumb(index) {
 $.ajax({
   type: "POST",
   url: "libBody.php",
   data: "echo=1&functionName=StoreImageThumb&functionParameters[index]=" + index + "",
   success: function(msg){
	 $("#storeImageThumbDiv").html(msg);
   }
 });
	
} // end of store images



function LoadStoreImage(index) {
 var contentURL = "libBody.php";
 var contentData = "echo=1&functionName=StoreImage&functionParameters[index]=" + index + ""; 
 $.ajax({
   type: "POST",
   url: contentURL,
   data: contentData,
   success: function(msg){
	 new Boxy(msg, {title: 'waterelated.com', y:15, modal: true, fixed:false});
   }
 });
}


function LoadTestimonial() {
 var contentURL = "_content_testimonial.php";
 var contentData = ""; 
 $.ajax({
   type: "POST",
   url: contentURL,
   data: contentData,
   success: function(msg){
	 new Boxy(msg, {title: 'waterelated.com - testimonials', y:15, modal: true});
   }
 });
}


function DisplayProductDescriptionPage(productID) {
 var divID = "#pageContent";
 $.ajax({
   type: "POST",
   url: "_content_productDescription.php",
   data: "product_id=" + productID,
   success: function(msg){
	$(divID).html(''); // clear the content
    $(divID).html(msg);
	DisplayBreadCrumbs();
   }
 });
}



function DisplayProductFullDescription(productID) { 
// alert("PRODUCT ID: " + productID + " LINK ID:" + linkID);
 var divID = "#productFullDescriptionDiv_" + productID;
 $.ajax({
   type: "POST",
   url: "libProductDisplay.php",
   data: "echo=1&functionName=ProductFullDescription&functionParameters[productID]=" + productID,
   success: function(msg){
    $(divID).html(msg);
	var position = $(divID).position();
    $.scrollTo((position.top - 70), 0);
   }
 });
} // end of DisplayProductFullDescription



function CloseProductFullDescription(divID) { 
 $(divID).html('');
} // end of DisplayProductFullDescription









function LoadIndexPage() {
 var loadingText = genericLoadingText;	
	
 LoadPageHeader();
 
 if(document.location.hash != "") { // if hash URL
  var url = "hashURL.php?hashURL=" + escape(document.location.hash);

 } else { // index page

 } // end of if hash URL
} // end of LoadIndexPage



function LoadPageLeftMenu(page_id) { 
 $.ajax({
   type: "POST",
   url: "libMenu.php",
   data: "echo=1&functionName=LeftMenu&functionParameters[page_id]=" + page_id + "",
   success: function(msg){
	 $("#leftMenu").html(msg);
   }
 });
} // end of LoadPageMenu



function DisplayProductSubCategory(parentID, title) { 
 var divID = "#subCategoryDiv_" + parentID;
 var linkDivID = "#subCategoryLinkDiv_" + parentID;
 var closeLink = '<a href="javascript:void(0);" onClick="CloseProductSubCategory(\'subCategoryDiv_' + parentID + '\', \'' + parentID + '\', \'' + title + '\')" id="productCategorySelectedLink">' + title + '</a>';

 $.ajax({
   type: "POST",
   url: "libProductCategoryDisplay.php",
   data: "echo=1&functionName=ProductSubCategory&functionParameters[parentID]=" + parentID,
   success: function(msg){
    $(divID).html(msg);
	$(linkDivID).html(closeLink);
	var position = $(divID).position();
    //$.scrollTo((position.top - 70), 0);
   }
 });
 
 
 
  
 
 
 
 $.ajax({
   type: "POST",
   url: "_content_category.php",
   data: "category_id=" + parentID,
   success: function(msg){
    $("#pageContent").html(msg);
	LoadPageMenu('products')
    //$.scrollTo((position.top - 70), 0);
   }
 }); 
 
} // end of DisplayProductFullDescription




function CategoryClickAction(id) {
	var divID = "#subCategoryDiv_" + id;
	var linkDivID = "#subCategoryLinkDiv_" + id;
	
	if(currentCategoryDivID != null) { // close category if open, null means fresh page, no category been open 
		$("#" + currentCategoryDivID).html('');
	} // end of close category if open, null means fresh page, no category been open
	
	currentCategoryDivID = divID;
	
	
	//var closeLink = '<a href="javascript:void(0);" onClick="CloseProductSubCategory(\'subCategoryDiv_' + parentID + '\', \'' + parentID + '\', \'' + title + '\')" id="productCategorySelectedLink">' + title + '</a>';

 $.ajax({
   type: "POST",
   url: "libProductCategoryDisplay.php",
   data: "echo=1&functionName=ProductSubCategory&functionParameters[parentID]=" + id,
   success: function(msg){
    $(divID).html(msg);
	$(linkDivID).html(closeLink);
	var position = $(divID).position();
    //$.scrollTo((position.top - 70), 0);
   }
 });
 
 
 
}



function CloseProductSubCategory(divID, parentID, title) { 
 var linkDivID = "#subCategoryLinkDiv_" + parentID;
 var openLink = '<a href="javascript:void(0);" onClick="DisplayProductSubCategory(\'' + parentID + '\', \'' + title + '\')" id="productCategoryLink">' + title + '</a>';
 $(('#' + divID)).html('');
 $(linkDivID).html(openLink);
} // end of DisplayProductFullDescription