// JavaScript Document
$(document).ready(function(){
	$('.map').each(function(){
		//performing map init
			var map = new GMap2(document.getElementById("gmap"));
	        map.addControl(new GLargeMapControl());
	        map.addControl(new GMapTypeControl());
			var coordinates=$('#coordinates').attr('title').split(',');
			if(coordinates[0]!=''&&coordinates[1]!=''){
				alert('sssssss');
				//geting point basing on coordinates vs. the geocoding
				var point = new GLatLng(coordinates[1],coordinates[0]);
				map.setCenter(point, 7);
					var icon=new Array();
					icon[0]=new GIcon();
					icon[0].image = "http://www.google.com/mapfiles/marker_green.png";
					icon[0].shadow = "http://www.google.com/mapfiles/shadow50.png";
					icon[0].iconSize = new GSize(20, 34);
					icon[0].shadowSize = new GSize(37, 34);
					icon[0].iconAnchor = new GPoint(9, 34);
					icon[0].infoWindowAnchor = new GPoint(5, 1);

				var marker = marker = new GMarker(point, icon[0]);
				map.addOverlay(marker);	
				
			}
			else{
				var geocoder = new GClientGeocoder();

				function showAddress(address) {
				  geocoder.getLatLng(
					address,
					function(point) {
					  if (!point) {
						//alert(address + " not found");
					  } else {
						map.setCenter(point, 13);
						var marker = new GMarker(point);
						map.addOverlay(marker);
						//marker.openInfoWindowHtml(address);
					  }
					}
				  );
				}	
				showAddress($(this).attr('title'));				
			}

		
	});
	
	$('.swatches').each(function(){
		//adding our functionality ;)
		$('.swatches span img').click(function(){
			//$('.swatches').parent().children('img').attr('src',$(this).attr('src').replace('swatches/','detailed/'));
			//$('.propPageBigImg').innerfade();
			$('.propPageBigImg').empty().append('<img src="'+$(this).attr('src').replace('swatches/','detailed/')+'" alt="" />')
		});
	});

//Front Form Changes

        $('#homepage_browse').submit(function(){
		var action=$('.selectBlock SELECT#property').val();
		if(action==''){
			alert('Please select a region');
			return false;
		}
		else{
			$(this).attr('action',action);
			return true;
		}
	});

	
	$('#homepage_form').submit(function(){
		var action=$('.selectBlock SELECT#suburb').val();
		if(action==''){
			alert('Please select a suburb');
			return false;
		}
		else{
			$(this).attr('action',action);
			return true;
		}
	});

	if($("#arrival").size()){
		$("#arrival").datepicker({dateFormat: 'dd/mm/yy'});
	}
	if($("#departure").size()){
		$("#departure").datepicker({dateFormat: 'dd/mm/yy'});
	}
	
	//loading slideshow
	$('.homePageBigImg').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		//type: 'one-time',
		containerheight: '287px'
	});	

	//loading slideshow
	$('.propPageBigImg').innerfade({
		speed: 1000,
		timeout: 5000,
		type: 'sequence',
		//type: 'one-time',
		containerheight: '295px'
	});	

	//activating the next/prev buttons
	$('.img-block .next').click(function(){
		//calculating the maximal offset to move
		var offset=$('.swatches span img:last').offset();
		offset.left+=10;

		delta=230;
		$('.swatches span').each(function(){
			var viewport_from=$('.swatches').offset().left;
			var viewport_to=viewport_from+$('.swatches').width();
			
			$('.swatches span img').each(function(){
				if($(this).offset().left+$(this).width()<viewport_from){
					$(this).insertAfter('.swatches span img:last');	
					left=$('.swatches span').css('left').replace('px','')*1;
					$('.swatches span').css('left',(left+$(this).width())+'px');				
				}
			});

			//var cx=$(this).css('left').replace('px','')*(-1);
			var cx=685;
			//if($.browser.msie) cx-=80;
			
			var dx=(offset.left-cx)<delta?offset.left-cx:delta;
			if(dx==delta){
				// we need to clone our images and get them inserted afterwards
				
			}

			if(dx>0){
				$(this).animate({'left':'-='+dx+'px'},'1000');
			}
		});
	});
	$('.img-block .prev').click(function(){
		//calculating the maximal offset to move
		var offset=$('.swatches span img:last').offset();
		offset.left+=10;

		delta=230;
		$('.swatches span').each(function(){
			var viewport_from=$('.swatches').offset().left;
			var viewport_to=viewport_from+$('.swatches').width()+100;
			
			$('.swatches span img').each(function(){
				if($(this).offset().left+$(this).width()>viewport_to){
					$(this).insertBefore('.swatches span img:first');	
					left=$('.swatches span').css('left').replace('px','')*1;
					$('.swatches span').css('left',(left-$(this).width())+'px');				
				}
			});

			var cx=$(this).css('left').replace('px','')*(-1);
			var dx=(cx)<delta?cx:delta;
			if(dx>0){
				$(this).animate({'left':'+='+dx+'px'},'1000');
			}
		});
	});
	
	marquee();
	
	//adding rollover for buttons
	
	$('.btn_submit').hover(function(){$(this).css('background-color','#0D5471')},function(){$(this).css('background-color','#7d99a3')});
	
	$('#sydney_time').each(function(){
		sydney_time();
	});
	
});
function sydney_time(){
		var currentTime=new Date();
		var dt=currentTime.getTime()+currentTime.getTimezoneOffset()*60*1000;
		dt+=10*3600*1000;
		currentTime.setTime(dt);
  		var hours = currentTime.getHours()
  		var minutes = currentTime.getMinutes()
  		var seconds = currentTime.getSeconds()

		var suffix = "AM";
		if (hours >= 12) {
			suffix = "PM";
			hours = hours - 12;
		}
		if (hours == 0) {
			hours = 12;
		}
		
		if (minutes < 10) minutes = "0" + minutes

  		$('#sydney_time').html(hours + ":" + minutes + ":" +seconds + " "+ suffix);
		
		window.setTimeout('sydney_time()',1000);
}
function marquee(){
	var width=$("#marquee span").width()+30;
	$("#marquee span").css('left','770px').animate({left:"-"+width+"px"},width*12,'linear',runMarquee());
}
function runMarquee(){
	var width=$("#marquee span").width()+30;
	window.setTimeout("marquee()",width*12+3000);
}

function fixPNG(element) {

	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var src;
		
		if (element.tagName=='IMG') {
			if (/\.png$/.test(element.src))
			{
				src = element.src;
				element.src = "images/blank.gif";
			}
		}
		
		else
		{
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src)
			{
				src = src[1];
				element.runtimeStyle.backgroundImage="none";

			}
		}
		
		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}


