/* * * * * * * * * * * * * * * *
 *                             *
 * @author   Mike Cavaliere    *
 * @company  DraftFCB          *
 *                             *
 * * * * * * * * * * * * * * * *
 *
 * Hampton Namespace and jQuery methods. Put all method declarations here.
 * It's also best to keep our JS completely Object-Oriented.
 *
 *
 *
 */
 
/*
 * Define the Hampton namespace. 
 */
if (typeof Hampton === 'undefined') {
	var Hampton = {};
}

(function() {
			  
	/*
	 * Extend the Hampton Namespace.
	 *
	 * Hampton.Config - Configuration parameters, and environment-specific settings
	 *
	 */
	Hampton.Config = {
		
		/*
		 * Take in an id, and return the url for the image/video popup
		 */
		videoPopupUrl: function(id)
		{
			return 'popup-video.aspx?id=' + id;
		},
		imagePopupUrl: function(id)
		{
			return 'popup-image.aspx?id=' + id;
		}
	};
	
	Hampton.Dom = {
		spanTemplate: $('<span>')
	};
	
	/* 
	 *
	 * Extensions to jQuery.fn namespace. This allows us to chain our methods.
	 */
	$.fn.extend({
		/*
		 * Inject a <span>
		 *
		 */
		inject: function( className )
		{
			return this.each(function()
			{
				$(this).append( Hampton.Dom.spanTemplate.clone().addClass(className) );
			});	
		},
	
		/*
		 * Inject a <span> into sliding doors buttons
		 *
		 */
		button: function()
		{
			return this.each(function()
			{
				var html = this.innerHTML;
				
				$(this).html('');
				$(this).append( Hampton.Dom.spanTemplate.clone().html(html) );
			});	
		},
		
        /*
         * Inject top and bottom spans
         */
        topBottom: function()
        {
            return this.each(function()
            {
               $(this)
                .prepend( Hampton.Dom.spanTemplate.clone().addClass('top') )
                .append( Hampton.Dom.spanTemplate.clone().addClass('bottom') ); 
            });
        },
		
		expandable: function()
		{
			return this.each(function() {
				$(this).click(function()
				{
				   $(this)
					.toggleClass('trigger-on')
					.toggleClass('trigger-off')
					.parent().find('.expandable-content').slideToggle(); 
				});
			});
		},
		
		/*
		 * Set default text in a text box; remove it when clicked
		 *
		 */
		defaultText: function(text)
		{
			return this.each(function()
		    {
				var $el = $(this);
				
				function off() 
				{
				  if (!$el.attr('value')) 
				  {
					  $el.attr('value', text);
				  }
				};
					  
				function on()
				{
				  if ($el.attr('value') === text)
				  {
					  $el.attr('value', '');
				  }
				};
				
				$el.focus(on).blur(off);
				
				off();
				
		    });
		},
		
		/*
		 * Add a click handler that displays an arbitrary video/image. The video id 
		 *  is pulled from the REL attribute of the clicked element.
		 */
		mediaPopup: function(type)
		{
			
			$(this).click(function(e) {
				e.preventDefault();
				
				// Call the correct url function from Hampton.Config; pass in the REL 
				var url = Hampton.Config[type + 'PopupUrl']( $(this).attr('rel') );
				
//				if( Hampton._mediaWindow )
//				{
//					Hampton._mediaWindow.focus();	
//				}
				Hampton._mediaWindow = window.open(url, 'videoWindow', 'width=400,height=400,resizable=yes,toolbar=no,menubar=no');
				return false;
			});	
		}
	});
})();