window.onload = setClient;
//window.onresize = setClient;

function setClient()
{	
	var windowHeight = getWindowHeight();
	var windowWidth = getWindowWidth();
	
	init();
	//externalLinks();
	new spPlaceholder('search_form');
	if($('login_form') != null)
	{
		new spPlaceholder('login_form');
	}
	 
	if(current_page == '' || current_page == 'home')
	{
		get_recent_property();
	}
	externalLinks(); 
	
	if($('map_canvas') != null)
	{
		loadScript();
	}
	
	if($('GM_container') != null)
	{
		loadScriptGM();
	}	
}

function externalLinks() 
{
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) 
	{
		var anchor = anchors[i];
		if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
			anchor.target = "_blank";
	}
} 

var my_glider;
function get_recent_property()
{
	new Ajax.Request(httproot+'ajax/homepage_properties/', {
		  method:'get',
		  onSuccess: function(transport)
		  {
		     var json = transport.responseText.evalJSON();
		     
		     box_counter = 0;
		     holder_count = 0;
		     
		     for(i=0; i<json.length; i++)
		     {
		    	 if(box_counter == 2)
		    	 {
		    		 var p_div = document.createElement('div');
		    		 p_div.addClassName('clear_both');
		    		 $(pbox_div).appendChild(p_div);
		    		 
		    		 $('recent_property_content').appendChild(pbox_div);
		    		 
		    		 box_counter = 0;
		    	 }
		    	 
		    	 if(box_counter == 0)
		    	 {
		    		 holder_count++;
		    		 var pbox_div = document.createElement('div');
		    		 Element.extend(pbox_div);
		    		 pbox_div.id = "section_"+holder_count;
		    		 pbox_div.addClassName('section');	
		    		 
		    	 }
		    	 
			     var p_div = document.createElement('div');
			     Element.extend(p_div);
			     rental_state = '';
			     if(json[i].state == 'rented')
			     {
			    	 rental_state = '<div class="img_note red_note">'+json[i].state_desc+'</div>';
			     }
			     if(json[i].state == 'option')
			     {
			    	 rental_state = '<div class="img_note blue_note">'+json[i].state_desc+'</div>';
			     }			     
			     p_div.innerHTML = '<div class="pb_image"><a href="'+json[i].link+'"><img src="'+json[i].image+'" alt="" />'+rental_state+'</a></div><div class="pb_info"><a href="'+json[i].link+'"><div class="pb_address"><strong>'+json[i].living_type+'</strong><br />'+json[i].street+' '+json[i].place+'</a></div><div class="pb_desc">'+json[i].description+'<br />'+json[i].date+'</div><div class="pb_price">'+json[i].price_total+'</div>';
			     p_div.addClassName('property_box');	

			     $(pbox_div).appendChild(p_div);
			     
			     box_counter++;
		     }
		     
    		 var p_div = document.createElement('div');
    		 p_div.addClassName('clear_both');
    		 $(pbox_div).appendChild(p_div);
    		 
		     $('recent_property_content').appendChild(pbox_div);
		    
		     
		     // build the manual controller
		     p_control = "";
		     for(i=0; i<holder_count; i++)
		     {
		    	 //<a href="#section1">Section 1</a> | 
		    	 if(p_control != "") p_control = p_control + " - ";
		    	 if(i==0)
		    	 {
		    		 p_control_class = "scroller_selected";
		    	 }
		    	 else
		    	 {
		    		 p_control_class = "scroller_normal";
		    	 }
		    	 p_control = p_control + "<a class='"+p_control_class+"' id='nav_section_"+(i+1)+"' href='#section_"+(i+1)+"'>"+(i+1)+"</a>";
		     }
		     $('controls').innerHTML = p_control;
		     
		     
		     my_glider = new Glider('recent_container', {autoGlide: true, frequency: 6, duration:2.5});
		     
		   }
		});
}

