//$(function(){
//jQuery.noConflict();
jQuery(document).ready(function($){
	
	//email spam protection - Example Markup: <span class="email">name[at]domain[dot]com</span>
	$('.email').each(function() {
		var $email = $(this);
		var address = $email.text()
		.replace(/\s*\[at\]\s*/, '@')
		.replace(/\s*\[dot\]\s*/g, '.');
		$email.html('<a href="mailto:' + address + '">'+ address +'</a>');
	});

	$('sup.reg').each(function() {
		var $sup = $(this);
		var registered = $sup.text()
		.replace(/\s*\[r\]\s*/g, '&reg;');
		$sup.html(''+ registered +'');
	});
	
	$('span.copy').each(function() {
		var $copy = $(this);
		var copyright = $copy.text()
		.replace(/\s*\[c\]\s*/g, '&copy;');
		$copy.html(''+ copyright +'');
	});


	//replace html text 'img' with a html image
	if ( $("body#GoogleMapSearch .mapContact table tr td:contains('img'):first").length == 1 ) {
		var img = $("body#GoogleMapSearch .mapContact table tr td:contains('img'):first");
		var htmlImg = "<img src='/_templateFiles/images/clientsImg.png' />"
		img.html(htmlImg);
	}
	//add an attribute to an existing <a> tag
	$("body#GoogleMapSearch .mapContact table tr td small a:contains('View Larger Map'):first").attr('target', '_blank').css('display', 'none').css('visibility', 'hidden');

	//Set mapcontact table Image + iFrame default sizes
	$('body#GoogleMapSearch .mapContact table tr td:first img').css('max-width','160').css('max-height','220');
	$('body#GoogleMapSearch .mapContact table tr td iframe').height(220);

	//Display home page sub-text located under Main Menu
	//if ( $('body#home').length = 1 ) {
	//	$('div#text').load("/home-text.txt");
	//} 


	var partnerpath = location.href.substring(location.href.lastIndexOf('/')+1);//+location.hash;
	if ( partnerpath )
		//$('#buttons a[href$="' + partnerpath + '"]').addClass('selected').parents('ul').css('display','block');
		$('#buttons a[href$="' + partnerpath + '"]').parent().addClass('selected').parent().css('display','block').parent().addClass('selected');

	  

	if ($("#buttons")) {
		//$('#buttons li li:last').addClass('last');
		$('#buttons ul li:last-child').addClass('last');
		//$("#buttons li:has('ul') > a").parent("li").addClass('parent');
		//$("#buttons li:has('ul') > a").parent("li").mouseover(function(){$(this).addClass('parent')});
		//$("#buttons li:has('ul') > a").parent("li").mouseout(function(){$(this).addClass('parent')});

		$('#buttons ul li ul').hide();
		$("#buttons ul li.selected ul").show();

		//$('#buttons li ul li ul').hide();
		//$("#buttons li.selected ul li.selected ul").show();

	}
	//Another piece of jquery similar to above
	//$("#buttons a").filter(function() {
	//		var currentURL = window.location.toString().split("/");
	//			return $(this).attr("href") == currentURL[currentURL.length-1];
	//			}).addClass("active");


	//username
	$('.search').focus(function() {
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('.search').blur(function() {
		if (this.value == ''){
			this.value = 'Enter keywords (e.g. coffee)';
		}
	});

	$('.search_find').focus(function() {
		if (this.value == this.defaultValue){
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	$('.search_find').blur(function() {
		if (this.value == ''){
			this.value = 'Enter postcode...';
		}
	});


	//Expanders
	//single toggle
	$('.expand').next('div').hide();
	//$('.expanded').next('div').show();
	$('.first').click(function() {
		$('.first').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});

	$('.second').click(function() {
		$('.second').toggleClass('expanded').next('div').slideToggle("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.third').click(function() {
		$('.third').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.forth').click(function() {
		$('.forth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.fifth').click(function() {
		$('.fifth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.sixth').click(function() {
		$('.sixth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.seventh').click(function() {
		$('.seventh').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('div.eigth').click(function() {
		$('.eigth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.ninth').click(function() {
		$('.ninth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		$('.tenth').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});
	
	$('.tenth').click(function() {
		$('.tenth').toggleClass('expanded').next('div').slideToggle("slow");
		$('.second').removeClass('expanded').next('div').slideUp("slow");
		$('.third').removeClass('expanded').next('div').slideUp("slow");
		$('.forth').removeClass('expanded').next('div').slideUp("slow");
		$('.fifth').removeClass('expanded').next('div').slideUp("slow");
		$('.sixth').removeClass('expanded').next('div').slideUp("slow");
		$('.seventh').removeClass('expanded').next('div').slideUp("slow");
		$('.eigth').removeClass('expanded').next('div').slideUp("slow");
		$('.ninth').removeClass('expanded').next('div').slideUp("slow");
		$('.first').removeClass('expanded').next('div').slideUp("slow");
		return false;
	});

	//multiple toggle
	$('.expander').next('div').css('width','100%').css('float','left').css('clear','both').hide();
	$('.expander').click(function() {
		$(this).toggleClass('expanded').next('div').slideToggle();
		return false;
	});


	//Background Change + Menu Select (based on query string)
	function getQuerystring(key, default_) {
	  if (default_==null) default_=""; 
	  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
	  var qs = regex.exec(window.location.href);
	  if(qs == null)
	  return default_;
	   else
	  return qs[1];
	  }

	//Australia Background Change	
	var url = window.location.pathname;
	  if (url.indexOf("/australia/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_aus.png) center top no-repeat");
	  }
	  else if (url.indexOf("/aus/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_aus.png) center top no-repeat");
	  }
	  else if (url.indexOf("/au/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_aus.png) center top no-repeat");
	  }
	  
	  else if ( getQuerystring("PageID") == '3549296') {
	  	$("#container").css("background","url(/_templateFiles/images/container_aus.png) center top no-repeat");
	  	$("ul#nav_828201 li:contains('News')").attr('class','selected');
	  }
	  
	  else if (url.indexOf("/united-kingdom/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_uk.png) center top no-repeat");
	  }
	  else if (url.indexOf("/uk/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_uk.png) center top no-repeat");
	  }	  
	  else if (url.indexOf("/gb/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_uk.png) center top no-repeat");
	  }	  
	  
	   else if (url.indexOf("/united-states/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_us.png) center top no-repeat");
	  }
	  else if (url.indexOf("/usa/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_us.png) center top no-repeat");
	  }	  
	  else if (url.indexOf("/us/") > -1) {
		$("#container").css("background","url(/_templateFiles/images/container_us.png) center top no-repeat");
	  }	  
	  
	  else if ( getQuerystring("PageID") == '3549811') {
	  	$("#container").css("background","url(/_templateFiles/images/container.png) center top no-repeat");
	  	$("ul#nav_829402 li:contains('News')").attr('class','selected');
	  }	  
	  else {
		$("#container").css("background","url(/_templateFiles/images/container.png) center top no-repeat");
	  }


	//Remove (AU) & (UK) from Announcements section
	$('ul.newsList li a').each(function() {
		$(this).html( $(this).html().replace(/\s*\(AU\) \s*/g, '') );
		$(this).html( $(this).html().replace(/\s*\(UK\) \s*/g, '') );
		$(this).html( $(this).html().replace(/\s*\(US\) \s*/g, '') );	
		
		//change page titles
		var Title = document.title;
		ChangeTitle(Title);
	});	
	
	$('.announcement-details h1').each(function() {
		$(this).html( $(this).html().replace(/\s*\(AU\) \s*/g, '') );
		$(this).html( $(this).html().replace(/\s*\(UK\) \s*/g, '') );
		$(this).html( $(this).html().replace(/\s*\(US\) \s*/g, '') );
		
		//change page titles
		var Title = document.title;
		ChangeTitle(Title);
	});	

	//Country Switcher.
	//Hide all but first dropdown item.
	$('#CountrySwitcher').click(function(){
		var NewHeight = 63; // $('#CountrySwitcher').height();
		$('#CountrySwitcher div').height(NewHeight);
		
		//Display the dropdown over the nav.
		$('#CountrySwitcher').css('zIndex', '10');
		$('#CountrySwitcherControl').css('zIndex', '10');
		
		$('#CountrySwitcher div span').hover(function(){
			$(this).css('backgroundColor','#eee');
		  }, 
		  function () {
			$(this).css('backgroundColor','#fff');
		  }
		);

		$('#CountrySwitcher div span').click(function(){
			var CountryChosen = $(this).attr('id');			
			switch (CountryChosen)
			{
			case 'au':
				window.location = 'http://cafe2u.com/au/';
			  break;
			case 'uk':
				window.location = ('href','http://cafe2u.com/uk/');
			  break;
			case 'us':
				window.location = ('href','http://cafe2u.com/us/');
			  break;
			}

		});
		
		//Close drop-down.
		/*$('#banner, #bannerUK, #bannerUS').mouseover(function(){
			$('#CountrySwitcher div').height(19);
		});*/
		
		$('#CountrySwitcherControl').mouseleave(function(){
			$('#CountrySwitcher div').height(19);
		});
		
		
		/*$('#bannerUK').mouseover(function(){
			$('#CountrySwitcher div').height(19);
		});
		$('#bannerUS').mouseover(function(){
			$('#CountrySwitcher div').height(19);
		});*/
	});
	
	/* Move awards to the left column. */
	/* Trigger if the page is a partner page. */
	if($('.mapContact').length > 0){
		$('.mapContact .award').each(function(){
			$('.side:first').append(this);
		});
	}
	
	
});


//change page titles
function ChangeTitle(T){
	var NewTitle = T.replace(/\s*\(AU\) \s*/g, '');
	NewTitle = NewTitle.replace(/\s*\(UK\) \s*/g, '');
	document.title = NewTitle; 
}


