d = document;

var _isIE, _isIE5, _isIE6, _isIE7, _isKonq, _isSafari, _isMoz, _isOpera, _isMac, _isMoz, _isSafari;
(function(){ var a=navigator.userAgent.toLowerCase(); _isIE = a.indexOf("msie") != -1; _isIE5 = a.indexOf("msie 5")!= -1 && document.all; _isIE6 = a.indexOf("msie 6") != -1 && document.all; _isIE7 = a.indexOf("msie 7") != -1 && document.all; _isKonq = a.indexOf("konqueror") != -1; _isSafari = a.indexOf("safari") != -1 || _isKonq; _isMoz = !_isIE && !_isSafari && a.indexOf("mozilla") != -1; _isOpera = !(!window.opera); })();

onload = function() {
	if ($('livemusic') && $('home')) {
		timer = setTimeout(avviaEffetto, 1500);
	}

	if ($('div_news_dx')) {
		timerNews = setInterval(scrollaNews, 5000);
	}

	var maxSize = 0, size1, size2;
	if ($('colonnadx')) {

		if (_isIE) {
			size1 = $('colonnadx').clientHeight;
			size2 = $('colonnasx').clientHeight;
		} else {
			size1 = $('colonnadx').scrollHeight;
			size2 = $('colonnasx').scrollHeight;
		}
		maxSize = Math.max(size1, size2);

		if (maxSize != 0) {
			$('colonnadx').setStyle({height: maxSize + "px"});
			$('colonnasx').setStyle({height: maxSize + "px"});
		}
	}
}

function avviaEffetto() {
	attesa = 10000 * Math.random();
	durata = Math.random();
	// transition: linear, sinoidal, reverse, flicker, wobble, pulse, full, none
	new Effect.Pulsate('livemusic', {duration: durata, transition: Effect.Transitions.flicker});
	if ($('livemusic').style.opacity < 1) $('livemusic').style.opacity = 1;
	timer = setTimeout(avviaEffetto, attesa);
}
function bloccaEffetto() {
	clearTimeout(timer);
}

function scrollaNews() {
	var idNews = parseInt($F('idDivNews'));
	var totNews = $F('totDivNews');

	if (parseInt(totNews) > 1) {
		var div = "divNewsDx" + idNews.toString();

		var next = (parseInt(idNews) < parseInt(totNews)) ? parseInt(idNews) + 1 : 1;
		var divNext = "divNewsDx" + next.toString();

		$(div).setStyle({display: 'none'});
		$(divNext).setStyle({display: 'block'});
		new Effect.Opacity(divNext, 1.0);

		//var idNews = next;
		$('idDivNews').value = next;
	}
}

// chiamata di gestione dell'errore Ajax
function errore(request, div) {
	if ($(div)) {
		$(div).innerHTML = "Errore nella chiamata Ajax";
	}
}

// apro/chiudo il menu
function swapDiv(id, link) {
	Effect.toggle(id, 'slide');

	if ($(id).style.display == 'none') {
		$(link).className = "t_menu";
	} else {
		$(link).className = "t_menu chiuso";
	}
}

// espando la descrizione delle birre
function espandiDesc(id) {
	new Effect.toggle(id.childNodes[1], 'appear', {duration: 0.5});
	id.childNodes[3].style.display = (id.childNodes[1].style.display == 'none') ? 'none' : 'inline';
}

// visualizzo il calendario di selezione data in un div
function showDivCalendario(field, top, left) {
	/* Risolviamo un problema di retrocompatibilità */
	var IE = document.all?true:false
	var isDOM      = (typeof(document.getElementsByTagName) != 'undefined' && typeof(document.createElement) != 'undefined') ? 1 : 0;
	var isIE4      = (typeof(document.all) != 'undefined' && parseInt(navigator.appVersion) >= 4) ? 1 : 0;
	var isNS4      = (typeof(document.layers) != 'undefined') ? 1 : 0;
	var capable    = (isDOM || isIE4 || isNS4) ? 1 : 0;
	// Uggly fix for Opera and Konqueror 2.2 that are half DOM compliant
	if (capable) {
		if (typeof(window.opera) != 'undefined') {
			var browserName = ' ' + navigator.userAgent.toLowerCase();
			if ((browserName.indexOf('konqueror 7') == 0)) { capable = 0; }
		} else if (typeof(navigator.userAgent) != 'undefined') {
			var browserName = ' ' + navigator.userAgent.toLowerCase();
			if ((browserName.indexOf('konqueror') > 0) && (browserName.indexOf('konqueror/3') == 0)) { capable = 0; }
		} // end if... else if...
	} // end if

	if (IE) {
		left = event.clientX;
		top = event.clientY;
	}
	openCalendar(field, top, left);
}

/* CALENDARIO --------------------------- */
var day;
var month;
var year;

