/* POPUP-SCRIPT */
/* By MarcusG */
var fadeTime = 250;
var hideInfoTime = 2500;
var popupHideByBackgroundClick = true;
var popupStack = new Array(0);
var popupActive = false;

$(document).ready(function(){
    $("#popupBackground").click(function() {
        if (popupHideByBackgroundClick) hidePopup();
    });
});

function showPopup(content, backgroundClickHide, saveExisting) {
    hidePopupWorking();
    if (typeof(saveExisting) == 'undefined') saveExisting = true;
    if (popupActive) {
        if (saveExisting != false) popupSave();
        $("#popupBody").fadeOut(fadeTime, function() {
            $("#popupBody").html(content);
            centerPopup();
            $("#popupBody").fadeIn(fadeTime);
            setPopupBackgroundClickHide(backgroundClickHide);
        });
    } else {
        $("#popupBackground").css({
            "opacity": "0",
            "display":"block"}
        );
        $("#popupBackground").fadeTo(fadeTime, 0.75);
        $("#popupBody").html(content);
        centerPopup();
        $("#popupBody").fadeIn(fadeTime);
        setPopupBackgroundClickHide(backgroundClickHide);
    }
    popupActive = true;
}
function setPopupBackgroundClickHide(backgroundClickHide) {
    if (typeof(backgroundClickHide) == 'undefined') {
        if ($('#backgroundClickHide').length > 0) {
            popupHideByBackgroundClick = ($('#backgroundClickHide').val() == 'true');
        } else {
            popupHideByBackgroundClick = true;
        }
    } else {
        popupHideByBackgroundClick = backgroundClickHide;
    }
}

function showPopupInfo(title, body, backgroundClickHide, saveExisting) {
    body += '<br /><button onClick="hidePopup()">Ok</button>';
    var content = '<div id="popupWrapper">';
    content += '<div id="popupTitle">'+title+'</div>';
    content += '<div id="popupContent">'+body+'</div>';
    content += '</div>';
    content += '<script type="text/javascript">$( "button" ).button();</script>';
    showPopup(content, backgroundClickHide, saveExisting);
}

function showPopupAjax(url, backgroundClickHide, saveExisting) {
    if (typeof(saveExisting) == 'undefined') saveExisting = true;
    if (popupActive) {
        if (saveExisting != false) popupSave();
        $("#popupBody").fadeOut(fadeTime, function() {
            $("#popupBody").load(url, '', function() {
                hidePopupWorking();
                centerPopup();
                $("#popupBody").fadeIn(fadeTime);
                setPopupBackgroundClickHide(backgroundClickHide);
            });
        });
    } else {
        $("#popupBackground").css({
            "opacity": "0",
            "display":"block"}
        );
        $("#popupBackground").fadeTo(fadeTime, 0.75);
        $("#popupBody").load(url, '', function() {
            hidePopupWorking();
            centerPopup();
            $("#popupBody").fadeIn(fadeTime);
            setPopupBackgroundClickHide(backgroundClickHide);
        });
    }
    popupActive = true;
}

function showPopupJson(url, backgroundClickHide, saveExisting) {
    if (typeof(saveExisting) == 'undefined') saveExisting = true;
    if (popupActive) {
        if (saveExisting != false) popupSave();
        $("#popupBody").fadeOut(fadeTime, function() {
            $.getJSON(url,'', function(data) {
                hidePopupWorking();
                $("#popupBody").html(data.content);
                centerPopup();
                $("#popupBody").fadeIn(fadeTime);
                setPopupBackgroundClickHide(backgroundClickHide);
            });
        });
    } else {
        $("#popupBackground").css({
            "opacity": "0",
            "display":"block"}
        );
        $("#popupBackground").fadeTo(fadeTime, 0.75);
        //console.log(url);
        $.getJSON(url,'', function(data) {
            hidePopupWorking();
            //console.log(data)
            $("#popupBody").html(data.content);
            centerPopup();
            $("#popupBody").fadeIn(fadeTime);
            setPopupBackgroundClickHide(backgroundClickHide);
        });
    }
    popupActive = true;
}
function updatePopup(title, content) {
    $('#popupBody').fadeOut(fadeTime, function() {
        hidePopupWorking();
        $('#popupTitle').html(title);
        $("#popupContent").html(content);
        centerPopup();
        $("#popupBody").fadeIn(fadeTime);
    });
}
function replacePopup(content) {
    $('#popupBody').fadeOut(fadeTime, function() {
        hidePopupWorking();
        $("#popupBody").html(content);
        centerPopup();
        $("#popupBody").fadeIn(fadeTime);
        setPopupBackgroundClickHide();
    });
}
function replacePopupJson(url) {
    $('#popupBody').fadeOut(fadeTime, function() {
        $.getJSON(url,'', function(data) {
            hidePopupWorking();
            $("#popupBody").html(data.content);
            centerPopup();
            $("#popupBody").fadeIn(400);
            setPopupBackgroundClickHide();
        });
    });
}

