$(document).ready(function() {
	$.loadDataEmbed('home','content/home.txt',parseReleases);
	$.loadDataEmbed('news','content/news.txt',parseNews);
	$.loadDataEmbed('highlight','content/highlight.txt',parseHighlight);
	
	$('#navleft').click(tickerPrevPage);
	$('#navright').click(tickerNextPage);
	$('#tickerbar-list').data('page',0);
	checkPage();
});

var itemsPerPage = 7;

function tickerPrevPage()
{
	var n = $('#tickerbar-list').data('page');
	n = Math.max(0,n - 1);
	$('#tickerbar-list').data('page', n);
	showTicker(Math.min($('#tickerbar-list #tickerbar-item').length - 1, (n + 1) * itemsPerPage - 1));
	checkPage();
}

function tickerNextPage()
{
	var n = $('#tickerbar-list').data('page');
	n =  Math.min($('#tickerbar-list .tickerbar-content-bar-area').length - 1, n + 1);
	$('#tickerbar-list').data('page', n);
	showTicker(n * itemsPerPage);
	checkPage();
}

function checkPage()
{
	var w = $('#tickerbar-wrapper').width();
	var n = $('#tickerbar-list').data('page');
	$('#tickerbar-list').animate({ left : n * -w }, 250);

	if ( n > 0 ) $('#navleft').show();
	else $('#navleft').hide();

	if ( n < $('#tickerbar-list .tickerbar-content-bar-area').length - 1 ) $('#navright').show();
	else $('#navright').hide();
	
}

function parseHighlight(data)
{
	var t = $('#templates #highlight-item');
	var l = $('#highlight-content');
	
	for(var n = 0; n < data.length; n++)
	{
		var d = data[n];
		var i = t.clone();
		l.append(i);
		
		$('#title',i).html(d['title'] || '');
		$('#icon',i).get(0).src = d['img'] || 'img/logo.png';
		$('#artist',i).html(d['artist'] || '');
		$('#date',i).html(d['date'] || '');
		$('#text',i).html(d['text'] || '');
		$('#url',i).attr('href', d['url']);
		
		i.show();
	}
}

function parseNews(data)
{
	var t = $('#templates #news-item');
	var l = $('#newslist');
	l.empty();
	
	for(var i = 0; i < data.length; i++)
	{
		var d = data[i];
		var c = t.clone();
		l.append(c);
		
		var dt = d['date'] || '';
		var da = dt.split(' ');
		
		$('#icon',c).get(0).src = d['img'] || 'img/logo.png';
		$('#title',c).html(d['title'] || 'News Flash!');
		$('#subtitle',c).html(d['subtitle'] || 'Read all about it');
		$('#date',c).html(dt); //da[0] + '<div class="news-item-subdate">' + da[1] + '<br>' + da[2] + ' ' + '</div>');
		$('#text',c).html(d['text'] || '');
		
		if ( !d['subtitle'] ) $('#subtitle',c).hide();
		
		$('#fblike',c).get(0).src = 'http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.mu-nest.com?news=' + escape(d['title']) + '&amp;layout=standard&amp;show_faces=true&amp;width=350&amp;action=like&amp;colorscheme=light';	
	}
}

function parseReleases(data)
{				
	var t = $('#templates #tickerbar-item');
	var l = $('#tickerbar-list');
	var s = $('#templates #ticker-item');

	var showWidth = 240; //Math.max(210, 950 - data.length * 100);
	var showTime = 250;
	l.data('showWidth',showWidth);
	l.data('showTime',showTime);
	l.data('length',data.length);
	
	var w = 0;
	for(var i = 0; i < data.length; i++)
	{
		var d = data[i];
		var c = t.clone();
		if ( i % itemsPerPage == 0 )
		{
			l.append('<div class="tickerbar-content-bar-area" style="width: 850px;"></div>');
			w += 850 + 20;
			l.css('width', w);	
		}
		$('.tickerbar-content-bar-area:last',l).append(c);
		//l.append(c);
		
		$('.tickerbar-content-iconarea',c).data('index',i);
		$('.tickerbar-content.iconarea',c).data('content','abc');
		$('.tickerbar-content-iconarea',c).click(function() { showTicker($(this).data('index')); });
		$('.tickerbar-content-iconarea',c).mouseenter(function() { $(this).addClass('tickerbar-content-iconarea-mouseover'); });
		$('.tickerbar-content-iconarea',c).mouseleave(function() { $(this).removeClass('tickerbar-content-iconarea-mouseover'); });

		// Set Data
		$('.tickerbar-content-icon',c).get(0).src = d['img'];
		$('.tickerbar-content-title',c).html(d['title']);
		$('.tickerbar-content-artist',c).html(d['artist']);
		$('.tickerbar-content-date',c).html(d['date']);
		$('.tickerbar-content-iconnew',c).fixpng();
		if ( d['new'] == 'yes' ) $('.tickerbar-content-iconnew',c).show();
		else $('.tickerbar-content-iconnew',c).hide();
		if ( d['title-font-size'] ) $('.tickerbar-content-title',c).css('font-size', d['title-font-size']);
		if ( d['artist-font-size'] ) $('.tickerbar-content-artist',c).css('font-size', d['artist-font-size']);
		
		// Add ticker
		var r = s.clone();
		$('#tickercontent').append(r);
		$('#background',r).html(d['content-background'] || '');
		$('#title',r).html(d['content-title'] || '');
		$('#subtitle',r).html(d['content-subtitle'] || '');
		$('#text',r).html(d['content-text'] || '');
		
		c.css({opacity: 0});
		c.animate({ opacity: 1}, showTime);
	}

	$(".tickerbar #tickerbar-loading").hide();//showTime);
	$(".tickerbar .tickerbar-content").show();//slideDown(showTime);		
	$(".tickerbar-content-box",l).show();
	$(".tickerbar-content-box",l).css('width',1);
	$(".tickerbar-content-box",l).css('overflow','hidden');
	showTicker(0);
}
	

function showTicker(i)
{
	var l = $('#tickerbar-list');
	var n = $(".tickerbar-content-box:eq(" + i + ")",l);
	var p = $(".tickerbar-content-box",l);
	var c = $("#tickercontent .tickercontent_item");
	var d = $("#tickercontent .tickercontent_item:eq(" + i + ")");

	var showWidth = l.data('showWidth');
	var showTime = l.data('showTime');
	var intervalTime = 10 * 1000;
	
	var a = l.data('interval');
	if ( a ) window.clearTimeout(a);
	a = window.setTimeout(nextTicker,intervalTime);
	l.data('interval',a);
		
	var b = l.data('index');
	if ( b == i ) return;
	l.data('index',i);
	
	p.animate({ width: 1, opacity: 0 },showTime);
	n.stop();
	n.animate({ width: showWidth, opacity: 1 },showTime);
	
	//c.slideUp(showTime);
	c.stop();
	c.slideUp(showTime); //animate({ opacity : 0 }, showTime);
	d.stop();
//	d.css('opacity', 0);
//	d.show();
//	d.animate({ opacity : 1 }, showTime);
	d.slideDown(showTime);
}

function nextTicker()
{
	var l = $('#tickerbar-list');
	var b = l.data('index');
	b++;
	if ( b >= l.data('length') ) b = 0;
	showTicker(b);
	
	var n = $('#tickerbar-list').data('page');
	if ( n != parseInt(b / itemsPerPage) )
	{
		$('#tickerbar-list').data('page', parseInt(b / itemsPerPage));
		checkPage();
	}
}
