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") 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);

// ------[ Skip Intro for home template ]---------------------------------- //	
BRP.HomeSkipIntro = {

   	ID : null,
	Value: false,
	
	GetValue : function() {
		var isSkip = this.Value;
		this.Value = true;
		BRP.Cookie.Save(this.ID, this.Value, true);
		return isSkip;
		},
	
	Init: function(id) {
		this.ID = id;
		this.Value = BRP.Cookie.Load(this.ID);
		
		/* Patch for SeaDooFilm and Future Of Boating*/
		if((id == 'TheFutureofBoating') || (id == 'SeaDooFilms')) {
			var q=BRP.QueryUrl.Get("skipIntro");
			if(q == 'false') {
				this.Value = false;
			} else {
				this.Value = 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));		
		}
};


// ------[ Google Analytics 4 DHTML/FLASH ]---------------------------------- //	
BRP.GoogleAnalytics = {
	Set : function(id) {
		
		switch(id) {
			case 'PWC2YearsOfferPDF' : 
				this.Track('/Promotions/PWC.2.Years.Offer.PDF');
				break;
			
			case 'SB2YearsOfferPDF' : 
				this.Track('/Promotions/SB.2.Years.Offer.PDF');
				break;		
			
			default : 
				this.Track('/' + cultureID + '/Consumer.Advisor.html/' + id);				
			}		
		},
		
	Track : function(path) {
		urchinTracker(path);
		}

};
BRP.Search = {
    GoogleSearch: function(e, triggerId) {
        //e is event object passed from function invocation
        var characterCode;
        if (e != null) { //if which property of event object is supported (NN4)
            e = e
            characterCode = e.keyCode //character code is contained in NN4's which property
        }
        else {
            e = event
            characterCode = e.keyCode //character code is contained in IE's keyCode property
        }

        if (characterCode == 13) { //if generated character code is equal to ascii 13 (if enter key)
            var targetElement = document.getElementById(triggerId);
            YAHOO.util.Event.preventDefault(e);
            targetElement.click();
            return false
        }
        else {
            return true
        }
    }
}

// ------[ Gallery Browser ]------------------------------------------------- //	
Gallery1 = new Array();
Gallery2 = new Array();
Gallery3 = new Array();

BRP.Gallery = {
	
	Picture : null,
	Gallery : null,
	Preview : null,
	ImageFader : null,
	Link : null,
	Animation : null,
	
	Init : function() {
		if(Gallery1.length > 0) this.SetupGallery(1,0);
		else if(Gallery2.length > 0) this.SetupGallery(2,0);
		else if(Gallery3.length > 0) this.SetupGallery(3,0);
		},
	
	SetupGallery : function(galleryID, pictureID) {
		this.Gallery = eval('Gallery' + galleryID);
		this.Link = document.getElementById("gal-" + galleryID + "-" + pictureID);
		this.Picture = pictureID;
		this.Preview = document.getElementById("BoxPicture");
		
		this.ImageFader = document.createElement("img");
		this.ImageFader.setAttribute("id", "image-fader");
		this.ImageFader.src = '/SeaDooPublic/_Static/neutral/Images/spc.jpg';
		this.ImageFader.width = this.Preview.width;
		this.ImageFader.height = this.Preview.height;
		this.ImageFader.style.position = "absolute";
		this.ImageFader.style.left = "6px";
		this.ImageFader.style.top = "7px";
		YAHOO.util.Dom.setStyle(this.ImageFader, "opacity", 0);
		
		this.Preview.parentNode.insertBefore(this.ImageFader,this.Preview);
		
		this.SetPicture(galleryID, pictureID, this.Link)
		},
		
	SetPicture : function(galleryID, pictureID, href) {
		if(this.Animation) this.Animation.stop();
		
		this.Link.className = "";
		this.Link = href;
		this.Link.className = "selected";
		
		this.Gallery = eval('Gallery' + galleryID);
		this.Picture = pictureID;
		
		YAHOO.util.Dom.setStyle(this.ImageFader, "opacity", 0);
		this.ImageFader.src = BRP.Gallery.Gallery[0][pictureID];
		this.Animation = new YAHOO.util.Anim(BRP.Gallery.ImageFader, { opacity: { to: 1 } }, 0.5);

		this.Animation.onComplete.subscribe(function(type, args) {
			BRP.Gallery.Preview.src = BRP.Gallery.ImageFader.src;
			});				

		this.Animation.animate();					
		},
		
	Save : function(size) {
		GalleryBrowser = window.open(this.Gallery[size][this.Picture],'GalleryBrowser','toolbar=1,location=0,directories=1,status=1,menubar=1,scrollbars=1,resizable=1');
		if(window.focus) GalleryBrowser.focus()
		}
};

