﻿//add function to bind all click events 
function bindAllClickEventsForBreadCrumb()
{
	$("*").each( function()
	{
		if($(this).attr("onclick") != undefined && $(this).attr("onclick") != null && $(this).attr("onclick") != "")
		{
			$(this).mousedown(function(){
						bcElementClicked(this);
						return true;});
		}
/*		else if($(this).attr("onchange") != undefined && $(this).attr("onchange") != null && $(this).attr("onchange") != "")
		{
			$(this).click(function(){
						bcElementClicked(this);
						return true;});
		}*/
		else if (this.nodeName.toLowerCase() == "a")
		{
			$(this).mousedown(function(){
						bcElementClicked(this);
						return true;});
		}
	}
	);
	$("form").submit( function () {
		disableRefreshDetection();
		return true;
	} );
}

function setUpBreadCrumbInfo(){
	bindAllClickEventsForBreadCrumb();
	if(!refreshDetected())
	{
		if(document.referrer.toLowerCase().indexOf("castore.ca") != -1)
		{
			if($("#divPageBodyBreadcrumbTopContent").length == 0)
			{
				appendToBreadCrumbCookie("|" + $("#divPageBodyFriendlyNameTopContent").html() + "|" + location.href + "|hideBC%%%" );
			}
			else
			{
				appendToBreadCrumbCookie("|" +  $("#divFriendlyName").html() + "|" + location.href + "|showBC%%%");
				createBreadCrumb();
			}
			//readBreadCrumbCookie();
		}
		else
		{
			resetBreadCrumbCookie();
			if($("#divPageBodyBreadcrumbTopContent").length == 0)
			{
				appendToBreadCrumbCookie("2|" + $("#divPageBodyFriendlyNameTopContent").html() + "|" + location.href + "|hideBC%%%" );
			}
			else
			{
				appendToBreadCrumbCookie("2|" +  $("#divFriendlyName").html() + "|" + location.href + "|showBC%%%");
				createBreadCrumb();
			}
		}
	}
	else
	{
		if($("#divPageBodyBreadcrumbTopContent").length != 0)
		{
			removeFromBreadCrumbCookie(1);
			createBreadCrumb();
		}
	}			
	
	
	$(window).unload( function () 
						{ 
							appendToBreadCrumbCookie(bcAction);	
							prepareForRefresh();
						} );
}



function createBreadCrumb()
{
	fullTravelledPath = readBreadCrumbCookie();
	PagesTraveresed = fullTravelledPath.split("%%%");
	var breadCrumbHTML = "";
	var add2HTML = true;
	for(i=PagesTraveresed.length - 2; i >= 0 ; i--)
	{
		pageInfo = PagesTraveresed[i].split("|");
		if(add2HTML && pageInfo[1].length > 0)
		{
			
			if(breadCrumbHTML.indexOf(">"  + pageInfo[1] + "<") == -1 )
			{
				if(breadCrumbHTML == "")
				{
					//breadCrumbHTML = "<span class=\"BreadcrumbTrailItem\">" + pageInfo[1] + "</span>" + breadCrumbHTML;
					breadCrumbHTML = "<li>" + pageInfo[1] + "</li>" + breadCrumbHTML;
				}
				else
				{
					//breadCrumbHTML = "<span class=\"BreadcrumbTrailItem\"><a href=\"" + pageInfo[2] + "\" onclick=\"bcElementClicked(this);return true;\" class=\"BreadcrumbTrail\">" + pageInfo[1] + "</a>&nbsp;&gt;&nbsp;</span>" + breadCrumbHTML;
					breadCrumbHTML = "<li><a href=\"" + pageInfo[2] + "\" onclick=\"bcElementClicked(this);return true;\" >" + pageInfo[1] + "</a></li>" + breadCrumbHTML;
				}
			}
		}
		if(pageInfo[1] == "Home")
		{
			break;
		}
		
		//my parent is home
		if(pageInfo[0] == 2 || pageInfo[0] == -1)
		{
			if(location.host.indexOf("local") != -1)
			{
				homeLink = location.protocol + "//" + location.host + "/Home.aspx";
			}
			else
			{
				homeLink = location.protocol + "//" + location.host + "/Home.aspx";
			}
			//breadCrumbHTML = "<span class=\"BreadcrumbTrailItem\"><a href=\"" + homeLink + "\" onclick=\"bcElementClicked(this);return true;\" class=\"BreadcrumbTrail\">Home</a>" + (breadCrumbHTML.length > 0 ? "&nbsp;&gt;&nbsp;" : "") + "</span>" + breadCrumbHTML;
			breadCrumbHTML = "<li><a href=\"" + homeLink + "\" onclick=\"bcElementClicked(this);return true;\" >" + GetString("str_CAStoreHome") + "</a></li>" + breadCrumbHTML;
			break;
		}
		else if (pageInfo[0] != 1)
		{
			add2HTML = true;
		}
		//my parent is a few pages back
		else if (pageInfo[0] == 1)
		{
			for(j = i - 1; j >= 0; j--)
			{
				pageInfo2 = PagesTraveresed[j].split("|");
				i=j+1;
				if(pageInfo2[1] == pageInfo[1])
				{
					add2HTML = false;
					break;
				}
				else if(j==0)
				{
					add2HTML = false;
					break;
				}
			}
		}
	}

	$("#divPageBodyBreadcrumbTopContent").html("<div class='breadCrumbHolder module'><div id='divBreadCrumb0' class='breadCrumb module'><ul>" + breadCrumbHTML + "</ul></div></div><div class='chevronOverlay main'></div>");
	$("#divBreadCrumb0").jBreadCrumb();
}



