/* Worthing Leisure Core Functions 

	Release 0.1a

	VJKL / September 2008
*/

	$(document).ready(function(){
		initTransmission();
	});

	this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$("span.tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$("span.tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
	};

	var initTransmission=function(){
		stripeTables();
		sortoutPopUnders();
		tooltip();

		/* process whether the anchor tag in the href is one for a popunder heading therefore jump down to it and expand it */
		var url=document.location.href.split('#')[1], url_plain;
		if(url!=null && url.split('-')[1].toLowerCase()=='heading'){
			url_plain=url.split('-')[0];

			$('#'+url).css({backgroundImage: 'url(/worthingleisure/styles/rsrc/down-arrow-popunder.png)'});
			$('#'+url_plain).show();
			$('#'+url_plain).addClass('popUpShowing');
			$('#'+url).addClass('popUpShowingHeading');

			$('#'+url_plain+'-popunder-container').addClass('popunder-container-selected');
		}
	}


	var sortoutPopUnders=function(){
			$('.popunder-heading').bind('click',function(){
			// first clear the currently visible headings if there are any

				var whatWantsAttention=$(this).attr('id')

				if($('#'+whatWantsAttention.split('-')[0]).css('display')=='none'){

					

					$('.popunder-container-selected').removeClass('popunder-container-selected');
					$('.popUpShowing').hide();
					$('.popUpShowingHeading').css({backgroundImage: 'url(/worthingleisure/styles/rsrc/left-arrow-popunder.png)'});
					$('.popUpShowing').removeClass('popUpShowing');

					$('#'+whatWantsAttention).css({backgroundImage: 'url(/worthingleisure/styles/rsrc/down-arrow-popunder.png)'});
					$('#'+whatWantsAttention.split('-')[0]).fadeIn(250);
					$('#'+whatWantsAttention.split('-')[0]).addClass('popUpShowing');
					$('#'+whatWantsAttention).addClass('popUpShowingHeading');

					$('#'+whatWantsAttention.split('-')[0]+'-popunder-container').addClass('popunder-container-selected');
				}
				else{
					$('#'+whatWantsAttention.split('-')[0]).hide();
					$('#'+whatWantsAttention).css({backgroundImage: 'url(/worthingleisure/styles/rsrc/left-arrow-popunder.png)'});
					$('#'+whatWantsAttention.split('-')[0]).removeClass('popUpShowing');
					$('#'+whatWantsAttention).removeClass('popUpShowingHeading');

					$('#'+whatWantsAttention.split('-')[0]+'-popunder-container').removeClass('popunder-container-selected');
				}


				return false;
			});
	}

	var stripeTables=function(){
		$('table.worthingTable tbody tr:not([th]):odd').addClass('odd');
		$('table.worthingTable tbody tr:not([th]):even').addClass('even');
		$('table.worthingTable tbody tr td').addClass('borderCell');
		
		$('table.worthingTable').each(function(){
			$(this).attr('cellspacing','0');
		});
	};

	var loadTicker=function(whichTicker){

		var defaultsFile = '/worthingleisure/xml/ticker/ticker.xml';
		var site;

		$.ajax({
				url:	defaultsFile,
				cache:	false,
				async:	true, /* Do not get file asynchronously or the page will have no gadgets */
				success: function(xmlData){	
						
						$(xmlData).find('site').each(function(){
							site=this;
							if($(site).attr('where')==whichTicker){

								$(site).find('message').each(function(){
									if($(this).find('text').text() != ''){
										if($(this).find('link').text() == ''){
											$('#wl-ticker').append($(this).find('text').text()+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
										}else{
											$('#wl-ticker').append('<a href="'+$(this).find('link').text()+'">'+$(this).find('text').text()+'</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
										}
									}

								});
							}
							
						});
					},
					error: function(){
						alert('Sorry could not find ticker tape file');
					}
			});	
	};