/**
 * GarminAsus core javascript
 *
 *	Provide core methods that are used to display the sign in status and the shopping cart display.
 *
 */

var COOKIE_NAME = 'GarminLocalePref_v1.0';
var options = { domain: '.garminasus.com', path: '/', expires: 10000 };
/**
* Core variables used throughout this js
*/
var quantity;
// By default the formatted price is empty it will only be populated if the session cookie is present.
var formattedPrice=""; 
var loggedIn;
var sessionCookie;
var locale;
var homePage = false;


/**
*	pageLoad
*	Update links to be environment specific - NEED TO DISCUSS THIS. RAMIFICATIONS ON SEO?
*
*/

function pageLoad(){

	$(".asus-shop").each(function() {
        	$(this).children("a").each(function() {
                	jQuery(this).attr("href",shopURL);
        	});

	});

}




/**
*	buildCart
*
*	Function that builds the shopping cart display for static pages. 
*/

function buildCart(){

	// If there are no items in the cart then display the empty cart display
	var cartSummary = "<!-- begin cart summary -->";
	cartSummary +="<div class=\"cartSummary\">";
	cartSummary +="<div class=\"summary\">";
	
	if (quantity < 1){	

		cartSummary +="<img class=\"summaryCart\" src=\"/m/g/cart-empty.gif\" alt=\""+cartImageAltText[locale]+"\" />";
		cartSummary +="<div><span class=\"summaryItems\">0</span> "+shoppingCartItemsText[locale]+" <span class=\"summaryTotal\">"+emptyCartPrice[locale]+"</span></div>";
		
		
	} else {
		// There are items in the cart. Display clickable cart.
		cartSummary +="<a href=\""+cartURL+"#\"><img class=\"summaryCart\" src=\"/m/g/cart.gif\" alt=\""+cartImageAltText[locale]+"\" /></a>";
		cartSummary +="<div><a href=\""+cartURL+"\"><span class=\"summaryItems\">"+quantity+"</span> "+shoppingCartItemsText[locale]+"</a> <span class=\"summaryTotal\">"+formattedPrice+"</span></div>";
		
	}
	
	cartSummary +="</div>";
	cartSummary +="</div>";
	cartSummary +="<!-- end cart summary -->";
	
	jQuery(".asus-hd-l").append(cartSummary);



}




/**
*	buildLogInStatus
*
*	Method builds the loginStatus div for static pages.
*/

function buildLogInStatus(){

	var loggedInStatement;
	loggedInStatement="<div id='signInStatus'>";	
	// Is the user logged in 
	if ((sessionCookie) && (sessionCookie.logged)){ 
		loggedInStatement += loggedInText[locale]+sessionCookie.uid+" | <a href=\""+signOutURL+"?target="+window.location+"\">"+signOutLinkText[locale]+"</a>";
	}else{ 
	// User is not logged in 
		loggedInStatement += notLoggedInText[locale]+"| <a href=\""+signInURL+"?target="+window.location+"\">"+signInLinkText[locale]+"</a>";
	}
	
	
	loggedInStatement +="</div>";	
	// Change the html for the SignInStatus div to the html created above.
	jQuery("#asus-header").append(loggedInStatement);
}	



/**
*	readSessionCookie
*
*	method to read the session cookie.
*/
function readSessionCookie(){
	// Read the session cookie. This cookie contains the user's current session info (user name if logged in, formatted price)
	var jsonSessionCookie = jQuery.cookie("GarminAsusSession");
	if (jsonSessionCookie !==  null){
		//Parse the JSON cookie
		sessionCookie = JSON.parse(jsonSessionCookie);
		if  (sessionCookie !== null) {
			// Need to unescape the formatted price as there can be spaces.
			formattedPrice =  decodeURIComponent(sessionCookie.fp);
		}
	}


}

/**
*	readCartCookie
*	read Cart from cookie
*/


function readCartCookie(){
	quantity = 0;
	var jCookie = jQuery.cookie("cart_v1.1");
	if (jCookie != null){
		var cart = JSON.parse(jQuery.cookie("cart_v1.1"));
		if (cart !==  null){
			if (typeof cart.l != "undefined"){
				if (cart.l == locale){
					for(var x=0; x < cart.cis.length; x++) {
						quantity = quantity + cart.cis[x].q;
					}
				}
			}
		}
	}


}

function showDialogToProductRegistration(){
	showDialog('Continue to myGarmin' , 'Close' , 'https://my.garmin.com/mygarmin/marketing/register.htm?locale='+locale);
}


function showDialog(continueButtonVal, cancelButtonVal, url){
	$("#interstitialDialog").wrap("<div class='outerDialog'></div>");
	$("#interstitialDialog").show().dialog("open");
	var btn = "<a href=\"#\" onclick=\"continueToMyGarmin('" + url+ "');return false;\" class=\"button\"><span>" + continueButtonVal + "</span></a>";
	btn += "<a href=\"#\" onclick=\"jQuery('#interstitialDialog').dialog('close');return false;\" class=\"button\"><span>" + cancelButtonVal + "</span></a>";
	$(".ui-dialog-buttonpane").html(btn).show();
}

function continueToMyGarmin(url) {
	window.open(url);
	jQuery('#interstitialDialog').dialog('close');
}

function setLocale(){
	var index = window.location.pathname.indexOf("/");
	locale = window.location.pathname.slice(index+1,index+6);

	jQuery.cookie(COOKIE_NAME, locale, options);
}


/**
*	core method that is called when the page completes loading
*	method will initiate all need functions.
*
*/

jQuery(document).ready(function() {

	pageLoad();
	if (homePage == false){
		setLocale();
		readSessionCookie();
		readCartCookie();
		buildLogInStatus();
		buildCart();
	}
	
	
	
		
 });
