/*********************************
Javascript Document for Kenny BrandMuse
©2008 cisforculture. All rights reserved. Forget it, we'd sue!!!
Scope: Home Page

************
META-DATA
************
@ date created :: 18 Apr 2008
@ last modified :: 18 Apr 2008
@ description :: Contains client-side logic for the home page :)
*/

$(document).ready(function(){
	
	//XMLHttpRequest object
	var xhr = null;
	
	//Create swf object
	var wmode = "transparent";
	var swf = new SWFObject("assets/swf/splash.swf","splash","305","440","8");

	swf.addParam("wmode",wmode);
	swf.addVariable("link","/pages/club.orange.php");
	
	//Place inside page
	swf.write("flashcontent");
	
	//Instantiate new XMLHttpRequest Object
	xhr = new XMLHttpRequest();
	xhr.open("POST","/extensions/notice-board.php?cb="+Math.random(),true);
	
	if(xhr.overrideMimeType)
		xhr.setRequestHeader("Connection","close");
		
	xhr.onreadystatechange = function(){
		if(xhr.readyState == 4){
			//var obj = eval("("+xhr.responseText+")");
			var obj = xhr.responseText;
			onNoticeBoardResult(obj);
			
			//Avoid memory leaks
			xhr = null;
		}
		
	}
	xhr.send();
	$(".notice-board-links").html("Loading latest items...");
	
	//XMLHttpRequest result handler
	function onNoticeBoardResult(obj){
		
		var items =[], i;
		var b = xml2json.parser(obj);

		b = b.news;
		
		if(b.item instanceof Array){
			for(i=0;i<b.item.length;i++)
				items.push({title:b.item[i].title,id:b.item[i].id});
		}else{
			items.push({title:b.item.title,id:b.item.id});
		}
		
		//console.dir(items);
		displayHeadlines(items);
		
	}
	
	//Displays the news headlines
	function displayHeadlines(a){
		
		var total = a.length;
		var currentIndex = 0;
		
		(function transit(){
			
			$(".notice-board-links").fadeOut("1",function(){
			
				if(total){
					//alert("Current Index: "+index);
					var title = decodeURIComponent(a[currentIndex].title);
					var link = decodeURIComponent(a[currentIndex].id);
					
					$(".notice-board-links").html("<a href='/pages/view.notice.board.php?uid="+link+"'>"+title+"</a>");
				}
			});
			
			$(".notice-board-links").fadeIn("1");
			
			currentIndex++;
			
			if(currentIndex > total - 1)
				currentIndex = 0;
			
			setTimeout(transit,5000);
		})();
	}
	
});