(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.4') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js");
    script_tag.onload = scriptLoadHandler;
    script_tag.onreadystatechange = function () { // Same thing but for IE
        if (this.readyState == 'complete' || this.readyState == 'loaded') {
            scriptLoadHandler();
        }
    };
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);

} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/******** Called once jQuery has loaded ******/
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery;
    // Call our main function
    main();
}

/******** Our main function ********/

function main() {

	/******* Load UI *******/
	jQuery.getScript('http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/jquery-ui.min.js', function() {
		jQuery.noConflict(true);
	});

	/******* Load UI CSS *******/
    var css_link = jQuery("<link>", {
    	rel: "stylesheet",
        type: "text/css",
        href: "http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.5/themes/base/jquery-ui.css"
	});
    css_link.appendTo('head');

	/******* Add further CSS *******/
	$("head").append('<style type="text/css" charset="utf-8"> ' +
	'.apr-holder{border:1px solid #000000; padding:0px; -moz-border-radius: 15px; border-radius: 15px; background-color:#e6e6e6}' +
	'.ui-datepicker{font-size:12px}'
	);

    /** Ok were ready lets load the widget  **/
    jQuery(document).ready(function($)
    {
        var date = new Date();
        var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
		$('.datepicker').live('focus', function(){
			$(this).datepicker({minDate: new Date(y, m, d), numberOfMonths: 2});
		});

		$("#apr-search-form").live('submit', function(eve){;
			// Validate Airport Selected if using a SELECT menu
			if ($("#airport_selected").length > 0)
			{
				if ($("#airport_selected").val() == '')
				{
					eve.preventDefault();
					alert('Please select an airport');
				};
			};

		});

        /******* Load HTML *******/
        $.ajax({
           type: "GET",
           url: "http://www.airportparkingreservations.com/search/form/78",
           dataType: 'jsonp',
           jsonp: 'affformv2'
        });

    });
}

})(); // We call our anonymous function immediately


function affformv2(json_data)
{
    document.getElementById('apr-widget-form').innerHTML = json_data.html;
}


