// JavaScript Document
//HINT

var offsetfrommouse=[10,20]; 
var displayduration= 5; 
var currentimageheight = 30;

var currentimagewidth = 50;	// maximum image size.
var timer;

function gettrailobj(){
if (document.getElementById)
return document.getElementById("hint").style
}

function gettrailobjnostyle(){
if (document.getElementById)
return document.getElementById("hint")
}


function truebody(){
return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}


function hide_hint(){	
	gettrailobj().display= "none";
	document.onmousemove="";
	gettrailobj().left="-500px"
	clearTimeout(timer);
}

function show_hint(value){

	v = value;
	timer = setTimeout("show('"+value+"');",10);
	followmouse();
}
function show(value){

    var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)
	
	docwidth=2000;
	docheight=2000;

	if( (navigator.userAgent.indexOf("Firefox")!=-1 || (navigator.userAgent.indexOf("Opera")==-1 && navigator.appVersion.indexOf("MSIE")!=-1))) {
		document.onmousemove=followmouse;
		gettrailobjnostyle().innerHTML = value;
		gettrailobj().display="block";
	}
}



function followmouse(e){

	var xcoord=offsetfrommouse[0]
	var ycoord=offsetfrommouse[1]

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth - offsetfrommouse[0]
	var docheight=document.all? Math.min(truebody().scrollHeight, truebody().clientHeight) : Math.min(window.innerHeight)

	docwidth=3000;
	docheight=3000;

	if (typeof e != "undefined"){
		if (docwidth - e.pageX < currentimagewidth){
			if(navigator.userAgent.indexOf("Firefox")!=-1)	{xcoord = e.pageX - xcoord - currentimagewidth + 2*offsetfrommouse[0]} else{ xcoord = e.pageX - xcoord - currentimagewidth + 6*offsetfrommouse[0] ;} // Move to the left side of the cursor
			} else {
				xcoord += e.pageX;
			}
			if (docheight - e.pageY < (currentimageheight + 110)){
				ycoord += e.pageY - Math.max(0,(110 + currentimageheight + e.pageY - docheight - truebody().scrollTop));
			} else {
			ycoord += e.pageY;
			}
	} else if (typeof window.event != "undefined"){
		if (docwidth - event.clientX < currentimagewidth){
			xcoord = event.clientX + truebody().scrollLeft - xcoord - currentimagewidth + 2*offsetfrommouse[0]; // Move to the left side of the cursor
		} else {
			xcoord += truebody().scrollLeft+event.clientX
		}
		if (docheight - event.clientY < (currentimageheight + 110)){
			ycoord += event.clientY + truebody().scrollTop - Math.max(0,(110 + currentimageheight + event.clientY - docheight));
		} else {
			ycoord += truebody().scrollTop + event.clientY;
		}
	}

	var docwidth=document.all? truebody().scrollLeft+truebody().clientWidth : pageXOffset+window.innerWidth-offsetfrommouse[0]
	var docheight=document.all? Math.max(truebody().scrollHeight, truebody().clientHeight) : Math.max(document.body.offsetHeight, window.innerHeight)
	if(ycoord < 0) { ycoord = ycoord*-1; }
	gettrailobj().left=xcoord+"px"
	gettrailobj().top=ycoord+"px"
	
	document.onclick=hide_hint;
}

//------------------------

$(function(){
	var speed = 6000;
	$(".slide img").hide();
	
	var tot = $(".slide img").length;

	$(".slide img").each(function(index){
		var slide_height = $(".slide").height();
		$(".slide").append("<div id=\"slide_"+index+"\" style=\"height:"+slide_height+"px; background-image:url('"+$(this).attr("src")+"')\"></div>");
		$("#slide_0").show();
	});
	
	var index = 0;
	setInterval(function(){
		
		if($("#slide_0").css("display")!="none")
			index++;
		//alert(index);
		$(".slide div").fadeOut("slow");
		$("#slide_"+index).fadeIn("slow");
		index++;
		if(index == tot)
			index = 0;
	},speed);
	
	
	//SUPPORT PLACEHOLDER
	$('[placeholder]').focus(function() {
	  var input = $(this);
	  if (input.val() == input.attr('placeholder')) {
		input.val('');
		input.removeClass('placeholder');
	  }
	}).blur(function() {
	  var input = $(this);
	  if (input.val() == '' || input.val() == input.attr('placeholder')) {
		input.addClass('placeholder');
		input.val(input.attr('placeholder'));
	  }
	}).blur();
	
	$('[placeholder]').parents('form').submit(function() {
	  $(this).find('[placeholder]').each(function() {
		var input = $(this);
		if (input.val() == input.attr('placeholder')) {
		  input.val('');
		}
	  })
	});
	
});

