var map = null;
var geocoder = null;

function location_map_load(address) {
   if (GBrowserIsCompatible()) {
     map = new GMap2(document.getElementById("map"));
     geocoder = new GClientGeocoder();
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
              alert(address + " not found");
            } else {
							map.addControl(new GSmallZoomControl());
							map.addControl(new GMapTypeControl());
              map.setCenter(point, 15);
              var marker = new GMarker(point);
              map.addOverlay(marker);
//              marker.openInfoWindowHtml('New house');
            }
          }
        );
      }
    }
}
function swapimg(link) {
	document.getElementById('main_img').src=link;
}

function setOpacity(e,opacity){
  var o=e.style;
}


var tim = new Array();
var delay = 300;
var menu_opacity = 100; //ranging from 0 to 100;
var original_bgd = '#052638';
var original_text = '#FFFFFF';
var highlight_bgd = '#1A3F54';
var highlight_text = '#FFFFFF';
function dropDown(id,action,options,links,orig,cntr) {
	if(action=='show') {
		document.getElementById('menu_'+id).style.backgroundColor = highlight_bgd;
		document.getElementById('menu_'+id).style.color = highlight_text;
		if(!document.getElementById(id)) {
			var options = options.split("#");
			var links = links.split("#");
            var newdiv = document.createElement('div');
			newdiv.setAttribute('id', id);
			newdiv.style.position = 'absolute';
			newdiv.style.width = 300;
			newdiv.style.display = 'none';
			var html = '';
			for(var i = 0;i<options.length;i++) {
				html += '<a onMouseOver="dropDown(\''+id+'\',\'clear\',\'\',\'\',\'\',\''+cntr+'\');" onMouseOut="dropDown(\''+id+'\',\'set\',\'\',\'\',\'\',\''+cntr+'\');" class="dropdown" href="'+links[i]+'">'+options[i]+'</a>';
			}
			newdiv.innerHTML = html;
			document.body.appendChild(newdiv);
		}
		var all_fields = document.getElementById('all_fields').value;
		var all_fields = all_fields.split(",");
		for(var i=0;i<all_fields.length;i++) {
			if(all_fields[i]!=id) {
				if(document.getElementById(all_fields[i])) {
					document.getElementById('menu_'+all_fields[i]).style.backgroundColor = original_bgd;
					document.getElementById('menu_'+all_fields[i]).style.color = original_text;
					document.getElementById(all_fields[i]).style.display = 'none';
				}
			}
		}
		var target = document.getElementById(id);
		var top = parseInt(orig.offsetHeight);
		var curleft = curtop = 0;
		if(orig.offsetParent) {
			do {
				curleft += orig.offsetLeft;
				curtop += orig.offsetTop;
			} while (orig = orig.offsetParent);
		}
		var top = top + curtop;
		var top = top+'px';
		var left = curleft;
		var left = left+'px';
        target.style.top = top;
        target.style.left = left;
        target.style.display = 'block';
		target.style.opacity=(menu_opacity/100); //Opera
		target.style.MozOpacity=(menu_opacity/100); //Mozilla+Firefox
		target.style.KhtmlOpacity=(menu_opacity/100); //Konqueror
		target.style.filter="alpha(opacity="+menu_opacity+")"; //IE
		window.clearTimeout(tim[cntr]);
	}
	if(action=='clear') {
		window.clearTimeout(tim[cntr]);
	}
	if(action=='set') {
		tim[cntr] = window.setTimeout("dropDown('"+id+"','hide','','','','"+cntr+"');", delay);
	}
	if(action=='hide') {
		var target = document.getElementById(id);
	    target.style.display = 'none';
		document.getElementById('menu_'+id).style.backgroundColor = original_bgd;
		document.getElementById('menu_'+id).style.color = original_text;
	}
}

