var markerHTML = [];
var stringArgs;
var label = [];
//var icon = new Array();
var icon1 = new Array();
var icon2 = new Array();
var icon3 = new Array();
var icon4 = new Array();
var icon5 = new Array();
var icon6 = new Array();
var icon7 = new Array();
var icon8 = new Array();

var newMarker = [];
var marker = [];
var gmarkers = [];

$(document).ready(function(){
						   
	$('.hiddenInfo').hide();
	
    $(".toggler").click(function(){								
		var newsItem = $(this).parent();
		newsItem.find('.hiddenInfo').slideToggle();					 
	});
	
	$('.rowNews').find('img').css('cursor','pointer').attr('title','Click for enlargement');
	
	$('.rowNews').find('img').click(function(){
		//alert(this.src);
		var popupSRC = this.src;
		//alert(popupSRC);
		var imgPath = popupSRC.match(/\.\.\/photos\/([\-\d\s\w\%]*)+\.+([\-\d\s\w\%]*)/);
		
		//alert(imgPath[0]);
		
		var newDiv = $('<div></div>');
		
		newDiv.attr('id','popupImg');
		newDiv.attr('class','popupImg');
		newDiv.css('cursor','pointer');
		newDiv.attr('title','Click to close');
		
		$(this).parent().append(newDiv);
		
		//$(".popupImg").hide();
		$(".popupImg").html('<img src="phpThumb/phpThumb.php?src='+imgPath[0]+'&w=400&q=90" onclick="$(function(){$(\'#popupImg\').remove();});"/>');
		//$(".popupImg").show();
		
	});
	
	$('.teamImg').css('cursor','pointer').attr('title','Click for enlargement');
	
	$('.teamImg').click(function(){
		//alert(this.src);
		var popupSRC = this.src;
		//alert(popupSRC);
		var imgPath = popupSRC.match(/\.\.\/photos\/([\-\d\s\w\%]*)+\.+([\-\d\s\w\%]*)/);
		
		//alert(imgPath[0]);
		
		var newDiv = $('<div></div>');
		
		newDiv.attr('id','popupImg');
		newDiv.attr('class','popupImg');
		newDiv.css('cursor','pointer');
		newDiv.attr('title','Click to close');
		
		$(this).parent().append(newDiv);
		
		//$(".popupImg").hide();
		$(".popupImg").html('<img src="phpThumb/phpThumb.php?src='+imgPath[0]+'&w=400&q=90" onclick="$(function(){$(\'#popupImg\').remove();});"/>');
		//$(".popupImg").show();
		
	});
	
	
	
	$('#noProperties').css('display','none');

	/*
     * Extend jQuery to include URL get variable parsing
     */
    $.extend({
      getUrlVars: function(){
        var vars = [], hash;
        var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        for(var i = 0; i < hashes.length; i++)
        {
          hash = hashes[i].split('=');
          vars.push(hash[0]);
          vars[hash[0]] = hash[1];
        }
        return vars;
      },
      getUrlVar: function(name){
        return $.getUrlVars()[name];
      }
    });
	
	function isset(chkvar) {
			return (chkvar === undefined);
		}
    //---------------------------------------------------


//args&op=search&propertyTypes=2,1,8,&sizeoption=on&minsize=2000&maxsize=999999999999&propertylocation=4,5,6,1,&subLocations=15,26,1,11,12,25,&uFname=&uSname=&uEmail=&ppolicy=yes

    //Retrieve get variables
   	var op = $.getUrlVar('op');
    if(!isset(op)){op = "search";}
	
	var propertyTypes = $.getUrlVar('propertyTypes');
	var minsize = $.getUrlVar('minsize');
	var maxsize = $.getUrlVar('maxsize');
	var propertylocations = $.getUrlVar('propertylocation');
	var subLocations = $.getUrlVar('subLocations');
	
	
	
	//Set criteria in search form on map from URL vars
	function setVars(){
		
		$(document).find('.ptype').each(function(){
			if(propertyTypes){								 
				var propertyType = propertyTypes.split(',');
				for(i=0; i<=propertyType.length; i++){
					
					if($(this).val()==propertyType[i]) {
					  $(this).checked = true;
					}
					
				}
			}
		});
		
		
		$(document).find('.propertylocation').each(function(){
			if(propertylocations){								 
				var propertylocation = propertylocations.split(',');
				for(i=0; i<=propertylocation.length; i++){
					
					if($(this).val()==propertylocation[i]) {
					  $(this).checked = true;
					}
					
				}
			}
		});
		
		$(document).find('.subLoc').each(function(){
			
			if(subLocations){
				var subLocation = subLocations.split(',');
				for(i=0; i<=subLocation.length; i++){
					
					if($(this).val()==subLocation[i]) {
					  $(this).checked = true;
					}
					
				}
			}
		});
		
		
	}
	
	/*
	 * Searches database for matching properties
	 */
	 getLocations();
	 
	 function getLocations(type){
		
		//var locationID = $('#propertylocation').val();
		var locationID = '';
		
		if(type=='search'){
			$(document).find('.propertylocationSearch').each(function(){
				if ($(this).is(':checked')) {
				  locationID += $(this).val()+',';
				 // alert($(this).val());
				}
			});
		}else{
			$(document).find('.propertylocation').each(function(){
				if ($(this).is(':checked')) {
				  locationID += $(this).val()+',';
				 // alert($(this).val());
				}
			});
		}
		//alert(locationID);
		//document.location = "get_locations_xml.php?locationID="+locationID+"";
		$.ajax({
			type: "GET",
			url: "get_locations_xml.php",
			dataType: 'xml',
			data: {'locationID' : locationID},
			success: function(xml){
				//alert('success');
				//Find the number of properties return and the number of pages required to display them
				var numLocations = ($(xml).find('location').length);
				//alert(numLocations);
				//-------------------------------------------------------------------------------------
	
				//For each propery returned, output the result panel if the property is on the current page
				var count = 0;
				var sublocNum = 0;
				var locationHTML = '<div class="checkbox"><input type="checkbox" name="All" value="0" checked="checked" id="subLocation0" class="subLoc" onclick="getMarkers()" /><label for="subLocation0">All</label></div>';
				$(xml).find('location').each(function(){
						sublocNum++;
						var subLocationName = $(this).attr('subLocationName');
						var subLocationID = $(this).attr('subLocationID');
						if(type=='search'){
							locationHTML += '<div style="width:240px; height:20px; float:left">';
						}else{
							locationHTML += '<div class="checkbox">';
						}
						locationHTML += '<input type="checkbox" name="subLocation'+sublocNum+'" value="'+subLocationID+'" id="subLocation'+sublocNum+'" class="subLoc" /><label for="subLocation'+sublocNum+'">'+subLocationName+'</label><br>';
						locationHTML += '</div>';
						
				}); 
				//alert(locationHTML);
				$('#sublocations').html(locationHTML);
				$('.subLoc').click( function(){getMarkers(); });
				setVars();
				getMarkers();
			}//end success
		});
	 }
	 
	$('#propertylocation').change( function(){getLocations();});
	$('.propertylocation').click( function(){getLocations();});
	$('.propertylocationSearch').click( function(){getLocations('search');});
	
	//Get Map Markers
	$('.ptype').click( function(){getMarkers(); });
	$('#minsize').change( function(){getMarkers(); });
	$('.propertylocation').click( function(){getMarkers(); });
	$('#maxsize').change( function(){getMarkers(); });
	
	function getMarkers(){
		//alert('jquery getMarkers');
		$('#noProperties').css('display','none');
		$('#preLoader').css('display','block');
		
		map.clearOverlays();
		
		var latlngbounds = new GLatLngBounds( );
		var latlngs = [];
		i = 0;
		// Create some custom icons

		  // This icon uses the same shape as the default Google marker
		  // So we can use its details for everything except the image
		  function makeIcons(i){
			  
				  icon1[i] = new GIcon();
				  icon1[i].image = "http://www.google.com/mapfiles/marker.png";
				  icon1[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon1[i].iconSize = new GSize(20, 34);
				  icon1[i].shadowSize = new GSize(37, 34);
				  icon1[i].iconAnchor = new GPoint(9, 34);
				  icon1[i].infoWindowAnchor = new GPoint(9, 2);
				  icon1[i].infoShadowAnchor = new GPoint(18, 25);
				  icon1[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon1[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon1[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon1[i].title = "Property Details";
				  
				  icon2[i] = new GIcon();
				  icon2[i].image = "http://www.google.com/mapfiles/marker_brown.png";
				  icon2[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon2[i].iconSize = new GSize(20, 34);
				  icon2[i].shadowSize = new GSize(37, 34);
				  icon2[i].iconAnchor = new GPoint(9, 34);
				  icon2[i].infoWindowAnchor = new GPoint(9, 2);
				  icon2[i].infoShadowAnchor = new GPoint(18, 25);
				  icon2[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon2[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon2[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon2[i].title = "Property Details";
				  
				  icon3[i] = new GIcon();
				  icon3[i].image = "http://www.google.com/mapfiles/marker_green.png";
				  icon3[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon3[i].iconSize = new GSize(20, 34);
				  icon3[i].shadowSize = new GSize(37, 34);
				  icon3[i].iconAnchor = new GPoint(9, 34);
				  icon3[i].infoWindowAnchor = new GPoint(9, 2);
				  icon3[i].infoShadowAnchor = new GPoint(18, 25);
				  icon3[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon3[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon3[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon3[i].title = "Property Details";
				  
				  icon4[i] = new GIcon();
				  icon4[i].image = "http://www.google.com/mapfiles/marker_purple.png";
				  icon4[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon4[i].iconSize = new GSize(20, 34);
				  icon4[i].shadowSize = new GSize(37, 34);
				  icon4[i].iconAnchor = new GPoint(9, 34);
				  icon4[i].infoWindowAnchor = new GPoint(9, 2);
				  icon4[i].infoShadowAnchor = new GPoint(18, 25);
				  icon4[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon4[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon4[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon4[i].title = "Property Details";
				  
				  icon5[i] = new GIcon();
				  icon5[i].image = "http://www.google.com/mapfiles/marker_yellow.png";
				  icon5[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon5[i].iconSize = new GSize(20, 34);
				  icon5[i].shadowSize = new GSize(37, 34);
				  icon5[i].iconAnchor = new GPoint(9, 34);
				  icon5[i].infoWindowAnchor = new GPoint(9, 2);
				  icon5[i].infoShadowAnchor = new GPoint(18, 25);
				  icon5[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon5[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon5[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon5[i].title = "Property Details";
				  
				  icon6[i] = new GIcon();
				  icon6[i].image = "http://www.google.com/mapfiles/marker_orange.png";
				  icon6[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon6[i].iconSize = new GSize(20, 34);
				  icon6[i].shadowSize = new GSize(37, 34);
				  icon6[i].iconAnchor = new GPoint(9, 34);
				  icon6[i].infoWindowAnchor = new GPoint(9, 2);
				  icon6[i].infoShadowAnchor = new GPoint(18, 25);
				  icon6[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon6[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon6[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon6[i].title = "Property Details";
				  
				  icon7[i] = new GIcon();
				  icon7[i].image = "http://www.google.com/mapfiles/marker_grey.png";
				  icon7[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon7[i].iconSize = new GSize(20, 34);
				  icon7[i].shadowSize = new GSize(37, 34);
				  icon7[i].iconAnchor = new GPoint(9, 34);
				  icon7[i].infoWindowAnchor = new GPoint(9, 2);
				  icon7[i].infoShadowAnchor = new GPoint(18, 25);
				  icon7[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon7[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon7[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon7[i].title = "Property Details";
				  
				  icon8[i] = new GIcon();
				  icon8[i].image = "http://www.google.com/mapfiles/marker.png";
				  icon8[i].shadow = "http://www.google.com/mapfiles/shadow50.png";
				  icon8[i].iconSize = new GSize(20, 34);
				  icon8[i].shadowSize = new GSize(37, 34);
				  icon8[i].iconAnchor = new GPoint(9, 34);
				  icon8[i].infoWindowAnchor = new GPoint(9, 2);
				  icon8[i].infoShadowAnchor = new GPoint(18, 25);
				  icon8[i].transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				  icon8[i].printImage = "http://www.google.com/mapfiles/marker.png";
				  icon8[i].mozPrintImage = "http://www.google.com/mapfiles/marker.png";
				  icon8[i].title = "Property Details";

		  }
	
		  // the icon information is passed to this function
		  function createMarker(point,name,html,icontype,i) {
			//alert('creating marker'+i);
		   // marker = new GMarker(point,icons[icontype][i]);
		   marker[i] = new GMarker(point,icontype);
	//		alert('marker');
			GEvent.addListener(marker[i], "click", function() {
			  marker[i].openInfoWindowHtml(html);
			});
	//		alert('click added');
			// save the info we need to use later for the side_bar
			gmarkers[i] = marker;
			gmarkers[i].title = "Property Details";
			
			return marker[i];
		  }
		  
		//alert('load markers');
		// get variables for php xml
	
		var stringArgs = "";
		var subLocations;
		var propertyTypes;

		var pTypes = [];

		var isPtype = false;
		var isSubloc = false;
				
		$(document).find('.ptype').each(function(){
			if ($(this).is(':checked')) {
				if(isPtype==false){
						stringArgs = stringArgs + '&propertyTypes='+$(this).val();
					}else{
						stringArgs = stringArgs + ','+$(this).val();
					}
				isPtype = true;
			}
		});
		
		var minsize = document.getElementById('minsize').value;
		var maxsize = document.getElementById('maxsize').value;
		//var propertylocation = document.getElementById('propertylocation').value;
		var propertylocation = '';
		
		$(document).find('.propertylocation').each(function(){
			if ($(this).is(':checked')) {
			  propertylocation += $(this).val()+',';
			 // alert($(this).val());
			}
		});
		
		var subLocations = '';
		
		$(document).find('.subLoc').each(function(){
			if ($(this).is(':checked')) {
				subLocations += $(this).val()+',';
				//alert($(this).val());
			}
		});
		
		if(minsize){ stringArgs = stringArgs + '&minsize='+minsize; }
		if(maxsize){ stringArgs = stringArgs + '&maxsize='+maxsize; }
		if(propertylocation){ stringArgs = stringArgs + '&propertylocation='+propertylocation; }
		if(subLocations){ stringArgs = stringArgs + '&subLocations='+subLocations; }
		
		//alert("get_properties_xml.php?args"+stringArgs);
		
		//var locationID = $('#propertylocation').val();
		//prompt("","get_properties_xml.php?args"+stringArgs);
		//document.location = "get_properties_xml.php?args"+stringArgs;
		$.ajax({
			type: "GET",
			url: "get_properties_xml.php?args"+stringArgs,
			dataType: 'xml',
			success: function(xml){
				
				//alert('success');
				//Find the number of properties return and the number of pages required to display them
				var numProperties = ($(xml).find('property').length);
				//alert(numProperties);
				//-------------------------------------------------------------------------------------
	
				//For each propery returned, output the result panel if the property is on the current page
				var count = 0;
				var propNum = 0;
				
				$(xml).find('property').each(function(){
						
						var lat = parseFloat($(this).attr('lat'));
           				var lng = parseFloat($(this).attr('lng'));
						var point = new GLatLng(lat,lng);
						latlngs[i] = point;
						var propid = $(this).attr('propid');
						var propertysizeFrom = $(this).attr('propertysizeFrom');
						var propertysizeTo = $(this).attr('propertysizeTo');
						var acreage = $(this).attr('acreage');

						var propertyaddress = $(this).find('propertyaddress').text();
						//alert(propertyaddress);
						var propertyphoto1 = $(this).find('pphoto').text();
						//alert(propertyphoto1);
						var propertytenure = $(this).find('propertytenure').text();
						var tenure = $(this).find('tenure').text();
						
						//alert(propertytenure);
						var propertypdf = $(this).find('propertypdf').text();
						//alert(propertypdf);
						var propertyprice = $(this).find('propertyprice').text();
						//alert(propertyprice);
						var propertyref = $(this).find('propertyref').text();
						//alert(propertyref);
						var icon = $(this).find('icon').text();
			
						markerHTML[i] = '<div style="width:320px; height:140px; overflow:auto; background-color:#FFFFFF; font-family:Verdana, Arial, Helvetica, sans-serif; font-size:12px; color:#000000; padding:5px;" >';
						markerHTML[i] += '<div style="width:280px; float:left; color:red; font-weight:bold; padding:0 0 5px 0;">'+propertyaddress+'</div>';
						markerHTML[i] += '<div style="border:dashed 1px red; padding:5px; float:left; ">';
						markerHTML[i] += '<div style="width:120px; float:left; padding:0 5px 0 0;">';
						if(propertyphoto1==""){}else{
							markerHTML[i] += '<a href="http://www.legatowen.co.uk/property_details.php?id='+propid+'"><img src="http://www.legatowen.co.uk/phpThumb/phpThumb.php?src=../photos/'+propertyphoto1+'&w=120"></a>';
						}
						markerHTML[i] += '</div>';
						markerHTML[i] += '<div style="width:150px; float:left;">';
						if(propertyprice!="" && propertyprice!=0){
								markerHTML[i] += '<span style="color:red; font-weight:bold; padding:10px 0 5px 0; font-size:12px;">&pound;'+propertyprice+' '+tenure+'</span>';
						}
						
						markerHTML[i] += '<span style="color:#0c4268; font-size:11px; line-height:16px;"><br /><br />';
						if(acreage!=""){
							markerHTML[i] += ''+acreage+' Acres<br />';
						}else{
							if(propertysizeFrom!=propertysizeTo){
								markerHTML[i] += ''+propertysizeFrom+' - '+propertysizeTo+' sq/ft<br />';
							}
						}
						markerHTML[i] += ''+propertytenure+'<br /><br />';
						markerHTML[i] += '<a href="http://www.legatowen.co.uk/property_details.php?id='+propid+'">View Full Details &gt;</a><br />';
						if(propertypdf){
							markerHTML[i] += '<a href="http://www.legatowen.co.uk/userfiles/file/'+propertypdf+'">Download Specification &gt;</a><br />';
						}
						markerHTML[i] += '<a href="http://www.legatowen.co.uk/prop_online_enquiry.php?ref='+propertyref+'&type=&det='+propertyaddress+'">Make an Enquiry &gt;</a><br />';
						markerHTML[i] += '</span></div>';
						markerHTML[i] += '</div>';
						markerHTML[i] += '</div>';
						
						//alert(markerHTML[i]);
						
						label[i] = propertyaddress;
						// create the marker
						//alert('\npoint\n'+point+'\nlabel\n'+label+'\nhtml\n'+html[i]+'\nicontype\n'+icontype);
						makeIcons(i);
						
						if(icon=="red"){
							icontype = icon1[i];
						}else if(icon=="brown"){
							icontype = icon2[i];
						}else if(icon=="green"){
							icontype = icon3[i];
						}else if(icon=="purple"){
							icontype = icon4[i];
						}else if(icon=="yellow"){
							icontype = icon5[i];
						}else if(icon=="orange"){
							icontype = icon6[i];
						}else if(icon=="grey"){
							icontype = icon7[i];
						}else{
							icontype = icon8[i];
						}
						
						//alert('set icon'+i);

						if(lat<0.000000000000000 || lng<0.000000000000000 || lat>0.000000000000000 || lng>0.000000000000000 ){
							newMarker[i] = createMarker(point,label[i],markerHTML[i],icontype,i);
							map.addOverlay(newMarker[i]);
							for ( var r = 0; r < latlngs.length; r++ ){
								latlngbounds.extend( latlngs[ r ] );
							}
						}
						
						i++;
				}); 
				//alert(locationHTML);
				if(numProperties==0){
					map.setCenter(  GLatLng(aLat, aLong), zoom );
					$('#noProperties').css('display','block');
					$('#preLoader').css('display','none');
				}else{
					map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );
					$('#noProperties').css('display','none');
					$('#preLoader').css('display','none');
				}
				
				//alert("property_search_list.php?args"+stringArgs);
				$('#viewAsList').attr("href", "property_search_list.php?args&op=search&"+stringArgs);
				$('#viewAsMap').attr("href", "property_search_map.php?args"+stringArgs);
			}//end success
		});
	 }
	
	
	$('#propertylocation').change( function(){
		getLocations();
	});
	
	
	$('#eformSubmit').click(function(){
		
		var minsize = $('#minsize').val();
		var maxsize = $('#maxsize').val();
		
		var ptypes;
		
		$(document).find('.ptype').each(function(){
			if ($(this).is(':checked')) {
				ptypes += ','+$(this).val();
			}
		});
		
		var propertylocation = '';
									 
		$(document).find('.propertylocation').each(function(){
			if ($(this).is(':checked')) {
			  propertylocation += $(this).val()+',';
			}
		});
		
		var subLocations = '';
		
		$(document).find('.subLoc').each(function(){
			if ($(this).is(':checked')) {
			  subLocations += $(this).val()+',';
			}
		});
		
		var uFname = $('#uFname').val();
		var uSname = $('#uSname').val();
		var uEmail = $('#uEmail').val();
		
		var args = 'uFname='+uFname+'&uSname='+uSname+'&uEmail='+uEmail+'&eUpdate=Yes'+'&minsize='+minsize+'&maxsize='+ maxsize+'&ptypes='+ptypes+'&propertylocation='+propertylocation+'&subLocations='+subLocations;
		
		//document.location = "eupdates.php?args="+args;
		
		$.ajax({
			type: "GET",
			url: "eupdates.php?args="+args,
			data: {'uFname' :  $('#uFname').val(), 'uSname' : $('#uSname').val(), 'uEmail' : $('#uEmail').val(), 'eUpdate' : 'Yes', 'minsize' : minsize, 'maxsize' : maxsize, 'ptypes' : ptypes, 'propertylocation' : propertylocation, 'subLocations' : subLocations },
			success: function(xml){
				var status;
				
				$(xml).find('success').each(function(){
					status = $(this).find('status').text();
				});
				
				if(status=='true'){
					$('#eForm').css('display', 'none');
					$('#eSent').css('display', 'block');
				}else{
					alert('Sorry, there was a problem please try again later');
				}
				
			}
		});
	});

});