// ------[ Generic Popup Window ]------------------------------------------------- //
function TellAFriend() {
	var str = "height=363,innerHeight=363";
	str += ",width=410,innerWidth=410";
	if(window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;

		var xc = (aw - 410) / 2;
		var yc = (ah - 363) / 2;

		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
		}
	var url = webSiteCMSUrl+'/'+cultureID+'/TellaFriend.htm';
	pop = window.open(url,'friend','toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,' + str).focus();	
	}

// ------[ Generic Popup Window ]------------------------------------------------- //
function popWin(url,w,h,scroll,tools,name,center,baseUrl) {
	var str = "height=" + h + ",innerHeight=" + h;
	str += ",width=" + w + ",innerWidth=" + w;
	if(baseUrl) url = webSiteCMSUrl + url;
	if(!center) var center = false;
	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;
		}
	
	var url2 = url;
	
	if (url.indexOf("TellaFriend") >= 0)
	{
		url2 += '?opener=' + encodeURIComponent(window.location.href)
	}	
	
	pop = window.open(url2,name,'toolbar=' + tools + ',location=0,directories=0,status=0,menubar=0,scrollbars=' + scroll + ',resizable=1,' + str).focus();
}


/**
 * Base namespace setup
 */
var Corporate = {
    globals: {
		labels: {}
	},
    pages: {
        all: {}
        
    },
    widgets: {}
}

/**
 * Init for all pages
 */ 
Corporate.pages.all.init = function (){
      Corporate.globals.overviewVideo = new YAHOO.widget.Panel(
		"overview-video",
		{ 
			width: "555px",
			height: "400px",
			constraintoviewport: true,  
			underlay:"shadow",  
			close:true,  
			visible:false,  
			fixedcenter: true,  
			draggable:false,
			zIndex: 5,
			modal: true //Window bkg
		}
	);
	//Render the panel for screen
	Corporate.globals.overviewVideo.render(document.body);
	
	//set the body to Nothing on Close
	Corporate.globals.overviewVideo.beforeHideEvent.subscribe(function () {
	    Corporate.globals.overviewVideo.setBody("");
	});

	
	

	//Create an array with all anchor with the classname "overview-video"
     var el = YAHOO.util.Dom.getElementsByClassName('overview-video','a');

     for (var i=0; el[i]; i++) {
		el[i].onclick = function() {		    
		    var pnl = Corporate.globals.overviewVideo;
		    
			pnl.setBody("<iframe allowtransparency='true' width='555' marginwidth='0' marginheight='0' scrolling='no' frameborder='0' height='400' src='" + this.href + "'></iframe>");
			pnl.render();
			pnl.show();
	        return false;
			}
		el[i].style.visibility = "visible";
      }
}

YAHOO.util.Event.addListener(window, "load", Corporate.pages.all.init);

//Javascript dropdownlist in the Awards section
var lastDiv = "2009";
function showDiv(divName) {
    // hide last div
    if (lastDiv) {
        document.getElementById(lastDiv).className = "hiddenDiv";
    }
    //if value of the box is not nothing and an object with that name exists, then change the class
    if (divName && document.getElementById(divName)) {
        document.getElementById(divName).className = "visibleDiv";
        lastDiv = divName;
    }
}