
	var visNews = 0;
	var visArchive = 0;
	var visDropbar = 0;
	var vertAuto = false;
	var vertPause = false;
	
	$(document).ready(function(){
		
		

		
		if (!$.cookie('agreement'))
		{
			$('#disclaimer').dialog({ 
				modal: true,
				closeOnEscape: false,
				width: 700,
				height: 420,
				position: ['center',100],
				draggable: false,
				resizable: false,
				zIndex: 4800,
				buttons: { 
					"Agree & Continue": function() { 
						$.get(BASEURL+'?/site/log_ip');
						$(this).dialog("close");
						$.cookie('agreement', 'yes');
					},
					"I don't understand": function() { 
						$(this).html("For further information about Crace, call 1800 751 878 to speak to one of our sales consultants.");
					}
				},
				open: function(){
					$(this).parents('.ui-dialog:first').find('.ui-dialog-titlebar-close').remove();
				}
			});
		} else {
			$('#disclaimer').hide();
		}
		
		$.historyInit(getURL);
		
		/*
		$('#dropbar .main a.main').click(function(){
			
			
			var p = $(this).parents("li:first");
			var s = $(p).find('ul');
			
			$('#dropbar .body').stop().scrollTo(p,800,{offset:{top:-10, left:0}});
			
			$('#dropbar ul.main ul').not(s).slideUp();
			$(s).slideToggle();
		});
		
		$('#dropbar .main a.main').mouseover(function(){
			$(this).click();
		});
		*/
		navBars();
		
		pageInit($('#right, #sidebar ul ul, #footer'));
		
		
		//$('#left #sidebar ul ul').not('#left #sidebar li.news ul').hide();
		$('#left #sidebar ul ul').hide(); // Change 20100324 by Ronty
		
			
		$('#vertScroller').mouseover(function(){
			vertPause = true;
			//$("#vertScroller ul").stop();
		}).mouseout(function(){
			vertPause = false;
		});
		
		setTimeout(function(){
			$('#loading').fadeOut('slow',function(){
				$(this).remove();
			});
		},1000);
	});
	
	function getURL(uri,into)
	{
		
		
		if (!uri || uri == '') {
			return;
		}
		
		if (uri.indexOf('http') < 0) {
			uri = BASEURL + uri;
		}
		
		if (uri.indexOf('http') == 0 && uri.indexOf(BASEURL) < 0) {
			window.open(uri);
			return;
		}
		
		//do it
		//alert('URL intercept');
		if (!into) into = $('#content');
		else into = $('#'+into);
		
		var inner = $(into).find('.inner');
		if (!inner.length) {
			inner = into;
		}
		

		
		$('#vertScroller_min').hide();
		vertAuto = false;
		
		
		
		$.get(uri,function(result){
			//reset content
			$(into).css('background-image','none');
			$(into).css('background-color','transparent');
			
			//reset content inner
			$(inner).css('padding',0);
			$(inner).css('margin',0);
			
			$(inner).hide().html(result).fadeIn('fast',function(){
				$.tooltip.hide;
				$('#tooltip').hide();
				
				var title = $(inner).find('title').html();
				if (title && title != '') {
					window.document.title = title;
				}
				
				pageTracker._trackPageview(uri);
				
			});
			
			pageInit($(inner));
			
			if (!vertAuto && $('#vertScroller:visible').length > 0 && !vertPause) {
				vertClose();
			}			
		});
	}
	
	function linkAction(e)
	{
		if ($(e).attr('rel') == "" || $(e).attr('rel') == undefined) {
			var link = $(e).attr('href');
			link = link.replace(/^.*\?/, '?');
			
			if ($(e).attr('rel')) {
				//link += ":rel="+$(e).attr('rel');
			}
			$.historyLoad(link);
		} else {
			getURL($(e).attr('href'),$(e).attr('rel'));
		}
	}
	
	function pageInit(e)
	{
		
		$(e).find('#newsletters').each(function(){
			var li = $(this).children('li');
			
			li.timer = null;
			
			$(li).mouseover(function(){
				clearTimeout(this.timer);
				
				
				var ul = $(this).find('ul');
				
				var margintop = 0 - $(this).height() - $(ul).height();
				
				
				$(ul).show().css('width',$(this).width()); //.css('margin-top',margintop+'px');
				$(ul).mouseover(function(){
					
					var parent = $(this).parents('li:first');
					
					clearTimeout(parent.timer);
					$(this).show();
				});
			}).mouseout(function(){
				var ul = $(this).find('ul');
				
				clearTimeout(this.timer);
				
				this.timer = setTimeout(function(){
					$(ul).fadeOut();
				},100);
				
			});
		});
		
		$(e).find('a')
		.not('a[href*=#]')
		.not('a[href$=jpg]')
		.not('a[href$=png]')
		.not('a[href$=flv]')
		.not('a[href$=swf]')
		.not('a[href$=gif]')
		.not('a[href$=doc]')
		.not('a[href$=docx]')
		.not('a[href$=pdf]')
		.not('a[href$=exe]')
		.not('a[href^=mailto]')
		.not('a[href*=javascript]')
		.click(function(){

			// moves to a new page. 
			// pageload is called at once. 
			
			linkAction(this);
			
			return false;
			
		});
		
		if ($('#dropbar:visible')) {
			$('.expanding').each(function(){
				
				var margin = parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
				var padding = parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
				
				if (padding > 0) {
					padding += 20;
				}
				
				var new_w = 570 - margin - padding;
				
				$(this).css('width',new_w+'px');
			});
		}
		
		$(e).find('.tooltip_bottombar').tooltip({track: true, delay: 0, showURL: false, showBody: " - ", extraClass: "bottombar"});
		$(e).find('.tooltip_vertbar').tooltip({track: true, delay: 0, showURL: false, showBody: " - ", extraClass: "vertbar"});
		
		
		$(e).find('.tooltip').tooltip({track: true, delay: 0, showURL: false, showBody: " - ", extraClass: "sml"});
		
		$(e).find('#gallery ul a').click(function(){
			
			clearTimeout(timeGallery);
			
			galleryClick(this);
			
			return false;
		});
		
		$(e).find('.urban_bar a')
		.not('a[href$=doc]')
		.not('a[href$=docx]')
		.not('a[href$=pdf]')
		.click(function(){
			var uri = $(this).attr('href');
			uri = uri.split('#');
			uri = uri.reverse();
			uri = '#'+uri[0];
			
			$('.zoomer').hide();
			$('#gallery .controlls').hide();
			
			$('#scrollPan div').each(function(){
				var ov = $(this).css('overflow');
				if (ov == 'auto') {
					$(this).addClass('unauto').css('overflow','hidden');
				}
			});
			
			//ie7 fix
			$('#scrollPan').scrollTo(uri,1200, {onAfter: function(){
				if (uri == '#gallery') { $('#gallery .controlls').fadeIn(); $('.zoomer').show(); }
				//$.historyLoad(uri);
				$('.unauto').css('overflow','auto');
			}});
			return false;
		});
		
		//$(e).find('#gallery .controlls ul a:first').click();
		
		$(e).find('.urban_navbar a').click(function(){
			$(e).find('.urban_navbar a.on').removeClass('on');
			$(this).toggleClass('on');
		});
		
		$(e).find('.suburban_navbar a').click(function(){
			$(e).find('.suburban_navbar a.on').removeClass('on');
			$(this).toggleClass('on');
		});
		
		$(e).find('#morenews .plus').click(function(){

			var scrolling = $('#newsbar ul');
			
			if (visArchive+1 < $(scrolling).find('li').length) {
				visArchive++;
			}
			
			if (visArchive+1 == $(scrolling).find('li').length) {
				$(this).addClass('off');
			} else {
				$(this).removeClass('off');
				$(this).siblings('.minus').removeClass('off');
			}
			
			$(scrolling).scrollTo('li:eq('+visArchive+')',800);
			return false;
		});
		$(e).find('#morenews .minus').click(function(){
			var scrolling = $('#newsbar ul');
			
			if (visArchive > 0) {
				visArchive--;
			}
			
			if (visArchive == 0) {
				$(this).addClass('off');
			} else {
				$(this).siblings('.plus').removeClass('off');
				$(this).removeClass('off');
			}
			
			$(scrolling).scrollTo('li:eq('+visArchive+')',800);
			return false;
		});
		
	}
	
	function galleryClick(e,width)
	{
		$('#gallery .slider').css({
			overflow: 'hidden',
			width: '717px'
		});
		var count = $('#gallery .slider ul li').length;
		var nw = ((count+1) * (65 + 8)) + 8 + 100;
		
		$('#gallery .slider ul').css({
			width: nw+'px'
		});
		
		$('#gallery .slider').mousemove(function(e){
			var c = $('#container').get(0).offsetLeft;
			var x = e.clientX - c - 160; //left 0
			
			var max = 730;
			
			var percent = x/max * 100;
			var move = nw/100 * percent;
			
			$('#gallery .slider').get(0).scrollLeft = move - 200;
		});
		
		if (!width) width = '750';
		
		$('#gallery ul li.on').removeClass('on');
		$(e).parents('li:first').addClass('on');
		
		var uri = $(e).attr('href');
		
		if ($(e).hasClass('zoomable')) {
			$('.zoomer').show();
	        $('.zoomin').show();
	        $('.zoomout').hide();
	        
			alt = uri.replace(".jpg","_large.jpg");
			alt = alt.replace(".png","_large.png");
			alt = alt.replace(".gif","_large.gif");
			
			alt = 'rel="'+alt+'"';
		} else {
	        $('.zoomer').hide();
	        $('.zoomin').hide();
	        $('.zoomout').hide();
			alt = '';
		}
		
		

		
		
		$('#gallery .img').fadeOut('fast',function(){
			if ($(e).hasClass('flv')) {				
				$(this).html('<div style="text-align: center;">\
				<object width="420" height="266">\
				<param name="movie" value="resources/swf/VideoPlayer.swf?flv='+uri+'">\
				<param name="wmode" value="opaque"/>\
				<param name="allowFullScreen" value="true"/>\
				<embed src="resources/swf/VideoPlayer.swf?flv='+uri+'" allowFullScreen="true" width="420" height="266" wmode="opaque">\
				</embed>\
				</object>\
				</div>').fadeIn('fast');
			} else {
				$(this).html('<img id="galpic" width="'+width+'" src="'+uri+'" '+alt+'/>').fadeIn('slow',function(){
					$("img#galpic").load(function(){
						$("img#galpic").panFullSize(750, 360);
					});
				});
				
				
				if ($(e).hasClass('impression')) {
					$(this).find('img').mouseover(function(){
						$('#impression').html($(e).attr('title')).show();
					}).mouseout(function(){
						$('#impression').hide();
					});
				}
				
			}
			
		});
	}
	
	function navBars()
	{
		$('#sidebar h3 a')
		.not('a[href$=doc]')
		.not('a[href$=docx]')
		.not('a[href$=pdf]')
		.click(function(){
			var kids = $(this).parents('h3:first').siblings('ul').length;
			
			if ($(this).hasClass('direct') || !kids) {
				$(this).parents('ul.main').find('ul').slideUp();
				$(this).parents('ul.main').find('.controlls').fadeOut();
				
				visNews = 0;
				
				linkAction(this);
				
				//getURL($(this).attr('href'));
			} else {
				var open = $(this).parents('h3:first').siblings('ul');
				var controlls = $(this).parents('h3:first').siblings('.controlls');
				
				visNews = 0;
				
				$(this).parents('ul.main').find('ul').not(open).slideUp();
				$(this).parents('ul.main').find('.controlls').not(controlls).fadeOut();
				$(open).slideDown();
				$(controlls).fadeIn();
			}
			
			return false;
		});
		
/* NEWS SCROLLER */
		
		$('#sidebar .controlls .plus').click(function(){
			
			var scrolling = $(this).parents('.controlls:first').siblings('ul');
			
			if (visNews+1 < $(scrolling).find('li').length) {
				visNews++;
			}
			
			if (visNews+1 == $(scrolling).find('li').length) {
				$(this).addClass('off');
			} else {
				$(this).removeClass('off');
				$(this).siblings('.minus').removeClass('off');
			}
			
			$(scrolling).scrollTo('li:eq('+visNews+')',800);
			return false;
		});
		$('#sidebar .controlls .minus').click(function(){
			var scrolling = $(this).parents('.controlls:first').siblings('ul');
			
			if (visNews > 0) {
				visNews--;
			}
			
			if (visNews == 0) {
				$(this).addClass('off');
			} else {
				$(this).siblings('.plus').removeClass('off');
				$(this).removeClass('off');
			}
			
			$(scrolling).scrollTo('li:eq('+visNews+')',800);
			return false;
		});
		
/* DROPBAR SCROLLER */
		
		$('#dropbar .controlls .plus').click(function(){
			var mtop = parseInt($('#dropbar .body ul.main').css('margin-top'));
			var mh = 0 - parseInt($('#dropbar .body ul.main').height());
			
			if (mtop-80 > mh) {
				$('#dropbar .body ul.main').stop().animate({marginTop:'-=80px'});
			}
		});
		$('#dropbar .controlls .minus').click(function(){
			var mtop = parseInt($('#dropbar .body ul.main').css('margin-top'));
			var mh = 0 - parseInt($('#dropbar .body ul.main').height());
			
			if (mtop+80 < 0) {
				$('#dropbar .body ul.main').stop().animate({marginTop:'+=80px'});
			} else {
				$('#dropbar .body ul.main').stop().animate({marginTop:'0'});
			}
		});
		
		
/* ARCHIVE SCROLLER */


		
	}
	
	
	
	
	
	
	
	
	
	function dropClose(e)
	{
		
		if (!e) {
			e = $('.dropbar_toggle a');
		}
		
		$(e).addClass('closed');
		$('#dropbar').slideUp();
		
		$('.expanding').each(function(){
			
			var margin = parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
			var padding = parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
			
			if (padding > 0) {
				padding += 20;
			}
			
			var new_w = 840 - margin - padding;
			
			$(this).animate({width:new_w+'px'},500);
		});
	}
	
	function dropOpen(e)
	{
		if (!e) {
			e = $('.dropbar_toggle a');
		}
		
		$(e).removeClass('closed');
		$('#dropbar').slideDown();
		
		$('.expanding').each(function(){
			
			var margin = parseInt($(this).css('margin-left')) + parseInt($(this).css('margin-right'));
			var padding = parseInt($(this).css('padding-left')) + parseInt($(this).css('padding-right'));
			
			if (padding > 0) {
				padding += 20;
			}
			
			var new_w = 570 - margin - padding;
			
			$(this).animate({width:new_w+'px'},500);
		});
	}
	
	function dropToggle(e)
	{
		
		if (!e) {
			e = $('.dropbar_toggle a');
		}
		if (!$(e).hasClass('closed')) {
			dropClose(e);
		} else {
			dropOpen(e);
		}
	}
	function CloseToggle()
	{}

	
	function galleryToggle()
	{
		if ($('#gallery .slider:visible').length) {
			//slide down
			$('#gallery .controlls').animate({marginTop: '-24px'},800);
			$('#gallery .slider').slideUp(900);
		} else {
			//slide down
			$('#gallery .controlls').animate({marginTop: '-94px'},800);
			$('#gallery .slider').slideDown(750);
			
		}
		$('#gallery .controlls .toggle').toggleClass('open');
	}
	
	var curGallery = 0;
	var timeGallery;
	
	function galleryAuto(resetter)
	{
		if (resetter == true) {
			curGallery = 0;
		}
		clearTimeout(timeGallery);
		
		var idx = $('#gallery ul a');
		
		if (curGallery+1 > idx.length) {
			curGallery = 0;
		}
		
		galleryClick($('#gallery ul a:eq('+curGallery+')'));
		
		curGallery++;
		
		//timeGallery = setTimeout(galleryAuto,4000);
	}

	
