var $p  =
{
	height: 400,
	items: {},
	slide: function(){return $("#thumb-items");},
	slides: function(){return $("#thumb-items .videos-link");},
	current_cont: function(){return $("#current-thumb");},
	get_current: function(){return $('.videos-links').eq($p.current_index);},
	current_index: -1,
	anim: false,
	load_items: function()
	{
		$.ajax({url:'/application/request/get_videos'
			   ,success:function(data)
			   {
					$p.items = utils.eval(data);   
					$p.init();
			   }});
		
	},
	init:function()
	{
		$p.i_length = $p.items.length;
		if($p.items.length == 0) return;
		
		var slide = $p.slide();
		var current_cat = 0;
		
		
		for(cat in $p.items)
		{
			var appendTo = $('<div class="videos-links" id="cat-'+cat+'" />').appendTo(slide);
			 $('<a href="" id="link-cat-'+cat+'"></a>')
			 	.data('id',cat)
			 	.bind('click',function()
					{
						$p.switch_cat('cat-'+$(this).data("id"),'');
						return false;
					})
				.appendTo('#categories');
			
			for(i in $p.items[cat])
			{
				$('<div class="videos-link" id="i-'+$p.items[cat][i].id+'"></div>')
					.height(0)
					.hide()
					.data("content",$p.items[cat][i])
					.html('<table cellpadding=0 cellspacing=0 width=100%><tr>'
						  +'<td style="padding-right:10px;width:1%;"><img src="'+$p.items[cat][i].thumbnail+'" height="39" /></td>'
						  +'<td>'+utils.short_string($p.items[cat][i].title,40)+'<div class="date">'+$p.items[cat][i].date+'</div></td></tr></table>')
					.bind("click",function(e){location.hash = '#load-'+$(e.currentTarget).data('content').id;})
					.appendTo(appendTo);
			}
		}
				 
		$(".arrow-bottom,.arrow-up")
			.unbind('click')
			.bind('click',function(){return false;})
			.addClass("inactive");
			
			
		$p.current = '';
		
		setInterval(function()
		{
			var hash = location.hash.replace('load','i');
			if(hash == '')
			{
				$p.slides().eq(0).trigger("click");
				return;
			}
			if(hash == $p.current) return;
			$p.current = hash;
			$p.change_vid();
		},25);
		
		
		
		
	},
	anime_top: function()
	{
		$(".arrow-down")
			.unbind('click',$p.anime_top)
			.bind('click',function(){return false;});
		
		$p.get_current().animate({marginTop:'-='+$p.height+'px'},500,'',$p.arrows);
		return false;
	},
	anime_bottom: function()
	{
		$(".arrow-top")
			.unbind('click',$p.anime_bottom)
			.bind('click',function(){return false;});
		$p.get_current().animate({marginTop:'+='+$p.height+'px'},500,'',$p.arrows);
		return false;
	},
	arrows: function()
	{
		var container = $p.get_current();
		var length = Math.ceil(container.find('.videos-link').length/8);
		var top = parseInt(container.css('marginTop').replace('px',''));
				
		if(top == -((length-1)*$p.height) || length == 1)
		{
			$(".arrow-down")
				.unbind('click',$p.anime_top)
				.bind('click',function(){return false;})
				.addClass('inactive');
		}
		else if(length>1)
		{
			$(".arrow-down").bind('click',$p.anime_top).removeClass("inactive");
		}

		if(top == 0 || length == 1)
		{
			$(".arrow-up")
				.unbind('click',$p.anime_bottom)
				.bind('click',function(){return false;})
				.addClass('inactive');
		}
		else if(length>1)
		{
			$(".arrow-up").bind('click',$p.anime_bottom).removeClass("inactive");
		}

		return this;
	},
	change_vid: function()
	{
		$p.slides().removeClass('active');
		var current = $($p.current)
		if(current.length == 0)
		{
			$p.slides().eq(0).trigger("click");
			return;
		}
		current.addClass('active');
		var data = current.data("content");
		
		
		var player  = document.getElementById("video_player");
		if(data.type == "vimeo")
		{
			vimeo_player_loaded = function(swf_id)
			{
				moogaloop = document.getElementById(swf_id);
				moogaloop.api_play();
			};
			
			var url 	= 'http://www.vimeo.com/moogaloop.swf?clip_id='+data.vid_id+'&fullscreen=1&js_onLoad=vimeo_player_loaded&js_api=1&js_swf_id=video_player';
		}
		else
		{
			var url 	= 'http://www.youtube.com/v/'+data.vid_id+'?enablejsapi=1&playerapiid=video-player&autoplay=1';
		}
		
		swfobject.embedSWF(url
							, 'video_player', '100%', '100%', '9.0.0'
							, null, null
							, {allowScriptAccess: "always"}
							, {allowfullscreen: 'true',id: 'video_player'});
		
		
		
		$("#current-thumb-title").html(data.title);
		$("#current-thumb-descr").html(data.txt.replace(/\n/g,'<br />'));
		
		$p.switch_cat(current.parent('.videos-links').attr("id"),current);
		
	},
	switch_cat: function(id,current)
	{

		if($p.anim == true) return;
		$p.anim = true;
		
		var callback = function()
		{
			$p.anim = false;
			$("#categories").removeClass("disabled-"+id);
			
			if(current == '') return $p.arrows();
			var index = $p.get_current().find('.videos-link').height(39).index(current);
			var suppose_top = Math.floor(index/8) * $p.height;
			$p.get_current().animate({marginTop:'-'+suppose_top+'px'},500,'',$p.arrows);
			return true;
		};
		
		if($('#'+id).hasClass("active-cat")) return callback();
		
		$("#categories a").removeClass('active-cat-link');
		$('#link-'+id).addClass('active-cat-link');
		
		$("#categories").removeAttr("class").addClass(id);
		$("#categories").addClass("disabled-"+id);

		$p.get_current()
			.removeClass("active-cat")
			.animate({marginTop:0},500)
			.find(".videos-link")
			.animate({height:0},500,'',function(){$(this).hide();});
		
		setTimeout(function()
		{
			$('#'+id)
				.css({marginTop:0})
				.addClass("active-cat")
				.find(".videos-link")
				.show()
				.animate({height:'39px'},500);
				$p.current_index = $p.slide().find('.videos-links').index($('#'+id));
				setTimeout(function(){callback();},520);
		},520);
			
			
	}
	
};

$($p.load_items);

