var popupStatus = 0;  

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
        "opacity": "0.3"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popup").fadeIn("fast");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("fast");
        $("#popup").fadeOut("fast");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popup").height();
    var popupWidth = $("#popup").width();
    //centering
    $("#popup").css({
    "position": "absolute",
    "top": 10,
    "left": 10
    });
    //only need force for IE6

    $("#backgroundPopup").css({
    "height": windowHeight
    });
}

$(document).ready(function() {

    $(".district").hide(); // hide all districts
    $(".mlsshow").click(
        function() {
            var district = (this.href.substring(this.href.indexOf("#") + 1)); // get the district number
            
            //$(".district").hide(); // hide all districts
            //$("#district" + district).fadeIn();
                        
            // put the html into the popup
            var newhtml = "<p style='float:right;'><a href='#' onClick='disablePopup();' class='closePop'>[x]</a> Close</p>";
            newhtml += $("#district" + district).html();         
            
            $("#popup").html(newhtml);
            $("#popup").height($("#district" + district).height() + 20);
            
            centerPopup();
            loadPopup();
                        
            return false;
        }
    )
    .css({cursor:"pointer"})
    ;
    
    $("#backgroundPopup").click(
        function() {
            disablePopup();
        }
    )
    .css({cursor:"pointer"})
    ;

    $(".closePop").click(
        function() {
            disablePopup();
        }
    )
    .css({cursor:"pointer"})
    ;    
    
});
