﻿$(document).ready(function() {
    //clear searchbox on focus
    var s = $('#header input:text:first');
    var v = s.val();
    $(s).focus(function() { if ($(this).val() == v) $(this).val(''); });
    $(s).blur(function() { if ($(this).val() == '') $(this).val(v); });

    //Manipulate report-table
    $('#tblReportList tr:odd').addClass('odd');
    $('#tblReportList tr td:last-child, #tblReportList tr th:last-child').addClass('no-border');
    $('#tblReportList tr').hover(function() {
        $(this).addClass('tr-hover');
    }, function() {
        $(this).removeClass('tr-hover');
    });
    $("#tblReportList tbody tr").easyTooltip({ xOffset: 5, yOffset: -22 });
    $("#tblReportList tbody tr").click(function() {
        location.href = $(this).children('td:first').children('a:first').attr('href');
    });
    //Link-collection
    $("#link-collection>li>a").click(function() {
        $(this).parent().toggleClass("open");
        $(this).toggleClass("open");
        $(this).siblings("ul:first").slideToggle(200);
        return false;
    });

    //contact-list
    $("#contact-list strong a").click(function() {
        $(this).parent().parent().toggleClass("open");
        //$(this).toggleClass("open");
        $(this).parent().siblings("div").slideToggle(200);
        $("#contact-list .open td").css("border-bottom", "1px solid #cedcde");
        $("#contact-list .open tr:last-child td").css("border-bottom", "0");
        $("#contact-list .open table tr:first-child td").css("padding-top", "0");
        return false;
    });

//    $("#contact-list td").css("border-bottom", "1px solid #cedcde");
//    $("#contact-list table tr:last td").css("background", "red");
     

    //LOADING POPUP  
    //Click the button event!
    $("#openContentLinks").click(function() {
        //centering with css  
        centerPopup();
        //load popup
        loadPopup();
        return false;
    });

    // OPENING SELECTED ITEM IN POPUP CONTENTLINKS
    // Click the button event!
    $("#popupContentLinksOpenSelected").click(function() {
        var newUrl = document.getElementById("selectedPopup").value;
        if (newUrl != null && newUrl.length > 0) {
            location = newUrl;
        } else {
            disablePopup();
        }
        return false;
    });


    //CLOSING POPUP
    //Click the x event!
    $("#popupContentLinksClose").click(function() {
        disablePopup();
        return false;
    });
    //Click out event!
    $("#backgroundContentLinks").click(function() {
        disablePopup();
    });

    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });
});

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;
var popupStatus = 0;

$(window).bind('resize', function() {
    if (popupStatus == 1) {
        centerPopup();
    }
});

//centering popup  
function centerPopup() {
    //request data for centering  
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContentLinks").height();
    var popupWidth = $("#popupContentLinks").width() + 26;

    var top = (windowHeight - popupHeight) /2 > 0 ? (windowHeight - popupHeight ) / 2 : 40;
    //centering
    $("#popupContentLinks").css({
        "position": "absolute",
        "top": top,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6
}

//loading popup with jQuery magic!  
function loadPopup() {
    //loads popup only if it is disabled  
    if (popupStatus == 0) {
        //        $("#backgroundContentLinks").css({
        //            "opacity": "0.1"
        //        });
        //        //$("#backgroundContentLinks").fadeIn("slow");
        //$("#backgroundContentLinks").fadeIn("fast");
        //$("#popupContentLinks").fadeIn("slow");
        $("#popupContentLinks").show();
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#popupContentLinks").hide();
        $("#backgroundContentLinks").fadeOut("fast");
        popupStatus = 0;
    }
}