$(function(){
	$("#SlideShow").cycle({ 
	    fx:    	'fade', 
	    speed:  2000,
	    timeout:9000
	});
	 
	$(".png").pngFix();
	 
	$(".feature span").css({opacity:0.7});
	 
	$(".feature, #property .photos a").hover(function(){ 
		$("img",this).animate({opacity: 0.6}, 200 );
	}, function(){
		$("img",this).animate({opacity: 1}, 200 );
	});
	 

	$("#property .photos").unbind().each(function(){
		$("a",this).lightBox();
	});

	
	$("#suburbSearch #suburb").autocomplete({
		source: availSub,
		select: function(event, ui) {
			if(ui.item){
				$("#suburbSearch").submit();
			}
		},
		focus: function(event, ui) {
			$('#suburb').val(ui.item.label);
			return false;
		}
	});
	
	//Map Code!
	var map = ".map";
	var address = $('.address').html() + " " + $('.address').attr("postcode");
	$(map).each(function(){
		if(point = $(map).attr("point")){
			point = point.split(",");
	   		placeMarkMap(map,address,[point[0],point[1]]);
		}
		else{
			//Get the address
			$("<div></div>").jmap('init', {
				'mapType':'map',
				"mapZoom":16,
				'mapShowjMapsIcon': false,
				'mapEnableType': true
			}).jmap('SearchAddress', {
		        'query': address,
		        'returnType': 'getLocations'
		    }, function(result, options){
		        var valid = Mapifies.SearchCode(result.Status.code);
		        if (valid.success) {
		       		$.each(result.Placemark, function(i, point){
		           		placeMarkMap(map,address,[point.Point.coordinates[1], point.Point.coordinates[0]]);
		                return false;
		            });
		        }
		    });
		}
	});
	
	$("#search select").change(function(e){
		$("#search form").submit();
	});
	
	
	//Menu Code
	$("#sideMenu").each(function(){
		$("li",this).hover(function(){ 
			$(">ul:not(:animated)", this).each(function(){
				$(this).show("fast");
				menuOpenCount++;
				selectCheck();
			}); 
		}, function(){
			$(">ul", this).each(function(){
				$(this).hide("fast");
				menuOpenCount--;
				selectCheck();
			}); 
		});
	});
	runLoop();
});



function runLoop() {
	if($('.photoarea').children().length <= 2) return;
	
	var size = $('.photoarea *:first').outerHeight();
	var t = $('.photoarea').css('top');
	if(t == 'auto'){
		t = 0;
	}
	else {
		t = t.substring(0,t.length-2);
	}
	var pcThrough = Math.abs(t) / size * 100;
	var speed = 37 * (100-pcThrough) / 100 * size;	
	$('.photoarea:not(:animated)').animate({'top':'-'+ size +'px'}, speed, "linear", function(){
		$('*:first',this).appendTo(this);
		$(this).css({'top':'0px'});
		setTimeout(function(){ runLoop() }, 10);
	}).hover(function(e){
		$(this).stop();
	},function(e){
		setTimeout(function(){ runLoop() }, 10);
	});
}


var menuOpenCount = 0;

function selectCheck(){
	if ($.browser.msie && $.browser.version.substring(0,1) === '6') {
		if(menuOpenCount > 0){
			$("select").css("visibility","hidden");
		}
		else{
			$("select").css("visibility","visible");
		}
	}
}

function placeMarkMap(map,address,p){
	$(map).jmap('init', {
		'mapType':'map',
		"mapZoom":16,
		'mapShowjMapsIcon': false,
		'mapEnableType': true,
		'mapCenter':p
	}).jmap('AddMarker',{
        'pointLatLng':p,
        'pointHTML':address,
		'centerMap': true
    });
}

