jQuery(document).ready(function($){

//Add 'selected' classes to menu and submenu items
var URL = location.href.substring(location.href.lastIndexOf('/') + 0);//+location.hash;
	if ( URL ) {
		$('#menu ul a[href$="' + URL + '"]').parent('li').addClass('selected');
		$('#bottom .info a[href$="' + URL + '"]').addClass('selected');
		$('#right-nav a[href$="' + URL + '"]').addClass('selected');
		$('#copyright a[href$="' + URL + '"]').addClass('selected');
		$('#copyright a[href$="http://www.prettypollution.com.au/"]').removeClass('selected');
		$('#copyright a[href$="http://www.prettypollution.com.au/business-catalyst"]').removeClass('selected');
		//console.log(URL);
}

//Loop through all the lists in a specified area and add span tags before/after specified tags
	var oMenu = $('#menu').find('ul:first');
	oMenu.children().each(function(){
	$(this).find('a:first').html('<span>' + $(this).find('a:first').text() + '</span>');
});

	var oSubMenu = $('#sub_menu').find('ul:first');
	oSubMenu.children().each(function(){
	$(this).find('a:first').html('<span>' + $(this).find('a:first').text() + '</span>');
});

//Menu Hover Functions	
$('#menu li').hover(
	function(){
		$(this).find('a:first').addClass('hover');	
	},
	function(){
		$(this).find('a:first').removeClass('hover');	
	}
);

//Display Menu with multiple submenu LEVELS
if ($("#menu")) {
		$('#menu ul ul').hide();
		
		$('#menu').find('li').hover(
			function(){
				var oUl = $(this).children().filter(function(){
					return $(this).is('ul');	
				});
				 if(oUl.length > 0) {
					oUl.fadeIn();
				}
			},
			function(){
				var oUl = $(this).children().filter(function(){
					return $(this).is('ul');	
				});
				 if(oUl.length > 0) {
					oUl.fadeOut();
				}
			}
		);
}


//Horzontal Rules
$('hr').css('display','none').css('visibility','hidden').wrap('<div class="hr"></div>');


//Add class 'last' to last Bottom Info section
$('#bottom .info:last').addClass('last');


//Customise Form Dropdown
if ( $('body#home').length == 0 ) {
	$('#query .cat_dropdown').selectbox();
}


//Remove 'DIV' tag if there is no text
$("#release-details").each(function() {
	var oDiv = $(this).text();
	if ( typeof oDiv == 'string' && ( $.trim(oDiv) == '' || $.trim(oDiv).toLowerCase() == 'none' ) ) { // or anything else you want to remove...
		$(this).hide();
	}
});


//Wrap 'textbox' span + style around the Captcha textfield
$('#CaptchaV2').wrap('<span class="textbox"></span>');

// Set CaptchaV2 input default value
var FormCaptchaV2 = "Enter code above";
jQuery("#query input#CaptchaV2").val(FormCaptchaV2).focus(function(){
	if(jQuery.trim(jQuery(this).val()) == 'Enter code above')jQuery(this).val('');
}).blur(function(){
	if(jQuery(this).val() == '') 
	{
		jQuery(this).val(FormCaptchaV2);
	}	
});


// Select values in form URL
//var oOffice = $('#query form').attr('action').split('=')[1].split('&')[0];
/*
$('#CAT_Custom_153471_container li').click(function(){
	var oForm = $('#query form');
	switch($.trim($(this).text()))
	{
		case 'Femantle':
			oForm.attr('action',oForm.attr('action') + '&WID=100');	
			break;
		case 'Dampier':
			oForm.attr('action',webQueryFromBris);
			break;
		default:	
	}
});
*/

//Home page resizing
if ( $('div#footer').length == 0 ) {
	$('body#home #info').css('margin','0 0 -70px').css('padding','33px 53px 70px 71px');
} else {
	$('body#home #info').removeAttr("style");
}


//Search Input Hover/Focus/Blur
$("#search input[type=text]").focus(function(){
	$("#search").addClass("hover");
});
$("#search input[type=text]").blur(function() {
	$("#search").removeClass("hover");
});

/* Australia Map Hover Functionality */
$(function (){

	var oMap = jQuery('#map');
	var oMapToolTip = oMap.find('.map-details');
	
	var mapAnchorWatch = null;
	var mapToolTipWatch = null;
	
	var mapToolTipWatchTimeout = null;
	
	var toolTipOn = null;
	
	var toolTipRemovalTimeout = 2000;
	
	
	mapAnchorWatch = oMap
		.find('.map-link')
		.WatchBounds({
			inBounds: function($e,a){//Passes in event object and target
				if(toolTipOn)return;//If toolt is on return
				if(mapToolTipWatchTimeout)//If timeout has been set clear it
				{
					clearTimeout(mapToolTipWatchTimeout);
					mapToolTipWatchTimeout = null;	
				}
				if(mapToolTipWatch)//If tooltip watch is on without it being hoevered disable it
				{
					mapToolTipWatch.release();
					mapToolTipWatch = null;	
				}
				var sRel = a.attr('rel');//Get relation attribute
				oMapToolTip.find('.pod').css('display','none');//Hide tool tip content of all links
				oMapToolTip.find('#' + sRel).css('display','block');//Display content of current link
				oMapToolTip//Display tool tip
					.css({
						left: Math.round(a.position().left + a.width()),
						top: Math.round(a.position().top + (a.height() / 2)),
						display: 'block'	
					});
				mapToolTipWatch = oMapToolTip//Enable listener for tool tip
					.WatchBounds({
						inBounds: function(){
							toolTipOn = true;//Tool tip is on
							clearTimeout(mapToolTipWatchTimeout);//clear timeout for mouseout of anchor tag
							mapToolTipWatchTimeout = null;	
						},
						outBounds: function(){
							toolTipOn = null;//Tool tip is off	
							oMapToolTip.css({display: 'none'});//Hide tool tip	
							mapToolTipWatch.release();//Detach mouse listening
							mapToolTipWatch = null;	
						}		
					});	
					
			},
			outBounds: function(){
				if(toolTipOn)return;//If tooltip is on ignore this mouse out	
				mapToolTipWatchTimeout = setTimeout(function(){
					oMapToolTip.css({display: 'none'});//Hide tool tip	
					mapToolTipWatch.release();//Release tool tip watch
					mapToolTipWatch = null;
				},toolTipRemovalTimeout);		
			}
		});
});


//Find which browser and apply correct script
$(function () {
    if ($.browser.msie) {
		$('body#home #rotator')
		.after('<div id="nav">')
		.cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			//next: 'a.link-next', 
			//prev: 'a.link-prev',
			speed: 0,
			pager: '#nav',
			cleartypeNoBg: true
		});
	}
	else {
		$('body#home #rotator')
		.after('<div id="nav">')
		.cycle({
			fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
			//next: 'a.link-next', 
			//prev: 'a.link-prev',
			pager: '#nav',
			cleartypeNoBg: true
		});
	  }	  
 });
 
//Add rounded corners to images
	//Loop through all the img in a specified area.
	$('#middle img.rounded').each(function(index){
		
		//Check if image has "NoRoundCorner" class.
		if ($(this).hasClass('no_rounded') == false){
		 
			//Get dimensions.
			var width = $(this).width();
			var height = $(this).height();
			
			//Remove Image Margin
			$(this).addClass('noMargin');
			
			//Add .overlay div before each image.
			$(this).after('<div class="overLay imgNum' + index + '" style="width:' + width + 'px; height:' + height + 'px"></div>');
			
			//Add corner divs to .overLay.
			$(this).prependTo('.imgNum' + index + '');
		}
	
	});
	
	//Add corner overlay divs.
	$('.overLay').append('<div class="cornerTopLeft"></div><div class="cornerTopRight"></div><div class="cornerBottomRight"></div><div class="cornerBottomLeft"></div>');	

});
