window.BRP = window.BRP || {};

var SelectedSiteMapNodes = "";

BRP = {
	// Main function call (page init)
	Init : function() {
		document.body.className += " hasJS";
		
		if(typeof MCMSConsoleInit == "function") {
			AdvancedHTMLToolsLoad = false;
			MCMSConsoleInit();
			}
		if(typeof initAdvancedHTMLTools == "function") 
		{
			if (AdvancedHTMLToolsLoad)	initAdvancedHTMLTools();		
		}
		}
		
};

YAHOO.util.Event.addListener(window, "load", BRP.Init, BRP, true);

// ------[ Expand ]------------------------------------------------- //	
BRP.Expand = function(el) {
	if (el) {
		this.Root = el;
		YAHOO.util.Event.addListener(window, "load", this.Init, this, true);
	}
};

BRP.Expand.prototype = {
	
	Root : null,

	Init : function() {
		div = document.getElementById(this.Root);
		if(div) {
			dl = div.getElementsByTagName("dl");	

			for (var j=0; j<dl.length; j++) {
				var dt = dl[j].getElementsByTagName('dt');
				dt[0].dl = dl[j];
				dl[j].className += ' closed';
				dt[0].onclick = function() { 
					state = (this.dl.className.indexOf("opened") != -1 ) ? false : true;
					if(state) {
						this.dl.className = this.dl.className.replace('closed','opened');
					} else {
						this.dl.className = this.dl.className.replace('opened','closed');
					}
				};
			}
		}	
	}
};

// ------[ Other Products ]------------------------------------------------- //	
BRP.OtherProducts = {
	IsShow : false,
	Timer : null,
	DivElement : null,
	
	Init : function() {
		this.DivElement = document.getElementById("other-brp-products");
		if(this.DivElement) {
			this.DivElement.onmouseover = this.Show;
			this.DivElement.onmouseout = this.Hide;
			}
		},
	
	Show : function() {
		clearTimeout(BRP.OtherProducts.Timer);
		BRP.OtherProducts.Timer = false;
		BRP.OtherProducts.DivElement.style.display = "block";
		BRP.OtherProducts.IsShow = true;
		window.scrollTo(0,10000000);		
		},
		
	Hide : function() {
		BRP.OtherProducts.Timer = setTimeout('BRP.OtherProducts.HideTimer()',500);
		},

	HideTimer : function() {
		BRP.OtherProducts.Timer = false;
		BRP.OtherProducts.DivElement.style.display = "none";
		BRP.OtherProducts.IsShow = false;			
		},
	
	Display : function() {
		if(this.DivElement) {
			if(!this.IsShow) {
				this.Show();
				//this.Timer = setTimeout('BRP.OtherProducts.HideTimer()',1500);
				}
			else this.Hide();
			}
		}
};
YAHOO.util.Event.addListener(window, "load", BRP.OtherProducts.Init, BRP.OtherProducts, true);

BRP.Cookie = {

	Load: function (id) {
		var search = id + "=";  
		var cookie = null;
		if (document.cookie.length > 0) { 					// if there are any cookies      
			offset = document.cookie.indexOf(search);       
			if (offset != -1) { 							// if cookie exists          
				offset += search.length; 					// set index of beginning of value         
				end = document.cookie.indexOf(";", offset);	// set index of end of cookie value         
				if (end == -1) end = document.cookie.length;
				cookie = unescape(document.cookie.substring(offset, end));   
				}    
			}	
		return cookie;	
	},

	Save: function (id, data, session) {
		if(session) {
			var now = new Date();
			var expires = now.getTime() + 365 * 24 * 60 * 60 * 1000;
			var expires = new Date(expires);
			session = "; expires=" + expires.toGMTString();
		} else session = "";

		document.cookie = id + "=" + escape(data) + session + "; path=/";
	}			


};


// ------[ QueryUrl ]---------------------------------- //
BRP.QueryUrl = {
	
	Search : function(q) {
		if(q.length > 1) this.q = q.substring(1, q.length);
		else this.q = null;
		this.keyValuePairs = new Array();
		
		if(q) {
			for(var i=0; i < this.q.split("&").length; i++) {
				this.keyValuePairs[i] = this.q.split("&")[i];
				}
			}
		
		this.getKeyValuePairs = function() { return this.keyValuePairs; }
		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if(this.keyValuePairs[j].split("=")[0] == s) return this.keyValuePairs[j].split("=")[1];
				}
			return;
			}
		
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
				}
			return a;
			}
		
		this.getLength = function() { return this.keyValuePairs.length; }		
		},
	
	Get : function(key) {
		var page = new this.Search(window.location.search);
		return unescape(page.getValue(key));		
		}
};



// ------[ Send to Friend pop up ]------------------------------------------------- //

/**
 * Base namespace setup
 */
var SKIDOO = {
    globals: {
		labels: {}
	},
    pages: {
        all: {}
    },
    widgets: {}
}

/**
 * Init for all pages
 */
