/**
 * @author JPatel
 */

/* GLOBAL FUNCTIONS */
/*SITE CONFIG PROPERTIES */
C4GTI.Global = C4GTI.Global || {};
C4GTI.Global.Controller = (function($) {
	function _initOpenNewWindow(e,index) {
		e = $(e);
		url = e.attr("href");
		if(url!=null) {
		//url = "http://test.channel.com/someyeah/something";
		pattern = /^https?:\/\/(.[^\/]*)\/?/;
		// /^https?             :\/\/        (.[^\/]*)             \/?                      /
		// | begins with http|s |contains ://|anything but a slash|ends with optional slash |
		matched = url.match(pattern);
		if(matched!==null) {
			if(matched[1]!==null) {
				//console.log(matched[1])
				url1 = matched[1];
				url1 = url1.match(/([^\.]+)\.\w+$/);
				/* catches
				word + . + ends with word 
				* */
				if(url1) {
					if(url1[1]!==null) {
						
						
						if(url1[1].toString()!="channel4") {
							//console.log("external to channel4: "+index + ":URL "+ e.attr("href"));
							e.removeAttr("target");
							e.append("<span class='accessibility'> (opens in a new window)</span>")
							if($(this).parents(".ModuleID-HR01")) {
								e.attr("rel","external");
							}
							e.bind("click", function(o) {
								o.preventDefault();
								var w=window.open(e.attr("href"));	
							});
						}
					}
				}
			}
		} 
		}
	}
	function _initLinkModifier() {
		$("#GTiContentWrapper div[id^='cms'] a,#VerticalNavigation a,#FooterContent div[id^='cms'] a").each(function(index) {
			if($(this).attr("name") != "fb_share") {
				_initOpenNewWindow(this,index);
			}
		});
	}
	function _disableLogging() {
		window.console = window.console || {};
		window.console.log = function() { return false;	};
		window.console.info = function() { return false; };
		window.console.warn = function() { return false; };
		window.console.dir = function(){ return false; };
		window.console.group = function(){ return false; };
		window.console.groupEnd = function(){ return false; };			
	}

	function _fixLogging(){
		if (typeof(window.console.clear) == "undefined") {
			window.console.clear = function() {
				return false;
			};
		};
	}
	
	function _backToTopHandler() {
		$('html, body').animate({
			scrollTop: $('#PrimaryContent').offset().top + "px"
		}, "slow");

		return false;
	}
	
	function _toolBoxInit() {

		
		
		/* Fancy Tool Tips */
		/*
		$("#GTiContentWrapper a.fancyTip").each(function() {
			$(this).attr("title","");
			$(this).find("img").attr("alt","");
		});
		*/
		$("#FooterContent a.fancyTip").each(function() {
			$(this).attr("title","");
			$(this).find("img").attr("alt","");
		});
		/* Print and Share */	
		
		$("#toolbox,.toolbox").each( function(){
			
			// Print page
			$(this).find("#print a").click( function(e){
				//console.log("Print page clicked");
				var h = window.location.href.split("#");
				var w = window.open(h[0] + "?print=true",C4GTI.sitename+"printer","height=480,width=640,menubar=yes,scrollbars=yes");
				e.preventDefault();
			});
	
			// Share via Facebook
			$(this).find("#facebook a").click( function(e){
				//console.log("Facebook sharer clicked");
				var w = window.open($(this).attr("href"),C4GTI.sitename+"sharer","height=350,width=640,menubar=no,toolbar=no");
				e.preventDefault();
			});
			
			// Share via Twitter
			$(this).find("#twitter a").click( function(e){
				//console.log("Twitter sharer clicked");
				var w = window.open($(this).attr("href"),C4GTI.sitename+"sharer");
				e.preventDefault();
			});
		});	
		
	}
	
	// Help IE out
	function _poorOldInternetExplorer(){
		$('#VerticalNavigation a span').mousedown(function(e){
			$(this).addClass("ieActive");
			$(this).parent("a").addClass("ieActive");
		}).mouseup(function(e){
			$(this).removeClass("ieActive");
			$(this).parent("a").removeClass("ieActive");
		}).mouseout(function(e){
			$(this).removeClass("ieActive");
			$(this).parent("a").removeClass("ieActive");
		});		
	}
	
	function _init() {
		//_disableLogging();
		//_fixLogging();
		// console.info("C4GTI: Global controller loaded");

		$(document).ready(function() {
			$('body').addClass('jsEnabled');			
			$('#PrimaryContent').delegate("a.back-to-top", "click", _backToTopHandler);
			
			if ($.browser.msie) {
				_poorOldInternetExplorer();
			}
					
			_toolBoxInit();
		});
		
		return true;
	}
	
	return {
		init: _init,
		initLinkModifier:_initLinkModifier
	}

})(jQuery);

C4GTI.Global.Controller.init();

	
	

