/**********************/
/********INIT*********/
/*********************/
jQuery('html,body').ajaxError(function()
{
	alert('Ett fel uppstod! Vänligen prova igen!');
	return false;
});
jQuery(window).resize(function()
{
	if(pageManager.last!=null) pageManager.last.goTo('fast');
});

Sdialog.body_overflow="hidden";

pageManager.onLoad=function()
{
	getElement('startWrap').style.display="none";
	jQuery('#startWrap').remove();
	jQuery('#hej_loadWrap').remove();
	jQuery('#tv_loadWrap').remove();
	jQuery('#radio_loadWrap').remove();
	pleppo.hej.startup();
	pleppo.radio.startup();
	pleppo.tv.startup();
	jQuery('#MainWrap .newsWrapper')[0].scrollTop=pleppo.hej.scroller.scrollTop;
	jQuery('.MENU').css({display:'none',visibility:'visible'});
	jQuery('.MENU').fadeIn(1000);
	if(pleppo.logged_in) jQuery('.logoutBox').show();
	document.body.onmousedown=grabNmove.start;
	pleppo.onReady();
	Sdialog.do_open=true;
}

/******************************************************/
/*****************************************************/
var pleppo =
{
	logged_in:false,
	
	onReady:function()
	{
		/*DUMMY*/
	},
	/********************************/
	/********* S C R O L L I N G *********/
	/********************************/

	animatedScroll:function(destX,destY)
	{
		var scroll= new Fx.Scroll(window,{wheelStops:false,
		duration:750,
		transition: Fx.Transitions.Expo.easeInOut,
		offset:{'x':destX,'y':destY}
		}).start(0,0);
		
	},
	
	/*************************/
	/******* L O A D B A R ******/
	/************************/
	
	update_loadbar:function(proc)
	{
		jQuery('.loadProc').html(proc + "%");
		var width=proc + "px";
		jQuery('.loadbar_front').css('width',width);
	},
	
	/******************************************/
	/************ L O G I N  S Y S T E M ************/
	/******************************************/
	
	get_login_page:function()
	{
		return Sdialog.open('ajax.php?do=get_login_page');
	},
	/******/
	login:function(formEl)
	{
		cForm.send(formEl,function(data)
		{
				var error=request_success(data);
				if(error!==false)
				{
					alert(error.response);
				}
				else
				{
					pleppo.logged_in=true;
					pleppo.onLogin();
					jQuery('.logoutBox').show();
				}
		});
	
	},
	/******/
	logout:function(section,id)
	{
		jQuery.get('ajax.php?do=logout',function(data)
		{
			var error=request_success(data);
			if(error!==false) alert(error.response);
			else
			{
				jQuery('.comment_holder').hide();
				if(pleppo.radio.playing) pleppo.radio.update_rating();
				if(pleppo.tv.playing) pleppo.tv.update_rating();
				
				getElement('newsWrap').scrollTop=0;
				jQuery('.logoutBox').hide();
				pleppo.logged_in=false;
			}
		});
	},
	/******/
	onLogin:function()
	{
		Sdialog.close();
	},
	/*******************************************************************/
	
	
	/**********************************************************/
	/********************* H E J - S E C T I O N ********************/
	/**********************************************************/
	hej:
	{	
		scroller:undefined,
		splash_image:false,
		visible:false,
		started:false,
		startup:function()
		{
			if(!this.started)
			{
				this.scroller=new cScroller('newsWrap');
				this.visible=true;
				runAjaxCode('ajax.php?do=hej_startup');
			}
			this.started=true;
		},
		add_splash:function(banner_path)
		{
			this.splash=1;
			this.splash_image=new Image();
			pageManager.loadQueue.run=false;
			this.splash_image.src=banner_path;
			this.splash_image.style.width='610px';
			this.splash_image.style.height='380px';
			this.splash_image.onload=this.splash_onload;
		},
		splash_onload:function()
		{
			pageManager.loadQueue.run=true;
			jQuery('#hejMainWrap').hide();
			jQuery('#Splash_Banner').show();
			getElement('Splash_Banner').appendChild(pleppo.hej.splash_image);
			jQuery('#Splash_Banner').show();
			setTimeout(function(){jQuery('#hejMainWrap').show();jQuery('#Splash_Banner').fadeOut('slow').remove();},3000);
		},
		/**************/
		toggle_comments:function(newsId)
		{
			if(!pleppo.logged_in)
			{
				pleppo.hej.login(newsId);
			}
			else
			{
				var el=getElement('comments_' + newsId);
				if(!el)return false;
				if(el.style.display=="block")
				{
					this.hide_comments(newsId);
				}
				else 
				{
					//jQuery(el).html('<b>Laddar...</b>').show();
					jQuery(el).load("ajax.php?do=get_news_comments&id=" + newsId,function(response)
					{
						if(response=="not_logged_in")
						{
							pleppo.logged_in=false;
							pleppo.hej.toggle_comments(newsId);
							return;
						}
						//jQuery(el).slideDown(500);
						el.style.display="block";
						
						var scroll=jQuery(el).offset().top-jQuery('#newsWrap').offset().top -50;
						if(scroll<0) scroll=0;
						jQuery('#newsWrap').animate({scrollTop: '+=' + scroll + 'px'},500);
						
					});
				}
			}
		},
		/**************/
		reload_comments:function(newsId)
		{
			jQuery('.comment_holder').hide();
			//jQuery('#comments_' + newsId).load("ajax.php?do=get_news_comments&id=" + newsId);
			pleppo.toggle_comments(newsId);
		},
		get_page:function(newsid,page)
		{
			jQuery('#comments_' + newsid).load("ajax.php?do=get_news_comments&id=" + newsid + "&p=" + page,function(response)
			{
				if(response=="not_logged_in")
				{
					pleppo.logged_in=false;
					pleppo.hej.toggle_comments(newsId);
					return;
				}
			});
		},
		/*******************/
		hide_comments:function(newsId)
		{
			var scroll=jQuery('#newsBlock_' + newsId).offset().top-jQuery('#newsWrap').offset().top;
			jQuery('#newsWrap').animate({scrollTop: '+=' + scroll + 'px'},500);
			jQuery('#comments_' + newsId).slideUp(500);
		},
		/***************/
		add_comment:function(formEl,newsid)
		{
			cForm.send(formEl,function(response)
			{
				var ok=true;
				var error=request_success(response);
				if(error!==false)
				{
					alert(error.response);
					if(error.code==false) ok=false;
				}
				if(ok) jQuery('#comments_' + newsid).load('ajax.php?do=get_news_comments&id=' + newsid);
			});
		},
		/************/
		login:function(newsId)
		{
			jQuery('#hejBannerWrap').css('visibility','hidden');
			pleppo.onLogin=function()
			{
				Sdialog.close();
				setTimeout(function(){pleppo.hej.toggle_comments(newsId);},100);
			}
			box=pleppo.get_login_page();
			box.onClose=function()
			{
				jQuery('#hejBannerWrap').css('visibility','visible');
				this.onClose=function(){}
			}
		}
	},//end of hej-object
	
	/*************************************************************/
	/******************* R A D I O  F U N C T I O N S *******************/
	/*************************************************************/
	
	radio:
	{	
		scroller:undefined,
		
		playing:false,
		clip_id:0,
		started:false,
		startup:function()
		{
			if(!this.started)
			{
				this.scroller=new cScroller('radioPlaylist');
				this.started=true;
			}
		},
		get_category:function(catid,highlight)
		{
			var url="ajax.php?do=get_audiocategory&catid=" + catid;
			if(typeof highlight!="undefined") url=url + "&highlight=" + highlight;
			jQuery('#radioPlaylist').load(url,function()
			{
				getElement('radioPlaylist').scrollTop=0;
			}
			);
		},
		get_clip_category:function(clip_id)
		{
			var response=jQuery.ajax({url:'ajax.php?do=get_clip_category&id=' + clip_id,async:false}).responseText;
			
			var error=request_success(response);
			if(error!==false)
			{
				return false;//no need to output any error-message here, if the clip was not found it will be notified elsewhere
			}
			return response;
		},
		/****/
		get_new_clips:function()
		{
			jQuery('#radioPlaylist').load("ajax.php?do=get_new_audioclips",function()
			{
				getElement('radioPlaylist').scrollTop=0;
			});
		},
		/****/
		search:function(formEl)
		{
			var query=cForm.get_query_string(formEl);
			jQuery('#radioPlaylist').load("ajax.php?do=search_audioclips&" + query,function()
			{
				getElement('radioPlaylist').scrollTop=0;
			});
		},
		/****/
		open_player:function(radio_id)
		{
			this.playing=true;
			pleppo.tv.close_player();
			jQuery('#radio_tip_a_friend').html('');
			jQuery.get(
				'ajax.php?do=fetch_media&id='+radio_id,function(response)
				{
					var error=request_success(response);
					if(error!==false)
					{
						jQuery('#radioRateWrap').html(
						"<h1>" + error.response + "</h1>"
						);
					}
					else
					{
						jQuery('#radio_tip_a_friend').html("<a onclick=\"pleppo.radio.tip_a_friend(" + radio_id + ")\">Tipsa en vän!</a>");
						jQuery('#radioPlayerWrap').html(eval(response));
						pleppo.radio.update_rating(radio_id);
					}
				}
			);
		},
		/****/
		highlight:function(audio_link)
		{
			jQuery(".radio_highlight").removeClass("radio_highlight").addClass("audio_clip");
			jQuery(audio_link).removeClass('audio_clip').addClass("radio_highlight");
			
			
		},
		/****/
		close_player:function()
		{
			this.playing=false;
			jQuery(".radio_highlight").removeClass("radio_highlight").addClass("audio_clip");
			jQuery('#radioPlayerWrap').html("");
			jQuery('#radioRateWrap').html("");
			jQuery('#radio_tip_a_friend').html('');
		},
		/****/
		rate_hover:function(num)
		{
			for(var i=0;i<num;i++)
			{
				jQuery("#radioRateStar" + i).attr('src','gfx/redstar_full.gif');
			}
		},
		/****/
		rate_out:function()
		{
			for(var i=0;i<5;i++)
			{
				jQuery("#radioRateStar" + i).attr('src','gfx/redstar_stroke.gif');
			}
		},
		/****/
		update_rating:function(radio_id)
		{
			if(radio_id==undefined) radio_id=this.clip_id;
			jQuery('#radioRateWrap').load('ajax.php?do=get_rating&type=radio&id=' + radio_id)
			this.clip_id=radio_id;
		},
		/****/
		rate:function(radio_id,rating)
		{
			jQuery.get('ajax.php?do=rate&id=' + radio_id + '&rating=' + rating,function()
			{
				pleppo.radio.update_rating(radio_id);
			});
		},
		tip_a_friend:function(radio_id)
		{
			Sdialog.open('ajax.php?do=tip_a_friend&type=audio&id=' + radio_id);
		},
		/****/
		login:function(radio_id)
		{
			pleppo.onLogin=function()
			{
				pleppo.radio.update_rating(radio_id);
				jQuery('.comment_holder').hide();
				Sdialog.close();
			}
			pleppo.get_login_page();
		},
		/********/
		monkey:function()
		{
			soundManager.setVolume('monkey',100);
			soundManager.play('monkey');
		}
		/****/
	},//end of radio object
	
	/***************************************************************************/
	/***************************** T V  F U N C T I O N S ****************************/
	/***************************************************************************/
	tv:
	{
		scroller:undefined,
		playing:false,
		clip_id:0,
		started:false,
		startup:function()
		{
			if(!this.started)
			{
				this.scroller=new cScroller('tvPlaylist');
				this.started=true;
			}
			jQuery('#kekkonen').hide();
		},
		
		open_player:function(video_id)
		{
			pleppo.radio.close_player();
			pleppo.tv.playing=true;
			jQuery('#tv_tip_a_friend').html("");
			jQuery.get(
				'ajax.php?do=fetch_media&id='+video_id,function(response)
				{
					var error=request_success(response);
					if(error!==false)
					{
						jQuery('#videoPlayerWrap').html(
						"<div class='tv_error'>" + error.response + "</div>"
						);
					}
					else
					{
						jQuery('#tv_tip_a_friend').html("<a onclick=\"pleppo.tv.tip_a_friend(" + video_id + ")\">Tipsa en vän!</a>");
						pleppo.tv.update_rating(video_id);
						jQuery('#videoPlayerWrap').html(eval(response));
					}
				}
			);
			
		},
		
		/****/
		highlight:function(video_link)
		{
			jQuery(".tv_highlight").removeClass("tv_highlight").addClass("video_clip");//remove highlight
			jQuery(video_link).removeClass('video_clip').addClass("tv_highlight");
			
		},
		/****/
		close_player:function()
		{
			jQuery(".tv_highlight").removeClass("tv_highlight").addClass("video_clip");//remove highlight
			jQuery("#tvRateWrap").html("");
			jQuery('#videoPlayerWrap').html('');
			jQuery('#tv_tip_a_friend').html("");
			pleppo.tv.playing=false;
		},
		/****/
		rate_hover:function(num)
		{
			for(var i=0;i<num;i++)
			{
				jQuery("#tvRateStar" + i).attr('src','gfx/yellowstar_full.gif');
			}
		},
		/****/
		rate_out:function()
		{
			for(var i=0;i<5;i++)
			{
				jQuery("#tvRateStar" + i).attr('src','gfx/yellowstar_stroke.gif');
			}
		},
		/****/
		update_rating:function(video_id)
		{
			if(video_id==undefined) video_id=this.clip_id;
			jQuery('#tvRateWrap').load('ajax.php?do=get_rating&type=tv&id=' + video_id);
			this.clip_id=video_id;
		},
		/****/
		rate:function(video_id,rating)
		{
			jQuery.get('ajax.php?do=rate&id=' + video_id + '&rating=' + rating,function()
			{
				pleppo.tv.update_rating(video_id);
			});
		},
		/****/
		tip_a_friend:function(video_id)
		{
			jQuery('#videoPlayerWrap').css('visibility','hidden');
			var dialog=Sdialog.open('ajax.php?do=tip_a_friend&type=video&id=' + video_id);
			dialog.onClose=function()
			{
				jQuery('#videoPlayerWrap').css('visibility','visible');
				this.onClose=function(){}
			}
		},
		login:function(video_id)
		{
			jQuery('#videoPlayerWrap').css('visibility','hidden');
			pleppo.onLogin=function()
			{
				pleppo.tv.update_rating(video_id);
				Sdialog.close();
			}
			var box=pleppo.get_login_page();
			box.onClose=function()
			{
				jQuery('#videoPlayerWrap').css('visibility','visible');
				this.onClose=function(){}
			}
		}
		/****/
	},//end of tv-object
	
	
	/************************************************************************/
	/**FUN STUFF**/
	/***********************************************************************/
	shakin_the_house:function(shake,counter,x,y)
	{
		if(typeof counter=="undefined")
		{
			x=getScrollWidth();
			y=getScrollHeight();
			counter=0;
		}
		if(counter<15)
		{
			var scroll= new Fx.Scroll(window,{wheelStops:false,
			duration:30,
			offset:{'x':x+shake,'y':y}
			}).start(0,0);
			shake=-shake;
			counter++;
			setTimeout(function(){pleppo.shakin_the_house(shake,counter,x,y);},50);
		}
		else
		{
		setTimeout(function()
		{
			var scroll= new Fx.Scroll(window,{wheelStops:false,
			duration:50,
			offset:{'x':x,'y':y}
			}).start(0,0);
		},100);
		}
	},
	lights_on:true,
	toggle_light:function()
	{
		if(this.lights_on)
		{
			this.lights_on=false;
			jQuery('#MainWrap').fadeTo('fast',0);
		}
		else
		{
			this.lights_on=true;
			jQuery('#MainWrap').fadeTo('fast',1);
		}
		
	},
	kekkonen_image:new Image(),
	kekkonen_loaded:false,
	kekkonen_visible:false,
	kekkonen:function()
	{
		if(this.kekkonen_visible) 
		{
			jQuery('#kekkonen').fadeOut(500);
			this.kekkonen_visible=false;
		}
		else
		{
			jQuery('#kekkonen').fadeIn(500);
			this.kekkonen_visible=true;
		}

	}
}