function load_suburbs(state) {
    agent.call("includes/ajax.php","load_suburbs","reload_suburbs",state);
}
function reload_suburbs(obj) {
	var suburbs = document.getElementById('suburb');
	suburbs.options.length = 0;
	for(var t=0;t<obj.length;t++) {
		var optn = document.createElement("option");
		optn.text = obj[t];
		if(obj[t]=='Suburb') optn.value = '';
		else optn.value = obj[t];
		suburbs.options.add(optn);
	}
}
function loadAgentProfile(agentID) {
	var obj = document.getElementById('gallery_content');
	obj.style.width = '520px';
	obj.style.height = '440px';
	var img = document.getElementById('gallery_image');
	img.style.width = '500px';
	img.style.height = '400px';
	var close = document.getElementById('gallery_close');
	close.style.width = '500px';
	var div_width = parseInt(obj.style.width);
	var div_height = parseInt(obj.style.height);
	var page_width = parseInt(document.body.clientWidth);
	var page_height = parseInt(document.body.clientHeight);

	var scroll = document.body.scrollTop;
	var leftOff = Math.round((page_width - div_width)/2);
	var topOff = Math.round((page_height - div_height)/2) + scroll;
	document.getElementById('gallery_container').style.top=scroll;

	obj.style.top=topOff+'px';
	obj.style.left=leftOff+'px';

	agent.call("includes/ajax.php","loadAgentProfile","showAgentProfile",agentID);
}
function showAgentProfile(obj) {
	var scroll = document.body.scrollTop;
	document.body.style.overflow='hidden';
	document.getElementById('gallery_image').innerHTML=obj;
   	Effect.Fade('searchform','1.0');
	document.body.scrollTop = scroll;
   	Effect.Appear('gallery_container','0.8');
	Effect.Appear('gallery_content','1.0');
	document.getElementById('slideshowbuttons').style.display = 'none';
}

function loadGallery() {
	var id = document.getElementById('id').value;
	var type = document.getElementById('slideshow').value;

	var obj = document.getElementById('gallery_content');
	var div_width = parseInt(obj.style.width);
	var div_height = parseInt(obj.style.height);
	var page_width = parseInt(document.body.clientWidth);
	var page_height = parseInt(document.body.clientHeight);

	var scroll = document.body.scrollTop;
	var leftOff = Math.round((page_width - div_width)/2);
	var topOff = Math.round((page_height - div_height)/2) + scroll;
	document.getElementById('gallery_container').style.top=scroll;

	obj.style.top=topOff+'px';
	obj.style.left=leftOff+'px';

	agent.call("includes/ajax.php","loadGallery","showGallery",type,id);
}
function showGallery(obj) {
	var scroll = document.body.scrollTop;
	document.body.style.overflow='hidden';
	document.getElementById('gallery_image').innerHTML=obj;
   	Effect.Fade('searchform','1.0');
	document.body.scrollTop = scroll;
   	Effect.Appear('gallery_container','0.8');
	Effect.Appear('gallery_content','1.0');
	document.getElementById('slideshowbuttons').style.display = 'block';
	waitGallery();
}
function closeGallery() {
	document.body.style.overflow='auto';
   	Effect.Appear('searchform','1.0');
   	Effect.Fade('gallery_container','0.8');
	Effect.Fade('gallery_content','1.0');
	document.getElementById('gallery_image').innerHTML='';
}

function nextGallery() {
	if(document.getElementById('currentID')) {
		if(document.getElementById('runSlide').value=='Y') {
			var current = document.getElementById('currentID').value;
			current = parseInt(current.substr(3));
			var total = document.getElementById('totalImg').value;
			var next = current + 1;
			if(next>total) {
				next = 1;
			}
			var imgID = 'img'+next;
			document.getElementById('currentID').value = imgID;
			document.getElementById('main_img_gallery').src = document.getElementById(imgID).value;
			waitGallery();
		}
	}
}

function waitGallery() {
	setTimeout("nextGallery();",4000);
}