function showPopupError(errorString, backgroundClickHide, saveExisting) {
    hidePopupWorking();
    if (typeof(saveExisting) == 'undefined') saveExisting = true;
    if (popupActive) {
        if (saveExisting != false) popupSave();
        $("#popupBody").fadeOut(250, function() {
            $('#popupBody').html('<div id="popupTitle">Ett fel uppstod!</div><div id="popupContent">'+errorString+'</div>');
            centerPopup();
            $("#popupBody").fadeIn(400);
        });
    } else {
        $("#popupBackground").css({
            "opacity": "0",
            "display":"block"}
        );
        $("#popupBackground").fadeTo(300, 0.75);
        $('#popupBody').html('<div id="popupTitle">Ett fel uppstod!</div><div id="popupContent">'+errorString+'</div>');
        centerPopup();
        $("#popupBody").fadeIn(400);
    }

    if (typeof(backgroundClickHide) == 'undefined') {
        popupHideByBackgroundClick = true;
    } else {
        popupHideByBackgroundClick = backgroundClickHide;
    }
    popupActive = true;
}

function hidePopup(callback, fadeBackground) {
    hidePopupWorking();
    if (popupStack.length > 0) {
            popupRestore(callback);
    } else {
        if (typeof(callback) != 'undefined' && callback != '') {
            $("#popupBody").fadeOut(400, function() {
                // call the callback function
                var fun = eval(callback);
                fun();
            });
            if (fadeBackground != false) $("#popupBackground").fadeOut(400);
        } else {
            $("#popupBody").fadeOut(400);
            $("#popupBackground").fadeOut(400);
        }
        popupActive = false;
    }
}

function showPopupWorking() {
    $('#popupWorking').width($('#popupBody').width());
    $('#popupWorking').height($('#popupBody').height());
    $('#popupWorkingBackground').width($('#popupBody').width());
    $('#popupWorkingBackground').height($('#popupBody').height());
    $('#popupWorkingBackground').css({
        'left':$('#popupBody').css('left'),
        'top':$('#popupBody').css('top')
    });
    $('#popupWorking').css({
        'left':$('#popupBody').css('left'),
        'top':$('#popupBody').css('top')
    });
    $('#popupWorkingBackground').fadeTo(fadeTime,0.6);
    $('#popupWorking').fadeIn(fadeTime);
}

function hidePopupWorking() {
    $('#popupWorking').fadeOut(fadeTime);
    $('#popupWorkingBackground').fadeOut(fadeTime);
}

function popupSave() {
    var newID = 'popupBody'+getRandom();
    popupStack[popupStack.length] = new Array(newID, popupHideByBackgroundClick);
    $('#popupBody').attr('id',newID);
    $('#'+newID).css('display','none');
    $('#'+newID).after('<div id="popupBody" class="popupBody"></div>');
}

function popupRestore(callback) {
    $("#popupBody").fadeOut(250, function() {
        myItem = popupStack.pop();
        $("#popupBody").remove();
        $('#'+myItem[0]).attr('id','popupBody');
        centerPopup();
        popupHideByBackgroundClick = myItem[1];
        $("#popupBody").fadeIn(250, function() {
            if (typeof(callback) != 'undefined' && callback != '') {
                // call the callback function
                var fun = eval(callback);
                fun();
            }
        });
    });
}

function centerPopup(){
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;  
    var windowHeight = document.documentElement.clientHeight;  
    var popupHeight = $("#popupBody").height();
    var popupWidth = $("#popupBody").width();  
    // checking size
    if (popupHeight < $("#popupTitle").height() + $('#popupBody').height()) {
        popupHeight = $("#popupTitle").height() + $('#popupBody').height() + 20;
        $("#popupBody").height(popupHeight);
    }
    //centering  
    $("#popupBody").css({
    "position": "absolute",
    "top": windowHeight/2-popupHeight/2,
    "left": windowWidth/2-popupWidth/2
    });  
    //only need force for IE6  
 
    $("#popupBackground").css({
            "height": windowHeight,
            "top" : 0
    });
}
/* END POPUP-SCRIPT */

function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  //return [ scrOfX, scrOfY ];
  return scrOfY;
}

var mayHideInfo = false;
function showInfo(text, autoHide) {
	mayHideInfo = false;
	$('#info_text').text(text);
	$('#info').css({"left":document.documentElement.clientWidth / 2 - $('#info').width() / 2});
	$('#info').fadeIn(fadeTime, function() {
		mayHideInfo = true;
                if (autoHide) setTimeout(hideInfo,hideInfoTime)
	});
}

function hideInfo() {
	if (!mayHideInfo) {
		setTimeout(hideInfo, 100);
	}
	$('#info').fadeOut(fadeTime * 8, function() {
		mayHideInfo = false;
	});
}

function showError(text) {
	showPopup(text);
}

