if (navigator.userAgent.match(/Chrome/i)) {
	window.addEvent('load', function() {resize();});
	window.addEvent('resize',function(){resize();});
}

function resize(){
	contenitoreW = 1200;
	win = window.getSize();
	
	if (win.x>960){ 
		c_left = (win.x-contenitoreW)/2;
		if ($('mainContainer').clientHeight > win.y) {
			c_left = c_left-9;
		}
		$('mainContainer').setStyles({'background-position':c_left+'px 0px'});
	}else{
		$('mainContainer').setStyles({'background-position':'center top'});
	}
}

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function isInternetExplorer(){
	var browserName=navigator.appName; 
	if (browserName=="Microsoft Internet Explorer")
		return true;
 	else
		return false;
}

function resetDeck(id){
	$$('#deck-lists li').each(function(element,index) {
		element.removeClass('selected');
	});
	
	$$('#deck-levels li').each(function(element,index) {
		element.removeClass('active');
	});
	$('deck-level'+id).addClass('active');
	
	$$('#deckPlans li').each(function(element,index) {
		element.removeClass('active');
	});
	$('deckPlans'+id).addClass('active');
	
	$$('.deck-plan-map-zoom a').each(function(element,index) {
		element.removeClass('active');
	});
	$('deck-plan-map-zoom'+id).addClass('active');
	
	$$('.legend div.divTable').each(function(element,index) {
		element.removeClass('active');
	});
	$('mapKey'+id).addClass('active');
}

function showDeck(id){
	resetDeck(id);
	$('deck-list'+id).addClass('selected');
}

function initDecks(totDeck){
	$$('#deck-lists li').each(function(element,index) {			
		element.addEvents({'click': function(){	
			id = this.id.replace('deck-list','');
			currentDeck = id;
			resetDeck(id);		
			this.addClass('selected');
		}});
	});
	
	$$('div.deckPlansPoint').each(function(element,index) {
		element.addEvents({'mouseover': function(){		
			id = this.id.replace('point','');
			$('tooltip'+id).setStyle('display','block');
		}});
	});
	$$('div.toolTip').each(function(element,index) {
		element.addEvents({'mouseleave': function(){		
			this.setStyle('display','none');
		}});
	});
	
	$('button-deckplan-down').addEvent('click', function(event){
		if (currentDeck < (totDeck-1)) {
			currentDeck++;
	    	showDeck(currentDeck);
	    }
	});
	$('button-deckplan-up').addEvent('click', function(event){
		if (currentDeck > 0) {
			currentDeck--;
		    showDeck(currentDeck);
	    }
	});
}

function teaserHeight(id){
	maxHeight = 0;
	$$("#"+id+" h3").each(function(element,index) {
		if (element.scrollHeight > maxHeight)
			maxHeight = element.scrollHeight;
	});
	
	$$("#"+id+" h3").each(function(element,index) {		
		element.setStyle("height",maxHeight);
	});
}

function openCloseSpan(spanId, spanImgId){
	spanId = document.getElementById(spanId);
	spanIdDisplay = spanId.style.display;
	if (spanIdDisplay == "none"){
	 	imgSpan = document.getElementById(spanImgId);
		valueSrcImg = imgSpan.src;
		valueSrcImg = valueSrcImg.replace("ico_chiuso.gif", "ico_aperto.gif");
		imgSpan.src = valueSrcImg;
		spanId.style.display = "block";
	}
	else if (spanIdDisplay == "block"){
	 	imgSpan = document.getElementById(spanImgId);
		valueSrcImg = imgSpan.src;
		valueSrcImg = valueSrcImg.replace("ico_aperto.gif", "ico_chiuso.gif");
		imgSpan.src = valueSrcImg;
		spanId.style.display = "none";
	}	
}

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

function reloadSelect(idToUpdate, pageUrl, widthToBeUpdate) {

	var req = new Request.HTML({url:pageUrl, 
		onRequest: function(){
			$(idToUpdate+'Container').setStyle('display','block');	
			$(idToUpdate+'Container').set('html', '<h4>loading...</h4>');								
		},
		onComplete: function(responsedue){
			$(idToUpdate+'Container').empty().adopt(responsedue);
		},
		onSuccess: function() {
			window.addEvent('domready', function(){
				$(idToUpdate).setStyle('display','none');
				//parent = $(idToUpdate).getParent();
				//parentId = parent.id;
				parentId = idToUpdate+'1';		
				numberOfChoice = 0;
				$$('#'+idToUpdate+' option').each(function(element,index) {
					if (element.className!="hidden") {
						numberOfChoice++;
					}
				});
				if (numberOfChoice<=0) {
					mandatory = false;
					$$('#'+idToUpdate+' h4 dfn').each(function(element,index) {
						if (element.className!="hidden") {
							mandatory = true
						}
					});
					if (!mandatory)
						$(idToUpdate+'Container').setStyle('display','none');
				}
				var newSelect = new elSelect( {container : parentId, tagContainer:'select', tagElements:'option', baseClass:'formSelection', width:widthToBeUpdate} );
			});
		}
	});
	
	req.send();
}
