$(document).ready(
	function()
	{					
		/**
		* Add data to tpl and return it (home page)
		**/		
		function videoTemplate(data)
		{
			if(data.video_views == null)
			{
				data.video_views = 0;
			}
			var first_frame_img = "";
			
			if(!data.video_first_frame_img)
			{
				first_frame_img = "http://img.youtube.com/vi/"+data.video_url+"/default.jpg";
			}
			else
			{
				first_frame_img = "uploads/img/catalog/2/{data.video_first_frame_img}";
			}
			// 
			var tpl = "<div class='item'><div class='wrapper'><a href='video/"+data.url_lt+"' title='"+data.video_title+"'><img width='168' height='126' src='" + first_frame_img + "'/></a><h2><a href='video/"+data.url_lt+"' class='u' title='"+data.video_title+"'>"+data.video_title+"</a></h2><div class='fs85p pad2'><span>Peržiūrėta:</span> <b>"+data.video_views+"</b></div></div></div>";
			return tpl;
		}
		
		/**
		* Add data to tpl and return it (inner page)
		**/		
		function videoInnerTemplate(data)
		{
			if(data.video_views == null)
			{
				data.video_views = 0;
			}
			var first_frame_img = "";
			
			if(!data.video_first_frame_img)
			{
				first_frame_img = "http://img.youtube.com/vi/"+data.video_url+"/default.jpg";
			}
			else
			{
				first_frame_img = "uploads/img/catalog/2/{data.video_first_frame_img}";
			}
			// 
			var tpl = "<div class='item2'><a href='video/"+data.url_lt+"' title='"+data.video_title+"'><img width='168' height='126' src='" + first_frame_img + "'/></a><h2><a href='video/"+data.url_lt+"' class='u' title='"+data.video_title+"'>"+data.video_title+"</a></h2><div class='fs85p pad2'><span>Peržiūrėta:</span> <b>"+data.video_views+"</b></div></div>";
			return tpl;
		}
		
		
		homeVideosRequest('popular');
		innerVideosRequest('popular');
		
		/**
		* Function that requests home videos
		**/
		function homeVideosRequest(type)
		{
			$.getJSON
			(
				"http://"+ document.domain + "/index.php/,ajxhomevideos.true,type."+type,
				{},
				function(server_data)
				{
					var full_html = "";
					
					$.each(server_data,
						function(i, item)
						{
							full_html = full_html + videoTemplate(item);
						}
					);
					
					$("#home_videos_container").html(full_html);
				}
			);
		}
		
		/**
		* Function that requests home videos
		**/
		function innerVideosRequest(type, cat_id)
		{
			$.getJSON
			(
				"http://"+ document.domain + "/index.php/,ajxinnervideos.true,type."+type + ",cat_id."+ cat_id,
				{},
				function(server_data)
				{
					var full_html = "";
					
					$.each(server_data,
						function(i, item)
						{
							full_html = full_html + videoInnerTemplate(item);
						}
					);
					
					$("#inner_videos_container").html(full_html);
				}
			);
		}
		
		/**
		* Request to get videos
		**/
		$("#home_page_video_selector").change(
			function()
			{
				var type = $("option:selected",this).attr("value");
				homeVideosRequest(type);
			}
		);
		
		/**
		* Request to get videos at inner page
		**/
		$("#inner_page_video_selector").change(
			function()
			{
				var cat_id = $("#video_category_id").attr("value");
				var type = $("option:selected",this).attr("value");
				innerVideosRequest(type, cat_id);
			}
		);
		
		/**
		* Request to sort videos at inner page
		**/
		$("#video_sort").change(
			function()
			{
				var type = $("option:selected",this).attr("value");
				var uri = $("#video_uri").attr("value");
				window.location.href= uri + ",sort." + type;
			}
		);
	}
);