// JavaScript Document

/* MENU & SUBMENU */
var MMint = 0;
$(document).ready(function() {
	$("#navigation_pri li").each(function(){
		var c = $("#navigation_pri li").index(this);
		$(this).mouseover(function(){
			$(this).stop();
			openSM(c);
		})
		$(this).mouseout(function(){
			MMint = setTimeout(closeSM, 500);
		})
	});
	$("#navigation_sec").mouseover(function(){
		clearTimeout(MMint);
	})
	$("#navigation_sec").mouseout(function(){
		MMint = setTimeout(closeSM, 500);
	})
	$("#navigation_sec ul").each(function(){
									  $(this).hide();
									  });
});

	
function openSM(c){
	clearTimeout(MMint);
	$("#navigation_pri a").removeClass("cur");
	$("#navigation_pri li").each(function(){
		var ix = $("#navigation_pri li").index(this);
		if(ix == c){
			$("#navigation_sec ul:eq("+c+")").stop().height('auto').slideDown("fast");
			$("#navigation_pri a:eq("+c+")").addClass("cur");
		}else{
			$("#navigation_sec ul:eq("+ix+")").stop().height('auto').slideUp("fast");
		}
	})
}

function closeSM(c){
	clearTimeout(MMint);
	$("#navigation_pri a").removeClass("cur");
	$("#navigation_sec ul").each(function(){
									  $(this).slideUp("fast");
									  });
}

// LANGUAGES 
$(document).ready(function() {
	$("#descr_language li").each(function(){
		var c = $("#descr_language li").index(this);
		$(this).click(function(){
			openLANG(c);
		})
	});
});

	
function openLANG(c){
	$("#descr_language li").removeClass("cur");
	$("#description div").hide();
	$("#descr_language li").each(function(){
		var ix = $("#descr_language li").index(this);
		if(ix == c){
			$("#description div:eq("+c+")").show();
			$("#descr_language li:eq("+c+")").addClass("cur");
		}
	})
}



/* GALLERY */
function myGallery(div, w, h){
	var curImg = 0;
	var galleryTo = 0;
	var DIV = "#"+div;
	var NAV = "#"+div+" .gal_navigation";
	var totGal = $(DIV+" ul li").length;
	
	$(document).ready(function() {  
		$(DIV).append('<div class="gal_navigation"><span class="numeric"></span> <div class="prev"><span></span></div> <div class="next"><span></span></div> | <span class="playpause">Pause</span></div>');
		$(DIV+" .playpause").html("Pause");
		$(DIV).css({
				   position: 'relative',
				   display: 'block',
				   width: w+"px",
				   height: h+17+"px"
				   });
		$(NAV).css({
				   display: 'block',
				   width: w+"px"
				   });
		$(DIV+" ul").css({
				   overflow: 'hidden',
				   display: 'block',
				   width: w+"px",
				   height: h+"px",
				   backgroundColor: '#000000',
				   marginBottom: 5+"px"
				   });
		$(DIV+" li").css({
						 position: 'absolute',
						 overflow: 'hidden',
						 height: h+"px",
						 top: 0,
						 left: 0,
						 margin: 0
						 })
		$(NAV+" .prev").css({
						   position: 'absolute',
						   top: 0,
						   left: 0,
						   height: h+'px',
						   width: Math.floor(w/2)+'px',
						   opacity: '0.5'
						 })
		$(NAV+" .next").css({
						   position: 'absolute',
						   top: 0,
						   right: 0,
						   height: h+'px',
						   width: Math.floor(w/2)+'px',
						   opacity: '0.5'
						 })
		$(NAV+" .prev span").css({
						   position: 'absolute',
						   left: 0+"px",
						   top: Math.floor(h/2)-30+"px",
						   display: 'block',
						   height: 60+'px',
						   width: 36+'px',
						   backgroundImage: 'url('+BASE_URL+'site/shut/images/gallery/prev.png)',
						   backgroundRepeat: 'no-repeat'
						 })
		$(NAV+" .next span").css({
						   position: 'absolute',
						   right: 0+"px",
						   top: Math.floor(h/2)-30+"px",
						   display: 'block',
						   height: 60+'px',
						   width: 36+'px',
						   backgroundImage: 'url('+BASE_URL+'site/shut/images/gallery/next.png)',
						   backgroundRepeat: 'no-repeat'
						 })
		$(NAV+" .prev, "+NAV+" .next").mouseover(function() { $(this).stop().fadeTo('slow', 1) }).mouseout(function() { $(this).stop().fadeTo('slow', 0.5) });
		$(DIV).mouseenter(function() { $(NAV+" .next, "+NAV+" .prev").stop().fadeTo('slow', 1) }).mouseleave(function() { $(NAV+" .next, "+NAV+" .prev").stop().fadeTo('slow', 0) }); 
	
		$(NAV+" .playpause").click(function(){
			var c = $(NAV+" .playpause").html();
			switch(c){
				case "Pause":
				$(NAV+" .playpause").html("Play");
				clearInterval(galleryTo);
				break;
				
				case "Play":
				$(NAV+" .playpause").html("Pause");
				galleryTo = setTimeout(nextImg, 500);
				break;
			}
		})
		$(NAV+" .playpause, "+NAV+" .prev, "+NAV+" .next").css({ cursor: 'pointer'});
		$(NAV+" .prev").click(function(){
			var next = curImg-1;
			if(next < 0) next = totGal-1;
			changeImg(next);
		})
		$(NAV+" .next").click(function(){
			var next = curImg+1;
			if(next == totGal) next = 0;
			changeImg(next);
		})
		$(DIV+" li").each(function(){
			var li = this;
			var ii = $(DIV+" li").index(li);
			$(NAV+" .numeric").append(' <a href="#" num="'+ii+'" class="num">'+(ii+1)+'</a> ');
		});
		$(NAV+" .numeric .num").click(function(){
			var ii = $(this).attr('num');
			curImg = ii;
			changeImg(ii);
			return false;
		})
		//galleryInterval = setInterval(nextImg, 6000);
	
		function changeImg(ix){
			//$(DIV+" li").stop().css({opacity: 1});
			$(DIV+" li").stop().fadeOut();
			$(DIV+" li:eq("+ix+")").stop().css({opacity: 1});
			$(DIV+" li:eq("+ix+")").hide();
			$(DIV+" li:eq("+ix+")").fadeIn();
			$(NAV+" .numeric a").removeAttr('style');
			$(NAV+" .numeric a:eq("+ix+")").css({color: '#000000'});
			var c = $(DIV+" .playpause").html();
			if(c == "Pause"){
				clearInterval(galleryTo);
				galleryTo = setTimeout(nextImg, 5000);
			}
			curImg = ix;
			return false;
		}
		
		
		function nextImg(){
			var ix = parseInt(curImg) + 1;
			if(ix == totGal) ix = 0;
			else if(ix < 0) ix = totGal;
			changeImg(ix);
		}
		
		changeImg(0);
	});
}


