/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: bilimum.net
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

$(document).ready(function() {
    $("#artalan").click(function() {
        disablePopup("#artalan", ".pencere");
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup("#artalan", ".pencere");
        }
    });
});

function pencerem(back, popup) {
    //pencereyi ortalıyor
    centerPopup(back, "#" + popup);
    //pencereyi yüklüyor
    loadPopup(back, "#" + popup);

    $("#" + popup + "Kapat").html("<input type='button' id='kapat' onclick='disablePopup(\"" + back + "\", \"#" + popup + "\");' value='Kapat' />");
}


//loading popup with jQuery magic!
function loadPopup(back, popup) {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $(back).css({
            "opacity": "0.7"
        });
        $(back).fadeIn("slow");
        $(popup).fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(back, popup) {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $(back).fadeOut("slow");
        $(popup).fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup(back, popup) {
    //request data for centering
    var windowWidth = $(document).width();
    var windowHeight = $(document).height();
    var popupHeight = $(popup).height();
    var popupWidth = $(popup).width();
    //centering
    if (windowHeight < popupHeight) {
        $(popup).css({
            "position": "absolute",
            "top": 20,
            "left": windowWidth / 2 - popupWidth / 2
        });
    }
    else {
        $(popup).css({
            "position": "absolute",
            "top": 20,
//             "top": windowHeight / 2 - popupHeight / 2,
            "left": windowWidth / 2 - popupWidth / 2
        });
    }
    //only need force for IE6

    $(back).css({
        "height": windowHeight
    });

}

