var st_swim = false;
var st_sauna = false;
var st_whirlpool = false;
//var postcode_regx = "/[,\b.]$";
var postcode_regx = /^[0-9]{4}\s{0,2}[a-zA-z]{0,2}$/;
var xmlHttp;
var ftFotosTimeout = 10000;

$(document).ready(function(){
	if($.url.segment(0) == "supplier" || $.url.segment(0) == "leverancier"){
		$("div.details-images div.submitaanvraag").css({top:15});
	}
	if( ($.url.attr("host") == "wellnesszoeker.nl") || ($.url.attr("host") == "www.wellnesszoeker.nl") || ($.url.attr("host") == "dev.wellnesszoeker.nl") ){
		Toggle('radio1','Enter Postcode');
		Toggle('radio2','Enter Postcode');
		Toggle('radio3','Enter Postcode');
	}
	if( ($.url.attr("host") == "zwembadzoeker.nl") || ($.url.attr("host") == "www.zwembadzoeker.nl") || ($.url.attr("host") == "dev.zwembadzoeker.nl") ){
		Toggle('radio1','Enter Postcode');
	}
	if( ($.url.attr("host") == "whirlpoolzoeker.nl") || ($.url.attr("host") == "www.whirlpoolzoeker.nl") || ($.url.attr("host") == "dev.whirlpoolzoeker.nl") ){
		Toggle('radio2','Enter Postcode');
	}
	if( ($.url.attr("host") == "sauna-zoeker.nl") || ($.url.attr("host") == "www.sauna-zoeker.nl") || ($.url.attr("host") == "dev.sauna-zoeker.nl") ){
		Toggle('radio3','Enter Postcode');
	}
	
	//$("div.fpfotos .innerwrap").animate({left:-1000},20000);
	$(".fpfotos div.logo").css({opacity:0.85});
	var fpTotos = setTimeout('reloadFPFotos()',3000);
});


function reloadFPFotos(){
	timestamp = fetch_unix_timestamp();
	$(".fpfotos-hidden").load("/home?"+timestamp+" .fpfotos-hidden", function(){
		$(".fpfotos-hidden div.img").each(function(i) {
			if ( $('.fpfotos .innerwrap').find('div.img#'+$(this).attr("id")).length > 0 ) {
				$(this).remove();
			}
		});
		//$(".fpfotos div.logo").css({opacity:0.85});
		$(".fpfotos div.img:eq(0)").idle(3000).animate({opacity:0},250, function(){ $(this).replaceWith( $(".fpfotos-hidden div.img:eq(0)") ).animate({opacity:0.85},250) });
		$(".fpfotos div.img:eq(1)").idle(6000).animate({opacity:0},250, function(){ $(this).replaceWith( $(".fpfotos-hidden div.img:eq(0)") ).animate({opacity:0.85},250) });
		$(".fpfotos div.img:eq(2)").idle(9000).animate({opacity:0},250, function(){ $(this).replaceWith( $(".fpfotos-hidden div.img:eq(0)") ).animate({opacity:0.85},250) });
		$(".fpfotos div.img:eq(3)").idle(12000).animate({opacity:0},250, function(){
			$(this).replaceWith( $(".fpfotos-hidden div.img:eq(0)") ).animate({opacity:0.85},250, function(){
				reloadFPFotos();
			})
		});
		
		/*$.ajaxSetup ({
			// Disable caching of AJAX responses
			cache: false
		});*/
		
		/*for (var i=0; i<$(".fpfotos div.img").length; i=i+1) {
			$(".fpfotos div.img:eq("+i+")").idle(3000+(i*3000)).animate({opacity:0},250, function(){ $(this).replaceWith( $(".fpfotos-hidden div.img:eq("+i+")") ).animate({opacity:0.85},250) });
		}*/
		//alert(textStatus);
		//setTimeout('reloadFPFotos()',ftFotosTimeout);
	});
}
function EnterAction(event,elem) {
	if (event.keyCode==13){
		if (elem == "search") {
			Request('Voer adres, postcode of plaatsnaam in','zoekresultaten');
		}
		/*else {
			document.getElementById(elem).focus();
		}*/
	}
}

