var block_id = '#webra_popup';
var top_offset = 0;
var tid = null;
var poplink='http://www.krasfair.ru/rus/info-2011/index.shtml?link/info';
var image1 = '';
var image2 = '';
var image1l='http://panel.krs-ix.ru/dk/sibir-l.png';
var image2l='';
var image1s='http://panel.krs-ix.ru/dk/sibir-s.png';
var image2s='';
var exp = new Date(); 
var expHours = 168;
var enabled = false;
exp.setTime(exp.getTime() + (expHours*3600*1000));
//exp.setTime(exp.getTime() + (60*1000));


function screenSize(){

    if (screen.height>=801)
    {
        if(image1l!='') image1 = image1l;
        if(image2l!='') image2 = image2l;
//            $(block_id).append('<img id="popimage" src="'+image1+'" />');
              
    }
    else
    {
        if(image1s!='') image1 = image1s;
        if(image2s!='') image2 = image2s;
//        $(block_id).append('<img id="popimage" src="'+image1+'" width="640" height="480" />');         
    }
}


function setCookie(name, value, expires, path, domain, secure) {
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
function blockPop() {
	$(block_id).hide();
    $('#overlay').hide();
//	$(window).unbind("scroll");
//	$(window).unbind("resize");
//	$(block_id).unbind("click");
	setCookie('webrapop', '1', exp);
}
document.onkeypress = func;
function func(event) {
	if (window.event) event = window.event;
	var key = event.keyCode ? event.keyCode : event.which ? event.which : null;
	if( (event.ctrlKey) && ( (key == 0xA) || (key == 0xD) ) ) {
		setCookie('webrapop', '');
		$('#popimage').attr("src", image1);
		showPop();
	}
}

function centerPop(){
    //request data for centering
    var windowHeight, windowWidth;
    if (typeof window.innerWidth != 'undefined')
    {
       windowWidth = window.innerWidth,
       windowHeight = window.innerHeight
    }
               
    // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
               
    else if (typeof document.documentElement != 'undefined'
         && typeof document.documentElement.clientWidth != 'undefined' 
         && document.documentElement.clientWidth != 0)
         {
            windowWidth = document.documentElement.clientWidth,
            windowHeight = document.documentElement.clientHeight
         }
                                           
    // older versions of IE
                                       
    else
    {
            windowWidth = document.getElementsByTagName('body')[0].clientWidth,
            windowHeight = document.getElementsByTagName('body')[0].clientHeight
    }
    
    var popupHeight = $(block_id).height();
//    var popupHeight = img.height;;
    var popupWidth = $(block_id).width();
//    var popupWidth = img.width;
    var mypopup = $(block_id);
    var curtop = $(block_id).css('top');
    //centering
//    alert(curtop+' '+popupHeight+' '+popupWidth+' '+windowHeight+' '+windowWidth);
    $(block_id).css({
           "top": parseInt(curtop)+windowHeight/2-popupHeight/2,
           "left": windowWidth/2-popupWidth/2
    });
//    alert(windowHeight + ' ' + popupHeight + ' ' + $(block_id).css('top'));
}

function showPop() {
	var pop = getCookie('webrapop');
	if (pop) return;
	var win_scrl = $(document).scrollTop();
	var top = parseInt(top_offset + win_scrl);
	$("#overlay").toggle();
//	centerPop();
	$(block_id).show();
	$(block_id).click(function() {
//	            $("#overlay").toggle();
	            if(image2!=''){
//	                $('#popimage').attr("src", image2);
                    $('#popimage').hide();
                    $('#popimage2').show();
                    $(block_id).unbind('click');
                    $(block_id).click( function(){ window.open(poplink,'_blank'); blockPop();});
//                    $(block_id).focus();
                    return;
                };
                window.open(poplink,'_blank');
                blockPop();
			});
}

$(document).ready(function() {
    if(enabled){
    $('body').append('<div id="webra_popup" style="padding:0px;"></div><div id="overlay"></div>');
    screenSize();
    $(block_id).append('<img id="popimage" src="'+image1+'" />');
    if(image2!=''){
        $(block_id).append('<img id="popimage2" src="'+image2+'" />');
        $('#popimage2').hide();
    };
    $("#overlay").css("height", $(document).height());
//d    centerPop();
//d	showPop();
    };
	
	
});

$(window).one("load",function(){
    if(enabled){
        showPop();
        centerPop();
    };
})
.each(function(){
if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6)) 
$(this).trigger("load");
});


$(window).scroll(function() {
				var win_scrl = $(document).scrollTop();
//				top_offset = $(block_id).css('top');
				var top = parseInt(top_offset + win_scrl);
				$(block_id).css('top', top + 'px');
				centerPop();
			});

$(window).resize(function() {
				var win_scrl = $(document).scrollTop();
				var top = parseInt(top_offset + win_scrl);
				$(block_id).css('top', top + 'px');
				centerPop();
			});
