/**
 * jquery.c42.header.js
 * 
 * Shared header widget for all sites.
 * 
 * @displayOpts:
 * 	- url : url for header view, defaults to : shared/js/html/jquery.c42.header.html
 */


$.widget('ui.header', {

	options: {
		displayOpts: {
			url: "shared/html/jquery.c42.header.html"
		},
		ready: function(self) { }
	},

	_create: function() {
		var self = this;
		var element = this.element;

		element.addClass('header');

		// Load the view URL contents as HTML then initialize the widget
		element.load(self.options.displayOpts.url, function() {
			
			// reset mega-menu active state
			element.find(".active-mega-menu").hide();
			
			// display menu items only based on current cp site
			var isConsumer = document.location.href.indexOf("consumer") != -1;
			var isEnterprise = document.location.href.indexOf("enterprise") != -1;
			var isBusiness = document.location.href.indexOf("business") != -1;
			var isPROConsole = window.location.pathname.match(/^\/console/); 
			var isPROSupport = document.location.href.indexOf("/business/support") != -1;
			var isPROSupportStg = document.location.href.indexOf("prowiki.support.c42") != -1;
			var isPROeSupport = document.location.href.indexOf("support.crashplanpro.com") != -1;
			var isPROeSupportStg = document.location.href.indexOf("proewiki.support.c42") != -1;
			
			if (isBusiness || isPROConsole || isPROSupport || isPROSupportStg) {
				// set buy/try links
				element.find("#nav-try").attr("href", "/business/signup.html").addClass('crashplanpro-app');
				element.find("#nav-buy").attr("href", "/business/store.vtl").addClass('crashplanpro-app');
				// set support link
				element.find("#nav-support a").attr("href", "http://www.crashplan.com/business/support");
				// activate current site's mega menu
				element.find("#business-mega-menu-inactive").hide();
				element.find("#business-mega-menu-active").show().stackTop();
				// set account link
				element.find(".account-link").attr("href", "/console/login.html").addClass('crashplanpro-app');
			} else if (isEnterprise || isPROeSupport || isPROeSupportStg){
				element.find("#nav-try").attr("href", "/enterprise/download.html");
				element.find("#nav-buy").attr("href", "/enterprise/store.vtl");
				element.find("#nav-support a").attr("href", "http://support.crashplanpro.com");
				element.find("#enterprise-mega-menu-inactive").hide();
				element.find("#enterprise-mega-menu-active").show().stackTop();;
				element.find(".account-link").hide();
				element.find("#nav-business-download").hide();
			} else {
				// default to root, for consumer and supportpport.crashplan.com
				element.find("#nav-try").attr("href", "/consumer/download.html");
				element.find("#nav-buy").attr("href", "/consumer/store.vtl");
				element.find("#nav-support a").attr("href", "http://support.crashplan.com");
				element.find("#consumer-mega-menu-inactive").hide();
				element.find("#consumer-mega-menu-active").show().stackTop();;
				element.find(".account-link").attr("href", "/account/login.vtl");
				element.find("#nav-business-download").hide();
			}
			
			if (isPROConsole) {
				element.find('.account-link').removeAttr('target');
			}
			
			// dynamically position drop down menus
			var learnMoreMenuPos = (element.find("#nav-learn-more").width() / 2) - (element.find("#mega-menu").width() / 2);
			var buyMenuPos = (element.find("#nav-try-buy").width() / 2) - (element.find("#try-buy-menu").width() / 2);
			element.find("#mega-menu").offset({top:element.find("#header-nav").height(),left:learnMoreMenuPos});
			element.find("#try-buy-menu").offset({top:element.find("#header-nav").height(),left:buyMenuPos});

			// learn more sub nav hover
			element.find("#nav-learn-more").hover(
				function(){
					element.find("#mega-menu", this).css("display","none");
					element.find("#mega-menu", this).fadeIn(100);
				},
				function(){
					element.find("#mega-menu", this).delay(50).fadeOut(100);
				}
			);
			
			// try buy sub nav hover
			element.find("#nav-try-buy").hover(
				function(){
					element.find("#try-buy-menu", this).css("display","none");
					element.find("#try-buy-menu", this).fadeIn(100);
				},
				function(){
					element.find("#try-buy-menu", this).delay(50).fadeOut(100);
				}
			);
			
			// stop click event from propagating up to nav-learn-more click event
			element.find("#mega-menu").click(function(e){ 
				e.stopPropagation(); 
			});
			
			element.find("#try-buy-menu").click(function(e){ 
				e.stopPropagation(); 
			});
			
			// special case for mobile browsers
			element.find("#nav-learn-more").click(function(){
				element.find("#mega-menu").toggle();
				return false;
			});
			
			element.find("#nav-try-buy").click(function(){
				element.find("#try-buy-menu").toggle();
				return false;
			});
			
			// main logo click 
			element.find("#logo").click(function(){
				if(isBusiness || isPROSupport || isPROSupportStg ){
					window.location = "http://www.crashplan.com/business/";
				} else if(isEnterprise || isPROeSupport || isPROeSupportStg){
					window.location = "http://www.crashplan.com/enterprise/";
				} else {
					window.location = "http://www.crashplan.com/";
				}
			});
			
			// append crashplan.com to header relative links
			element.find("a").each(function(){
				var orig = jQuery(this).attr("href");
				if(!orig.match(/^http/)){
					jQuery(this).attr("href", 'http://www.crashplan.com' + orig);
				}
				
			});
			

			self.options.ready && self.options.ready.call(self);
		});


	}
});

