
var web_path='/';

/**
 * Global Onload Method
 * 
 * currently this sets up the tell a friend popup in a lightbox as well as sets up the bookmark site link.
 */
$(function() {
	
	var def_subsc_text;
	
	// attach fancybox to the send to friend link in the footer if both exist
	if ($.fancybox)
	{
		$('#footer a.footer-navLink').each(function()
		{
			var str=''+this.href;

			if (str.indexOf('__popupemailpage')>0)
			{
				$(this).fancybox({
					'autoScale'			: false,
					'scrolling'			: 'no',
					'centerOnScroll'	: true,
					'overlayOpacity'	: 0.5,
					'overlayColor'		: '#000', 
					'showNavArrows'   : false,
					'width'           : 580,
					'height'          : 380,
					'transitionIn'		: 'elastic',
					'transitionOut'   : 'elastic',
					'type'				: 'iframe'
				});
			}
			else if (str.indexOf(':addToFavourites()')>0)
			{
				this.href='#';
				$(this).jFav();
			}

		});
	}

	// attach jquery bookmark plugin to the bookmark site link.
	//if ($('#bookmarkSite').length >= 1) {
	//	$('#bookmarkSite').jFav();
	//}

	// Init home page slider; if not on home page then do nothing.

	/*$('div#home-slideshow').cycle(
	{
		fx:       'scrollLeft',
		timeout:  5000,
		speed:    700,
		pager:    '#home-slideshow-pager',
		pagerAnchorBuilder: function(num, img)
		{
			return '<a href="#">&nbsp;</a>';
		}
	});*/
	
	// Subscribe box
	
	def_subsc_text='Email address';

	$('input.subscribe-email-box').each(function()
	{
		if (this.value=='')
		{
			this.value=def_subsc_text;
			this.style.color='#909090';
		}
	});
	$('input.subscribe-email-box').focus(function()
	{
		if (this.value==def_subsc_text)
		{
			this.value='';
			this.style.color='#101010';
		}
	});
	$('input.subscribe-email-box').blur(function()
	{
		if (this.value=='')
		{
			this.value=def_subsc_text;
			this.style.color='#909090';
		}
	});

	// Login

	$('input.login-username-box').each(function()
	{
		if (this.value=='')
		{
			this.value='Username';
			this.style.color='#909090';
		}
	});
	$('input.login-username-box').focus(function()
	{
		if (this.value=='Username')
		{
			this.value='';
			this.style.color='#101010';
		}
	});
	$('input.login-username-box').blur(function()
	{
		if (this.value=='')
		{
			this.value='Username';
			this.style.color='#909090';
		}
	});

	$('input.login-password-box').each(function()
	{
		if (this.value=='')
		{
			this.value='pass';
			this.style.color='#909090';
		}
	});

	$('input.login-password-box').focus(function()
	{
		if (this.value=='pass')
			this.value='';
	});

});


/**
 * This is a jquery plugin that I use to preload a bunch of images, its easy to use, just call 
 * 
 * $.preLoadImages("image1.jpg", "image2.jpg")
 * 
 */
(function($) {
	var cache = [];
	// Arguments are image paths relative to the current page.
	$.preLoadImages = function() {
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			cache.push(cacheImage);	
		}
	};
})(jQuery);


function changePerPage(osel) {

	var val = osel[osel.selectedIndex].value;
	url = new Url(location.href);
	url.setVar('perpage', val);
	url.go();

}

function changeOrder(osel) {
   var val = osel[osel.selectedIndex].value;
   url = new Url(location.href);
   url.setVar('order', val);
   url.go();
}

function toggleDirection(dir) {
   var set = (dir == 'ASC') ? 'DESC' : 'ASC';
   url = new Url(location.href);
   url.setVar('dir', set);
   url.go();
} 

function showHide(element) {
	if(jQuery(element).css('display') == "none"){
		jQuery(element).slideDown('fast');
	}else{
		jQuery(element).slideUp('fast');
	}
}

function over(obj) {
	// first lets see if the src of this image is blank.gif and if its IE6 - Based on these conditions
	// we know that its a png fix so we need to alter the method of changing the image background as oposed
	// to the image source.
	var filename = obj.src.substring(obj.src.lastIndexOf('/')+1);

	// first lets check to see if its IE6 and if the image in a png file
	if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
		var newFilter = (obj.style.filter.replace('.png', '-over.png'));
		obj.style.filter = newFilter;
	} else {
		obj.src=obj.src.replace('.gif', '-over.gif');
		obj.src=obj.src.replace('.jpg', '-over.jpg');
		obj.src=obj.src.replace('.png', '-over.png');
	}
}
 
function out(obj) {
	var filename = obj.src.substring(obj.src.lastIndexOf('/')+1);
	if(filename == 'blank.gif' && $.browser.msie && $.browser.version == '6.0') {
		obj.style.filter = obj.style.filter.replace('-over', '');
	} else {
		obj.src=obj.src.replace('-over', '');
	}
}

//////////////////////////////////////////////////////////////////////////////
/**
* State box on-change event
*/

var last_other_state_value='', last_suburb_value='';

function ChangeStateBox(box)
{
	if (box.value!='')
	{
		last_other_state_value=$('#in_state_other').attr('value');
		$('#in_state_other').attr('value', '');
		PopulateCityBox($('#in_state').attr('value'), '');
	}
	else if ($('#in_state_other').attr('value')=='')
	{
		$('#in_state_other').attr('value', last_other_state_value);
		last_other_state_value='';
	}
}

//////////////////////////////////////////////////////////////////////////////

function PopulateCityBox(state_val, suburb_val)
{
	var url, last_city_value;

	url='account.php?__getsuburblist='+escape( state_val );
	if (suburb_val!='')
		last_city_value=suburb_val;
	else
		last_city_value=$('#in_city_suburb').attr('value');

	$('#suburb-container').html('<select name="in_city_suburb"><option value="">loading</option></select>');

	$.get(url, function(rval)
	{
		$('#suburb-container').html(rval);

		if (last_city_value!='')
		{
			var len, c1, box;

			box=document.getElementById('in_city_suburb');
			if (box)
			{
				len=box.options.length;
				for (c1=0; c1<len; c1++)
				{
					if (box.options[c1].value==last_city_value)
					{
						box.selectedIndex=c1;
						break;
					}
				}
			}
		}
	});

}

//////////////////////////////////////////////////////////////////////////////

function CitySuburbChange(in_val)
{
	if (in_val!='')
	{
		last_suburb_value=$('#in_city_suburb_other').attr('value');
		if (last_suburb_value!='')
			$('#in_city_suburb_other').attr('value', '');
	}
	else if ($('#in_city_suburb_other').attr('value')=='')
	{
		$('#in_city_suburb_other').attr('value', last_suburb_value);
		last_suburb_value='';
	}
}

//////////////////////////////////////////////////////////////////////////////

function SuburbOtherChange(in_val)
{
	if (in_val!='')
		$('#in_city_suburb').attr('selectedIndex', '');
}

//////////////////////////////////////////////////////////////////////////////


