// usa mycookie.js

function bnReplacer() {
    clearTimeout(GLOBAL_bn_timeout);
    GLOBAL_bn_timeout = setTimeout(function(){
		jQuery('#bn_popup_cover,#bn_popup_dialog').css({
			'top':	'-10000px',
			'left':	'-10000px'
		});
		setTimeout(function(){
			bnPlacer();
		},100);
    },500);
}
function bnPlacer() {	    
    var pagina_wid = jQuery(document).width();
    var pagina_hei = jQuery(document).height();
    if (jQuery.browser.msie) { /* offset per le varie versioni di IE */
	if (jQuery.browser.version == 8) {
	    pagina_wid -= 21;
	    pagina_hei -= 4;
	}
	else if (jQuery.browser.version == 6) {
	    pagina_wid -= 21;
	}
    }
    jQuery('#bn_popup_cover').css({
       'background':	'black',
       'opacity':	0.42,
       'top':		0,
       'left':		0,
       'width':		pagina_wid,
       'height':	pagina_hei
    });
    var dialog = jQuery('#bn_popup_dialog');
    var dialog_wid = dialog.width();
    var dialog_hei = dialog.height();
    var window_wid = jQuery(window).width();
    var window_hei = jQuery(window).height();
    var pagina_top = jQuery(document).scrollTop();
    var pagina_lft = jQuery(document).scrollLeft();
    dialog.css({
       'top':	 pagina_top + Math.round((window_hei - dialog_hei) / 2),
       'left':	 pagina_lft + Math.round((window_wid - dialog_wid) / 2)
    });	   
}
/* funzione che nasconde le select in IE6, in quanto essendo "di sistema" stanno SEMPRE in primo piano */
function ie6selfix(disp) {
    if(jQuery.browser.msie && jQuery.browser.version==6) {
		if (disp=="show") {
			jQuery('select').css('visibility','visible');
		}
		else {
			jQuery('select').css('visibility','hidden');
		}
    }
}
jQuery.noConflict();
jQuery(function(){	/*alert(location.href);*/
    $hostName = 'http://www.gruppohera.it/';
    if ((''+location.href).indexOf('eng.gruppohera') >= 0) {
        $hostName = 'http://eng.gruppohera.it/';
    }
    else if ((''+location.href).indexOf('.od.loc') >= 0) {
		if ((''+location.href).indexOf('heraeng') >= 0) {
			$hostName = 'http://heraeng.od.loc/';
		}
		else {
			$hostName = 'http://hera2.od.loc/';
		}
    }
    var ts = new Date().getTime(); /* timestamp anticache */
    jQuery.ajax({
		'url':		$hostName+'breakingnews.php?bnu='+escape(''+location.href)+'&ts='+ts,
		'success':	function(data, status, xhr) {
				    /*alert(location.href+"\n"+status+"\n"+data);return;*/
				    data = jQuery.trim(data);
				    if (data != '') { /* style="opacity:0.42;" */
						jQuery('body')
							.append('<div id="bn_popup_cover" class="bn_box_popup_cover">&nbsp;</div>')
							.append('<div id="bn_popup_dialog" class="bn_box_popup_dialog">'+data+'</div>');
						
						var id = ''+jQuery('.bn_box').attr('id');
						if (myCookie.get('hera_'+id) != null) {
							jQuery('#bn_popup_cover,#bn_popup_dialog').remove();
						}
						ie6selfix('hide');
						bnPlacer();
						GLOBAL_bn_timeout = null;
						jQuery(window).bind('resize',bnReplacer);
						jQuery(window).bind('scroll',bnReplacer);
						if (jQuery.browser.msie && jQuery.browser.version == 6) {
							DD_belatedPNG.fix('#bn_popup_dialog div');
						}						
						GLOBAL_bn_interval = null;
						GLOBAL_bn_interval = setInterval(function(){
							var closer = jQuery('#bn_close');
							var tempo = closer.html()-0;
							if (tempo <= 0) {
								clearInterval(GLOBAL_bn_interval);
								closer.html('<span id="bn_close_real" class="bn_close_x">X</span>');
								closer.addClass('bn_box_close_x');
								jQuery('#bn_close_text').hide();
								jQuery('#bn_close_real')
									.css({
										'cursor': 'pointer'
									})
									.click(function(){
										var id = ''+jQuery('.bn_box').attr('id');
										myCookie.set('hera_'+id, 'already seen', 365);
										jQuery('#bn_popup_cover,#bn_popup_dialog').remove();
										jQuery(window).unbind('resize',bnReplacer);
										jQuery(window).unbind('scroll',bnReplacer);
										ie6selfix('show');
									});
							}
							else {
								closer.html(tempo-1);
							}
						}, 1000);
				    }
				}
    });
});
