$.noConflict();
jQuery(document).ready(function($) {	

	$('#plc_lt_zoneContent_PagePlaceholder_PagePlaceholder_lt_zoneLeft_BizForm_viewBiz_ctl00_RegisterType_0').click(function(){
			if($('#CoffeeShopDetails').css('display') == 'none'){
				$('#CoffeeShopDetails').css({'display':''});
			}
			else
			{
				$('#CoffeeShopDetails').css({'display':'none'});
			}
	});

	var filename = window.location.href.substr(window.location.href.lastIndexOf("/")+1);
	
	var hash = window.location.hash.substr(1);
	if(hash!="")
	{
		filename = filename.substr(0,(filename.length-(hash.length+1)));
	}
	
	if(filename.toLowerCase()=="what-is-uk-coffee-week.aspx")
	{	
		function loadAboutAbout()
		{
			hideOpen('About-OurTrustees');
			hideOpen('About-SteeringCommittee');
			hideOpen('About-Press');
			loadContent('About-About');
		}
		
		function loadOurTrustees()
		{
			hideOpen('About-About');
			hideOpen('About-SteeringCommittee');	
			hideOpen('About-Press');
			loadContent('About-OurTrustees');
		}
		
		function loadSteeringCommittee()
		{
			hideOpen('About-About');
			hideOpen('About-OurTrustees');
			hideOpen('About-Press');
			loadContent('About-SteeringCommittee');
		}
		
		function loadPress()
		{
			hideOpen('About-About');
			hideOpen('About-OurTrustees');
			hideOpen('About-SteeringCommittee');
			loadContent('About-Press');
		}
		
		if(hash!="")
		{
			switch(hash.toLowerCase())
			{
				case "about": loadAbout()
				break;
				case "ourtrustees": loadOurTrustees()
				break;
				case "steeringcommittee": loadSteeringCommittee()
				break;
				case "press": loadPress()
				break;
			}
		}
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-001-subMenu-000').click(function(e){
			loadAboutAbout();
			return false;
		});
	
		$('#ButtonAbout-About').click(function(e){
			loadAboutAbout();
			return false;
		});
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-001-subMenu-001').click(function(e){
			loadOurTrustees();
			return false;
		});
		
		$('#ButtonAbout-OurTrustees').click(function(e){
			loadOurTrustees();
			return false;
		});
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-001-subMenu-002').click(function(e){
			loadSteeringCommittee();
			return false;
		});
		
		$('#ButtonAbout-SteeringCommittee').click(function(e){
			loadSteeringCommittee();
			return false;
		});
		
		$('#ButtonAbout-Press').click(function(e){
			loadPress();			
			return false;
		});
	}
	
	if(filename.toLowerCase()=="project-waterfall.aspx")
	{
		function loadAbout()
		{
			hideOpen('ProjectWF-TheNeed');
			hideOpen('ProjectWF-TheDifferenceItMakes');
			loadContent('ProjectWF-About');
		}
		
		function loadTheNeed()
		{
			hideOpen('ProjectWF-About');
			hideOpen('ProjectWF-TheDifferenceItMakes');
			loadContent('ProjectWF-TheNeed');
		}
		
		function loadTheDifferenceItMakes()
		{
			hideOpen('ProjectWF-About');
			hideOpen('ProjectWF-TheNeed');
			loadContent('ProjectWF-TheDifferenceItMakes');
		}
		
		if(hash!="")
		{
			switch(hash.toLowerCase())
			{
				case "about": loadAbout()
				break;
				case "theneed": loadTheNeed()
				break;
				case "thedifferenceitmakes": loadTheDifferenceItMakes()
				break;
			}
		}
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-002-subMenu-000').click(function(e){
			loadAbout();
			return false;
		});
				
		$('#ButtonProjectWF-About').click(function(){
			loadAbout();
			return false;
		});
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-002-subMenu-001').click(function(e){
			loadTheNeed();
			return false;
		});
		
		$('#ButtonProjectWF-TheNeed').click(function(){
			loadTheNeed();
			return false;
		});
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-002-subMenu-002').click(function(e){
			loadTheDifferenceItMakes();
			return false;
		});
		
		$('#ButtonProjectWF-TheDifferenceItMakes').click(function(){
			loadTheDifferenceItMakes();
			return false;
		});
	}
	
	if(filename.toLowerCase()=="participants.aspx" || filename.toLowerCase()=="participating-venues.aspx" || filename.toLowerCase()=="supporters.aspx" || filename.toLowerCase()=="test.aspx")
	{
		function load2011()
		{
			hideOpen('Participants-2012');
			showLoader('http://www.ukcoffeeweek.com/participants2011.aspx','#ukcwHTML','Participants-2011');
		}
		
		function load2012()
		{
			hideOpen('Participants-2011');
			loadContent('Participants-2012');
		}
		
		function load2012Location(LookupData)
		{
			if($('#2012-'+LookupData).css("display") == "none")
			{
				showLoader('http://www.ukcoffeeweek.com/participants/participating-venues/2012/'+LookupData+'.aspx','#'+LookupData+'HTML','2012-'+LookupData);
			}
			else
			{
				hideOpen('2012-'+LookupData);
			}
		}
		
		if(hash!="")
		{
			switch(hash.toLowerCase())
			{
				case "2011": load2011()
				break;
				case "2012": load2012()
				break;
			}
		}
				
		$('#ButtonParticipants-2011').click(function(){
			load2011();						
			return false;
		});
				
		$('#ButtonParticipants-2012').click(function(){
			load2012();
			return false;
		});
			
		$('#Button-2012-LondonEast').click(function(){
			load2012Location('LondonEast');
			return false;
		});
		
		$('#Button-2012-LondonNorth').click(function(){
			load2012Location('LondonNorth');
			return false;
		});
		
		$('#Button-2012-LondonSouth').click(function(){
			load2012Location('LondonSouth');
			return false;
		});
		
		$('#Button-2012-LondonWest').click(function(){
			load2012Location('LondonWest');
			return false;
		});
		
		$('#Button-2012-LondonCityCentre').click(function(){
			load2012Location('LondonCityCentre');
			return false;
		});
		
		$('#Button-2012-EastMidlands').click(function(){
			load2012Location('EastMidlands');
			return false;
		});

		$('#Button-2012-EastEngland').click(function(){
			load2012Location('EastEngland');
			return false;
		});

		$('#Button-2012-NorthEast').click(function(){
			load2012Location('NorthEast');
			return false;
		});

		$('#Button-2012-NorthWest').click(function(){
			load2012Location('NorthWest');
			return false;
		});

		$('#Button-2012-SouthEast').click(function(){
			load2012Location('SouthEast');
			return false;
		});

		$('#Button-2012-SouthWest').click(function(){
			load2012Location('SouthWest');
			return false;
		});

		$('#Button-2012-WestMidlands').click(function(){
			load2012Location('WestMidlands');
			return false;
		});

		$('#Button-2012-YorkshireHumber').click(function(){
			load2012Location('YorkshireHumber');
			return false;
		});

		$('#Button-2012-NrIreland').click(function(){
			load2012Location('NrIreland');
			return false;
		});

		$('#Button-2012-Scotland').click(function(){
			load2012Location('Scotland');
			return false;
		});

		$('#Button-2012-Wales').click(function(){
			load2012Location('Wales');
			return false;
		});
		
	}
	
	if(filename.toLowerCase()=="get-involved.aspx")
	{
		function loadCoffeeLovers()
		{
			hideOpen('GetInvolved-CoffeeIndustry');
			loadContent('GetInvolved-CoffeeLovers');
		}
		function loadCoffeeIndustry()
		{
			hideOpen('GetInvolved-CoffeeLovers');
			loadContent('GetInvolved-CoffeeIndustry');
		}
		
		if(hash!="")
		{
			switch(hash.toLowerCase())
			{
				case "coffeelovers": loadCoffeeLovers()
				break;
				case "coffeeindustry": loadCoffeeIndustry()
				break;
			}
		}
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-004-subMenu-000').click(function(e){
			loadCoffeeLovers();
			return false;
		});
				
		$('#ButtonGetInvolved-CoffeeLovers').click(function(){
			loadCoffeeLovers();
			return false;
		});
		
		$('#plc_lt_zoneMenu_cmsmenu_menuElem-004-subMenu-001').click(function(e){
			loadCoffeeIndustry();
			return false;
		});
		
		$('#ButtonGetInvolved-CoffeeIndustry').click(function(){
			loadCoffeeIndustry();
			return false;
		});
	}
	
	if(filename.toLowerCase()=="events.aspx")
	{
		function load2011Events()
		{
			hideOpen('Events-FindAnEvent');
			hideOpen('Events-TheBigChallenge');
			hideOpen('Events-CoffeeAtWork');
			hideOpen('Events-OrganiseAnEvent');
			loadContent('Events-2011Events');
		}
		
		function loadFindAnEvent()
		{
			hideOpen('Events-2011Events');
			hideOpen('Events-TheBigChallenge');
			hideOpen('Events-CoffeeAtWork');
			hideOpen('Events-OrganiseAnEvent');
			loadContent('Events-FindAnEvent');
		}
		
		function loadTheBigChallenge()
		{
			hideOpen('Events-2011Events');
			hideOpen('Events-FindAnEvent');
			hideOpen('Events-CoffeeAtWork');
			hideOpen('Events-OrganiseAnEvent');
			loadContent('Events-TheBigChallenge');
		}
		
		function loadCoffeeAtWork()
		{
			hideOpen('Events-2011Events');
			hideOpen('Events-FindAnEvent');
			hideOpen('Events-TheBigChallenge');
			hideOpen('Events-OrganiseAnEvent');
			loadContent('Events-CoffeeAtWork');
		}
		
		function loadOrganiseAnEvent()
		{
			hideOpen('Events-2011Events');
			hideOpen('Events-FindAnEvent');
			hideOpen('Events-TheBigChallenge');
			hideOpen('Events-CoffeeAtWork');
			loadContent('Events-OrganiseAnEvent');
		}
		
		if(hash!="")
		{
			switch(hash.toLowerCase())
			{
				case "2011events": load2011Events()
				break;
				case "findanevent": loadFindAnEvent()
				break;
				case "thebigchallenge": loadTheBigChallenge()
				break;
				case "coffeeatwork": loadCoffeeAtWork()
				break;
				case "organiseanevent": loadOrganiseAnEvent()
				break;
			}
		}
				
		$('#ButtonEvents-2011Events').click(function(){
			load2011Events();
			return false;
		});
		
		$('#ButtonEvents-FindAnEvent').click(function(){
			loadFindAnEvent();
			return false;
		});
		
		$('#ButtonEvents-TheBigChallenge').click(function(){
			loadTheBigChallenge();
			return false;
		});
		
		$('#ButtonEvents-CoffeeAtWork').click(function(){
			loadCoffeeAtWork();
			return false;
		});
		
		$('#ButtonEvents-OrganiseAnEvent').click(function(){
			loadOrganiseAnEvent();
			return false;
		});
	}
	
	function hideOpen(OpenDiv)
	{
		//$('#'+OpenDiv).hide();
		//$('#'+OpenDiv).hide("blind",{},600);
		$('#'+OpenDiv).fadeOut("slow");
		$('#Button'+OpenDiv).attr("src", "/UKCoffeeWeek/images/submenu/Button"+OpenDiv+".png");
	}
	
	function loadContent(OpenDiv)
	{
		$('#Button'+OpenDiv).attr("src", "/UKCoffeeWeek/images/submenu/Button"+OpenDiv+"-Over.png");
		//$('#'+OpenDiv).show();
		//$('#'+OpenDiv).show("blind",{},600);
		//$('#'+OpenDiv).fadeIn("slow",function(){if ($.browser.msie){this.style.removeAttribute('filter');}});
		$('#'+OpenDiv).delay('500').fadeIn("slow",function(){if ($.browser.msie){this.style.removeAttribute('filter');}});
	}
	
	function showLoader(OpenHTML,OpenID,OpenDiv)
	{
		if ($(OpenID).length <= 0){

			$('#loader').remove();
			$('#Button'+OpenDiv).attr("src", "/UKCoffeeWeek/images/submenu/Button"+OpenDiv+"-Over.png");
			setTimeout(function(){
				$('#'+OpenDiv).append('<div align="center"><span id="loader" style="color:#555;">Loading...<br /><img src="/UKCoffeeWeek/loading.gif" alt="" border="0" /></span></div>');
				$('#loader').fadeIn('normal',loadHTMLContentLocal(OpenHTML+' '+OpenID,OpenDiv));
			}, 700);
		}
		else
		{
			$('#Button'+OpenDiv).attr("src", "/UKCoffeeWeek/images/submenu/Button"+OpenDiv+"-Over.png");
			$('#'+OpenDiv).delay('700').fadeIn("slow",function(){if ($.browser.msie){this.style.removeAttribute('filter');}});
		}
	}
	
	function loadHTMLContentLocal(OpenHTML,OpenDiv)
	{
		 $('#'+OpenDiv).load(OpenHTML,'',showNewContent(OpenDiv))   
	}
	
	function showNewContent(OpenDiv) {   
		$('#'+OpenDiv).show();   
	}  
		
	function loadHTMLContentRemote(OpenDiv)
	{
		requestCrossDomain('http://www.ukcoffeeweek.com/participants2011.aspx','ukcwHTML', function(results) {   
		   $('#'+OpenDiv).html("<div align=\"center\">"+results.replace(/<p>/gi,'').replace(/<\/p>/gi,'')+"</div>");
		});
	}
		
	function requestCrossDomain( site, div, callback ) {   
  		// If no url was passed, exit.   
		if ( !site ) {   
			alert('No site was passed.');   
			return false;   
		}   
		  
		// Take the provided url, and add it to a YQL query. Make sure you encode it!   
		var yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from html where url="' + site + '" and xpath=\'//div[@id="'+div+'"]\'') + '&format=xml&callback=?';

		// Request that YSQL string, and run a callback function.   
		// Pass a defined function to prevent cache-busting.   
		$.getJSON( yql, cbFunc ); 

		function cbFunc(data) {   
		// If we have something to work with...   
		if ( data.results[0] ) {
			// Strip out all script tags, for security reasons.   
			// BE VERY CAREFUL. This helps, but we should do more.   
			data = data.results[0].replace(/<script[^>]*>[\s\S]*?<\/script>/gi, '');
	  
			// If the user passed a callback, and it   
			// is a function, call it, and send through the data var.   
			if ( typeof callback === 'function') {   
				callback(data);   
			}
		}   
		// Else, Maybe we requested a site that doesn't exist, and nothing returned.   
		else throw new Error('Nothing returned from getJSON.');   
		}   
	}
});
