function openPopup(address) 
{
    openPopupUrl(address, "", "center", 0, 0, 900, 685, "");      
}


//SETTING UP OUR POPUP
var winToOpen;
var background;
var placement = "center";
var xpos;
var ypos;
var backgroundCreated = 0;
/*var popupDivStatus = 0;
var popupUrlStatus = 0;
var popupConfirmStatus = 0;
var ismodal = 0;
var closeOnClick = 0;*/



function createBackground() {

    if (backgroundCreated == 0) {
        $background = $(document.createElement('div'));
        $background.attr("id", "popupBackground");
        $('body').append($background);

        $background.css({
            "display": "none",
            "opacity": "0.7",
            "height": "100%",
            "width": "100%",
            "top": "0",
            "left": "0",
            "z-index": "999"
        });

        var viewportheight;
        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
        if (typeof window.innerWidth != 'undefined') {
            viewportheight = window.innerHeight
        } else {
            viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }

        //only need force for IE6
        /*$background.css({
            "height": viewportheight
        });*/

        /*$('body').css({
            "position": "absolute"
        });*/

        backgroundCreated = 1;
    }
}



function createPopup(opendiv, placement, xpos, ypos) {

    if (typeof placement == 'undefined' || placement == null) {
        $placement = "center";
    } else {
        $placement = placement;
        $xpos = xpos;
        $ypos = ypos;
    }

    winToOpen = opendiv;

    /*$(".tieto_popup_button_close").mouseover(function() {
        $(this).addClass("tieto_popup_button_close_over");
    }).mouseout(function() {
        $(this).removeClass("tieto_popup_button_close_over");
    });*/

    $(".tieto_popup_button_close").click(function() {
        disablePopup();
    });

    //centering with css
    centerPopup(winToOpen);

    //load popup
    loadPopup(winToOpen);

}


function openPopupUrl(openurl, title, placement, xpos, ypos, width, height, custom) {
    //    if (navigator.appName == 'Netscape') {
    //        var delta = 10;
    //        width = width + delta;
    //        height = height + delta;
    //    }


    if (winToOpen != null) {
        winToOpen.empty();
    }

    if ($('.popupUrlContainer') != null) {
        $('.popupUrlContainer').remove();
    }

    winToOpen = $(document.createElement('div'));
    $('body').append(winToOpen);
    winToOpen.addClass("tieto_popup_container");
    winToOpen.addClass("popupUrlContainer");

    //winToOpen.append("<div class='tieto_popup_header'><table><tr><td class='tieto_popup_title'><h3>" + title + "</h3></td><td valign='top' class='tieto_popup_cell_close'><div class='tieto_popup_button_close'></div></td></tr></table></div>");

    /*$('body').css({
        "position": "absolute"
    });*/

    popupUrlStatus = 1;
    winToOpen.width(width);
    winToOpen.height(height);

    winToOpen.css({
        "height": "" + height + "",
        "width": "" + width + ""
    });
    /*$(document).bind("keydown.cbox_close", function(e) {
        if (e.keyCode === 27) {
            disablePopup();
        } 
    });*/
    
    winToOpen.width(width);

    winToOpen.append("<div id='modalCloseWindow' style='white-space:nonwrap;height:30px; position:absolute; right:30px;top:30px;'><img src='/images/serla-close.gif' class='tieto_popup_button_close' /></div><iframe src=\"" + openurl + "\" width=\"" + width + "\" height=\"" + height + "\" frameborder=\"0\"></iframe>");
    //createBackground();
    createPopup(winToOpen, placement, xpos, ypos, 1);

}

function closePopupAndRefresh() {
    closeMessage(null);
    window.location.reload();
}

function openPopupDiv(opendiv, placement, xpos, ypos) {
    ismodal = 0;

    winToOpen = $("#" + opendiv);

    //createBackground();
    createPopup(winToOpen, placement, xpos, ypos);
}

function openDirectoryPopupDiv(opendiv, placement, xpos, ypos) {
    ismodal = 0;
    winToOpen = $("#" + opendiv);

    winToOpen.addClass("popupContainer");
    $top = $(document.createElement('div'));
    winToOpen.append($top);
    $top.addClass("popupContainer-top");

    //createBackground();
    createPopup(winToOpen, placement, xpos, ypos);

    winToOpen.bind("mouseenter", function () {
    }).bind("mouseleave", function () {
    disablePopup();
    });

}

function openDirectoryPopupUrl(openurl, placement, xpos, ypos, width, height) {
    winToOpen = $(document.createElement('div'));
    $('body').append(winToOpen);

    winToOpen.addClass("popupContainer");
    $top = $(document.createElement('div'));
    winToOpen.append($top);
    $top.addClass("popupContainer-top");

    winToOpen.width(width);
    winToOpen.height(height);

    winToOpen.css({
        "height": "" + height + "",
        "width": "" + width + ""
    });

    winToOpen.width(width);
    winToOpen.append("<iframe src=\"" + openurl + "\" width=\"" + width + "\" height=\"" + height + "\" frameborder=\"0\"></iframe>");


    //createBackground();
    createPopup(winToOpen, placement, xpos, ypos);

    winToOpen.bind("mouseenter", function () {
    }).bind("mouseleave", function () {
    disablePopup();
    });

}


function findPos(obj) {
    var curleft = curtop = 0;

    if (obj.offsetParent) {


        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);

        return [curleft, curtop];
    }
}


function closeMessage(obj) {
    try {
        /* object = document.getElementById(obj);
        document.body.className = '';
        object.style.display = 'none';*/
        //$background.fadeOut("fast");
        winToOpen.fadeOut("fast");

    } catch (err) {
    }

}

function closeAll() {
    document.body.className = '';
    object.style.display = 'none';
    //$background.fadeOut("fast");
}


//loading popup 
function loadPopup(div) {
    //$background.fadeIn("fast");
    div.fadeIn("fast");
}

//disabling popup
function disablePopup() {
    //$background.fadeOut("fast");
    winToOpen.fadeOut("fast");
    
    //window.location.reload(true);
    //window.location.replace(window.location.href);
    
}


//centering popup
function centerPopup(div) {

    try {
        var viewportwidth;
        var viewportheight;

        if (typeof window.innerWidth != 'undefined') {
            viewportwidth = window.innerWidth,
		    viewportheight = window.innerHeight
        } else {
            viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		    viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }

        var popupHeight = parseInt(div.height()) + parseInt(div.css("padding-top")) + parseInt(div.css("padding-bottom"));
        var popupWidth = parseInt(div.width()) + parseInt(div.css("padding-left")) + parseInt(div.css("padding-right"));

        if ($placement == "center") {
            //centering            
            div.css({
                "top": viewportheight / 2 - popupHeight / 2,
                "left": viewportwidth / 2 - popupWidth / 2,
                "z-index": "1000"
            });
        } else if ($placement == "centerTop") {
            div.css({
                "top": $ypos,
                "left": viewportwidth / 2 - popupWidth / 2,
                "z-index": "1000"
            });
        } else if ($placement == "absolute") {
            div.css({
                "top": $ypos,
                "left": $xpos,
                "z-index": "1000"
            });
        }

        //only need force for IE6
        /*$background.css({
            "height": viewportheight
        });*/

        // fix&hack for ie6
        if ($placement == "center") {
            if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {
                scroll(0, (viewportheight / 2 - popupHeight / 2) - (document.getElementsByTagName('html')[0].clientHeight / 2 - popupHeight / 2));
            }
        }

    } catch (err) {
    }
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && ismodal != 1) {
            disablePopup();
        }
    });

    $(window).resize(function() {
        centerPopup();
    });


});

