//$(function(){
//jQuery.noConflict();
// JavaScript Documentwindow.onload = function() {
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 a[href$="' + URL + '"]').parent('li').addClass('selected');
		$('#topNav a[href$="' + URL + '"]').parent('li').addClass('selected');
		
		//Keep CBT > Course Descriptions menu links selected
		var oCourses = window.location.pathname;
		if (oCourses.indexOf("/course-descriptions/") > -1){
			$('#menu li.cbt').addClass('selected');
			$('#menu li.cbt ul li a:contains("Course Descriptions")').parent('li').addClass('selected');
		}

	}
	//Keep Course Descriptions Menu Selected
	// Horizontal subnav (selected) menu rollover effect
	var oMenu = $('#menu').find('ul:first').children('li');
	var oSelected = null;
	oMenu.hover( function() {
		oSelected = $('#menu').find('ul:first').children('li.selected');
		oSelected.removeClass('selected selected selected');
		$(this).addClass('selected');
	}, function() {
		$(this).removeClass('selected selected selected');
		oSelected.addClass('selected');
	});	
	//Loop through all the lists in a specified area and add span tags before/after specified tags
	var oMenu = $('#menu,#topNav').find('ul');
	oMenu.children().each(function(){
		//$(this).find('a:first').html('<span>' + $(this).find('a:first').html() + '</span>');
	});
	//Remove bottom image from SubMenu
	var oSubMenu = $('#menu ul').find('ul:first');
	oSubMenu.each(function(){
		//$(this).children('li:last').find('a:first').css('background-image','none');
	});	
	//Loop through SubNav and update menu items
	var oSubNav = $('#subNav').find('ul:first');
	oSubNav.each(function(){
		//Add classes to first and last SubMenu items	
		//$(this).children('li:first').attr('class','firstNav');
		//$(this).children('li:last').attr('class','lastNav');
		//Add rounded corners to SubMenu
		//$(this).prepend('<li class="topNav"></li>');
		//$(this).append('<li class="bottomNav"></li>');
	});
	

	//Footer Menu Styling
	//Keep menu ID's unique
	var nFooter = '-footer';
	var footerID = $('#footer').find('[id*="1060187"]');
	footerID.each(function(){
		var footerAttr = $(this).attr('id');
		$(this).attr('id',footerAttr+nFooter);
	});
	
	//Re-arrange menu items
	$('#footer').each(function(){
		var toolsWidth = $('#footer li.tools').outerWidth();
		var toolsHeight = $('#footer li.tools').outerHeight();
		if (toolsHeight > 66){
			$(this).find('li.resources').css({'margin':'0px 13px 0px 0px'})
			$(this).find('li.blog').css({'margin':'0px 0px 16px'});
		} else {
			$(this).find('li.resources').css({'margin-top':toolsHeight});
		}
	});

		
	//Select Required Menu's based on Visible H1 ==== same also also exists within booking.js for general page backup
		if($('h1.booking-title.demo').is(':visible')){
				var oCBT = $('#menu ul').find('li.cbt');
				oCBT.addClass('selected');
				oCBT.find('ul li:contains("Demo Sessions")').addClass('selected');
		}
		else if($('h1.booking-title.public').is(':visible')){
				var oCBT = $('#menu ul').find('li.cbt');
				oCBT.addClass('selected');
				oCBT.find('ul li:contains("Public Courses")').addClass('selected');
		}


	//Horzontal Rules
	$('hr').css('display','none').css('visibility','hidden').wrap('<div class="hr"></div>');


	//Toggle Open / Close Buttons on FAQs Page
	$(".faq-question a").click (function() {
		$(this).parent(".faq-question").toggleClass("close");
		return false;
	});

	
	//Multiple toggle
	/*$('.expander').next('div').hide();
	$('.expander').click(function() {
		$(this).next('div').slideToggle(0);
		return false;
	});*/
		
	//Single Toggle - H3 + Div
	$('.expander').next('.inner').hide();
	$('.expander').next('.current').show();
	
	$('.expander').click(function() {
		if ( $(this).next('.inner').hasClass('current') ) {
			//all other Expanders
			$('.expander').next('.inner').removeClass('current').hide(400);
			$('.expander').parent('li').css('border','1px solid red');
			//this Expander
			$(this).next('.inner').removeClass('current').hide(400);
			$(this).parent('li').css('border','1px solid red');
		}
		else {
			//all other Expanders
			$('.expander').next('.inner').removeClass('current').hide(400);
			$('.expander').parent('li').css('border','1px solid red');
			//this Expander
			$(this).next('.inner').addClass('current').show(400);
			$(this).parent('li').css('border','1px solid green');
		}
		return false;
	});

	
	//Remove bottom border from last 	
	$('.booking-grouping').each(function(){
		if ($(this).find(".course-list").length >= 1) {
			$(this).find('.booking-grouping-footer').css('margin-top','0px');	
		}
	});

	
	//Remove background from last section with class
	/*if  ( $("ul.accordion li").length > 1 ) {
		$("ul.accordion li:last").addClass('last');
	}*/


	// Add background to Search input area on focus
	$('#topNav #search input.search').focus(function(){
			$(this).css({'background':'transparent url("/_templateFiles/images/bg-right.png") 0px 0px repeat'});
		}).blur(function(){
			$(this).removeAttr('style');
	});


	/*
		The if statement below checks to see if the current captcha is the one on the booking form.
		It has been disabled in that form because it is conflicting with the validation
	
	*/
	
	
	// Control Form Fields Focus and Blur Colors
	$('.customForm').find('input[type="text"], textarea').each(function(){
		var oInput = $(this).val();
		$(this).focus(function(){
			$(this).css('color','#00bac0');
			//if($(this).val() != oInput) 
			//{
			//	$(this).css('color','#00bac0');
			//} else {$(this).css('color','#666666');}
		}).blur(function(){
			if($.trim($(this).val()) == oInput) 
			{
				$(this).css('color','#666666');
			} else {$(this).css('color','#00bac0');}
			//$(this).css('color','#666666');			
		});
	});
	
	
	if($('#form-booking #CaptchaV2').length == 0){
		
		// Set CaptchaV2 input default value
		var NewsletterCaptchaV2 = "Confirm the text above";	
		
		$("input#CaptchaV2").not('#form-booking input#CaptchaV2').val(NewsletterCaptchaV2).focus(function(){
			if($.trim($(this).val()) == 'Confirm the text above')
			{
				$(this).val('');
			}
		}).blur(function(){
			if($(this).val() == '') 
			{
				$(this).val(NewsletterCaptchaV2);
			}
		});
	}
	
	


	//Remove 'a' tag if there is no href
	$("a").each(function() {
		var hRefer = $(this).attr("href");
		if ( typeof hRefer == 'string' && ( $.trim(hRefer) == '' || $.trim(hRefer).indexOf('tag_') != -1 ) || $.trim(hRefer) == '#' ) { // or anything else you want to remove...
			$(this).removeAttr('href').removeAttr('target');
		}
	});


	// Limit the number of characters to display + Implement href value into main a href for Calendar Bookings
	$('.cEvent').each(function(){
	//	var $limit = 75;		
	//	var oLimit = $(this).find('.cInfo h3').html();	
	//	if( $('body#home').length > 0 && oLimit && oLimit.length > 75 )
	//	{
	//		var $strtemp = oLimit.substr(0,$limit); // Get the visible part of the string
	//		oLimit = $strtemp + '...' + '<span style="display:none;">' + oLimit.substr($limit,oLimit.length) + '</span>'; 
	//		$(this).find('.cInfo h3').html(oLimit);	
	//	}
		
		// Add link to Calender Booking List section
		var cLink = $(this).find('.cInfo a').attr('href');
		$(this).find('.cDate').attr('href',cLink);
	});

	
	//Remove 'Booking Sections' if there is no booking(s)
	$('.booking-grouping').each(function(){	
		if ( $(this).filter(':contains("No items found.")').length > 0 ) {
			$(this).html($(this).html().replace('No items found.',''));
	//		$(this).hide();
		}
	});

	
	// Add target to a tags in Newsletters list
	//$('.sNewsletter').each(function(){
	//	$(this).find('a').attr('target','_blank');
	//});

	
	// Remove '_' from Breadcrumbs name
	var oBreadcrumbs = $('#breadcrumbs').find('a');
	oBreadcrumbs.each(function(){
		if ( $('#breadcrumbs').find('a:contains("_")') ) {
			//if(oBreadcrumbs.length > 0) {
				//$(this).html($(this).html().replace(/[_]/g,""));
				$(this).html($(this).html().replace(/\_/g,''));
			//}
		}
	});

	var oSearchResult = $('.search-result h3').find('a');
	oSearchResult.each(function(){
		if ( $('.search-result h3').find('a:contains("_")') ) {
			//if(oSearchResult.length > 0) {
				//$(this).html($(this).html().replace(/[_]/g,""));
				$(this).html($(this).html().replace(/\_/gi,''));
			//}
		}	
	});	


	// Remove 'DIV' tag if there is no text
	//$(".listIntro").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();
	//	}
	//});
	//$(".commImg").each(function() {
	//	var oCommImg = $(this).html();
	//	if( oCommImg == '' ) { // or anything else you want to remove...
	//		$(this).hide();
	//	}
	//});


	// Find which browser and apply correct script
	$(function() {
		if ( $.browser.msie && $('.partners img').length > 0 ) {
			//$('#banner').prepend('<div id="sNav">')
			$('.partners')
			.cycle({
				fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				//next: 'a.link-next', 
				//prev: 'a.link-prev',
				speed: 0,
				//pager: '#sNav',
				cleartypeNoBg: true
			});
			//Number navigation ID
			//$('#sNav').find('a').each(function($key,$value){
			//	$(this).attr('id','num' + ($key + 1));
			//});
			
		}
		else if ( $('.partners img').length > 0 ) {
			//$('#banner').prepend('<div id="sNav">')
			$('.partners')
			.cycle({
				fx: 'fade', // choose your transition type, ex: fade, scrollUp, shuffle, etc...
				//next: 'a.link-next', 
				//prev: 'a.link-prev',
				//pager: '#sNav',
				speed: 1000,
				cleartypeNoBg: true
			});
			//Number navigation ID
			//$('#sNav').find('a').each(function($key,$value){
			//	$(this).attr('id','num' + ($key + 1));
			//});
		  }	  
	 });