function autoNextVideo()
{
	/*
	var next = $('#large_gallery li.on').next('li');
	if (next.length) {
		$('#large_gallery .buttons .right').click();
	} else {
		$('.slider ul').animate({marginLeft: 0});
		$('#large_gallery li:first a').click();
	}
	*/
}

	function largeGallery()
	{
	
		$('#large_gallery .slider a').click(function(){
			
			$('#large_gallery .slider li.on').removeClass('on');
			$(this).parents('li:first').addClass('on');
			
			var uri = $(this).attr('href');
			
			if ($(this).hasClass('flv')) {
				
				var uri = $(this).attr('rel');
				
				$('#large_gallery .img').fadeOut('fast',function(){
					$(this).html('<div style="text-align: center;">\
					<object width="420" height="266">\
					<param name="movie" value="resources/swf/VideoPlayer.swf?flv='+uri+'">\
					<param name="wmode" value="opaque"/>\
					<param name="swliveconnect" value="true"/>\
					<param name="allowFullScreen" value="true"/>\
					<embed src="resources/swf/VideoPlayer.swf?flv='+uri+'" swliveconnect="true" allowFullScreen="true" width="420" height="266" wmode="opaque">\
					</embed>\
					</object>\
					</div>').fadeIn('fast');
				});
			} else {
				$('#large_gallery .img').fadeOut('fast',function(){
					$(this).html('<img id="galpic" src="'+uri+'"/>').fadeIn('fast');
				});
			}

			return false;
		});
		$('#large_gallery .slider a:first').click();
		
		$('#large_gallery .buttons .left').click(function(){
			var prev = $('#large_gallery li.on').prev('li');
			if (prev.length)
			{
				$(prev).find('a').click();
				if (parseInt($('.slider ul').css('margin-left')) < 0) 
				{
					$('.slider ul').animate({marginLeft: '+='+ ($(prev).width() + 12)});
				}
			}
			return false;
		});
		
		$('#large_gallery .buttons .right').click(function(){
			var next = $('#large_gallery li.on').next('li');
			if (next.length)
			{
				var total = $('#large_gallery li').length - 5;
				var width = total * ($(next).width() + 12);
				var maxmargin = 0 - width;
				
				$(next).find('a').click();
				
				if (parseInt($('.slider ul').css('margin-left')) > maxmargin) 
				{
					$('.slider ul').animate({marginLeft: '-='+ ($(next).width() + 12)});
				}
			}
			return false;
		});
	}
	
	/*==========Home Function Start================*/
	
	function HomeFinder()
	{
		//alert('aa');
		$('.landId').hide('fast');
		$('#homefinder_results .slider a').click(function(){
			
			$('#homefinder_results .slider li.on').removeClass('on');
			$(this).parents('li:first').addClass('on');
			
			var uri = $(this).attr('href');
			//alert(uri);
						
			
			var urlArrau = new Array();
			urlArrau = uri.split('#');
			//homesearchID=urlArrau[1];
			homesearchID="#LandID_"+urlArrau[1];
			
			
			var valu=$(homesearchID).html();
			
			
			$('#home_details').fadeOut('fast',function(){
						//alert(data);
						$('#home_details').html(valu).fadeIn('slow');
						//jslog(1,data);
					});
		
		//alert(homesearchID);
			/*$.post("home.php", {queryString: ""+homesearchID+""}, function(data){
				if(data.length >0) {
					//$('#suggestions').show();
					$('#home_details').fadeOut('fast',function(){
						//alert(data);
						$('#home_details').html(data).fadeIn('fast');
						//jslog(1,data);
					});
				}
			});*/

			


	//$('#homefinder_results .image_wrapper').fadeOut('fast',function(){
				//	$(this).html('<img id="galpic" src="'+uri+'" width="568" height="345"/>').fadeIn('fast');
				//});
			

			return false;
		});
		$('#homefinder_results .slider a:first').click();
		
		
		
		$('#homefinder_results .buttons .left').click(function(){
			var prev = $('#homefinder_results li.on').prev('li');
			if (prev.length)
			{
				$(prev).find('a').click();
				if (parseInt($('.slider ul').css('margin-left')) < 0) 
				{
					$('.slider ul').animate({marginLeft: '+='+ ($(prev).width() + 12)});
				}
			}
			return false;
		});
		
		$('#homefinder_results .buttons .right').click(function(){
			var next = $('#homefinder_results li.on').next('li');
			if (next.length)
			{
				var total = $('#homefinder_results li').length - 5;
				var width = total * ($(next).width() + 12);
				var maxmargin = 0 - width;
				
				$(next).find('a').click();
				
				if (parseInt($('.slider ul').css('margin-left')) > maxmargin) 
				{
					$('.slider ul').animate({marginLeft: '-='+ ($(next).width() + 12)});
				}
			}
			return false;
		});
	}
function gup(uri, name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
 //var regexS = "[\\?&]"+name+"=([^&#]*)";
 

  var regex = new RegExp( regexS );
  var results = regex.exec(uri);
  if( results == null )
	return "";
  else
	return results[1];
}
	/*==========Home Function End================*/