function submit_search()
{

	if($('search_query').value != "" && $('search_query').value != ph_text)
	{
		document.location.href = httproot + "huuraanbod/search/" + encodeURI($('search_query').value);
		return false;
	}
	
	one_check = false;
	search_string = "";
	if($('living_type_room').checked == true) { search_string = search_string + "_room";  one_check = true }
	if($('living_type_studio').checked == true) { search_string = search_string + "_studio";  one_check = true }
	if($('living_type_appartment').checked == true) { search_string = search_string + "_appartment";  one_check = true }
	if($('living_type_house').checked == true) { search_string = search_string + "_house";  one_check = true }
	if($('living_type_luxe').checked == true) { search_string = search_string + "_luxe";  one_check = true }
	if($('living_type_other').checked == true) { search_string = search_string + "_other";  one_check = true }
	
	if(!one_check) 
	{
		search_string = "room_studio_appartment_house_luxe_other";
	}
	else
	{
		search_string = search_string.substring(1);
	}

	search_string = "type/"+search_string;
	if($('price_from').value != '0') search_string = search_string + "/from/"+$('price_from').value;
	if($('price_max').value != '') search_string = search_string + "/to/"+$('price_max').value;
	
	document.location.href = httproot + "huuraanbod/" + search_string;
	
	
	return false;
}

function init()
{
	init_slideshow(0);
}



var slideshow_delay = 5500;
var slideshow_img = new Array('header_');
var img_prefix = httproot + "upload/";



var current_index = -1;
var current_layer = 0;
var current_slide;
var next_slide;

var current_index = -1;
var current_layer = 0;
var current_slide;
var next_slide;

function init_slideshow(start_idx)
{
	if(current_index == -1)
	{
		current_index = start_idx;
		current_slide = new Array(new Image(), new Image(), new Image());
		next_slide = new Array(new Image(), new Image(), new Image());
		
	    if(slideshow.length == current_index+1) next_index = 0;
	    else next_index = current_index+1;
	    
		for(i=0; i<slideshow_img.length; i++)
		{
			$(slideshow_img[i] + current_layer).src = img_prefix + slideshow[current_index][i];
			$(slideshow_img[i] + current_layer).style.display = "";
			next_slide[i].src = img_prefix + slideshow[next_index][i];
		}
		
		setTimeout(switch_slides(), slideshow_delay);
	}
}
                        
function switch_slides() 
{
    return (function() 
    {
		for(i=0; i<slideshow_img.length; i++)
		{
			$(slideshow_img[i] + current_layer).style.zIndex = 1;
		    Effect.Fade(slideshow_img[i]+current_layer, {duration: 2.0});
		}

		if(slideshow.length == current_index+1) current_index = 0;
		else current_index = current_index+1;
     
		if(current_layer == 0) current_layer = 1; else current_layer = 0;

		for(i=0; i<slideshow_img.length; i++)
		{
			$(slideshow_img[i] + current_layer).src = next_slide[i].src;
			$(slideshow_img[i] + current_layer).style.zIndex = 0;
			$(slideshow_img[i] + current_layer).style.display = "";
		}	
	
	    if(slideshow.length == current_index+1) next_index = 0;
	    else next_index = current_index+1;
		
		next_slide = new Array(new Image(), new Image(), new Image());
		for(i=0; i<slideshow_img.length; i++)
		{
			next_slide[i].src = img_prefix + slideshow[next_index][i];
		}
		
		setTimeout(switch_slides(), slideshow_delay + 650);
   	})
 }




function initializeGM() {
	var latlng = $('GM_container').innerHTML.split(';');
	$('GM_container').style.display = "block";

	  var myLatlng = new google.maps.LatLng(latlng[0], latlng[1]);
	  var myOptions = {
	    zoom: 15,
	    center: myLatlng,
	    mapTypeId: google.maps.MapTypeId.ROADMAP
	  }
	  var map = new google.maps.Map(document.getElementById("GM_container"), myOptions);
	  var marker = new google.maps.Marker({
	      position: myLatlng
	  });		
	  
		google.maps.event.addListener(marker, 'click', function() {
			  infowindow.open(map,marker);
			});	
    	marker_a[i] = new google.maps.Marker({
    		position: new google.maps.LatLng(latlng[0], latlng[1]),
    		icon: httproot + "lib/website_impl/area/website_impl_area/img/marker_red.png"
   	  	});	
    	marker_a[i].setMap(map);
	
}