function Request(default_text,pagename) {
	if(!Check_Postcode(default_text))
	  return;
	if(!Check_Toggles())
	  return;

		xmlHttp = HTTPObject();
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				if(xmlHttp.responseText != "") {
					pageTracker._trackPageview( pagename+document.getElementById("pcode").value );
					$('#result').html(xmlHttp.responseText);
				}
				//$('#body_content').show();
				//$('#result').show();
			}
		} 
		str = document.getElementById("pcode").value;
		//if(str.search(postcode_regx) != -1) {
		/*if(!postcode_regx.exec(str)) {
			alert('Onjuiste postcode ingevoerd.');
			//document.getElementById("pcode").value = "";
			return;
		}*/
		calcZipcode(document.getElementById("pcode").value, "Nederland", getNearest);
		$('#details').hide();
		$('.description').hide();
}

function Paginate(offsetPaid,offsetTotal,pagename) {
		xmlHttp = HTTPObject();
		xmlHttp.onreadystatechange=function() {
			if(xmlHttp.readyState==4) {
				if(xmlHttp.responseText != "") {
					pageTracker._trackPageview("/"+pagename );
					$('#result').html(xmlHttp.responseText);
				}
			}
		} 

		if($.url.segment(0) == "merk" || $.url.segment(0) == "brand") {
			var req = "/ajax/remote/get_postcode_result/"+$('#lat-result').val()+"/"+$('#lng-result').val()+"/"+st_swim+"/"+st_sauna+"/"+st_whirlpool+"/"+offsetPaid+"/"+offsetTotal+"/"+$.url.segment(1);
		} else {
			var req = "/ajax/remote/get_postcode_result/"+$('#lat-result').val()+"/"+$('#lng-result').val()+"/"+st_swim+"/"+st_sauna+"/"+st_whirlpool+"/"+offsetPaid+"/"+offsetTotal;
		}
		
		
		//alert(req);
		$('#details').hide();
		$('.description').hide();
		xmlHttp.open("GET",req,true);
		xmlHttp.send(null);
}

function HTTPObject()
{
  var xmlHttp;
  try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  
  return xmlHttp;
  
}


function calcZipcode(zip, country, callback){
	geocoder = new GClientGeocoder();
	geocoder.getLatLng(zip+", "+country, callback);
}

function getNearest(response){
	if(response==null){
		alert("Adres/postcode is niet gevonden.");
	}
	else{
		$('#lat-result').val(response.lat());
		$('#lng-result').val(response.lng());
		if($.url.segment(0) == "merk" || $.url.segment(0) == "brand") {
			var req = "/ajax/remote/get_postcode_result/"+response.lat()+"/"+response.lng()+"/"+st_swim+"/"+st_sauna+"/"+st_whirlpool+"/0/0/"+$.url.segment(1);
		} else {
			var req = "/ajax/remote/get_postcode_result/"+response.lat()+"/"+response.lng()+"/"+st_swim+"/"+st_sauna+"/"+st_whirlpool+"/0/0";
		}
		//alert(req);
		xmlHttp.open("GET",req,true);
		xmlHttp.send(null);
	}
}

function getXY(response){
	if(response==null){
		alert("Bad adress");
	}
	else{
		geoAddr['x'] = response.lat(); // X = lat
		geoAddr['y'] = response.lng(); // Y = long
		geoResponse = true;
	}
}

function getResponse(){
	geoResponse = geoResponse;
}

function fetch_unix_timestamp(){
	return parseInt( new Date().getTime().toString().substring(0, 10) )
}

(function(jQuery) {
	jQuery.fn.idle = function(time) {
		var o = $(this);
		o.queue(function() {
			setTimeout(function() {
				o.dequeue();
			}, time);
		});
		return this;
	}
})(jQuery);
