$(document).ready(function() {		
	initLightbox();
													 
	$('.box_contact').hover( function() {
		$(this).css('background-color', 'white');
	}, function() {			
		$(this).css('background-color', '#fbfbfb');
	})
	
	$('.box_contact').click( function() {
		if ($(this).find('.content').is(":hidden")) {
			$(this).find('a.close').fadeIn('slow');
			$(this).find('.content').slideToggle('slow');
			$(this).css('cursor', 'default');
		}
	});
	
	$('a.close').click( function() {			
		$(this).parent().parent().find('.content').slideToggle('slow');
		$(this).parent().parent().css('cursor', 'pointer');
		$(this).fadeOut('normal');
	});
		
	$('ul.dotted a').click(function(){
		rel = $(this).attr('rel');
			
		if(rel){
			href = $(this).attr('href');	
			if(href.indexOf('/')){ //opera 8.53 bug
				href = href.substring(href.lastIndexOf('/')+1);
			}
			
			showLightbox(href, rel);			
			category = $(this).parents('.box_contact').find('h1 img').attr('alt');
			question = $(this).text();
			
			to = href.substring(0, href.indexOf('.'));
			$('input[@name=PopupTo]').attr('value', to);
			$('input[@name=PopupCategory]').attr('value', category);
			$('input[@name=PopupQuestion]').attr('value', question);

			return false;
		}
	});
});

/* by Lokesh Dhakar - http://www.huddletogether.com */

function getPageScroll(){

	var yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}


//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		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
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


//
// showLightbox()
// Preloads images. Pleaces new image in lightbox then centers and displays.
//


function showLightbox(href, rel){
	var rel;
	var href;

	if(rel.substr(0, 4) == "<h1>"){ //Si <h1> est défini (sinon texte)
		rel = rel.substr(4);
		rel = rel.split('</h1>');	
		
		$('#lightbox h1').html(rel[0]);
		
		$('div#info').show();
		$('div#suggestion').hide();
		$('#lightbox .content').css('background-image', 'url(/img/contact/icon-info.gif)'); 
		
		if(rel[1].indexOf('.inc.php') != -1){ //Si c'est une page php
			$('#contentRel').load('/contact/inc/fattura-in-formato-pdf.inc.php');
		} else {
			
			$('#contentRel').html(rel[1]);
		}
		
	} else {
		$('#lightbox h1').text('Ti ricordiamo che ..');
		$('#lightbox #contentRel').html(rel);
		$('#lightbox .content').css('background-image', 'url(/img/contact/icon-tip.gif)');
		$('div#info').hide();
		$('div#suggestion').show();
	}

	// prep objects
	var objOverlay = document.getElementById('overlay');
	var objlightbox = document.getElementById('lightbox');

	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	
	// set height of Overlay to take up whole page and show
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	$('#overlay').fadeIn('fast');
	
	// Hide select boxes as they will 'peek' through the image in IE
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}

	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3]) / 2);
	objlightbox.style.top = (lightboxTop < 0) ? "0px" : lightboxTop + "px";
	
	$('#lightbox form').attr('action', href);
	$('#lightbox').fadeIn('slow', fct_show());
	
	function fct_show(){
		timeoutShow = setTimeout("$('#lightbox').css('margin-top', -Math.floor(($('#lightbox').height())/2))",10);	
	}
	
	return false;
}


//
// hideLightbox()
//
function hideLightbox(){
	clearTimeout(timeoutShow);
	
	$('#lightbox').fadeOut('slow');
	$('#overlay').fadeOut('fast');

	// make select boxes visible
	selects = document.getElementsByTagName("select");
    for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}


//
// initLightbox()
// Function runs on window load, going through link tags looking for rel="lightbox".
// These links receive onclick events that enable the lightbox display for their targets.
// The function also inserts html markup at the top of the page which will be used as a
// container for the overlay pattern and the inline image.
//
function initLightbox(){
	var objBody = document.getElementsByTagName("body").item(0);
	
	// create overlay div and hardcode some functional styles (aesthetic styles are in CSS file)
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.onclick = function () {hideLightbox(); return false;}
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
	objOverlay.style.zIndex = '2000';
 	objOverlay.style.width = '100%';
	objBody.insertBefore(objOverlay, objBody.firstChild);
	
	$('#overlay').css('background', 'black');
	$('#overlay').css('opacity', '0.3');
	
	var popup = document.createElement("div");
	objBody.insertBefore(popup, objBody.firstChild);
	popupLoaded = false;
	$(popup).load('/contact/inc/popup-suggestion.php');
}