// JavaScript Document

// il foglio di stile che mi serve è quello relativo al file template.css
var allStyleSheets = document.styleSheets;
for (i=0; i<allStyleSheets.length; i++) {
	if (allStyleSheets[i].href &&  allStyleSheets[i].href.match('css/template.css')) {
		var myStyleSheet = allStyleSheets[i];
	}
}

if (myStyleSheet.cssRules) {
	var rules = myStyleSheet.cssRules;
} else if (myStyleSheet.rules) {
	var rules = myStyleSheet.rules;
}

for(i=0; i<rules.length; i++) {
	if (rules[i].selectorText && rules[i].selectorText.toLowerCase() == '#container') {
		var myRule = rules[i];
		break;
	}
}

var originalHeight = parseInt(myRule.style.height);
var originalTop = myRule.style.top;
var originalMargin = myRule.style.margin;

function setContainerPosition() {
	if (($(window).height()<originalHeight) && (parseInt($('#container').css('top'))>0)) {
		$('#container').css('top', '0px');
		$('#container').css('margin-top', '0px');
	} else if (($(window).height()>=originalHeight) && (parseInt($('#container').css('top'))==0)) {
		$('#container').css('top', originalTop);
		$('#container').css('margin', originalMargin);
	}
}

String.prototype.trim = function () {
	return this.replace(/^[\t\r\n\s]+|[\t\r\n\s]+$/ig, '');
}

function getByClass(myClass, myTag) {
	if (document.getElementsByClass) {
		return document.getElementsByClass(myClass);
	} else if (document.getElementsByTagName) {
		myTag = (myTag)?myTag:'*';
		var allElements = document.getElementsByTagName(myTag);
		var myElements = new Array();
		for (i=0; i<allElements.length; i++) {
			if (allElements[i].className == myClass) {
				myElements.push(allElements[i]);
			}
		}
		if (myElements.length) {
			return myElements;
		} else {
			return false;
		}
	} else if (document.all) {
		myTag = (myTag)?myTag:'*';
		var allElements = eval('document.'+myTag);
		var myElements = new Array();
		for (i=0; i<allElements.length; i++) {
			if (allElements[i].hasAttribute('class') && allElements[i].getAttribute('class') == myClass) {
				myElements.push(allElements[i]);
			}
		}
	}
	return false;
}

function toggleCategories(myTitle) {
	$(myTitle).click(function() {
		var tempSubcategories = $(this).parent().find('.subcategory:visible');
		$('.subcategory').hide();
		$('.subcategory .faq').hide();
		if (!tempSubcategories.length) {
			$(this).parent().find('.subcategory').toggle();
		}
	});
}

function toggleSubcategories(myTitle) {
	$(myTitle).click(function() {
		var tempFAQs = $(this).parent().find('.faq:visible');
		$('.subcategory .faq').hide();
		if (!tempFAQs.length) {
			$(this).parent().find('.faq').toggle();
		}
	});
}

function createTooltip(id_tooltip) {
	if (document.getElementById('tooltip')) {
		$('#tooltip').fadeOut(function() {
			$(this).remove();
			return createTooltip(id_tooltip);
		});
	} else {
		$('body').append('<div id="tooltip"><img id="bg-tooltip" src="images/bg-tooltip.png" alt="Background Tooltip" /><div id="closeTooltip">[Chiudi]</div><div id="titleTooltip"></div><div id="contentTooltip"></div></div>');
	}
	$('#closeTooltip').click(function() {
		$('#tooltip').fadeOut(function() {
			$(this).remove();
		});
	});
	$('#titleTooltip').load(
		'script/php/ajax.titleTooltip.php',
		{'id_tooltip': id_tooltip},
		function() {
			$('#contentTooltip').load(
				'script/php/ajax.contentTooltip.php',
				{'id_tooltip': id_tooltip},
				function() {
					$('#tooltip').fadeIn();
				}
			);			
		}
	);
	return false;
}

function richiediServizio(mioServizio) {
	if (document.getElementById('bgRichiediServizio')) {
		$('#bgRichiediServizio').fadeOut(function() {
			$(this).remove();
			return richiediServizio(mioServizio);
		});
	} else {
		var newDiv = '<div id="bgRichiediServizio"><img id="imgBGRighiediServizio" src="images/faded-bg.png" alt="richiedi servizio" /><div id="containerRichiediservizio"><div id="closeRichiediServizio" title="Chiudi"></div><div id="contentRichiediServizio"></div></div></div>';
		$('body').append(newDiv);
	}
	$('#closeRichiediServizio').click(function() {
		$('#bgRichiediServizio').fadeOut(function() {
			$(this).remove();
		});
	});
	$('#contentRichiediServizio').load(
		'../script/php/ajax.contentRichiediServizio.php',
		{servizio: mioServizio},
		function(data) {
			$(this).html(data);
			$('#bgRichiediServizio').fadeIn();
		}
	);
	$('#bgRichiediServizio').fadeIn();
	return false;
}

function download(id_circolare) {
	if ($('#download_circolare').get(0)) {
		$('#download').val(id_circolare);
		$('#download_circolare').submit();
	}
	return false;
}

function validateForm(myForm) {
	myForm['nome_cognome'].value = myForm['nome_cognome'].value.trim();
	myForm['citta'].value = myForm['citta'].value.trim();
	myForm['indirizzo'].value = myForm['indirizzo'].value.trim();
	myForm['tel_fax'].value = myForm['tel_fax'].value.trim();
	myForm['email'].value = myForm['email'].value.trim();
	myForm['messaggio'].value = myForm['messaggio'].value.trim();

	$('.error').removeClass('error');

	var errors = new Array();

	if (myForm['nome_cognome'].value=='') {
		errors.push('nome_cognome');
	}
	if (myForm['tel_fax'].value=='') {
		errors.push('tel_fax');
	}
	if (myForm['email'].value!='' && !myForm['email'].value.match(/^[a-zA-Z0-9\.\-\_]+@[a-zA-Z0-9\.\_\-]+\.[a-zA-Z0-9]{2,}$/ig)) {
		errors.push('email');
	}

	if (errors.length>0) {
		myForm[errors[0]].focus();
		for (i=0; i<errors.length; i++) {
			$(myForm[errors[i]]).addClass('error');
		}
		window.alert("Ricontrolla i campi in rosso.");
		return false;
	}

	$.post("send-email.php", {
		nome_cognome: myForm['nome_cognome'].value,
		citta: myForm['citta'].value,
		indirizzo: myForm['indirizzo'].value,
		tel_fax: myForm['tel_fax'].value,
		email: myForm['email'].value,
		servizio: myForm['selectservizio'].value,
		messaggio: myForm['messaggio'].value
	}, function (data) {
		$('#bgRichiediServizio').fadeOut();
		$('#closeRichiediServizio').fadeOut();
		$('#containerRichiediservizio').fadeOut();
		window.alert("Email inviata con successo.");	
	} );
	
	return false;
}

$(document).ready(function(){
	//setContainerPosition();
	var allFAQsCategories = getByClass('category');
	if (allFAQsCategories && allFAQsCategories.length) {
		$('.category .subcategory').hide();
		$('.subcategory .faq').hide();
		toggleCategories('.titleCategory');
		toggleSubcategories('.titleSubcategory');
	}
});

//$(window).resize(function() {
	//setContainerPosition();
//});

