$(document).ready(
    function() {
        if ($("label.useLabel").length > 0) {
            SetupInputsWithLabelText();
        }
		
		if ($(".pdf-list").length > 0) {
			addGoogleTrackingToPdf();
		}
		
        //SetupTextResizer();
        SetupLanguageCheckboxes();

        $("input").focus(function() {
            // Focus the item
            $(this).addClass("focus");
            $(this).blur(function() { $(this).removeClass("focus"); });
            // Focus the parent for additional styling
            $(this).parent().addClass("focus");
            $(this).blur(function() { $(this).parent().removeClass("focus"); });

        });

    }
);

function addGoogleTrackingToPdf() {
	var pdfs = $('.pdf-list a');
	pdfs.click(function(e) {
		var thisHref = unescape(decodeURI(this.href));
		thisHref = thisHref.replace(/\s+/g, '-').toLowerCase();
		
		var pdfName = unescape($.trim(decodeURI($(this).text())));
		pdfName = pdfName.replace(/\s+/g, '-').toLowerCase();
		
		var currentPageURL = unescape(decodeURI(window.location));
		currentPageURL = currentPageURL.replace(/\s+/g, '-').toLowerCase();
		
		var title = unescape(decodeURI($(this).closest('.inner').find('h3').text()));
		title = title.replace(/\s+/g, '-').toLowerCase();
		
		/*if (typeof console == "object") {
			console.log(pdfName + '/' + thisHref + '/' + title + '/');
			return false;
		}*/
		
        
		_gaq.push(['_trackPageview','/pdf-tracking/' + pdfName + '/' + thisHref + '/' + title + '/']);
		
	});
}

function SetupInputsWithLabelText() {
    $("label.useLabel").addClass("hide");
    var labels = $("label.useLabel");

    for (var ii = 0; ii < labels.length; ii++) {
        var inputId = $(labels[ii]).attr("for");
        if ($("input#" + inputId).val().length == 0) {
            var value = $("label[for='" + inputId + "']").text();
            $("input#" + inputId).val(value);
            $("input#" + inputId).focus(function() { $(this).val(""); });
            $("input#" + inputId).blur(function() {
                var value = $("label[for='" + $(this).attr("id") + "']").text();
                $(this).val(value);

            });
        }
    }
}


function SetupTextResizer() {
    $("#textResize a").click(function() {
        var request = $(this).attr("href");
        $.ajax({
            type: "GET",
            url: request,
            success: function(msg) {
                //alert("Data Saved: " + msg);
            }
        });
        /*var oldHeald = $("head");
        $("head").load(request + " link", "", function(oldHead) { $("head").empty(); $("head").append(oldHead); });*/
        var styleElement = $("<link/>").attr("rel", "Stylesheet").attr("href", "/css/" + $(this).attr("class") + ".css").attr("type", "text/css");
        $("head").append(styleElement);
        $(this).addClass("current");
        return false;
    });
}

function SetupLanguageCheckboxes() {
    $("form#languageSelect input.button").hide(); // Hide the button that performs the selection if no JavaScript

    /* This is due to a bug raised that when you have 2 languages selected, de-select one, perform a search a use the
    back button, it appears as though the 2 are still selected*/
    var checkedInputs = $("ul.language-options input[checked='true']"); // Get all inputs that are currently selected
    var uncheckedInputs = $("ul.language-options input[checked!='true']"); // Get all inputs that are currently de-selected

    checkedInputs.parent("li").addClass("selected");
    uncheckedInputs.parent("li").removeClass("selected");

    // When an option is click perform the update of the checkboxes
    $("form#languageSelect input").click(function() {
        var selectedCheckbox = $(this);

        // Select the li of the clicked checkbox
        if (selectedCheckbox.attr("checked") == false) {
            selectedCheckbox.attr("checked", "");
            selectedCheckbox.parent().removeClass("selected");
        }
        else {
            $("ul.language-options input[name='all']").attr("checked", "");
            $("ul.language-options input[name='all']").parent().removeClass("selected");
            selectedCheckbox.attr("checked", "checked");
            selectedCheckbox.parent().addClass("selected");
        }

        // Build up the AJAX call
        var ajaxUrl = $("form#languageSelect").attr("action"); // The Ajax call should be made to the action page

        var inputs = $("ul.language-options li.selected input"); // Get all inputs that are currently selected

        if (selectedCheckbox.attr("name") == "all") {
            inputs.attr("checked", "");
            inputs.parent().removeClass("selected");
            selectedCheckbox.attr("checked", "checked");
            selectedCheckbox.parent().addClass("selected");
        }

        var query = "?ajax=1"; // Specify a parameter to state this is an ajax call

        if (selectedCheckbox.attr("name") != "all") {
            inputs.each(function(i) {
                var name = $(this).attr("name");
                if (name != "all") {
                    query += "&" + name + "=on";
                }
            });
        }
        else {
            query += "&all=on";
        }

        /* Added due to IE doing some crazy caching, the selection seemed to change fine but didn't update the session
        this random int seems to remove the issue */
        var randomnumber = Math.floor(Math.random() * 1100)
        //alert(ajaxUrl + query);
        query += "&_=" + (randomnumber);
        $.ajax({
            type: "GET",
            url: ajaxUrl + query,
            success: function(msg) {
                //alert("Data Saved: " + msg);
            }
        });

        //$("ul.language-options").load(ajaxUrl + query + " li", "", function() { SetupLanguageCheckboxes(); });





    });
}
