function popup(src, w, h, unique, scrollbars) {
	if (w == null) w = 450;
	if (h == null) h = 550;
	
	if (!scrollbars)
		scrollbars = 'no';
	else
		scrollbars = 'yes';
	
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	
	var wname = (unique) ? 'myPopup' : '';
	window.open(src, wname, 'scrollbars='+ scrollbars +',width='+w+',height='+h+', top='+ wint +',left='+ winl);
}

function popup_image(imageName,alt) {
  newWindow = window.open("","newWindow","scrollbars=no");
  newWindow.document.open();
  newWindow.document.write('<html>');
  newWindow.document.write('<script language="javascript">\n');
  newWindow.document.write('var NS = (navigator.appName=="Netscape")?true:false;\n');
  newWindow.document.write('function FitPic(){iWidth = (NS)?window.innerWidth:document.body.clientWidth; iHeight = (NS)?window.innerHeight:document.body.clientHeight; iWidth = document.images[0].width - iWidth; iHeight = document.images[0].height - iHeight; window.resizeBy(iWidth, iHeight); self.focus(); };')
  newWindow.document.write('</script>');
  newWindow.document.write('<title>'+alt+'</title><body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onLoad="FitPic()" onBlur="self.close()">'); 
  newWindow.document.write('<img src=\"'+imageName+'\" alt=\"'+alt+'\" name=\"imgz\">');
  newWindow.document.write('</body></html>');
  newWindow.document.close();
  newWindow.focus();
}

function toggle(id) {
	var d = document.getElementById(id);
	d.style.display = (d.style.display == 'none') ? 'block' : 'none';
}

function do_rollovers() {
	if (!document.getElementById) return
		var imgOriginSrc;
		var imgTemp = new Array();
		var imgarr = document.getElementsByTagName('img');
		
		for (var i = 0; i < imgarr.length; i++) {
			if (imgarr[i].getAttribute('hsrc')) {
				imgTemp[i] = new Image();
				imgTemp[i].src = imgarr[i].getAttribute('hsrc');
				
				imgarr[i].onmouseover = function() {
					imgOriginSrc = this.getAttribute('src');
					this.setAttribute('src',this.getAttribute('hsrc'))
				}
				
				imgarr[i].onmouseout = function() {
					this.setAttribute('src',imgOriginSrc)
				}
				
			}
		}
}

function copy2clipboard(s) {
	if (window.clipboardData && clipboardData.setData )	{
		clipboardData.setData("Text", s);
	}	else {
		user_pref("signed.applets.codebase_principal_support", true);
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;[[[[1]]]]'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip) return;
		var trans = Components.classes['@mozilla.org/widget/transferable;[[[[1]]]]'].createInstance(Components.interfaces.nsITransferable);
		if (!trans) return;
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;[[[[1]]]]"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=meintext;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*[[[[2]]]]);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip) return false;
		clip.setData(trans,null,clipid.kGlobalClipboard);	   
	}
}

function vScroll(content,container,speed) {

	var content = document.getElementById(content);
	var container = document.getElementById(container);
	var interval;
	
	if (speed == null) var speed = 2;
	
	content.style.top = container.style.height;

	var move = function() { 
		interval = setInterval(
			function() {
				var actual_top = content.style.top.substr(0,content.style.top.length - 2);
				if (-(actual_top) > content.offsetHeight) content.style.top = container.style.height;
				else content.style.top = (actual_top - 1) + "px";
			}
		,Math.round(40 / speed));
	}
	
	var stop = function() { clearInterval(interval); }
	
	container.onmouseover = function() { stop(); }
	container.onmouseout = function() { move(); }
	
	move();
}

function view_props(obj) {
	for (prop in obj)
		alert('Property: '+ prop +'\nValue: '+ obj[prop]);
}

function submitToStep(step) {
	document.getElementById('nextstep').value = step;
	document.getElementById('form_steps').submit();
}

var location_values_changed = false;
function trigger_change() { location_values_changed = true; }

function openMap(step){
	
	var lat = document.getElementById('ho_lat').value;
	var lng = document.getElementById('ho_lng').value;
	var address = document.getElementById('ho_gmaps_addr').value;
	var citta = document.getElementById('ho_citta').value;
	var indirizzo = document.getElementById('ho_indirizzo').value;
	var paese = document.getElementById('ho_paese').selectedIndex;
	
    if (
		(location_values_changed || lat == '' || lng == '' || address == '') &&
		(citta != '' && indirizzo != '' && paese > 0)
	) {
        popup('../maps/confirm.php', 600, 600, true, false);
    }
    else {
        submitToStep(step);
    }
}

function preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

Array.prototype.in_array = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/*
window.onload = function() {
	vScroll('news_content', 'news_container');
	preloadImages('../img/chi_siamo_on_en.png', '../img/chi_siamo_on_it.png', '../img/come_funziona_on_en.png', '../img/come_funziona_on_it.png',
	              'contatti_on_en.png', 'contatti_on_it.png', 'dicono_di_noi_on_en.png', 'dicono_di_noi_on_it.png', 'faq_on_it.png');
}
*/

function selAllCheckboxes(fld) {
	var checked = fld.checked;
	var els = fld.form.elements;
	for (var i = 0; i < els.length; i++) {
		els[i].checked = checked;
	}
}

function selectItemSearch(li) {
	if (li.extra) {
		$('#lat_hlat').val(li.extra[0]);
		$('#lat_hlng').val(li.extra[1]);
	}
	return false;
}

function check2fields(fld1_id, fld2_id, error_msg) {
	var val1 = $('#'+fld1_id).val();
	var val2 = $('#'+fld2_id).val();
	if (val1 != '' && val2 != '') {
		return true;
	}
	alert(error_msg);
	return false;
}

$(document).ready( function(){
	vScroll('news_content', 'news_container');
	preloadImages('../img/chi_siamo_on_en.png', '../img/chi_siamo_on_it.png', '../img/come_funziona_on_en.png', '../img/come_funziona_on_it.png',
	              'contatti_on_en.png', 'contatti_on_it.png', 'dicono_di_noi_on_en.png', 'dicono_di_noi_on_it.png', 'faq_on_it.png');
	
	/*$("#lat_autocity").autocomplete("../search/cities_geolocations.php", {
		delay: 10,
		minChars: 2,
		matchSubset: 1,
		matchContains: 0,
		mustMatch: 1,
		cacheLength: 10,
		maxItemsToShow:10,
		onItemSelect: selectItemSearch,
		autoFill: false
	});
	
	
	$(".simple_autocity").autocomplete("../search/cities_geolocations.php", {
		delay: 10,
		minChars: 2,
		matchSubset: 1,
		matchContains: 0,
		mustMatch: 0,
		cacheLength: 10,
		maxItemsToShow:10,
		autoFill: false
	});
	*/
	
	$(".speedy_country").change( function() {
		var sel_country = $(this).val();
		var city_el = $('.speedy_city', $(this).parent().parent());
		city_el.html('<option value=""></option>');
		$('.ac_loading').show();
		if (sel_country != '') {
			city_el.load('../search/speedy_select.php', {'country': $(this).val()}, function() {
				city_el.removeAttr('disabled');
				$('.ac_loading').hide();
			});
		} else {
			city_el.html('<option value=""></option>');
			city_el.attr('disabled', 'disabled');
		}
	});
	
	
});

