$(function(){
		   
	var w = $("html").width();
        if(w < 1010 && w > 850){
            $("body").css({overflowX : "hidden"});
        }
		
	/*$(".theme").live("click", function(){
    	$("<a href="+$(this).attr("href")+" />").colorbox({open:true, maxWidth: 800,maxHeight: 600, resize: true, slideshow: true, slideshowAuto: true, next: 'Successiva'});
		return false;
	});*/
	
	/*$("#mycarousel li a").live("click", function(){
	  $(this).colorbox({href:this.src, maxWidth: 800,maxHeight: 600, resize: true, slideshow: true,slideshowAuto: true, next: 'Successiva',	rel:'lightbox'});
	  return false;
	})*/ 	
	
	$('#mycarousel').jcarousel({
    	scroll: 1,
		auto: 2,
		visible: thumbs_number,
		wrap: 'circular',
		initCallback: initColorBox,
        itemVisibleInCallback: {onBeforeAnimation: mycarousel_itemVisibleInCallback},
        itemVisibleOutCallback: {onAfterAnimation: mycarousel_itemVisibleOutCallback}
	});
	
	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:  'slow', 
		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();
	showcentermap(map, geocoder, my_latitude, my_longitude); 
	} 
}

function showcentermap(map, geocoder, my_latitude, my_longitude) { 
var oLatLng = new GLatLng(my_longitude, my_latitude);
map.setCenter(oLatLng, my_zoom);
var marker = new GMarker(oLatLng); 
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml("<table width='315' height='100'><tr><td><img style='border:2px solid #ccc;' src='"+base_url+js_thumb+"'/></td></tr><tr><td style='font-weight:bold;'>"+js_entry_title+"</td></tr></table><br /><a target='_blank' href='http://maps.google.com/maps?q="+js_entry_address+"'>"+js_entry_address+"<br />"+js_entry_zip+" - "+js_entry_city+"</a>");
});
//map.addOverlay(marker); 
map.addOverlay();  
}

//]]> 

