$(document).ready(function(){
    if( !$('images_next')){
	    	$('#name').tooltip({ 
		    delay: 0, 
		    showURL: false, 
		    bodyHandler: function() { 
		        return $("<img/>").attr("src", this.src); 
		    } 
		});
    	
    }

	// submit wyszukiwania na glownej
    $("#searcher").submit(function() {
        if ( $('#searchCity').val() || $('#primaryMarket').val() == 'true') {
            return true;
        }
		centerPopupSearch();
		loadPopupSearch();
		return false;
    });

    // wyswietlanie komunikatow
	if( $('#contactArea').text() ) {
        centerPopup();
        loadPopup();
	}
    $("#infoPopUpClose").click(function(){
        disablePopup();
    });
    $("#infoPopUpBackground").click(function(){
        disablePopup();
    });

	// komunikat search'a
    $("#infoPopUpCloseSearch").click(function(){
        disablePopup();
    });
    $("#infoPopUpBackgroundSearch").click(function(){
        disablePopup();
    });
    $(document).keypress(function(e){
        if(e.keyCode==27 && popupStatus==1){
            disablePopup();
        }
    });
});

var popupStatus = 0;
var popupStatusSearch = 0;
var popupTimeLeft = 4000;

function loadPopup(){
    if(popupStatus==0){
	$('#banner_box_top').css("visibility", "hidden");
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
        $("#infoPopUpBackground").css({
            "opacity": "0.7",
			"height": windowHeight,
			"width": windowWidth
        });
        if (!$.browser.msie ||  ($.browser.msie && $.browser.version.substr(0, 1) > 6) ) {
            $("#infoPopUpBackground").fadeIn("slow");
        }
        $("#infoPopUp").fadeIn("slow");
        popupStatus = 1;
		setTimeout(disablePopup, popupTimeLeft);
    }
}

function disablePopup(){
    if(popupStatus==1){
        $("#infoPopUpBackground").fadeOut("slow");
        $("#infoPopUp").fadeOut("slow");
        popupStatus = 0;
		if( popupStatusSearch == 1) {
	        $("#infoPopUpBackgroundSearch").fadeOut("slow");
	        $("#infoPopUpSearch").fadeOut("slow");
	        popupStatusSearch = 0;
		}
		$('#banner_box_top').css("visibility", "visible");
    }
}

function centerPopup(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#infoPopUp").height();
    var popupWidth = $("#infoPopUp").width();
    $("#infoPopUp").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2,
        "left": windowWidth/2-popupWidth/2
    });
    
    $("#infoPopUpBackground").css({
        "height": windowHeight
    });
}

function loadPopupSearch(){
    if(popupStatus==0){
	$('#banner_box_top').css("visibility", "hidden");
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
        $("#infoPopUpBackground").css({
            "opacity": "0.7",
            "height": windowHeight,
            "width": windowWidth
        });
		if (!$.browser.msie ||  ($.browser.msie && $.browser.version.substr(0, 1) > 6) ) {
			$("#infoPopUpBackground").fadeIn("slow");
		}
        $("#infoPopUpSearch").fadeIn("slow");
        popupStatus = 1;
		popupStatusSearch = 1;
//		setTimeout(popupStatusSearch, popupTimeLeft);
    }
}

function centerPopupSearch(){
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#infoPopUpSearch").height();
    var popupWidth = $("#infoPopUpSearch").width();
    $("#infoPopUpSearch").css({
        "position": "absolute",
        "top": windowHeight/2-popupHeight/2+10,
        "left": windowWidth/2-popupWidth/2
    });
    
    $("#infoPopUpBackground").css({
        "height": windowHeight
    });
}