SKIDOO.pages.all.init = function () {
      
	/**
	* Initialize Send to a Friend Pop up page
	*/
	SKIDOO.globals.sendEmail = new YAHOO.widget.Panel(
		"sendtofriend",
		{ 
			width: "600px",
			height: "300px",
			constraintoviewport: true,  
			underlay:"none",  
			close:true,  
			visible:false,  
			fixedcenter: true,  
			draggable:false,
			zIndex: 5,
			modal: false
		}
	);
	//Render the panel for screen
	SKIDOO.globals.sendEmail.render(document.body);
 
	//Create an array with all anchor with the classname "sendfriend"
     var el = YAHOO.util.Dom.getElementsByClassName('sendfriend','a');

     for (var i=0; el[i]; i++) {
		el[i].onclick = function() {
			var pnl = SKIDOO.globals.sendEmail;
			pnl.setBody("<iframe width='600' marginwidth='0' marginheight='0' scrolling='no' frameborder='0' height='300' src='" + this.href + "'></iframe>");
			pnl.render();
			pnl.show();
	        return false;
			}
		el[i].style.visibility = "visible";
      }
 
    

	SKIDOO.globals.clubSupportForm = new YAHOO.widget.Panel(
		"club-support-popup",
		{ 
			width: "837px",
			height: "450px",
			constraintoviewport: true,  
			underlay:"none",  
			close:true,  
			visible:false,  
			fixedcenter: true,  
			draggable:false,
			zIndex: 6,
			modal: true
		}
	);
	//Render the panel for screen
	SKIDOO.globals.clubSupportForm.render(document.body);

	//Create an array with all anchor with the classname "clubsupportpopup"
		var elm = YAHOO.util.Dom.getElementsByClassName('clubsupportpopup','a');

		for (var i=0; elm[i]; i++) {
		elm[i].onclick = function() {
			var pnlt = SKIDOO.globals.clubSupportForm;
			pnlt.setBody("<iframe width='837' marginwidth='0' marginheight='0' scrolling='no' frameborder='0' height='450' src='" + this.href + "'></iframe>");
			pnlt.render();
			pnlt.show();
			return false;
			}
		elm[i].style.visibility = "visible";
		}
 }


// ------[ Generic Popup Window ]------------------------------------------------- //
function popWin(url,w,h,scroll,tools,name,center) {
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;
	if(!center) center = true;
	if(!scroll) scroll = 0;
	if(!tools) tools = 0;
	if(!name) name = "pop";

	if((window.screen) && (center)) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - w) / 2;
		var yc = (ah - h) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		}
	window.name = "main";
	pop = window.open(url,name,'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=0,' + str).focus();
	}

// ------[ Pull-down menu Faq Template ]------------------------------------------------- //

function setFaqCategory (newAddress) {

	if(newAddress!="") {

		window.top.location.href=newAddress;

		}

	}
	
// ------[ Home - Find a dealer button and textField ]--------------------------------------------- //

BRP.FindADealer = {
	
	TextBoxInstance : null,
	ButtonInstance : null,
	Culture : null,
	//initialisation function that must called on page load to enable functionality
	HookEvents: function(culture,textBoxId, buttonId)
	{
		this.TextBoxInstance = document.getElementById(textBoxId);
		this.ButtonInstance = document.getElementById(buttonId);
		this.Culture = culture;
		YAHOO.util.Event.on(this.TextBoxInstance,'keypress',this.LocationKeyPressed);
		YAHOO.util.Event.on(this.TextBoxInstance,'focus',this.Focused);
		YAHOO.util.Event.on(this.ButtonInstance,'click',this.ButtonPressed);
	},
	//if the user pressed enter while typing in the search box open the pop up.
	LocationKeyPressed: function(e, params)
	{
		e = e || event;
		
		if((e.keyCode === 13))
		{
			YAHOO.util.Event.preventDefault(e);
            BRP.FindADealer.OpenFindADealerPopup();
		}
	},
	//if the user set focuses on the textbox pre-filled with the hint default value text (ex: "Address & City") 
	//clear the box 
	Focused:function(e,params)
	{
		if(this.getAttribute('initialValue')==this.value)
		{
			this.value='';
		}
	},	
	//the user has clicked on the search button triggers the pop up opening
	ButtonPressed: function(e, params)
	{
		e = e || event;
		YAHOO.util.Event.preventDefault(e);
        BRP.FindADealer.OpenFindADealerPopup();
	},
	
	OpenFindADealerPopup: function()
	{
		var requestedLocation=BRP.FindADealer.TextBoxInstance.value;
		//if the user has clicked on find a dealer without entering any information in the textbox it stills contains the default hint text
		//so we open the popup with an empty location as parameter.
		if(BRP.FindADealer.TextBoxInstance.getAttribute('initialValue')==BRP.FindADealer.TextBoxInstance.value)
		{
			requestedLocation='';
		}
		//popWin('http://www.brp.com/' + BRP.FindADealer.Culture + '/Dealer.Locator/Ski-Doo?location='+escape(requestedLocation),850,615,1,0);		
		popWin('http://www.brp.com/BRPDealerLocator/Search.aspx?culture=' + BRP.FindADealer.Culture + '&location='+escape(requestedLocation),920,615,1,0);
	}
};
