/// <reference path="jquery-1.6.1-vsdoc.js"/>

// HTML5 Boilerplate nifty log
window.log = function () {
    log.history = log.history || [];
    log.history.push(arguments);
    if (this.console) {
        console.log(Array.prototype.slice.call(arguments));
    }
};

$('#tubePlayerBackground').live('click', function() {
	$(this).remove();
	$('.tubePlayer').html('');
});

// Ensure MadPaaFarten namespace
var MadPaaFarten = MadPaaFarten || {};

// Remap jQuery to $ and create closure
; (function ($, log, undefined) {
// Properties
	MadPaaFarten.Properties = {
		ajaxPostType: 'post',
		ajaxContentType: 'application/json; charset=utf-8',
		ajaxDataType: 'json'
	};

    // HTML Snippets
	MadPaaFarten.HTMLSnippets = {
	};

    // Current context
	MadPaaFarten.CurrentContext = {
	};

    // Handlers
    MadPaaFarten.Handlers = {
        init: function () {
            
			$( '#content .recipeImage .play' ).click( MadPaaFarten.Utils.tubeplayer );

			$( '#header .search' ).toggleDefaultValue();
        },
		onload : function(){
			$('body').addClass('withBiggie');
		}
    };

    // Utilities
	MadPaaFarten.Utils = {
		ajax: function(options) {
			var defaults = {
				data: { },
				type: MadPaaFarten.Properties.ajaxPostType,
				contentType: MadPaaFarten.Properties.ajaxContentType,
				dataType: MadPaaFarten.Properties.ajaxDataType,
				url: '',
				success: function(result) {
					var res = result.d;
					if ($.isFunction(options.callback)) options.callback(res);
				},
				error: function(XMLHttpRequest) {
					if ($.isFunction(options.serverError)) options.serverError(XMLHttpRequest);
				}
			};
			var opts = $.extend({ }, defaults, options);

			if (opts.type !== 'get') {
				opts.data = JSON.stringify(opts.data);
			}

			$.ajax(opts);
		},
		tubeplayer: function(e) {
			e.preventDefault();
			var $el = $(this),
			    $player = $el.next('.tubePlayer'),
			    iframe = '<iframe src="' + $el.attr('href') + '" width="456" height="257" frameborder="0" border="0" scrolling="no"></iframe>',
			    background = '<div id="tubePlayerBackground"></div>',
			    topMargin = Math.max((parseInt($el.closest('.recipeImage').height(), 10) - 257) / 2, 0) + 'px';

			// $("body").append(background); // adds a white semi transparent background 
			$player.html($(iframe).css('margin-top', topMargin));
		},
		setToggleDefaultValue: function($input) {

			// Set defaultValue data for compatibility
			$input.data('defaultValue', $input.val());

			$(document).delegate($input.selector, 'focusin focusout', function(e) {

				// For compatibility set defVal to defaultValue or element data
				var $this = $(this),
				    defVal = e.target.defaultValue || $this.data('defaultValue');

				// If value is the same as default value, then clear field on focus
				if (e.type === 'focusin' && $this.val() === defVal) {
					$this.val('').removeClass('inactive');
				}

					// Else if the field is empty set value back to default value on blur
				else {
					$this.val($this.val() || defVal);
					if ($this.val() === defVal) {$this.addClass('inactive');};
				}

			});

		}
	};

	// Extensions
	$.fn.extend({
		'toggleDefaultValue': function() {
			MadPaaFarten.Utils.setToggleDefaultValue( $( this ) );
			return this;
		}		
	});

    // On DOM ready
    $(MadPaaFarten.Handlers.init);

	//onload
	window.onload = MadPaaFarten.Handlers.onload;

})(this.jQuery, window.log);