function stopStartSlide(action,newID) {
	if(action=='pause') {
        if(newID!='') document.getElementById('currentID').value = 'img'+newID;
		document.getElementById('runSlide').value = 'N';
		document.getElementById('pauseBtn').src = 'images/pause_active.gif';
		document.getElementById('playBtn').src = 'images/play.gif';
	}
	if(action=='play') {
		document.getElementById('runSlide').value = 'Y';
		waitGallery();
		document.getElementById('pauseBtn').src = 'images/pause.gif';
		document.getElementById('playBtn').src = 'images/play_active.gif';
	}
}

function scroller(direction) {
	var runscroll = document.getElementById('runscroll').value;
	var images_content = document.getElementById('images_content');
	var finish = parseInt(images_content.offsetWidth);
	finish = finish - 572;
	finish = 0 - finish;
    var speed = 200;
	var wait = 1000 / speed;
	var current = parseInt(images_content.style.left);
	if(runscroll=='Y')
		{
		if(current>=finish&&direction=='right') setTimeout("moveRight()",wait);
		if(current<0&&direction=='left') setTimeout("moveLeft()",wait);
		}
}
function moveRight() {
	var images_content = document.getElementById('images_content');
	var current = parseInt(images_content.style.left);
    images_content.style.left = current - 1+'px';
    scroller('right');
}
function moveLeft() {
	var images_content = document.getElementById('images_content');
	var current = parseInt(images_content.style.left);
    images_content.style.left = current + 1+'px';
    scroller('left');
}

function clearSlashes(text) {
    var newText = text.replace("\'","'");
    newText = newText.replace("\\'","'");
    newText = newText.replace('\"','"');
    newText = newText.replace('\\"','"');
	return newText;
}

function getNewSlideshow(type) {
	var code = '';
	if(type=='residential') code = 'SH';
	if(type=='rural') code = 'SR';
  document.getElementById(type+'_link').value = '#';
	if(document.getElementById(type+'_st')) {
		var st = document.getElementById(type+'_st').value;
		agent.call("includes/ajax.php","getNewSlideshow","loadNewSlideshow",code,st);
	}
}
function loadNewSlideshow(obj) {
	var type = obj[0]
	document.getElementById(type+'_img').src = obj[1]
	document.getElementById(type+'_headline').innerHTML = clearSlashes(obj[2]);
	document.getElementById(type+'_desc').innerHTML = clearSlashes(obj[3]);
	document.getElementById(type+'_link').innerHTML = clearSlashes(obj[4]);
	document.getElementById(type+'_st').value = obj[5];
  document.getElementById(type+'_click_link').value = obj[6];
//	document.getElementById(type+'_table').setAttribute("onclick",obj[6]);
	SlideshowAppear(type);
}
function load_feature(type) {
  var ft_link = document.getElementById(type+'_click_link').value;
	location.href = ft_link;
}
function SlideshowAppear(type) {
   	Effect.Appear(type+'_img','1.0');
   	Effect.Appear(type+'_headline','1.0');
   	Effect.Appear(type+'_desc','1.0');
   	Effect.Appear(type+'_link','1.0');
	if(type=='residential') setTimeout("SlideshowFade('residential')",10000);
	if(type=='rural') {
		if(document.getElementById('delay').value=='N') {
			document.getElementById('delay').value='Y';
			setTimeout("SlideshowFade('rural')",15000);
		} else {
			setTimeout("SlideshowFade('rural')",10000);
		}
	}
}
function SlideshowFade(type) {
   	Effect.Fade(type+'_img','1.0');
   	Effect.Fade(type+'_headline','1.0');
   	Effect.Fade(type+'_desc','1.0');
   	Effect.Fade(type+'_link','1.0');
	if(type=='residential') setTimeout("getNewSlideshow('residential')",700);
	if(type=='rural') setTimeout("getNewSlideshow('rural')",700);
}