/* MAP */

function initialize_map(DIV, w, h, cr, s, lat_i, long_i, zoom, theid, move) {
	$('#'+DIV).css({
			  width: w+'px',
			  height: h+'px'
			  });
	
	// baseIcon
	var markers = [];
	var baseIcon = new GIcon();
	baseIcon.image = "http://www.grupporeali.it/site/reali/css/images/map_icon.png";
	//baseIcon.image = "http://grupporeali.it/site/reali/css/images/map_icon.png";
	baseIcon.iconSize = new GSize(30, 33);
	baseIcon.iconAnchor = new GPoint(15, 33);
	baseIcon.infoWindowAnchor = new GPoint(20, 35);

	
	//our map container and instance of GLatLngBounds
	var mapContainer = $("#"+DIV)[0];
	var bounds = new GLatLngBounds();
	
	//if browser is compatible
	if (GBrowserIsCompatible()) {
		//if div with given id was found
		if(mapContainer) {
			//instance of the GMap2 and some controls
			var map = new GMap2(mapContainer);
			map.addControl(new GLargeMapControl3D);
			map.addControl(new GMapTypeControl);
			map.addControl(new GScaleControl);
			map.setCenter(new GLatLng(lat_i, long_i),zoom);
		}
		
		//AJAX request
		$.get("http://www.grupporeali.it/page/Xml/"+cr+"?"+s,function(data) {
		//$.get("www.google.it"+cr+"?"+s,function(data) {
			var index = 0;
			//we are looking for all marker and looping through them
			$(data).find('marker').each(function(){
				//$("markers").index(this);
				//serializing some data from the request
				var id    = $(this).attr('id');
				var rif    = $(this).attr('rif');
				var lat    = $(this).attr('lat');
				var lng    = $(this).attr('lng');
				var tipo    = $(this).attr('type');
				var descr    = $(this).attr('descr');
				var zona    = $(this).attr('zona');
				var prezzo    = $(this).attr('prezzo');
				var image    = $(this).attr('image');
				var latlng  = new GLatLng(lat,lng);
				
				if(tipo == 'agenzia'){
					var tab_infos = '<div style="width: 250px;"><a href="/page/Cerca/op/view/id/' + id +'" style="color: red;"><img src="'+image+'" style="max-height: 90px;max-width: 220px;" /></a><p style="font-size:12px;"><strong>' + zona + '</strong><br />' + descr + '<br><strong>' + prezzo + '</strong><br><br><a href="/page/Cerca/op/agenzia/id/' + id +'" style="color: red;">Vai alla scheda &gt;&gt;</a></p>';
				}else{
					if(image) var tab_infos = '<div style="width: 100px; height: 90px; float: left; border: solid 1px #666666; margin-right: 10px; overflow: hidden; text-align: center;"><a href="/page/Cerca/op/view/id/' + id +'" style="color: red;"><img src="'+image+'" style="height: 90px;" /></a></div><p style="width: 140px; font-size:12px; float: left;"><strong>' + rif + ' ' + zona + '</strong><br><em style="color: #333333">' + tipo + '</em><br /><br />' + descr + '<br><strong>' + prezzo + '</strong><br><br><a href="/page/Cerca/op/view/id/' + id +'" style="color: red;">Vai alla scheda &gt;&gt;</a></p>';
					else var tab_infos = '<div style="width: 250px;"><h3>' + rif + ' ' + zona + '</h3><em>' + tipo + '</em><br><br>' + descr + '<br ><strong>' + prezzo + '</strong><br><br><a href="/page/Cerca/op/view/id/' + id +'" style="color: red;">Vai alla scheda &gt;&gt;</a></p></div>';
				}
				
				
				var tab_image = false;
				
				if(id == theid){
					var spotIcon = new GIcon(G_DEFAULT_ICON);
					spotIcon.image = "http://www.grupporeali.it/site/reali/css/images/"+tipo+".png";
					//spotIcon.image = "http://grupporeali.it/site/reali/css/images/"+tipo+".png";
					spotIcon.iconSize = new GSize(48, 48);
					spotIcon.iconAnchor = new GPoint(24,48);
					spotIcon.infoWindowAnchor = new GPoint(30, 35);
					var icon = spotIcon;
				}else
					var icon = baseIcon;
					
				//new marker
				if((lat!=0) && (lng!=0)){
					index++;
					var marker = new GMarker(latlng, icon);
					// Set mouse Actions
					if(id != theid){
						GEvent.addListener(marker, "click", function() {
							marker.openInfoWindowHtml(tab_infos);
						});
					}
					markers.push(marker);
					//extending the boundaries
					bounds.extend(latlng);
				}
								
			});
			
			
			var mcOptions = {gridSize: 30, maxZoom: 14};
			var markerCluster = new MarkerClusterer(map, markers, mcOptions);
			
			//setting the zoom level and the center
			if((s!='')||(move==1)){
				map.setZoom(map.getBoundsZoomLevel(bounds));
				map.setCenter(bounds.getCenter());
			}
		});
	}
}