/*
	// If an 'alt' tag attribute exists within an image make a 'title' tag attribute with the same value
	$('img').each(function() {
		var altTag = $(this).attr("alt");
		var titleTag = $(this).attr("title");
		if(altTag && altTag.length > 0 && titleTag.length == 0) {
			$(this).attr('title',altTag);
		}
		if(titleTag && titleTag.length > 0 && altTag.length == 0) {
			$(this).attr('alt',titleTag);
		}
	});
 
 
// 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>');
 

	// Text shadow effect
	/*var textshadow = $(".shadow"); //replace with your own!
	textshadow.prepend("<em></em> ");
	textshadow.each(function() { var dupeText = $(this).text(); $(this).find("em").text(dupeText); });*/
	
	
	// Customise Dropdown	
	/*if ( $('.cat_dropdown') ) {
		$('.cat_dropdown').selectbox();
	}*/


	/*$('#latest-issue').find('li').each(function(){
		var oDiv = $(this).text();
		if ( typeof oDiv == 'string' && ( $.trim(oDiv) == '' || $.trim(oDiv).toLowerCase() == 'no items found' ) ) { // or anything else you want to remove...
			$(this).hide();
		}
	});

	var oLatest = $('#latest-issue').find('li');
	oLatest.each(function(){
		var oDiv = $(this).text();
		if( $.trim(oDiv).toLowerCase().indexOf('no items found') !== -1 ) {
			$(this).hide();
		}
	});


	// NO Background or Border to Last Item(s)
	$(':last').addClass('noBG noBorder');

	
	//Find the text within the h1 and wrap a span around the first word
	var oBlogh1 = $('.blog-container').find('h1.bTitle');
	oBlogh1.html(oBlogh1.text().replace('Radar','<span>Radar</span>'));
	
	if ( $('.trackbacks').length > 0 ) {
			$('#catblogoutput').next('.hr').css('background','none');
	}
	
*/	
	

	// Australia Map Hover
	var oMenu = $('#ausMap').find('a');
	oMenu.each(function(){
	$(this).hover( function() {
			$(this).fadeIn('slow');
		}, function() {
			$(this).fadeOut('slow');
		});
	
	});

	
	// Customer Logged In OR Logged Out
	if (loggedIn) {
		$(".logged-in").show();
		$(".logged-out").hide();
		// Update all URLS to become SSL if loggedIn
			//var anchors = $("a[href*='communicorpgroup.com.au'], a[href*='communicorpgroup.prettypollution'], a[href^='/']");
			$("a[href*='communicorpgroup.prettypollution.com'], a[href*='communicorpgroup.com'], a[href^='/']").each(function() {
				if (!$(this).hasClass("logout-link")) {
					//this.href = this.href.replace(/^https?:\/\/[\w.]*\.(com|org)\.*a*u*/, "https://communicorpgroup.worldsecuresystems.com");
					this.href = this.href.replace(/^http:\/\/[\w.]*\.com\.*a*u*/, "https://communicorpgroup.worldsecuresystems.com");
				}
			});			
	} else {
		$(".logged-in").hide();
		$(".logged-out").show();
		if($('body#logout-page').length != 0)
		{
			$("a[href*='https://communicorpgroup.worldsecuresystems.com'], a[href^='/']").each(function() {
				//this.href = this.href.replace(/^https*\:\/\/[\w.]*\.(com|org)]\.*a*u*/, "http://www.communicorpgroup.com");				
				this.href = this.href.replace(/^https*\:\/\/[\w.]*\.com\.*a*u*/, "http://www.communicorpgroup.com");			
			});	
		}
	}
	
		
});

