
var w = 0;
var h = 0;
var screenheight = 0;
var resizeCalled = false;
var ie = false;
var opened = false;

function PlacePopup() {
    if (!resizeCalled) {
        if (document.documentElement.clientHeight) {
            screenheight = document.documentElement.clientHeight;
        } else {
            screenheight = $(window).height();
        }
        w = ($(window).width());
        h = (screenheight); // <-- visible screen portion

        if ((screenheight + 100) <= $("html").height()) {
            h_full = ($("html").height()); // <-- full length of scrollable content        
        } else {
            h_full = ($("body").height()); // <-- full length of scrollable content        
        }
        var scrollTop = window.pageYOffset || $.boxModel && document.documentElement.scrollTop || document.body.scrollTop;


        $("#Popup_NetworkFlyer").width(w - 300);
        $("#Popup_NetworkFlyer").css("top", 70 + scrollTop);
        $("#Popup_NetworkFlyer").css("left", 150);

        $(".cover").width(w);
        $(".cover").height(h_full);

        $(".Popup").css("top", (((h / 2) - ($(".Popup").height() / 2))) - 50 + scrollTop); // <-- these place the popup in 
        $(".Popup").css("left", ((w / 2) - ($(".Popup").width() / 2))); //          the centre of the screen

        //ie6 fix:
        resizeCalled = true;
        setTimeout("resizeCalled = false", 10)
    }
}

function Popup(id, show) {
    PlacePopup();
    if (show) {
        $("#" + id).fadeIn(500);
        $(".cover").show();
    }
    else {
        $("#" + id).fadeOut(250);
        $(".cover").hide();
    }
}

$(document).ready(function() {
    $("#login_text").click(function() {
        SwitchLoginForm();
    });
    $("#login_arrow").click(function() {
        SwitchLoginForm();
    });
    $("#area_info_h2").click(function() {
        SwitchInfo();
    });
    
    $(".loginform_user").click(function() {
        if ($(this).val() == "Email address") $(this).val("");
    });
    $(".loginform_password").focus(function() {
        $("#loginform_passwordCover").hide();
    });
    $("#loginform_passwordCover").click(function() {
        $(this).hide();
        $(".loginform_password").focus();
    });
    
    
    // BANNER ROTATOR
    images = $('.site-banner').children('li');
    var imageIndex = images.length;
    var i = imageIndex;
    hideAll();
    function hideAll() {
        var zIndex = 9999;
        for(var j = 0; j < imageIndex -1; j++)
        {
            zIndex--;
            $(images[j]).css('z-index', zIndex); //this ensures the images are in the correct order of zindex
            if(j==0) {
                $(images[j]).fadeOut(1000); //fade the last image out a lil slower than the rest
            } else {
                $(images[j]).fadeOut('fast'); //fade all of the images out
            }
        }
        changeImage(); //start again!
    };
    function changeImage() {
        i--;
        if(i < 0) {
            i = imageIndex;
            hideAll();
        } else {
            $(images[i]).fadeIn(1000);
            setTimeout(function() { changeImage() }, 4000);
        }
    };

});

var loginFormOpen = false;
function SwitchLoginForm() {
    if (!loginFormOpen) {
        $(".loginform").slideDown();
        $("#login_arrow").removeClass("shut");
        loginFormOpen = true;
    } else {
        $(".loginform").slideUp();
        $("#login_arrow").addClass("shut");
        loginFormOpen = false;
    }
}

var infoOpen = false;
function SwitchInfo() {
    if (!infoOpen) {
        $("#sectionDescription").slideDown();
        $("#info_arrow").removeClass("shut");
        infoOpen = true;
    } else {
        $("#sectionDescription").slideUp();
        $("#info_arrow").addClass("shut");
        infoOpen = false;
    }
}


function OpenPlayer(id) {
    if (ie) {
        window.open("/Player.aspx?id=" + id, "MusicPlayer", "location=0,status=1,scrollbars=0,width=274,height=510");
    } else {
        var playerWindow = window.open("/Player.aspx?id=" + id, "MusicPlayer", "location=0,status=1,scrollbars=0,width=274,height=510");
        playerWindow.focus();
    }
}
function FocusPlayer() {
    if (ie) self.focus();
}

function validateEmail(email) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    if (!reg.test(email)) {
        return false;
    }
    else {
        return true;
    }
}


function textCounter(controlId,counterPanelId,maxLimit) {
    var text = $("#" + controlId).val();
    if (text.length > maxLimit)
    {
        text = text.substring(0, maxLimit);   
          $("#"+controlId).val(text)
          $("#" + counterPanelId).html("Maximum character limit (" + maxLimit + ") reached.");
     }
     else if (text.length < maxLimit)
     {
        var textLength = text.length;
        var charsLeft = maxLimit - textLength;
        $("#" + counterPanelId).html(charsLeft + " characters left.");
     }
 }