$(document).ready(function() {  
	$(".lightBox a").lightBox();
	$(".trigger").click(function(){
		var target = $(this).attr('rel'); 
		$(this).parent().slideUp();
		$("#"+target).slideDown();
		$("#map").animate({height: '421px'})
	});
	
	$(".cntct_trigger").click(function(){
		$("#cntct_form").slideDown();
	})
	
	// FOCUS
	$("#cntct_form input").focus(function(){
										  $(this).removeClass('error');
										  });
	$("#cntct input[name='email']").focus(function(){
												   if($(this).val() == 'e-mail')
														$(this).val('');
												   });
	$("#cntct input[name='nome']").focus(function(){
												   if($(this).val() == 'nome e congome')
														$(this).val('');
												   });
	$("#cntct input[name='telefono']").focus(function(){
												   if($(this).val() == 'telefono/cellulare')
														$(this).val('');
												   });
	// FOCUS OUT
	/*$("#cntct input[name='email']").focusout(function(){
												   if($(this).val() == '')
														$(this).val('e-mail');
												   });
	$("#cntct input[name='nome']").focusout(function(){
												   if($(this).val() == '')
														$(this).val('nome e congome');
												   });
	$("#cntct input[name='telefono']").focusout(function(){
												   if($(this).val() == '')
														$(this).val('telefono/cellulare');
												   });*/


	$("#cntct_send").click(function(){
		var error = 0;
		var values = $("#cntct_f").serializeArray();
		
		
		if(!error){
			var request = [{
								name: "cntct_send",
								method: "cntct_send",
								params: values
						}];
					
			$.ajax({
				type: 'POST',
				cache: false,
				url: "http://www.grupporeali.it/_ajax.php",
				//url: "http://grupporeali.it/_ajax.php",
				processData: false,
				data: $.toJSON(request),
				dataType: 'json',
				beforeSend:function() { 
						$("#cntct .loading").show()
				},
				success: function(json) {
					if(json.cntct_send.result == true){
						$("#cntct_form").html('<div class="success">richiesta di contatto inviata!</div>');
					}else{
						$("#cntct.loading").hide()
						var ids = json.cntct_send.message.ids;
						$("#cntct_form .error").removeClass('error');
						for(var i = 0; i<ids.length; i++){	
													$("#cntct_form input[name='"+ids[i]+"']").parent().addClass('error');
													};
					}
					return false;
				},
				error: function(er){
					$("#cntct .loading").hide()
					alert('Impossibile inviare il messaggio, riprovare');
					return false;
				}
			});	
			return false;
		}else{
			 $("#cntct .loading").hide()
		}
		return false;
	});
})





var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


