var IWT = new Object();
//document.domain= 'iwillteachyoutoberich.com';
/**
 * Cookie Functions
 */
IWT.cookie = {
	create: function(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
			document.cookie = name+"="+value+expires+"; path=/";
		},
	
	read: function(name){
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	},
	
	erase: function(name) {
		createCookie(name,"",-1);
	}
};

/**
 * URL and location functions
 */
IWT.url = {
//	getParam: function(name) {		
//		var val = decodeURI(		
//			(RegExp('[\\?&]' + name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
//			);
//		
//		return (val == null || val == 'null') ? null : val;

	getParam: function(key, default_)
	{		
		//if (default_==null) default_=""; 
		key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
		var qs = regex.exec(window.location.href);		
		if(qs == null || qs[1] == "")			
			return default_;		
		else
			return decodeURI(qs[1]);

	}
}

/**
 * Browser Detection Functions
 */
IWT.browser = {
	
	/*
	 * This does not actually seem to matter, as even googlebot will not follow a redirect
	 * that is done in JS
	 */
	isBot: function(){
		var botListLen = this.botList.length;
		for (var i=0; i < botListLen; i++) {
			if( navigator.userAgent.indexOf( this.botList[i] ) != -1 ){
				return true;
			}
		}
		return false;
	},
			
	botList: ["Teoma", "alexa", "froogle", "Gigabot", "inktomi",
		    "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory",
		    "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot",
		    "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp",
		    "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz",
		    "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot",
		    "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot",
		    "Butterfly","Twitturls","Me.dium","Twiceler"]
}

/**
 * LandingPage popup functions
 */
IWT.lp = {
	open: function(pageToOpen){
		
		if (pageToOpen==null) pageToOpen="http://iwillteachyoutoberich.com/welcome/videos/"; 
		
		//Hide ScrollBars
		jQuery('body').css('overflow', 'hidden');
		
		//Set "Viewed" Cookie
		IWT.cookie.create('iwillteach_email', 1, 60);
		
		//Create LP Page
		var $lpWelcome = jQuery(
			'<div id="lpWelcome">'	+
				'<iframe style="border: 0px; width: 100%; height: 100%;" src="'	+
				pageToOpen	+			
				'">' +
				'Your browser doesn\'t support iframes. <a href="javascript:closeLP();"> Click here to skip this and go straight to the blog</a>' +
				'</iframe>' +			
			'</div>'
			);
		
		//Set to Size
		$lpWelcome.css({
			width: $(window).width(),
			height: $(window).height(),
			position: 'absolute',
			top: 0,
			left: 0,
			background: 'white',
			zIndex: 1001
		});	
		
		jQuery('body').prepend(
			$lpWelcome
			);
	},
	
	openNoJQ: function(pageToOpen){
		
		if (pageToOpen==null) pageToOpen="http://iwillteachyoutoberich.com/welcome/videos/"; 
		
		// Get H & W
		var winW = 640, winH = 480;
		if (document.body && document.body.offsetWidth) {
		 winW = document.body.offsetWidth;
		 winH = document.body.offsetHeight;
		}
		if (document.compatMode=='CSS1Compat' &&
			document.documentElement &&
			document.documentElement.offsetWidth ) {
		 winW = document.documentElement.offsetWidth;
		 winH = document.documentElement.offsetHeight;
		}
		if (window.innerWidth && window.innerHeight) {
		 winW = window.innerWidth;
		 winH = window.innerHeight;
		}
		
		// Set cookie
		IWT.cookie.create('iwillteach_email', 1, 60);
		
		// Create Iframe
		var iPopup = '<div id="lpWelcome" style="width: ' + winW + 'px; height: ' + winH + 'px; position: absolute; top: 0; left: 0; background: white; z-index: 1001">'	+
				'<iframe style="border: 0px; width: 100%; height: 100%;" src="'	+
				pageToOpen	+			
				'">' +
				'Your browser doesn\'t support iframes. <a href="javascript:closeLP();"> Click here to skip this and go straight to the blog</a>' +
				'</iframe>' +			
			'</div>';

		//Set Overflow and display popup;
		document.write('<style type="text/css">body{overflow:hidden;}</style>');
		document.write(iPopup);			
	},
	
	close: function(){
		jQuery('#lpWelcome').remove();
		jQuery('body').css('overflow', 'auto');
		
		jQuery.fancybox.close();
	},
	
	needsPopup: function(){
		// Google Requires links from google to not have the splash screen shown
		if(document.referrer.indexOf('google.') > -1){
			return false;
		}
		// People coming from the mailing list should not be counted
		else if(document.URL.indexOf('awt_m') > -1){
			IWT.cookie.create('iwillteach_email', 1, 1825);
			return false;		
		}
		// Planahead links (PBS) get a small cookie
//		else if(document.URL.indexOf('/planahead') > -1){
//			IWT.cookie.create('iwillteach_email', 1, 2);
//			return false;		
//		}
		else{
			return ( IWT.cookie.read('iwillteach_email') != 1);
		}
	}
};
