// usa mycookie.js

// ------------------------------------------- Functions
function bbObjContainer() {
	return jQuery('#jq_bottombanner_container');
}
function bbObjRepeat() {
	return jQuery('#jq_bottombanner_repeat');
}
function bbObjImage() {
	return jQuery('#jq_bottombanner_image');
}
function bbObjClose() {
	return jQuery('#jq_bottombanner_close');
}
function bbGetREF() {
	return bbObjContainer().attr('ref');
}
function bbSetCookie() {
	myCookie.set('hera_bb_'+bbGetREF(), 'already seen', 1);
}
function bbTestCookie() {
	return (myCookie.get('hera_bb_'+bbGetREF()) != null);
}
function bbGetRepositionTop(bannerHei) {
	return jQuery(window).height() + jQuery(document).scrollTop() - bannerHei;
}
function bbReposition(top) {
	bbObjContainer().css({ 'top': top });
}
function bbNotFixedBrowser() {
	return (jQuery.browser.msie && jQuery.browser.version == '6.0');
}

function bbCloseAnimation(hei, top) {
	bbSetCookie();
	if (bbNotFixedBrowser()) {
		bbObjContainer().animate({
			'top':			top,
			'height':		hei
		}, {
			'duration':		G_bottombanner_animation_time * 1000,
			'complete': 	function(){
								bbObjContainer().remove();
							}
		});
	}
	else {
		bbObjRepeat().animate({
			'marginTop':	hei
		}, {
			'duration':		G_bottombanner_animation_time * 1000,
			'complete':		function(){
								bbObjContainer().remove();
							}
		});
	}
}

function doAnimation() {
	if (bbNotFixedBrowser()) { // per browser senza position:fixed
		try{
			DD_belatedPNG.fix('.jq_ie6fix');
		} catch(e) {}
		var top = bbGetRepositionTop(0);
		bbReposition(top);
		var bannerHei = bbObjRepeat().height();
		top -= bannerHei;
		setTimeout(function() {
			bbObjContainer().animate({
				'top':		top,
				'height':	bannerHei
			}, {
				'duration':	G_bottombanner_animation_time * 1000,
				'complete':	function(){
								jQuery(window).resize(function(){
									bbReposition(bbGetRepositionTop(bannerHei));
								});
								jQuery(window).scroll(function(){
									bbReposition(bbGetRepositionTop(bannerHei));
								});
								bbObjClose().click(function(){
									var bannerHei = bbObjRepeat().height();
									bbCloseAnimation(bannerHei, bbGetRepositionTop(0) - bannerHei);
								});
								bbObjImage().find('a').click(function(){
									var bannerHei = bbObjRepeat().height();
									bbCloseAnimation(bannerHei, bbGetRepositionTop(0) - bannerHei);
								});
							}
			});
		}, G_bottombanner_animation_start_delay * 1000);
	}
	else { // per browser con supporto corretto a position:fixed
		var contHei = bbObjRepeat().height();
		bbObjRepeat().css({
			'margin-top':	contHei
		});
		bbObjContainer().css({										
			'height':		contHei,
			'position':		'fixed',
			'bottom':		'0px',
			'top':			null
		});									
		setTimeout(function() {
			bbObjRepeat().animate({
				'marginTop':	0
			}, {
				'duration':	G_bottombanner_animation_time * 1000,
				'complete':	function(){
								bbObjClose().click(function(){
									bbCloseAnimation(bbObjRepeat().height());
								});
								bbObjImage().find('a').click(function(){
									bbCloseAnimation(bbObjRepeat().height());
								});
							}
			});
		}, G_bottombanner_animation_start_delay * 1000);
	}
}

// ------------------------------------------- MAIN -------------------------------------------
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/';
		}
    }
	//alert($hostName);
	// ##########################################################	PARAMETRI [begin]
	G_bottombanner_animation_time = 1;
	G_bottombanner_animation_start_delay = 0.5;
	// ##########################################################	PARAMETRI [end]
    var ts = new Date().getTime(); /* timestamp anticache */
	G_bottombanner_startup = setInterval(function(){
		if (jQuery('#js_tuc_sfondo').length <= 0) {
			clearInterval(G_bottombanner_startup);
			jQuery.ajax({
				'url':		$hostName+'bottombanner.php?bbu='+escape(''+location.href)+'&ts='+ts,
				'success':	function(data, status, xhr) {
								//alert(location.href+"\n"+status+"\n"+data);return;
								data = jQuery.trim(data);
								if (data == '') { // se non ho contenuto non faccio nulla
									return;
								}
								jQuery('body').append(data);
								if (bbTestCookie()) { // se l'ho già visto non lo mostro
									bbObjContainer().remove();
									return;
								}
								bbObjContainer().ready(function(){
									doAnimation();
								})
							}
			});
		}
	}, 500);
    
});
