////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////
// oc_s.js
//
// This script is the core engine for powering our Gen3 widgets. It is
// used for loading a fully functional widget into a div embedded on
// a non-Directgov site. It does the following:
//
//	* Looks to see if JQuery 1.4.2 is running on the embedding page.
//	* If not, it loads up JQuery 1.4.2 - importantly, into a private
//	  variable namespace, so it won't conflict with other JS on page.
//	  (Done by the whole thing being in an anonymous function.)
//	* The main function then:
//		+ Loads up an external stylesheet for the widget.
//		+ Gets the HTML for the widget by calling a PHP script on
//		  the server, via JSONP (thus bypassing same-origin
//		  restriction).
//		+ Loads that HTML into the embedded div.
//		+ Records this as a widget view with our Widget Tracking
//		  system on innovate-apps.
//		+ Adds a handler to form - when it is submitted, it registers
//		  a widget use on the tracking system.
//
//
// Developed by Michael Fuchs,
//   Michael.Fuchs@directgov.gsi.gov.uk / fuchs@michaelfuchs.org
//
// _revision history_
// 2010.03.16, MF: first written
// 2010.07.19, MF: modified to support two different types of widget,
//				   hopefully not duplicating too much code. Now it looks
//				   for the contents of a 'widget_type' attribute in the
//				   embedded div. If it's type 2, it does the following:
//					+ Tells the PHP script to serve the 2nd HTML file.
//					+ Loads up a supplementary stylesheet.
//					+ Loads up a supplementary external JS file.
// 2010.09.11, MF: modified for this (Get Online) widget.
//
//
////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

(function() {

// Localize jQuery variable
var jQuery;

/******** Load jQuery if not present *********/
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type","text/javascript");
    script_tag.setAttribute("src",
        "http://innovate-apps.direct.gov.uk/widgets/js/jquery-1-4-2.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.noConflict(true);
    // Call our main function
    main(); 
}

/******** Our main function ********/
function main() { 
    jQuery(document).ready(function($) {
    
    
        /******* Load main CSS *******/
        var css_link = $("<link>", { 
            rel: "stylesheet", 
            type: "text/css", 
            href: "http://innovate-apps.direct.gov.uk/widgets/online_centres/assets/oc_s.css" 
        });
        css_link.appendTo('head');
        


        /******* Load HTML *******/
        var jsonp_url = "http://innovate-apps.direct.gov.uk/widgets/online_centres/assets/oc_s.php?&callback=?";
        $.getJSON(jsonp_url, function(data) {

			$('#oc_search_widget').html(data.html);
			
			
			// Once loaded, add handler to to make sure search expression entered;
			// and, if so, to track a 'use'.
			
			$('#oc_s_search_form').submit(function() {

				if ( $('#oc_s_query').val() == "Postcode, town or city" ) {

					alert ( 'Please enter a location' );

					$('#oc_s_search_form').focus();

					return false;

				} else {
				
					trackWidget ( 'u' );
					
					return true;
				
				}

			});

			
			// If IE6, bump the width out.

			if ( ($.browser.msie && $.browser.version.substr(0,1)<7)  ) {

				$('#oc_search_widget').css('width','236');
				
				$('#oc_search_widget').css('max-width','236');

			}


		  });


		  // Record a view in the widget tracking system, using helper function.

		  trackWidget ( 'v' );


    });
}




// Utility function for registering views/uses in tracking system
// Type is either 'v' (view) or 'u' (use). Our widget num is 7.
// We're just doing this with image sources (again).

function trackWidget ( type ) {
	
	// Get URL we are embedded on.
	
	var my_referer = window.location;

	var my_img = new Image();
	
	my_img.src = "http://innovate-apps.direct.gov.uk/t.php?w=13&t="+type+"&r="+my_referer+"&callback=?";
	
	return true;
	
}



})(); // We call our anonymous function immediately
