var heightClasses = [];

jQuery(document).ready(function() {

    jQuery("#menuElem li a").slice(1, 4).css("font-weight", "bold");

    if (document.URL == "http://www.uksp.co.uk/") {
        jQuery("#menuElem li").first().addClass('CMSListMenuHighlightedLI');
    }

    doCufon();

    matchHeights();
    //jQuery("img").load(function() { matchHeights(); });

    setupLogin();

    // Implemented to handle Login Panel issue -- When button was clicked page postbacks which closes login box panel
    // Gets value for Login Error
    var loginErrorMessage = jQuery.trim(jQuery(".loginForm p:last").text());
    // Get value for Forget Password Error
    var forgetPasswordErrorMessage = jQuery("#plc_lt_zoneLogin_logonform_lblResult").text();

    // If login error exists it will show the login form panel
    if (loginErrorMessage != '') {
        jQuery(".loginBox").css("display", "block");
    }
    // If forget password error exists it will show login form panel
    if (forgetPasswordErrorMessage != '') {
        jQuery(".loginBox").css("display", "block");
    }

    setupSiteSearch();

    prettyKenticoPagination();

    jQuery('textarea.autoResize').autoResize({ limit: 300 });

    prettyForms();

    findRolesH = jQuery('.findRoles').parent().css('height');
    //checkLoginError();

    // Checks signout button link
    var control = jQuery(".signoutLink").text();
    // Hides login box if signout is found
    if (control != '') {
        jQuery(".widgetPanel .login").css("display", "none");
    };

});

function doCufon() {
    if (!jQuery("body").is(".IE6, .IE7, .IE8")) {
        //Check if I'm in CMS edit mode, if so, don't render Cufon 
        if (jQuery("body.EditMode").length == 0) {
            Cufon.replace('.sectionHead .pageSectionCMSListMenuHighlightedLI');
            Cufon.replace('h2');
            Cufon.replace('.h2Sub');
            Cufon.replace('h3');
            Cufon.replace('.header .CMSListMenuUL');
            Cufon.replace('.header .tabs');
            Cufon.now();
        }
    }
}

function prettyForms() {

    jQuery(".form input[type='text']").each(function() {
        paddInput(this);
    });

    jQuery(".TextAreaField").each(function() {
        paddInput(this);
    });

    var now = new Date();
    var futureYear = now.getFullYear() + 10;

    jQuery(".CalendarTextBox").datepicker({ dateFormat: 'dd/mm/yy', changeYear: true, changeMonth: true, yearRange: "1900:" + futureYear });

    /*jQuery("form").validate({
    errorClass: "EditingFormErrorLabel",
    errorElement: "span"
    });*/

}

function paddInput(input) {
    var width = jQuery(input).width();
    var borders = parseFloat(jQuery(input).css("borderRightWidth").replace("px", ""))
			+ parseFloat(jQuery(input).css("borderLeftWidth").replace("px", ""));
    var padding = parseFloat(jQuery(input).css("paddingRight").replace("px", ""))
			+ parseFloat(jQuery(input).css("paddingLeft").replace("px", ""));
    jQuery(input).css({
        "width": (width + padding - 8 - borders) + "px",
        "padding": "4px"
    });
}

function matchHeights() {
    for (i in heightClasses) {
        jQuery(heightClasses[i]).matchHeights();
    }
}

function setupLogin() {
    jQuery(".loginLink").click(function() {
        if (jQuery(this).hasClass('active')) {
            hideLogin();
        } else {
            // Make Login Visible and....        
            jQuery(this).addClass('active')
            jQuery(this).siblings(".loginBox").slideDown(200, function() {
                $(this).find("input:text:first").focus()
            });
            // Hide The Search Widget            
            jQuery(".sitesearchLink").removeClass('active')
            jQuery(".sitesearchLink").siblings(".sitesearchBox").slideUp(200);            
        }
        return false;
    });

    jQuery(".login").click(function(event) {
        event.stopPropagation();
    });

    jQuery("html").click(function() {
        hideLogin();
    });

    function hideLogin() {
        jQuery(".loginLink").removeClass('active')
        jQuery(".loginLink").siblings(".loginBox").slideUp(200);
    }
}


function setupSiteSearch() {

    jQuery(".sitesearchLink").click(function() {
        if (jQuery(this).hasClass('active')) {
            hideSearch();
        } else {
            // Make Search Visible and....
            jQuery(this).addClass('active')
            jQuery(this).siblings(".sitesearchBox").slideDown(200, function() {
                $(this).find("input:text:first").focus()
            });
            // Hide The Login Widget
            jQuery(".loginLink").removeClass('active')
            jQuery(".loginLink").siblings(".loginBox").slideUp(200);
        }
        return false;
    });

    jQuery(".sitesearch").click(function(event) {
        event.stopPropagation();
    });

    jQuery("html").click(function() {
        hideSearch();
    });

    function hideSearch() {
        jQuery(".sitesearchLink").removeClass('active')
        jQuery(".sitesearchLink").siblings(".sitesearchBox").slideUp(200);
    }

}

function prettyKenticoPagination() {
    var keepers = [".UnselectedPrev", ".UnselectedNext", ".SelectedPage", ".UnselectedPage"];
    jQuery(".PagerNumberArea span").each(function() {
        var found = false;
        for (var i = 0; i < keepers.length; i++) {
            if (jQuery(this).hasClass("SelectedPage")) {
                found = true;
                break;
            }
            if (jQuery(this).find(keepers[i]).length > 0) {
                found = true;
                break;
            }
        }
        if (!found) {
            jQuery(this).hide();
        } else {
            //remove nbsp
            //var html = jQuery(this).html();
            //html.replace("&nbsp;", "");
        }
    });
}

function selectCaseStudy(CaseStudy) 
{
    //$('input[name*="man').attr("style", "display:none");
    $(".caseStudy").each(function(index) {
        if ($(this).attr("id") == "CaseStudy" + CaseStudy) {
            $(this).attr("style", "display:block");
            
            //Set the selected thumbnail with a border
            $(this).find('#CaseStudyItem' + CaseStudy + ' > img').attr("class","selected");

        }
        else {
            $(this).attr("style", "display:none");

        }
    });

    return false;
}