var current_info_window_open = -1;
	var marker_a = new Array();
  	var infowindow_a = new Array();
  	var infowindow;
  	
function initialize() {
	  latlng = housing_data.location;

	  // load relevant markers
	  	 infowindow = new google.maps.InfoWindow({
		    content: ""
		});	  
	  
	  if(housing_data.type == 'list')
	  {
		  var myLatlng = new google.maps.LatLng(latlng[0], latlng[1]);
		  var myOptions = {
		    zoom: 14,
		    center: myLatlng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  }
		  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		  latlng[0] = 1000;
		  latlng[1] = 1000;
		  
		  if(search_display == 'list')
		  {
			  setTimeout(function() { switch_list() }, 1000);
		  }
	  }
	  else
	  {
		  var myLatlng = new google.maps.LatLng(latlng[0], latlng[1]);
		  var myOptions = {
		    zoom: 15,
		    center: myLatlng,
		    mapTypeId: google.maps.MapTypeId.ROADMAP
		  }
		  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
		  var marker = new google.maps.Marker({
		      position: myLatlng
		  });		
		  
			google.maps.event.addListener(marker, 'click', function() {
				  infowindow.open(map,marker);
				});		  
	  }
			
	   
	  

	

	  
	  new Ajax.Request(httproot+'ajax/gmap_location/', {
		  method:'get',
		  onSuccess: function(transport)
		  {

		    var json = transport.responseText.evalJSON();
		    for(i=0; i<json.length; i++)
		    {
		    	if(json[i].location[0] != latlng[0] && json[i].location[1] != latlng[1])
		    	{
			    	marker_a[i] = new google.maps.Marker({
			    		position: new google.maps.LatLng(json[i].location[0], json[i].location[1]),
			    		icon: httproot + "lib/website_impl/area/website_impl_area/img/marker_red.png"
			   	  	});
			    	marker_a[i].setMap(map);
			    	marker_a[i].set("id", i);
			    	marker_a[i].infowindow = "<div class='iw_photo'><img src='"+json[i].picture+"' width='100px'></div><div class='iw_content'><strong>"+json[i].title+"</strong><br />"+json[i].description+"<br />"+json[i].price+"<div class='iw_link'><a href='"+json[i].link+"'>"+json[i].open_link+"</a></div></div>"
			    	
			    	google.maps.event.addListener(marker_a[i], 'click', function() {
			    			infowindow.setContent(this.infowindow);
			    		  infowindow.open(map,this);
			    		});

		    	}
		    	else
		    	{
			    	marker_a[i] = new google.maps.Marker({
			    		position: new google.maps.LatLng(json[i].location[0], json[i].location[1]),
			    		icon: httproot + "lib/website_impl/area/website_impl_area/img/marker_green.png"
			   	  	});
			    	marker_a[i].setMap(map);
			    	marker_a[i].set("id", i);
			    	marker_a[i].infowindow = "<div class='iw_photo'><img src='"+json[i].picture+"' width='100px'></div><div class='iw_content'><strong>"+json[i].title+"</strong><br />"+json[i].description+"<br />"+json[i].price+"<div class='iw_link'>"+json[i].current_link+"</div></div>"
			    	
			    	google.maps.event.addListener(marker_a[i], 'click', function() {
			    			infowindow.setContent(this.infowindow);
			    		  infowindow.open(map,this);
			    		});
		    	}
		     }
		  }
	  });
		     	  
	  
	  
	}
	  
	function loadScript() {
	  var script = document.createElement("script");
	  script.type = "text/javascript";
	  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize";
	  document.body.appendChild(script);
	}
	
	  
	function loadScriptGM() {
	  var script = document.createElement("script");
	  script.type = "text/javascript";
	  script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initializeGM";
	  document.body.appendChild(script);
	}	
	
	function switch_list()
	{
		$('search_list').style.display = "block";
		$('search_map').style.display = "none";		
	}
	
	function switch_map()
	{
		$('search_list').style.display = "none";
		$('search_map').style.display = "block";
	}
