
/*
 * HAL
 * Author: Greg Sabia
 * Support: http://gregsabia.com
 *
*/

HAL = {
	openThePodBayDoors : function(){
		return console.log('I\'m sorry Greg, I\'m afraid I can\'t do that.');
	},
	_extended : false,
	_getTimeout : 300,
	init : function(){
		// DOM Ready
		HAL.gregboxes.init();
		HAL.subscribe.init();
		HAL.youtube.init();
	},
	please : {
		init : function(){
			$(function(){
				if(HAL._extended.init) HAL._extended.init();
			});
		},
		extend : function(obj){
			return $.extend(HAL._extended,obj);
		},
		clearText : function(thefield){
			if(thefield.defaultValue==thefield.value){ thefield.value = "" }
		},
		replaceText : function(thefield){
			if(thefield.value==""){ thefield.value = thefield.defaultValue }
		},
		preloadImages : function(){
			var d=document;
			if(d.images){
				if(!d.p) d.p=new Array();
				var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length;i++)
				if(a[i].indexOf("#") != 0){
					d.p[j]=new Image; d.p[j++].src=a[i];
				}
			}
		},
		getAnchor : function(){
			var url_anchor = document.location.toString();
			if(url_anchor.match('#')){
				return url_anchor.split('#')[1];
			}else{
				return false;
			}
		},
		setAnchor : function(anchor){
			var url_anchor = document.location.toString();

			document.location = url_anchor.split('#')[0]+'#'+anchor;
		},
		setCookie : function(name,value,expiredays){
			// Set a cookie
			var exdate = new Date();
			exdate.setDate(exdate.getDate()+expiredays);
			document.cookie = name+"="+escape(value)+((expiredays == null)?"":";expires="+exdate.toUTCString())+"; path=/";
		}
	},
	gregboxes : {
		init : function(){
			$('.gregbox').each(function(){
				// Add wrapper
		        if(!$(this).attr('dontrender')) $(this).html('<div class="top"></div><div class="content">'+$(this).html()+'</div><div class="bottom"></div>');

				// Allow lightbox to be closed when the user clicks off of the box
				if($(this).attr('closeonblur')){
				    $(this).bind('dialogopen',function(event,ui){
			            var gb = this;
			            $('.ui-widget-overlay').click(function(){
				        	$(gb).dialog('close');
			            });
			        });
				}

				// Assign widths
				if($(this).hasClass('skinny')){
					width = 536;
				}else{
					width = 936;
				}

		        $(this).dialog({
		        	'width': width,
		        	'modal': true,
		        	'autoOpen': false,
		        	'resizable': false
		        });
		    });
		},
		open : function(selector){
			$(selector).dialog('open');
		}
	},
	subscribe : {
		selector : '#complete-subscription',
		form : 'form.subscribe',
		init : function(){
			// Form onsubmit
			$(HAL.subscribe.form).each(function(i,form){
				default_value = $('input.subscribe-email',form).val();
				
				$('a.arrow',form).click(function(){
					$(form).submit();
				});
				
				$(form).submit(function(){
					if(!$('input.optin-checkbox:checked',form).length){
						alert('You must select the checkbox before you can get Kirstie\'s updates!');
						return false;
					}else if(!$('input.subscribe-email',form).val() || $('input.subscribe-email',form).val() == default_value){
						alert('Please enter a valid email address!');
						return false;
					}else{
						// All good, process form
						return true;
					}
				});
			});
			
			switch(HAL.please.getAnchor()){
				case 'complete_subscription' : 
					// Complete the subscription
					HAL.gregboxes.open(this.selector);
					
					// Track event
					HAL.analytics.track.pageview('/subscribe');
					
					// Set cookie
					HAL.please.setCookie('ka_subscribed',true,365);
				break;
				case 'error_email' : 
					alert('Please enter a valid email address!');
				break;
			}
		}
	},
	youtube : {
		init : function(){
			$('a.youtube').click(function(){
				HAL.youtube.playHighlight($(this).parent());
			});
			
			if(HAL.please.getAnchor() && typeof $('#'+HAL.please.getAnchor()) !== 'undefined'){
				HAL.youtube.playHighlight($('div.thumb',$('#'+HAL.please.getAnchor())));
			}
		},
		playHighlight : function(e){
			$(e).html('<iframe width="363" height="230" src="http://www.youtube.com/embed/'+$('a.youtube',e).attr('rel')+'?autoplay=1" frameborder="0" allowfullscreen="true"></iframe>');
		}
	},
	analytics : {
		executable : function(){
			if(typeof _gaq !== 'undefined'){
				return true;
			}else{
				this.log('Couldn\'t track event because analytics is not executable');
				return false;
			}
		},
		log : function(s){
			if(typeof console !== 'undefined') console.log(s)
		},
		track : {
			pageview : function(url,callback){
				// Track the pageview
				if(HAL.analytics.executable()){
					var c = _gaq.push(['_trackPageview',url]);
					HAL.analytics.log('Tracked '+url+' using _trackPageview');
				}

				if(callback) callback();

				return c;
			},
			event : function(category,action,label,value){
				if(HAL.analytics.executable()){
					var c = _gaq.push(['_trackEvent',category,action,label,value]);
					HAL.analytics.log('Tracked '+category+':'+action+' using _trackEvent');
				}

				return c;
			}
		}
	}
}


