function positionWomen()
{
try
{
	var woman_left = ($('#main').offset().left) - 38;
	$('#flash-donna').css('left', woman_left);
}
catch(e) {toDebug(e.toString());}
}


function initMenu()
{
try
{
	var sottomenu = $('.sottomenu');
	sottomenu.each(function()
	{
		if(!$(this).parent().is('.active')) $(this).hide();
		if($(this).width() < $(this).parent().width()) $(this).width($(this).parent().width());
	});
	
	var menu = $('#menu > li');
	menu.each(function()
	{
		if($(this).children('.sottomenu') && !$(this).is('.active'))
		{
			var sottomenu = $('.sottomenu', this);
			$(this).hover(function()
			{
				sottomenu.show();
			},
			function()
			{
				sottomenu.hide();
			});
			
		}
	});
}
catch(e) {toDebug(e.toString());}
}


function solariumMachinesDetail()
{
try
{
	var handler = $('#solarium-machines .macchinario');
	handler.each(function()
	{
		var detail = $(this).next();
		var prev_detail = $(this).prev('.dettaglio');
		var next_detail = detail.nextAll().filter('.dettaglio');	// è un array, quindi dopo prendo il primo elemento: next_detail[0], cioè l'immediato successivo di detail
		var prev_button = $('.prev',detail);
		var next_button = $('.next',detail);

		if(prev_detail.length > 0)
		{
			prev_button.show();
			prev_button.click(function(){toggleDetail(prev_detail);});
		}
		if(next_detail.length > 0)
		{
			next_button.show();
			next_button.click(function(){toggleDetail(next_detail[0]);});
		}
		
		$(this).css('cursor', 'pointer');
		$(this).click(function(){toggleDetail(detail);});
		$('.close',detail).click(function(){toggleDetail(detail);});
	});
}
catch(e) {toDebug(e.toString());}
}

function toggleDetail(detail)
{
try
{
	var detail = $(detail);
	if(detail.is(':hidden'))
	{
		closeAllDetails(detail.siblings('.dettaglio').filter(':visible'));
		detail.slideDown();
	}
	else detail.slideUp();
}
catch(e) {toDebug(e.toString());}	
}

function closeAllDetails(elements_to_hide)
{
try
{
	elements_to_hide.slideUp();
}
catch(e) {toDebug(e.toString());}	
}


function activateLocationCenter()
{
try
{
	var localita = $('.localita_detail');
	var elenco_localita = $('#elenco-localita a');
	
	//	mostro solo la localita active nel load della pagina
	localita.each(function()
	{
		if($(this).is('.active')) $(this).show();
	});
	
	elenco_localita.each(function(){
		$(this).bind('click',function()
		{
			var finder = $(this).attr('localita');
			var localita_toshow;
			var localita_tohide;
			
			elenco_localita.removeClass('active');
			$(this).addClass('active');
			localita.each(function()
			{
				if($(this).is('.active')) localita_tohide = $(this);
				if($(this).attr('container_localita') == finder) localita_toshow = $(this);
			});
			
			localita_tohide.slideUp();
			localita_tohide.removeClass('active');
			localita_toshow.slideDown();
			localita_toshow.addClass('active');
			
			return false;
		});
	});
	
	
	
}
catch(e) {toDebug(e.toString());}	
}

//inizio funzioni per prendere dimensioni finetra
function getPageSize() 
{
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};

function setupFlash(){
	
    var flash = $j('#flash');
    var main = $j('#main');		
    var bottom = $j('#bottom-wrap');
	var page = getPageSize();	    
	
    if($j('#flash') && $j('.index'))
	{
		var hMin = 735;
		flash.width(page[2]);
		flash.height(hMin);		
	}		

	//alert(bottom.offset().top+"|"+main.offset().top+"|"+main.height());
	/*
	alert(page[3])
	alert(main.offset().top + main.height())
	*/
	if(page[3] <= main.offset().top + main.height()+bottom.height())
    {
        bottom.css({'position':'relative'});
    }else{
        bottom.css({'position':'fixed'});	
	}

}

function openLightbox(src){
	$('#boxLightbox').html("<a href='"+src+"'></a>")
	$('#boxLightbox a').lightBox();
	$('#boxLightbox a').click();
}




/* INIZIO NEWS */

function initNews(id)
{
	createOverlay();
    showOverlay(showBox, id);
	createBox()	
	
	
}

function createOverlay()
{
try
{
    $('body').append('<div id="my_overlay"><div id="close-button"></div>');
    var overlay = $('#my_overlay');
    overlay.hide();
    overlay.css(
    {
        'position': 'fixed',
        'top': '0px',
        'left': '0px',
        'background-color': '#5b2204',
        'opacity': '0.6',
        'width': '100%',
        'height': '100%'
    });
}
catch(e) {toDebug(e.toString());}
}

function showOverlay(callback, param)
{
try
{
    var overlay = $('#my_overlay');
    overlay.slideDown('slow',function()
    {
        overlay.click(function()
        {
            closeBox(closeOverlay);
        });
        if(callback) callback(param);
    });
}
catch(e) {toDebug(e.toString());}
}

function closeOverlay()
{
try
{
    var overlay = $('#my_overlay');
    overlay.slideUp('slow',function()
    {
        overlay.remove();
    });
}
catch(e) {toDebug(e.toString());}
}

function createBox()
{
try
{
    $('body').append('<div id="news-box"><div id="close-bt"><img src="images/close.gif"></div><div id="container-news"></div></div>');
	
	var box = $('#news-box');
    box.hide();
    box.css(
    {
        'position': 'fixed',
        'top': '0px',
        'left': '50%',
        'margin-left': '-300px',
        'background-color': '#ffffff',
        'width': '600px',
        'height': '300px'
    });
	
	
	var closeButton = $('#close-bt');
	closeButton.one('click',function(){closeBox(closeOverlay);});
}
catch(e) {toDebug(e.toString());}
}

function showBox(param)
{
try
{
    var box = $('#news-box');
    var container = $('#container-news');	
    box.slideDown('slow',function()
    {
        box.animate({'top':'100px'}, function()
        {
			container.load("news.php?id="+param)
        });
    });
}
catch(e) {toDebug(e.toString());}
}

function closeBox(callback)
{
try
{
    var box = $('#news-box');
    box.animate({'top':'0px'},function()
    {
        box.slideUp('slow',function()
        {
            if(callback) callback();
            box.remove();
        });
    });
}
catch(e) {toDebug(e.toString());}
}


/* FINE NEWS */

/****************************************************************************/
//	inizio funzioni di debug
debug_msg = false;
function toDebug(string)
{
	if (debug_msg)
	{
		if((typeof window.console=="undefined")) {alert(string);}
		else {
			console.error();
			console.group(string);
			console.trace();
			console.groupEnd();
		}
	}
}
function toConsole(string)
{
	if((typeof window.console=="undefined")) {alert(string);}
	else console.log(string);
}
//fine funzioni di debug
function $j(argument)
{
	var result = jQuery(argument);
	if (result.length == 0) return null;
	else return result;
}

$j(document).ready(function()
{ 
    $(function(){$('a[rel*=lightbox]').lightBox();});
    if($('body').is('.homepage')) positionWomen();
    if($j('#solarium-machines')) solariumMachinesDetail();
    if($j('#localita-centri')) activateLocationCenter();
	
	setupFlash();
    initMenu();
});

$j(window).load(function()
{
//initNews(1);
});
$j(window).resize(function()
{
	setupFlash();
	if($('body').is('.homepage')) positionWomen();
});
/*****************************************************************************************/