// JavaScript Document

$(document).ready(function(){
	initAll();
	
});

function initAll(){
	addClickEvents();
	$("#wrapper").pngFix();
	setUpHeaderSlides();
	setRoundedElements();;
	setUpNews();
	setUpRacingNews();
}

function addClickEvents(){
	/*$('#winTicketsBox').click(function(){
		window.location.href = "motor_racing.html";
	});*/
	
	$('.orangeButton').click(function(){
		window.location = $(this).children("a").attr("href");
	});
	
	$('.blueButton').click(function(){
		window.location = $(this).children("a").attr("href");
	});
}

function initMenu(){
	
	switch(window.location.href){
		case "http...":
			//$(".listItem#default").addClass('selected');
			break;
		
		default:
			//$(".listItem#default").addClass('selected');
			break;
	}
}

function redirectPage(linkLoc) {
	window.location = linkLoc;
}

function setUpHeaderSlides(){
    $('.slideshow').cycle({
		speed: 6000,
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});
}
 
function setRoundedElements(){
	$('#latestNewsInner').corner("round 13px").parent().css('padding', '1px').corner("round 14px")
}

function setUpNews(){
	//Just need an event to load load truncate
	$('.newsDiv').click(function(){
		$('p.article').jTruncate({  
			 length: 60,  
			 minTrail: 0,  
			 moreText: "more",  
			 lessText: "less",  
			 ellipsisText: "...",  
			 moreAni: "slow",  
			 lessAni: "slow"  
		});
	});
	
	$('.newsDiv').html('');
	$.ajax({
		 type: "GET",
		 url: "news.xml",
		 dataType: "xml",
		 success: function(xml) {
			 $(xml).find('newsItem').each(function(){
				var $newsItem = $(this);
				var html = '<h2 class="date orange">' + $newsItem.find('date').text() + '</h2>';
				html += '<p class="article">' + $newsItem.find('article').text() + '</p>';
				$('.newsDiv').append($(html));
			});
			
			//triggered then removed event once fully loaded
			$('.newsDiv').trigger('click');
			$('.newsDiv').unbind('click');
		 }

	 });
}

function setUpRacingNews(){
	//Just need an event to load load truncate
	$('.racingNewsDiv').click(function(){
		$('p.article').jTruncate({  
			 length: 60,  
			 minTrail: 0,  
			 moreText: "more",  
			 lessText: "less",  
			 ellipsisText: "...",  
			 moreAni: "slow",  
			 lessAni: "slow"  
		});
	});
	
	$('.racingNewsDiv').html('');
	$.ajax({
		 type: "GET",
		 url: "racing_news.xml",
		 dataType: "xml",
		 success: function(xml) {
			 $(xml).find('newsItem').each(function(){
				var $newsItem = $(this);
				var html = '<h2 class="date orange">' + $newsItem.find('date').text() + '</h2>';
				html += '<p class="article">' + $newsItem.find('article').text() + '</p>';
				$('.racingNewsDiv').append($(html));
			});
			
			//triggered then removed event once fully loaded
			$('.racingNewsDiv').trigger('click');
			$('.racingNewsDiv').unbind('click');
		 }

	 });
}