function readBreadCrumbCookie(){
	var currentCookie = $.cookie("CAStoreBreadCrumbsCookie");
	if(currentCookie == null)
	{
		return "";
	}
	return currentCookie;
}

function resetBreadCrumbCookie(){
	 var options = { path: '/', expires: 10 };
	 $.cookie("CAStoreBreadCrumbsCookie", "", options); 
}

function appendToBreadCrumbCookie(val){
	var options = { path: '/', expires: 10 };
	var currentCookie = $.cookie("CAStoreBreadCrumbsCookie");
	if(currentCookie!=null && currentCookie.length > 3000)
	{
		//alert(currentCookie);
		//currentCookie = currentCookie.replace(/^%%%.*/, "");
		currentCookie = currentCookie.replace(/^.[^%%%]*%%%/, "");
	}
	if(currentCookie == null)
	{
		currentCookie = "";
	}
	$.cookie("CAStoreBreadCrumbsCookie", currentCookie + val, options); 
}

function removeFromBreadCrumbCookie(numberOfRemoves)
{
	var currentCookie = $.cookie("CAStoreBreadCrumbsCookie");
	var newCookieArray = currentCookie.split("%%%");
	newCookieArray.length = newCookieArray.length - numberOfRemoves;
	var options = { path: '/', expires: 10 };
	var currentCookie = newCookieArray.join("%%%") + "%%%";
	$.cookie("CAStoreBreadCrumbsCookie", currentCookie, options);	
}


var bcAction = -1;

function bcElementClicked(elm){
	//add to shopping cart case
	if($(elm).parents("#tdAddToShoppingCart").length != 0 )
	{
		bcAction = 2;
	}
	else if($(elm).parents("#divPageBodyBreadcrumb").length != 0 )
	{
		//alert("Clicked in body do not do anything");
		//clicked in page body, i am the parrent of next
		bcAction = 0;
	}
	else if ($(elm).parents("#divLeftNav").length != 0)
	{
		if(elm.checked)
		{
			bcAction = 1;
		}
		else
		{
			bcAction = 0;
		}
	}
	else if ($(elm).parents("#divPageBodyBreadcrumbTop").length != 0)
	{
		//alert("Clicked in breadcrumb, go back");
		//clicked in breadcrump, one of my parents are the parent of next
		bcAction = 1;
	}
	else
	{
		//alert("Clicked outside, go home");
		//clicked outside, home is the parent
		bcAction = 2;
	}
}

/* *********************** */
/* refresh detection begin */
/* *********************** */
function refreshDetected()
{
	// Get the time now and convert to UTC seconds
	var today = new Date();
	var now = today.getUTCSeconds();

	// Get the cookie
	//var cookie = document.cookie;
	//var cookieArray = cookie.split('; ');

	// Parse the cookies: get the stored time
	/*for(var loop=0; loop < cookieArray.length; loop++)
	{
		var nameValue = cookieArray[loop].split('=');
		// Get the cookie time stamp
		if( nameValue[0].toString() == 'SHTS' )
		{
			var cookieTime = parseInt( nameValue[1] );
		}
		// Get the cookie page
		else if( nameValue[0].toString() == 'SHTSP' )
		{
			var cookieName = nameValue[1];
		}
	}*/
	
	var cookieTime = $.cookie("SHTS");
	var cookieName = $.cookie("SHTSP");

	if( cookieName &&
		cookieTime &&
		cookieName == escape(location.href) &&
		Math.abs(now - cookieTime) < 5 )
	{
		// Refresh detected

		// Insert code here representing what to do on
		// a refresh
		return true;
		// If you would like to toggle so this refresh code
		// is executed on every OTHER refresh, then 
		// uncomment the following line
		// refresh_prepare = 0; 
	}	

	return false;
	// You may want to add code in an else here special 
	// for fresh page loads
}

function prepareForRefresh()
{
	if( refresh_prepare > 0 )
	{
		// Turn refresh detection on so that if this
		// page gets quickly loaded, we know it's a refresh
		var today = new Date();
		var now = today.getUTCSeconds();
		var options = { path: '/', expires: 1 };
		$.cookie("SHTS", now, options); 
		$.cookie("SHTSP", escape(location.href), options); 
		//document.cookie = 'SHTS=' + now + ';';
		//document.cookie = 'SHTSP=' + escape(location.href) + ';';
	}
	else
	{
		// Refresh detection has been disabled
		var options = { path: '/', expires: 1 };
		$.cookie("SHTS", "", options); 
		$.cookie("SHTSP", "", options); 
		//document.cookie = 'SHTS=;';
		//document.cookie = 'SHTSP=;';
	}
}

function disableRefreshDetection()
{
	// The next page will look like a refresh but it actually
	// won't be, so turn refresh detection off.
	refresh_prepare = 0;

	// Also return true so this can be placed in onSubmits
	// without fear of any problems.
	return true;
} 

// By default, turn refresh detection on
var refresh_prepare = 1;
/* *********************** */
/*  refresh detection end  */
/* *********************** */