// Initializes calendar window.
function initCalendar() {
	var isDOM      = (typeof(document.getElementsByTagName) != 'undefined' && typeof(document.createElement) != 'undefined') ? 1 : 0;
	var isIE4      = (typeof(document.all) != 'undefined' && parseInt(navigator.appVersion) >= 4) ? 1 : 0;
	var isNS4      = (typeof(document.layers) != 'undefined') ? 1 : 0;
	var capable    = (isDOM || isIE4 || isNS4) ? 1 : 0;
	// Uggly fix for Opera and Konqueror 2.2 that are half DOM compliant
	if (capable) {
		if (typeof(window.opera) != 'undefined') {
			var browserName = ' ' + navigator.userAgent.toLowerCase();
			if ((browserName.indexOf('konqueror 7') == 0)) { capable = 0; }
		} else if (typeof(navigator.userAgent) != 'undefined') {
			var browserName = ' ' + navigator.userAgent.toLowerCase();
			if ((browserName.indexOf('konqueror') > 0) && (browserName.indexOf('konqueror/3') == 0)) { capable = 0; }
		} // end if... else if...
	} // end if

	var month_names = new Array("Gennaio", "Febbraio", "Marzo", "Aprile", "Maggio", "Giugno", "Luglio", "Agosto", "Settembre", "Ottobre", "Novembre", "Dicembre");
	var day_names = new Array("Lun", "Mar", "Mer", "Gio", "Ven", "Sab", "Dom");

	if (!year && !month && !day) {
		/* Called for first time */
		if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) {
			dt      = new Date();
			year    = dt.getFullYear();
			month   = dt.getMonth();
			day     = dt.getDate();
		}
	} else {
		/* Moving in calendar */
		if (month > 11) {
		   month = 0;
		   year++;
		}
		if (month < 0) {
		   month = 11;
		   year--;
		}
	}

	if ($) {
		cnt = $("calendario");
	} else if (document.all) {
		cnt = document.all["calendario"];
	}
	$("calendario").innerHTML = "";
	str = ""

	//heading table
	str += '<table class="intestazione" cellpadding="0" cellspacing="0">';
	str += '<tr>';
	str += '<th width="40%"><a href="javascript:month--; initCalendar();"><img src="immagini/b_left.gif" alt="&laquo;" title="Mese precedente" /></a> ' + month_names[month] + ' <a href="javascript:month++; initCalendar();"><img src="immagini/b_right.gif" alt="&raquo;" title="Mese successivo" /></a></th>';
	str += '<th width="20%"><a href="javascript:dt=new Date();year=dt.getFullYear();month=dt.getMonth();day=dt.getDate();initCalendar();"><img src="immagini/b_oggi.gif" alt="Oggi" title="Data odierna" /></a></th>';
	str += '<th width="40%"><a href="javascript:year--; initCalendar();"><img src="immagini/b_left.gif" alt="&laquo;" title="Anno precedente" /></a> ' + year + ' <a href="javascript:year++; initCalendar();"><img src="immagini/b_right.gif" alt="&raquo;" title="Anno successivo" /></a></th></tr></table>';

	str += '<table class="calendar"><tr>';
	for (i = 0; i < 7; i++) {
		str += "<th>" + day_names[i] + "</th>";
	}
	str += "</tr>";

    var firstDay = new Date(year, month, 1).getDay();
    var lastDay = new Date(year, month + 1, 0).getDate();
	if (firstDay == 0) firstDay = 7;
    str += "<tr>";

    dayInWeek = 0;
    for (i = 0; i < firstDay - 1; i++) {
        str += "<td>&nbsp;</td>";
        dayInWeek++;
    }
    for (i = 1; i <= lastDay; i++) {
        if (dayInWeek == 7) {
            str += "</tr><tr>";
            dayInWeek = 0;
        }

        dispmonth = 1 + month;

		actVal = formatNum2(i, 'day') + "/" + formatNum2(dispmonth, 'month') + "/" + formatNum4(year);
        if (i == day) {
            style = ' class="selected"';
        } else {
            style = ' class="linkvuoto"';
        }
        str += "<td" + style + "><a href=\"javascript:alert('" + actVal + "');\">" + i + "</a></td>"
        dayInWeek++;
    }
    for (i = dayInWeek; i < 7; i++) {
        str += "<td>&nbsp;</td>";
    }

    str += "</tr></table>";

    $("calendario").innerHTML = str;

}

/* Formattiamo un numero a 2 cifre
 * @param   int number to format. */
function formatNum2(i, valtype) {
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') {
        switch(valtype) {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;

            case 'day':
                f = (f > 31 ? 31 : f);
                break;

            case 'hour':
                f = (f > 24 ? 24 : f);
                break;

            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

/* formattiamo un numero a 4 cifre */
function formatNum4(i) {
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}

/**
 * Ritorniamo la data dal calendario
 * @param   string     date text
 */
function returnDate(d) {
    txt = d;
    // dateField.value = txt;
    // document.getElementsByTagName("body")[0].removeChild($("divCalendario"));
}

/* MAPS --------------------------------- */
function load() {
	if (GBrowserIsCompatible()) {
		var map = new GMap2(document.getElementById("map"));
		map.addControl(new GSmallMapControl());
		map.setCenter(new GLatLng(45.211369, 11.677360), 13);
		map.enableContinuousZoom();
		map.enableScrollWheelZoom();

		var icon = new GIcon();
		icon.image = "http://www.zebbrapub.it/immagini/icon_20.png";
		icon.iconSize = new GSize(32, 40);
		icon.iconAnchor = new GPoint(25, 40);
		icon.infoWindowAnchor = new GPoint(5, 1);

		var point = new GLatLng(45.211369, 11.677360);

		var marker = new GMarker(point, icon);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<strong>Zebbra Pub</strong><br />Via Deserto, 46<br />35042 Este (PD)");
		});

		map.addOverlay(marker);
	}
}
