$(function() {
	$widescreen = 190;
	var date = new Date();
	date.setTime(date.getTime()+365*24*60*60*1000);

	$('#stream').css('width','576px'); // Thanks, Chrome!
	$('#chat').css('width','510px'); // Thanks, Chrome!
	$('#chat embed').css('width','510px'); // Thanks, Chrome!
	
	if(!$.cookie('coolwit')) {
		$('#stream').attr('src','../rules.html');
		setTimeout(loadstream,77000);
	}
	else loadstream();
	function loadstream() {
		$('#stream').attr('src',$urls['streamframe']);
	}
	
	$('#toggles > div').click(function(e){
		$(this).css('background-position-y',parseInt($(this).css('background-position-y'))+9 % 18);
	});
	$('#aspecttoggle').click(function(e) {
		$('#stream').css('width',parseInt($('#stream').css('width'))+$widescreen);
	    $('#stream').attr('src',$('#stream').attr('src'));
		$('#chat').css('width',parseInt($('#chat').css('width'))-$widescreen);
		$('#chat embed').css('width',parseInt($('#chat embed').css('width'))-$widescreen);
		$widescreen *= -1;
		$.cookie('aspect',$widescreen,{path:'/',expires:date});
	});
	if($.cookie('aspect') && $.cookie('aspect') != $widescreen) $('#aspecttoggle').click();
	
	$('#popoutstream').click(function(e) { window.open($urls['stream'],'popoutstream','width=640,height=360'); });
	$('#popoutchat').click(function(e) { window.open($urls['chat'],'popoutchat','width=640,height=360'); });
	
	$('#background').change(function(){
		$('body').css('background-image','url("../img/bg/'+$('#background option:selected').data('file')+'")');
		$.cookie('bg',$('#background option:selected').attr('value'),{path:'/',expires:date});
	});
	
	function ticker() {
		$('#ticker-content').css('left',$('#ticker').width());
		$.ajax({
			type: "GET",
		    url: "../ticker.txt",
		    cache: false,
		    dataType: "html",
		    success: function(data) {
				$("#ticker-content").empty();
				$.each(data.split("\n"),function(i,item) {
					$('#ticker-content').html($('#ticker-content').html()+item+' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'+
																			'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');			
				});
			},
			complete: function(xhr,status) {
				$("#ticker-content").animate({ left: -$('#ticker-content').width() },Math.max($('#ticker-content').width()/0.05,$('#ticker').width()/0.05),"linear",function(){ setTimeout(ticker,0); });
			}
		});	
	}
	setTimeout(ticker,0); // what


	$.getJSON("index.php",{wplist:true}, function(data) {
		$.each(data.wp,function(i,wp) {
			$('#background').append($('<option>',{'data-file':wp.file,'value':wp.nm,'text':wp.nm}));
		});
		if($.cookie('bg')) {
			$("#background option[value='"+$.cookie('bg')+"']").attr('selected',true);
			$('#background').change();
		}
	});
});