$(document).ready(function(){
	//FORM DI RICERCA DEI RIVENDITORI
	var changed = 0;
	
	$("#regione, #provincia, #citta").focus(function(){
		changed = 0;	
	});

	
	$("#regione").autocomplete({
		delay: 0,
		minLength: 1,
		source: function(request, response) {
			$.ajax({
				type:"post",
				url:"include/ajax/comuni.php",
				dataType: "json",
				data:{
					what: "regione",
					term: request.term
				},
				success:function(data){
					response($.map(data, function(item) {
						return {
							id: item.id,
							label: item.label.replace(/\|/gi,"'"),
							value: item.value.replace(/\|/gi,"'")
						}
					}))
				}
			})
		},
		select: function(event,ui){
			changed = 1;
		},
		change: function(event,ui){
			//$("#provincia").val("");
			if(!ui.item && changed==0){
				//$("#regione").val("Scrivi la regione");
				//$("#regione").val("")
			}
			changed=0;
		}
	});

	$("#provincia").autocomplete({
		delay: 0,
		minLength: 1,
		source: function(request, response) {
			$.ajax({
				type:"post",
				url:"include/ajax/comuni.php",
				dataType: "json",
				data:{
					what: "provincia",
					term: request.term,
					regione:($("#regione").val()!="")?$("#regione").val():""
				},
				success:function(data){
					response($.map(data, function(item) {
						return {
							id: item.id,
							label: item.label.replace(/\|/gi,"'"),
							value: item.value.replace(/\|/gi,"'")
						}
					}))
				}
			})
		},
		select: function(event,ui){
			changed = 1;
		},
		change: function(event,ui){
			//$("#citta").val("");
			if(!ui.item && changed==0){
				//$("#provincia").val("Scrivi la provincia");
				//$("#provincia").val("");
			}
			changed=0;
		}
	});


	$("#citta").autocomplete({
		delay: 0,
		minLength: 1,
		source: function(request, response) {
			$.ajax({
				type:"post",
				url:"include/ajax/comuni.php",
				dataType: "json",
				data:{
					what: "citta",
					term: request.term,
					regione:($("#regione").val()!="")?$("#regione").val():"",
					provincia:($("#provincia").val()!="")?$("#provincia").val():""
				},
				success:function(data){
					//alert(data);
					response($.map(data, function(item) {
						return {
							id: item.id,
							label: item.label.replace(/\|/gi,"'"),
							value: item.value.replace(/\|/gi,"'")
						}
					}))
				}
			})
		},
		select: function(event,ui){
			changed = 1;
		},
		change: function(event,ui){
			if(!ui.item && changed==0){
				//$("#citta").val("Scrivi la città");
				//$("#citta").val("");
			}
			changed=0;
		}
	});

});

function sposi_sostenitori(){
	$.ajax({
		type: "get",
		url: "ajax.php",
		data:{
			filetocall:"sposi_sostenitori",
			page: page	
		},
		success: function(html){
			$("#elenco_commenti").html(html);
		}
	});
}

function rivenditori(){
	$.ajax({
		type: "post",
		url: "ajax.php",
		data:{
			filetocall:"rivenditori",
			page: page	,
			regione: regione,
			provincia: provincia,
			citta: citta
		},
		success: function(html){
			$("#rivenditori_container").html(html);
		}
	});
}

function prodotti(){
	$.ajax({
		type: "get",
		url: "ajax.php",
		data:{
			filetocall: "prodotti",
			id: categoria,
			page: page
		},
		success: function(html){
			//alert(html);
			$("#prodotti_container").html(html);
			$('#categoria a').lightBox();
			$('#prodotti a').lightBox();
		}
	});
}


function showdetails(id){
	if($('#dettagli_'+id).css("display")=='none'){
		$('#dettagli_'+id).show();
	}else{
		$('#dettagli_'+id).hide();
	}
}

function Mappa(indirizzo, riv){
	window.open('google.php?indirizzo='+indirizzo+'&rivenditore='+riv,'MappaGoogle','width=525,height=445,left=300,top=150, toolbar=0, menubar=0, scrollbar=0,resizable=1');
}

function sfoglia_catalogo_online(){
	window.open('catalogo-bomboniere-solidali/index.html','onlinecatalog','toolbar=0, menubar=0, scrollbar=0,resizable=1');
	
}
