/**
 * jquery.c42.footer.js
 * 
 * A shared footer widget.
 * 
 * @displayOpts:
 *  - barc42 : turn c42 bar on / off 
 * 	- barClients : turn client bar on / off, pass in scrollgallery options to customize it
 * 	- tweets : turn tweets on / off
 *  - contained : footer contained to site default width 930px
 *  - mainBar : turns the main bar on / off, this includes the tweets
 * 	- url : url for footer view, defaults to : /js/html/jquery.c42.footer.html
 */

$.widget('ui.footer', {

	options: {
		displayOpts: {
			mainBar: true,
			barc42: true,
			barClients: true,
			tweets: true,
			contained: false,
			url: "shared/html/jquery.c42.footer.html"
		},
		ready: function(self) { }
	},

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

		element.addClass('footer');

		// Load the view URL contents as HTML then initialize the widget
		element.load(self.options.displayOpts.url, function() {

			var isEnterprise = document.location.href.indexOf("enterprise") != -1;
			var isBusiness = document.location.href.indexOf("business") != -1;
			var isPROSupport = document.location.href.indexOf("/business/support") != -1;
			var isPROeSupport = document.location.href.indexOf("support.crashplanpro") != -1;

			if(isEnterprise || isPROeSupport){
				element.find("#twitter-footer").attr("href","http://www.twitter.com/crashplanpro");
				element.find("#facebook-footer").attr("href","http://www.facebook.com/pages/CrashPlan-PRO/33510439819");
			}else if(isBusiness || isPROSupport){
				element.find("#twitter-footer").attr("href","http://www.twitter.com/crashplanpro");
				element.find("#facebook-footer").attr("href","http://www.facebook.com/pages/CrashPlan-PRO/33510439819");
			}else {
				element.find("#twitter-footer").attr("href","http://www.twitter.com/crashplan");
				element.find("#facebook-footer").attr("href","http://www.facebook.com/CrashPlan");
			}

			// set footer width to 930px if contained is true
			if(self.options.displayOpts.contained){
				jQuery(this).css("width","930px");
			}

			self.barc42 = jQuery(this).find('#c42-bar');
			self.tweets = jQuery(this).find('#footer-tweet');
			self.mainBar = jQuery(this).find('#main-footer');

			if (typeof(self.options.displayOpts.barClients) == "object") {
				self.barClients = jQuery(this).find('#client-scroller').scrollgallery(self.options.displayOpts.barClients);
			} else {
				self.barClients = jQuery(this).find('#client-scroller').scrollgallery();
			}

			self.options.displayOpts.tweets ? self.tweets.tweet() : self.tweets.hide();
			self.options.displayOpts.barClients ? self.barClients.show() : self.barClients.hide();
			self.options.displayOpts.barc42 ? self.barc42.show() : self.barc42.hide();
			self.options.displayOpts.mainBar ? self.mainBar.show() : self.mainBar.hide();

			
			// append crashplan.com to footer 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);
		});


	}

});



