
$(document).ready(function()
{
	
	$.ajax({
	    type: "GET",
	    url: "data/news.xml",
	    dataType: "xml",
	    success: parseNews
	});

	$.ajax({
	    type: "GET",
	    url: "data/media.xml",
	    dataType: "xml",
	    success: parseMedia
	 	});

	$.ajax({
	    type: "GET",
	    url: "data/buzz.xml",
	    dataType: "xml",
	    success: parseBuzz
	});	
	
	$.ajax({
	    type: "GET",
	    url: "data/backstage.xml",
	    dataType: "xml",
	    success: parseBackstage
	 });
	
		startApp();
	
});


function parseNews(xml)
{
	
  $(xml).find("item").each(function()
  {
	
	var fileType = $(this).attr("type");
	var url = "/news/" + $(this).attr("uri");
	var title = $(this).find("title").text();
	var image = $(this).find("image").text();
	
	switch ( fileType ) {
		
		case "image" :
			createImageHolder("chained1", "news", image, url, title);
		break;
		
		case "windows" :
			createTextHolder("chained1", "news", title, url);
		break;
		
		case "video" :
			createVideoHolder("chained1", "news", image, url, title);
		break;
		
	}
    
  });

	var api = $("#newsBox").scrollable(); 
	api.reload().next();


};

function parseMedia(xml)
{
	$(xml).find("item").each(function()
  {

	var fileType = $(this).attr("type");
	var url = "/multimedia/" + $(this).attr("uri");
	var title = $(this).find("title").text();
	var image = $(this).find("image").text();

	switch ( fileType ) {

		case "image" :
			createImageHolder("chained3", "media", image, url, title);
		break;

		case "windows" :
			createTextHolder("chained3", "media", title, url, "windows");
		break;

		case "video" :
			createVideoHolder("chained3", "media", image, url, title);
		break;

	}

  });

	var api = $("#multimediaBox").scrollable(); 
	api.reload().next();

};

function parseBuzz (xml ) 
{
	var buzzCounter = 0;

	$(xml).find("item").each(function()
  {

	if(buzzCounter >= 24 ) return false;
	buzzCounter++;
	
	var fileType = "text"//$(this).attr("type");
	var url = "/buzz/";//$(this).find("url").text();
	var title = $(this).find("title").text();
	var image = $(this).find("image").text();
	var source = $(this).attr("type");//$(this).attr("source");
	var sourceType = source;
	
	
	
	if( source != "twitter" && source != "facebook" && source != "windows") sourceType = "generic";

	switch ( fileType ) {

		case "image" :
			createImageHolder("chained2", "buzz", image, url, title);
		break;

		case "windows" :
			createWindowsHolder("chained2", "buzz", title, url, title);
		break;
		
		case "text" :
			createTextHolder("chained2", "buzz", title, url, sourceType, $(this).attr("blogName"), false);
		break;

		case "video" :
			createVideoHolder("chained2", "buzz", image, url, title);
		break;

	}

  });

	var api = $("#buzzBox").scrollable(); 
	api.reload().next();
};

function parseBackstage(xml)
{
	
  $(xml).find("item").each(function()
  {
	
	var fileType = $(this).attr("type");
	var url = $(this).find("url").text();//"/backstage/" + $(this).attr("uri");
	var title = $(this).find("title").text();
	var image = $(this).find("image").text();
	
	switch ( fileType ) {
		
		case "image" :
			createImageHolder("chained4", "backstage", image, url, title);
		break;
		
		case "windows" :
			createTextHolder("chained4", "backstage", title, url);
		break;
		
		case "arrow" :
			createTextHolder("chained4", "backstage", title, url, fileType);
		break;
		
		case "video" :
			createVideoHolder("chained4", "backstage", image, url, title);
		break;
		
	}
    
  });

	var api = $("#backstageBox").scrollable(); 
	api.reload().next();

};


function createImageHolder ($stageID, $type, $imageAddress, $url, $pContent ) 
{
	$('#'+$stageID + ' .items').append("<div class=\"imageBox-" + $type +"\" onclick=\"loadPage('" +$url+ "/');\"><img src='" + $imageAddress + "' /><p>" + $pContent + "</p></div>");
}

function createTextHolder ($stageID, $type, $pContent, $url, $sourceType, $source ) 
{
	var div = "";
	
	if ($sourceType == undefined ) $sourceType == "";
	
	( $sourceType != "twitter" && $sourceType != "facebook" && $sourceType != "windows" && $sourceType != "arrow" && $sourceType != undefined) ? (div = "<div>" + $source + "</div>") : (div = "<div></div>");
	
	if ($pContent.length > 50 ) $pContent = $pContent.substring(0,68) + "...";
	
	$('#'+$stageID + ' .items').append("<div class=\"imageBox-"+ $type + "-copy " + $sourceType + "\" onclick=\"loadPage('" +$url+"');\">" + div + "<p>" + $pContent + "</p></div>");
}

function createNewTextHolder ($stageID, $type, $pContent, $url, $sourceType, $source ) 
{
	var div = "";
	
	if ($sourceType == undefined ) $sourceType == "";
	
	( $sourceType != "twitter" && $sourceType != "facebook" && $sourceType != "windows" && $sourceType != undefined) ? (div = "<div>" + $source + "</div>") : (div = "<div></div>");
	
	if ($pContent.length > 50 ) $pContent = $pContent.substring(0,68) + "...";
	
	$('#'+$stageID + ' .items').append("<div class=\"imageBox-"+ $type + "-copy " + $sourceType + "\" onclick=\"loadPage('/buzz/');\">" + div + "<p>" + $pContent + "</p></div>");
}

function createVideoHolder ($stageID, $type, $imageAddress, $url, $pContent ) 
{
	$('#'+$stageID + ' .items').append("<div class=\"imageBox-" + $type + "-video\" onclick=\"loadPage('" +$url+ "');\"><div></div><img src='" + $imageAddress + "' /><p>" + $pContent + "</p></div>");
}


function trace($string, type) 
{
	if(type == undefined) type = "log";
	if(window.console != undefined )
	window.console[type]($string);
}

function loadPage(uri){document.location.href=uri;}
