var jW = {
	volumeButtons: [],
	volume: 24,
	twCarousel: false,
	updateTwitterNow: false,
themes: [{name: "original jizzradio 2.5",author:"livercake",author_url:"http://livercake.net",src: ""},
					 {name: "progressive",author:"livercake",author_url:"http://livercake.net",src:"theme-progressive.css"},
					 {name: "rollergirl",author:"koneja feat livercake",author_url:"http://koneja.carbonmade.com",src:"theme-rollergirl.css"},
					 {name: "goldensky",author:"livercake",author_url:"http://livercake.net",src:"theme-goldensky.css"},
					 {name: "chroma",author:"livercake",author_url:"http://livercake.net",src:"theme-chroma.css"},
					 {name: "NYC",author:"livercake",author_url:"http://livercake.net",src:"theme-NYC.css"},
					 {name: "minima texturized",author:"livercake",author_url:"http://livercake.net",src:"theme-minima.css"},
					 {name: "elly",author:"livercake",author_url:"http://livercake.net",src:"theme-elly.css"},
					 {name: "in lak ech",author:"livercake",author_url:"http://livercake.net",src:"theme-inlakech.css"}
					 
					 ],
	selectedTheme: 0,	
	defaultTheme: 0,
	videoContainer: "#video",
	unselectable:"#themeswitcher,.content-title,.jizzualizer-title,.tagline,.nowplaying p,.nowplaying h2,.nowplaying h3,.nowplaying,#prendidism ul li,.friends3",
	videoStyles: {on:[
										
										{n:"#jizzualizer",p:[{n:"height",v:"500px"}]},
										{n:"#peaks",p: [{n:"height",v:"67px"},{n:"top",v:"420px"}]},
										{n:".peakElement",p: [{n:"height",v:"39px"}]},
										{n:".nowplaying",p:[{n:"text-align",v:"right"}]},
										{n:".nowplaying h2",p:[{n:"font-size", v:"29px"},{n:"line-height",v: "39px"}]},
										{n:".nowplaying h3",p:[{n:"font-size",v: "13px"}]},
										{n:"#paused",p:[{n:"height",v:"500px"}]},
									//	{n:"#dynamic-content",p:[{n:"top",v:"-95px"}]},
										{n:"#video",p:[{n:"width",v:"100%"},{n:"height",v:"100%"}]}
										
										],off:[
										{n:"#jizzualizer",p:[{n:"height",v:"255px"}]},
										{n:"#peaks",p: [{n:"height",v:"227px"},{n:"top",v:"10px"}]},
										{n:".peakElement",p: [{n:"height",v:"111px"}]},
										{n:".nowplaying",p:[{n:"text-align",v:"left"}]},
										{n:".nowplaying h2",p:[{n:"font-size", v:"45px"},{n:"line-height",v: "57px"}]},
										{n:".nowplaying h3",p:[{n:"font-size",v: "23px"}]},
										{n:"#paused",p:[{n:"height",v:"255px"}]},
										{n:"#dynamic-content",p:[{n:"top",v:"0px"}]}
										
										]},
	setMenu: function()
	{
		$("#controls").hover(
			function(){$(this).animate({height:"115px"},{duration:800,easing:"easeInOutQuart"})},
			function(){$(this).stop(true).animate({height:"50px"},{duration:800,queue: false,easing:"easeOutBack"});}
		);
		$("#themeswitcher").hover(
			function(){$(this).animate({top:"0px"},{duration:800,easing:"easeInOutQuart"})},
			function(){$(this).stop(true).animate({top:"-133px"},{duration:800,queue: false,easing:"easeOutBack"});}
		);

	},
	setVolumeSlide: function()
	{
		_jW = this;
		var inx = 0;
		$("#volume ul").children("li").each(function(a,b){
			_jW.volumeButtons[a] = b;
			$(this).click(function(){
				_jW.setVolume(a);
			});	
			$(this).disableSelection(); 
		})
		$("#volume ul").css({width:"245px",height:"35px"}).bind("drag",function(e){ 
	
		if(inx==0) {
	    $(this).children("li").each(function(){
	        if(this.offsetLeft > e.layerX-15)
	        {  $(this).click(); return false;}
	    });
		}
		inx++;if(inx>3) inx = 0; 
		});
		
	},
	setVolume: function(vol)
	{
		if(vol > 30) vol = 30;
		if(vol < 0) vol = 0;
		for(i=0;i<=this.volumeButtons.length;i++)
		{
			if(i<=vol)
			{
				$(this.volumeButtons[i]).addClass("volume-ON");
			}
			else
			{
				$(this.volumeButtons[i]).removeClass("volume-ON");
			}
		}
		this.volume = vol;
		$(document).trigger("changeVolume",[vol]);
	},
	setJizzualizer: function()
	{
		$(document).bind("jizzualizer",function(event,peakData){
			$("#peakL").css("width",peakData.left*100+"%");
			$("#peakR").css("width",peakData.right*100+"%");
		});
		
		$(".jizzualizercontroller").children("ul")
	},
	setKeyHandler:function()
	{
		_jW = this;
		$(document).keydown(function(e){_jW.processKey(e);});
	},
	processKey:function(e)
	{
	    var key = e.which;
	    switch(key)
	    {
	        case 32:
	        	e.preventDefault();
				$(document).trigger("togglePlay");
	        	break;	
	        case 38:
	        	e.preventDefault();
	        	var vol = this.volume+1;
				this.setVolume(vol);
		    break
		    case 40:
		      	e.preventDefault();
		       	var vol = this.volume-1;
		      	this.setVolume(vol);
		    break;
		    case 76:
		       	e.preventDefault();
		     //  	$("#like").click();
		    break;
		    case 81:
		       	e.preventDefault();
		     //  	this.togglePeak();
		    break;
		    case 67:
		      	e.preventDefault();
		   //    	this.changeTheme();
		    break;
		    case 83:
		       	e.preventDefault();
		   //    	this.changeStream();
		    break;
		    case 86:
		       	e.preventDefault();
		       //	this.toggleVideo();
		    break;
		    case 27:
		      //	$('#shortcuts,#jizzeverywhere').hide();
		    break;
		    
		    default:
		       	//console.log(key);
		    break;
		}	    
	},
	setTitleDataHandler: function()
	{
		$(document).bind("setTitleData",function(event,artist,title){
				$("#artist").fadeOut("500",function(){$(this).html(artist).fadeIn("500")});
				$("#trackname").fadeOut("500",function(){$(this).html(title).fadeIn("500")});
		});
	},
	setPlayerHandlers: function()
	{
		$(document).bind("playing",function(){
			$("#paused").fadeOut("slow");
		});
		$(document).bind("stopping",function(){
			$("#paused").fadeIn("slow");
			$("#peakL,#peakR").animate({width:"0%"},"slow");			
		});
		$(document).bind("isMapple",function(){

		 $("#jizzlogo").remove()
		 $('<video autobuffer poster="images/jizzlogo.png" id="jizzlogo"><source src="'+jP.getPlayingMountURL()+'" /></audio>').css({width:"200px",height:"113px"}).insertBefore($("#header h1"));
		});
		
	},
	setTwCarousel: function()
	{	
		_jW=this;
		if(!this.twCarousel)
		{
			$(".jizzfeed").jCarouselLite({
				btnNext: "#jizzfeed-next a",
				btnPrev: "#jizzfeed-prev a",
				speed: 800,
				auto: 4000,
				circular: true,
				afterEnd: function(){
					if(_jW.updateTwitterNow)
					{
						
						_jW.updateTwitterNow = false;
					}
				
				}
			});
			
						this.twCarousel = true;
		}
	},
	setTwitter: function()
	{
		_jW = this;

		this.updateTwitter();
		$(document).everyTime("180s",function(){ _jW.updateTwitter();; })

	},
	updateTwitter: function()
	{
		_jW = this;
		$.jTwitter("#jizzradio+OR+@jizzradio+OR+jizzradio",24,function(posts){
			
				$("#tweets").children("li").remove().end();
	
				$(posts.results).each(function(){
				
					$("#tweets").append('<li><img alt="'+this.from_user+'" src="'+this.profile_image_url+'"><h1 class="blue">@'+this.from_user+'</h1><p>'+this.text+'</p><span class="timestamp">'+relative_time(this.created_at)+'</span></li>');
					
				});
				
				_jW.setTwCarousel();
			
			});
	},
	setThemeSwitcher: function()
	{
		_jW = this;
		setTheme = this.selectedTheme;
		$(".prevtheme,.nexttheme").click(function(){
			if($(this).attr("class") == "prevtheme")
			{
				if(_jW.selectedTheme-1 < 0)
				{
					setTheme = _jW.themes.length-1;
				}
				else
				{
					setTheme = _jW.selectedTheme-1;
				}
			}
			else
			{
				if(_jW.selectedTheme+1 >= _jW.themes.length)
				{
					setTheme = 0;
				}
				else
				{
					setTheme = _jW.selectedTheme+1;
				}
			}
			$('#themeswitching').fadeIn("slow",function(){ _jW.loadTheme(setTheme); $(this).fadeOut("slow");  })		
		 });
		 this.loadTheme(setTheme);
	},
	loadTheme: function(id)
	{
		_jW = this;
		if($("head").children("link[rel2=theme]").eq(0).attr("href") != this.themes[id].src)
		{
			
			$("head").children("link[rel2=theme]").remove();
			
			this.selectedTheme = id;
			this.saveCookie("selectedTheme",id);
			link=(this.themes[id].author&&this.themes[id].author_url)?'<strong>by <a href="'+this.themes[id].author_url+'">'+this.themes[id].author+'</a></strong>':"";
		
			//$("#themeswitcher h3").children("span").animate({top:"-40px",opacity:"0"},"800",function(){ $(this).html(_jW.themes[id].name+link);  });
			$("#themeswitcher h3").children("span").css("position","relative").animate({top:"-70",opacity:"0"},"800",function(){ $(this).html(_jW.themes[id].name+link);$(this).animate({top:"0",opacity:"1"})  })
			
			if(this.themes[id].src != "")
			{
				css = $("<link>");
				css.attr({
				  rel:  "stylesheet",
				  type: "text/css",
				  href: this.themes[id].src,
				  rel2: "theme"
				});
				
				$("head").append(css);
				
			}
			
			
		}	
	},
	setVideoHandler: function()
	{
		_jW = this;
		$(document).bind("videoMode",function(event,mode){ 
			$(_jW.videoStyles[mode]).each(function(a,b){
			    
			    el = b.n;
			    css = {};  
			    ani = {};  
			    $(b.p).each(function(c,d){
			        
			        if((this.n == "height" || this.n == "top" ) && el != "#paused")
			        {      
			            ani[this.n] = this.v;
			        }
			        else
			        {
			            css[this.n] = this.v;
			        }
			        
			    })
			    $(el).animate(ani);
			    $(el).css(css);
			});
			mode == "on" ? _jW.enableVideo() : _jW.disableVideo();
		});
	},
	enableVideo:function()
	{
		$(this.videoContainer).fadeIn().flowplayer({src:"flowplayer/flowplayer-3.2.2.swf",wmode:"transparent"}, 
      
      { 
          onLoad: function() {this.setVolume(0);} ,
          clip: { 
             url: 'livestream', 
             live: true, 
             provider: 'maxcdn', 
             bufferLength:1,
             onFinish:function(){ this.play();}    
          }, 
       		plugins: 
       		{     
          	controls:null
               ,maxcdn: { 
                 	url: 'flowplayer/flowplayer.rtmp-3.2.1.swf', 
                 	netConnectionUrl: 'rtmp://wow2.cl.mediastre.am:80/jizzradio'
                 } 
          } 
      });

	},
	disableVideo:function()
	{
		$(this.videoContainer).flowplayer(0).stop().close();
		$(this.videoContainer).fadeOut();
	},
	setUnselectable: function(){
		$(this.unselectable).each(function(){ $(this).disableSelection(); })
	},
	saveCookie: function(name,value)
	{
		return $.cookies.test()?$.cookies.set(name,value):false;
	},
	getCookie: function(name)
	{
		return $.cookies.test()?$.cookies.get(name):false;
	},
	loadUserSettings: function()
	{
		this.selectedTheme = (this.getCookie("selectedTheme") || this.defaultTheme);
	},
	init: function()
	{
		this.loadUserSettings();
		this.setUnselectable();
		this.setMenu();
		this.setVolumeSlide();
		this.setJizzualizer();
		this.setKeyHandler();
		this.setVolume(this.volume);
		this.setPlayerHandlers();
		this.setTitleDataHandler();
		this.setTwitter();
		this.setThemeSwitcher();
		this.setVideoHandler();
		
		//$("body").css({"background-image": "url(images/moco.jpg)"})
	//	$(document).trigger("togglePlay");
	}	

	
}

$(document).ready(function(){jW.init();});

