var cook = document.cookie;
function getCookie(name) {
    var index = cook.indexOf(name + "=");
    if (index == -1) return null;
    index = cook.indexOf("=", index) + 1; // first character
    var endstr = cook.indexOf(";", index);
    if (endstr == -1) endstr = cook.length; // last character
    return unescape(cook.substring(index, endstr));
}

function setCookie(cookieName, cookieValue, nDays) {

    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue)
		                + ";path=/;expires=" + expire.toGMTString();
}

if ( typeof window.$ != 'function' ) {
    window.$ = function () {
      var elements = new Array();
      for (var i = 0; i < arguments.length; i++) {
        var element = arguments[i];
        if (typeof element == 'string')
          element = document.getElementById(element);

        if (arguments.length == 1)
          return element;

        elements.push(element);
      }

      return elements;
    }
}

//Busy pause
function pause(milliseconds) {
    var dt = new Date();
    while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
}

function webSearch(){
    var link = "/search?q=";
    var elm = $("#seekword");

    if (elm.val() != "") {
        document.location.href = link + encodeURI(elm.val()); 
        return false;
    }
    else
    {
        var elmQ = $("#q");
        if(elmQ){
            document.location.href = link + encodeURI(elmQ.val());
            return false;
        }
    }
    
    
}

function popup(file,header,width,height){
     var _popup = document.open(file,header,'left='+Math.round(screen.availWidth/2-width/2)+',top='+Math.round(screen.availHeight/2-height/2)+',width='+width+',height='+height+',resizable=no,location=no,directories=no,menubar=no,scrollbars=no,status=no');
     _popup.window.focus();
}

function showhideContainer(elementID)
{
    var e = $(elementID);
    
    if(!e) return;
    
    e.style.display = e.style.display == "" ? "none" : "";
}

$(document).ready(function() {
    $('.head', this).css("cursor", "pointer");
    $('.head', this).siblings().hide();
    $('.expanded', this).siblings().slideDown();
    $('.expanded', this).addClass('selected');
    $('.expanded', this).parent().attr({ id: 'expanded' });

    var expandedid = 'expanded';
    $('.head', this).bind('click', function() {
        $('#' + expandedid + ' .head').siblings().hide('fast');
        $('#' + expandedid + ' .head').siblings().removeClass('selected-item');
        $('#' + expandedid + ' .head').removeClass('selected');
        var currentid = $(this).parent().attr('id');
        $('#' + expandedid).attr({ id: '' });
        if (currentid !== expandedid) {
            $(this).siblings().addClass('selected-item');
            $(this).siblings().slideDown();
            $(this).addClass('selected');
            $(this).parent().attr({ id: expandedid });
        }
        return false;
    })
}

)

$(document).ready(
    function() {
        $('.expanded-list', this).css("cursor", "pointer");
        $('.expanded-list', this).children('ul').hide();

        $('.expanded-list', this).bind('click', function() {
            var visible = $(this).children('ul').is(':visible');
            $(this).children('ul').show('fast');
            return visible;
        })
    }
)

$(function() {
    var tabContainers = $('#document div.tabs > div');
    tabContainers.hide().filter(':first').show();

    $('div.tabs ul.tabNavigation a').click(function() {
        tabContainers.hide();
        tabContainers.filter(this.hash).show();
        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');
        return false;
    }).filter(':first').click();
});

$(document).ready(function() {

    //select all the a tag with name equal to modal
    $('a[name=modal]').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        //Get the A tag
        var id = $(this).attr('type');
        if (id == "") id = "#dialog";

        //Get the screen height and width
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        //Set heigth and width to mask to fill up the whole screen
        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });

        //transition effect
        $('#mask').fadeIn(500);
        $('#mask').fadeTo("fast", 0.5);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Set the popup window to center
        $(id).css('top', winH / 2 - $(id).height() / 2);
        $(id).css('left', winW / 2 - $(id).width() / 2);
       
        //transition effect
        $(id).fadeIn(100);

    });

    //if close button is clicked
    $('.window .close').click(function(e) {
        //Cancel the link behavior
        e.preventDefault();

        $('#mask').hide();
        $('.window').hide();
    });

    //if mask is clicked
    $('#mask').click(function() {
        $(this).hide();
        $('.window').hide();
    });

});

$.extend({ URLEncode: function(c) {
    var o = ''; var x = 0; c = c.toString(); var r = /(^[a-zA-Z0-9_.]*)/;
    while (x < c.length) {
        var m = r.exec(c.substr(x));
        if (m != null && m.length > 1 && m[1] != '') {
            o += m[1]; x += m[1].length;
        } else {
            if (c[x] == ' ') o += '+'; else {
                var d = c.charCodeAt(x); var h = d.toString(16);
                o += '%' + (h.length < 2 ? '0' : '') + h.toUpperCase();
            } x++;
        } 
    } return o;
},
    URLDecode: function(s) {
        var o = s; var binVal, t; var r = /(%[^%]{2})/;
        while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
            b = parseInt(m[1].substr(1), 16);
            t = String.fromCharCode(b); o = o.replace(m[1], t);
        } return o;
    }
});




//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
var popupSurveyEnabled = getCookie("enabledSurvey");
popupSurveyEnabled = popupSurveyEnabled == null

//loading popup
function loadPopupSurvey() {

    if (!popupSurveyEnabled) return;
    
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popupSurvey").fadeIn("fast");
        popupStatus = 1;
    }
}

//disabling popup
function disablePopupSurvey() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("fast");
        $("#popupSurvey").fadeOut("fast");
        popupStatus = 0;
        setCookie("enabledSurvey", "no", 360);
    }
}


//loading popup
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("fast");
        $("#popup").fadeIn("fast");
        popupStatus = 1;
    }
}

//disabling popup
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("fast");
        $("#popup").fadeOut("fast");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(tjekSurvey, popup) {
    var popupID = "#popup";

    if (tjekSurvey != undefined && tjekSurvey && !popupSurveyEnabled)
        return;
   
    if(popup != undefined)
        popupID = popup;
    
    
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(popupID).height();
    var popupWidth = $(popupID).width();
    //centering
    $(popupID).css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });
}

$(document).ready(function () {

    //LOADING POPUP
    //Click the button event!
    $("#popupButton").click(function () {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupClose").click(function () {
        disablePopup();
    });
    //Click the x event!
    $("#popupCloseSurvey").click(function () {
        disablePopupSurvey();
    });
    //Click out event!
    $("#backgroundPopup").click(function () {
        //disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});


