$(function(){
		   
	var w = $("html").width();
        if(w < 1010 && w > 850){
            $("body").css({overflowX : "hidden"});
        }	
		
	$('#carosello-home').bxSlider({
            displaySlideQty: 3,
            moveSideQty: 1,
			auto: true,
			controls:false,
			pager: false,
			speed: 1500,
			pause: 6000
        });
	$('#contenitore-tmb').bxSlider({
            displaySlideQty: 5,
            moveSideQty: 2,
			auto: true,
			controls:true,
			pager: false,
			prevText: '',
			nextText: ''
        });

	$("a[rel='lightbox']").colorbox({open:false, maxWidth: 800,maxHeight: 600, resize: true, slideshow: true, slideshowAuto: true, next: 'Successiva', previous: 'Precedente'});

	
	var current_id = $("select#select_categories").val();
	if(current_id)
	{
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: base_url + "api/get_typologies",
		   data: "category_id="+current_id+"",
		   success: function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
				$("select#select_typologies").html(options);
				$('select#select_typologies option:first').attr('selected', 'selected');
		   }
		});
	}
	
	
	$("select#select_categories").change(function(){
		$.ajax({
		   type: "POST",
		   dataType: "json",
		   url: base_url + "api/get_typologies",
		   data: "category_id="+$(this).val()+"",
		   success: function(j){
				var options = '';
				for (var i = 0; i < j.length; i++) {
					options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
				}
				$("select#select_typologies").html(options);
				$('select#select_typologies option:first').attr('selected', 'selected');
		   }
		});
	})

	$("select#form_city").change(function(){
		$('select#select_subtypologies option:first').attr('selected', 'selected');
	})

	$('#cycle').cycle({ 
		fx:     'fade', 
		speed:  9000, 
		timeout: 7000, 
		next:   '#next', 
		prev:   '#prev',
		after:   onAfter
	});
});

function initColorBox(){
	$(".theme").live("click", function(){
    	$("a[rel='lightbox']").colorbox({open:true, maxWidth: 800,maxHeight: 600, resize: true, slideshow: true, slideshowAuto: true, next: 'Successiva', previous: 'Precedente'});
		return false;
	});
} 

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
function mycarousel_getItemHTML(item)
{
	return '<a class="theme" href="' + item.linkurl +'" rel="lightbox"><img src="' + item.url + '" width="130" height="90" alt="' + item.title + '" /></a>';
};


function onAfter() { 
    //$('.text div.' + this.id).prev(".payoff").fadeOut('fast');
	var text = $('.text div.' + this.id).text();
	$('.text div#text').html(text);
}

function changeInputTextColor (oObj, sText, bOnFocus, sColor)
{
	if (!oObj)
		return(false);



	sbHPOnInputFocus(oObj, bOnFocus);

	if(oObj.value=='' || oObj.value==sText)
	{
		if (bOnFocus)
			oObj.value="";
		else	oObj.value=sText;
	}
	else	sColor = oObj.style.color;

	oObj.style.color=sColor;


	return (true);
}

function sbHPOnInputFocus(oInput, bOnFocus) {
	if (!oInput)
		return(false);

	if (bOnFocus)
		oInput.style.backgroundColor = '#fff';
	else	oInput.style.backgroundColor = '';
}

var gvaMiniMaps = new Array();
var gvaMiniMapsTI = new Array();
var gvaMiniMapsLL = new Array();
var label_title;
var tab_2_title;
function isdefined(variable)
{
    return (typeof(window[variable]) == "undefined") ? false : true;
}

function googleMap(nPos, nLat, nLon, nAcc, myZoom)
{
	if(label_title != undefined)
	{
		var tab_contatti = '<h4>'+label_phone+'</h4><p>'+value_phone+'</p><h4>'+label_fax+'</h3><p>'+value_fax+'</p><h4>'+label_email+'</h4><a href="mailto:'+value_email+'">'+value_email+'</a><h4>'+label_address+'</h4><p>'+value_address+'</p>';
	}
	else
	{
		//var image = (js_first_photo == "") ? '' : '<img src="'+js_first_photo+'" alt="'+js_entry_title+'" />';
		var tab_contatti = '<h4>'+js_label_rif+js_entry_cod+'</h4><p><strong>'+js_entry_city+'</strong></p><p>'+js_entry_zip+' '+js_entry_address+' '+js_entry_district+' '+js_entry_region+'</p>';
		var tab_2_title  = '';
	}
	
	var oGMap = new GMap2(document.getElementById("google_map_" + nPos));
	if (!oGMap)
		return(false);

	gvaMiniMaps[nPos] = oGMap;
	gvaMiniMapsLL[nPos] = new Array(nLat, nLon);

	oGMap.addControl(new GSmallMapControl());
	oGMap.addControl(new GMapTypeControl());

	var oLatLng = new GLatLng(nLat, nLon);
	gvaMiniMapsLL[nPos] = oLatLng;

	// Creiamo le varie tabs, possono anche contenere codice HTML!
	var schede = [
	new GInfoWindowTab(tab_2_title, tab_contatti)
	];				
	
	var marker = new GMarker(oLatLng);
	
	GEvent.addListener(marker, "click", apri_schede);
	
	var nZoom = myZoom;

	nZoom = myZoom;
	
	
	oGMap.setCenter(oLatLng, nZoom);
	oGMap.savePosition();
	oGMap.addOverlay(marker);

	function apri_schede() {
		marker.openInfoWindowTabsHtml(schede);
	}

}

//<![CDATA[
function load() { 
	if (GBrowserIsCompatible()) { 
		var mapOptions = {
		googleBarOptions : { style : "new" }
		}
		var map = new GMap2(document.getElementById("map"), mapOptions); 
		var geocoder = new GClientGeocoder(); 
		map.setUIToDefault();
		map.enableGoogleBar();
		var oLatLng = new GLatLng(my_longitude, my_latitude);
		map.setCenter(oLatLng, my_zoom);
		if (my_view_marker == "1")
		{
			showcentermap(map, oLatLng, geocoder);
		}
	} 
} // load

function showcentermap(map, oLatLng, geocoder) { 
	map.setCenter(oLatLng, my_zoom);
	var marker = new GMarker(oLatLng); 
	GEvent.addListener(marker, "click", function() {
		if (my_view_address == "1")
		{
			marker.openInfoWindowHtml("<table width='300' cellpadding='0' cellspacing='0'><tr><td style='font-weight:bold;'>"+js_entry_title+"</td></tr><tr><td style='font-weight:normal;color:#c00;'><a href='"+currentUrl+"#contattaci'>Contattaci subito</a></td></tr></table><br />"+js_entry_address+" - "+js_entry_zip+" - "+js_entry_city);
		}
	});
	map.addOverlay(marker);
//map.addOverlay();
}

//]]> 


