/******************************************************************************/
/*																			  */
/*    This is the Widget Script of f*ckRGB. Its a bag of surprises            */
/*    -------------------------------------------------------------------	  */
/*    Code copyright by Simon Jentsch 2009									  */
/*																			  */
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

$(document).ready(function() {
		
	/* Click Function to open Widget Chooser Window */
	$('#widgets').click(function() {
		addWindow("widgetChooser", 70, 200, "fuckRGB Widgets", function(){
			$("#window_widgetChooser").find('.windowContent').append("<ul class='widgetMenu'></ul>");
		
			/* Last.fm Radio Widget */
			$("#window_widgetChooser").find('.windowContent ul').append('<li><a href="#" class="widgetNavi" name="widget_lastfmradio">Last.fm Radio</a></li>');

			/* Twitter RSS Feeds */
			$("#window_widgetChooser").find('.windowContent ul').append('<li><a href="#" class="widgetNavi" name="widget_twitter">Follow us on Twitter</a></li>');
			
			/* Last.fm RSS Feeds */
			$("#window_widgetChooser").find('.windowContent ul').append('<li><a href="#" class="widgetNavi" name="widget_recenttracks">Zuletzt gehört</a></li>');
			
		
			/* Click Function to open Widgets */
			$(".widgetNavi").click(function() {
				  openWidget($(this).attr("name"));			
			});
		});
		$('#window_widgetChooser').css('top',100);
		return false;
	});
		
	
	
	
	/* Add Widgets */
	function openWidget(widgetName, mode) {
		
		if (mode == undefined) { mode = "new"; }
		
		if (mode == "new") {
			createCookie(widgetName,'open',375);
			createCookie(widgetName+'_posx','30',375);
			createCookie(widgetName+'_posy','100',375);
		} 
		 		
		switch (widgetName) {
		
		    case "widget_lastfmradio":	
				addWindow("widget_lastfmradio", 186, 204, "fuckrgb Last.fm Radio",function(){
					$("#window_widget_lastfmradio").find('.windowContent').append('<menu class="selectName"><li><a href="#" id="reen_lastfmradio" class="select">René</a></li><li><a href="#" id="simon_lastfmradio">Simon</a></li><li><a href="#" id="david_lastfmradio">David</a></li></menu><div id="widget_lastfmRadioBox"></div>');
					$("#widget_lastfmRadioBox").load("/wp-content/themes/fuckrgb/widgets/lastfmradio.php?radio=rene");
					
					$('#reen_lastfmradio').click(function(){
						$("#widget_lastfmRadioBox").load("/wp-content/themes/fuckrgb/widgets/lastfmradio.php?radio=rene");
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						return false;
					});
					
					$('#simon_lastfmradio').click(function(){
						$("#widget_lastfmRadioBox").load("/wp-content/themes/fuckrgb/widgets/lastfmradio.php?radio=simon");
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						return false;
					});
					
					$('#david_lastfmradio').click(function(){
						$("#widget_lastfmRadioBox").load("/wp-content/themes/fuckrgb/widgets/lastfmradio.php?radio=david");
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						return false;
					});
					
				});	
			break;
						
			case "widget_twitter":
				addWindow("widget_twitter", 280, 204, "Folow us on Twitter",function(){
					$("#window_widget_twitter").find('.windowContent').append('<menu class="selectName"><li><a href="#" id="reen_twitter" class="select">René</a></li><li><a href="#" id="simon_twitter">Simon</a></li><li><a href="#" id="david_twitter">David</a></li></menu><div id="widget_twitterBox"></div>');
					
					$("#widget_twitterBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://twitter.com/statuses/user_timeline/93701174.rss");
					
					$('#david_twitter').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_twitterBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://twitter.com/statuses/user_timeline/16984113.rss");
						return false;
					});
					
					$('#reen_twitter').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_twitterBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://twitter.com/statuses/user_timeline/93701174.rss");
						return false;
					});
					
					$('#simon_twitter').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_twitterBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://twitter.com/statuses/user_timeline/92997450.rss");
						return false;
					});
				});			
			break;
			
			case "widget_recenttracks":
				addWindow("widget_recenttracks", 280, 204, "Zuletzt auf last.fm",function(){
					$("#window_widget_recenttracks").find('.windowContent').append('<menu class="selectName"><li><a href="#" id="reen_recenttracks" class="select">René</a></li><li><a href="#" id="simon_recenttracks">Simon</a></li><li><a href="#" id="david_recenttracks">David</a></li></menu><div id="widget_recenttracksBox"></div>');
					
					$("#widget_recenttracksBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://ws.audioscrobbler.com/1.0/user/reenlovelauli/recenttracks.rss");
					
					$('#david_recenttracks').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_recenttracksBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://ws.audioscrobbler.com/1.0/user/rngstrm/recenttracks.rss");
						return false;
					});
					
					$('#reen_recenttracks').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_recenttracksBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://ws.audioscrobbler.com/1.0/user/reenlovelauli/recenttracks.rss");
						return false;
					});
					
					$('#simon_recenttracks').click(function(){
						$(this).parent().parent().find('a').removeClass('select');
						$(this).addClass('select');
						$("#widget_recenttracksBox").load("/wp-content/themes/fuckrgb/widgets/getrss.php?rssfile=http://ws.audioscrobbler.com/1.0/user/sYmnn/recenttracks.rss");
						return false;
					});
				});			
			break;		
			
		}
		
		if (mode == "cookie") {
			$("#window_"+widgetName).css('left',readCookie(widgetName+'_posx'));
			$("#window_"+widgetName).css('top',readCookie(widgetName+'_posy'));
		}
		
	}
	
	
	
	
	
	/* Open Windows on DOMready if cookie is set to "open" */
	
	/* Last.fm Radio Simon */
	
	if (readCookie('widget_lastfmradio') == 'open') {
		openWidget('widget_lastfmradio','cookie');	
	}
	
	/* Twitter */
	if (readCookie('widget_twitter') == 'open') {
		openWidget('widget_twitter','cookie');
	}
	
	/* Twitter */
	if (readCookie('widget_recenttracks') == 'open') {
		openWidget('widget_recenttracks','cookie');
	}
		
	
	
	/* Adds a new Window */
	function addWindow(windowName, windowHeight, windowWidth, windowTitle, callBack) {
		
		if ($('#window_'+windowName).html() == null) {
			$("#allesBox").before('<div class="window" id="window_'+windowName+'"><div class="dragBar"><span>'+windowTitle+'</span><div><a href="#" class="windowCloser">Close Window</a></div></div><div class="windowContent"></div><div class="roundBottom"></div></div>');
			$("#window_"+windowName).css('opacity', 0);
			$("#window_"+windowName).css('width', 0);
			$("#window_"+windowName).css('height', 0);
			$("#window_"+windowName).animate({opacity: 1, width: windowWidth, height: windowHeight+46});
			$("#window_"+windowName).find('.windowContent').animate({width: windowWidth-20, height: windowHeight-20}, callBack);
			initWindow();
		}
	}
	
	/* Initialize Window (if a new is created) */
	function initWindow() {
		$('.window').draggable({ handle: '.dragBar', stop: function(event, ui) {
			var attrname = $(this).attr("id").substring(7,$(this).attr("id").length);
			createCookie(attrname+'_posx',$(this).css('left'),375);
			createCookie(attrname+'_posy',$(this).css('top'),375);
		}});
		$('.windowCloser').click(function(){
			$(this).parent().parent().parent().animate({width: 0, height: 0, opacity: 0},function(){ 
				var attrname = $(this).attr("id").substring(7,$(this).attr("id").length);
				eraseCookie(attrname); 
				eraseCookie(attrname+"_posx");
				eraseCookie(attrname+"_posy");  
				$(this).remove(); 
			});
			return false;
		});
	}
	
	
	
	/* Cookie Functions */
	
	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

	function eraseCookie(name) {
		createCookie(name,"",-1);
	}
	
	
	
	
	
});	