function Videos( strControllerURL ){
	this.ControllerURL = strControllerURL;
	this.Init();
};

Videos.prototype.objPostProperties = function( objPostProperties ){
	this.PostProperties = objPostProperties;
};

Videos.prototype = new PageControllerClass;
Videos.prototype.Init = function(){
	var objSelf = this;
	this.SetBusy( true );
	$('div#small-playlist-tabs > a').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.GetTabContents( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('div#favorite > a').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.GetMarkUpdateFavorite( $this, $('div#favorite').attr("title") );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('#commentForm').submit(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitComment( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('div#comments-container div.pagination a').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.GetPagedComments( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('div#comments-container > div#subscriptionLinksContainer > a[class=subscriptionlink]').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.SubmitSubscription( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	$('a#more-description').click(
		function( objEvent ){
			var $this = $(this);
			$('div#mini-description').css('display','none');
			$('div#full-description').css('display','block');
			//objSelf.GetPagedComments( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
	this.SetBusy( false );
};

Videos.prototype.ReRegisterPagination = function() {
	var objSelf = this;
	$('div#comments-container div.pagination a').click(
		function( objEvent ){
			var $this = $(this);
			objSelf.GetPagedComments( $this );
			objEvent.preventDefault();
			return( false );
		}
	);
};

Videos.prototype.GetTabContents = function( jLink ){
	var objSelf = this;
	if(objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties = {
		"do": "api.videos.renderHTMLPlaylist",
		"renderby": jLink.attr("title"),
		"ObjectID": jLink.attr("id")
		};
	$.get(
		this.ControllerURL,
		objPostProperties,
		function( objResponse ){
			objSelf.GetTabContentsHandler( objResponse , jLink );
		},
		"json"
		);
};

Videos.prototype.GetTabContentsHandler = function( objResponse , jLink ){
	if(objResponse.SUCCESS){
		var tabs = $('div#small-playlist-tabs > a');
			tabs.removeClass();
		jLink.addClass('orange');
		$('#smallplaylist-holder').html(objResponse.DATA);
		this.SetBusy( false );
	} else {
		this.ShowAPIErrors( objResponse.ERRORS );
	};
};

Videos.prototype.GetMarkUpdateFavorite = function( jLink, objectID ){
	var objSelf = this;
	if(objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties={"do":"api.videos.markUpdateFavorite","ObjectID":objectID};
	$.get(this.ControllerURL,objPostProperties,function(objResponse){objSelf.GetMarkUpdateFavoriteHandler(objResponse,jLink);},"json");
};

Videos.prototype.GetMarkUpdateFavoriteHandler = function( objResponse , jLink ){
	if(objResponse.SUCCESS){
		jLink.html(objResponse.DATA);
		this.SetBusy( false );
	} else {this.ShowAPIErrors( objResponse.ERRORS );};
};

Videos.prototype.SubmitComment = function( jForm ){
	var objSelf = this;
	if (objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var objPostProperties = {
		"do": "api.forums.postReply",
		message: jForm.find("textarea[name='message']").val(),
		TopicID: jForm.find("input[name='TopicID']").val(),
		TopicName: jForm.find("input[name='TopicName']").val(),
		includeInPost: jForm.find("input[name='includeInPost']").val()
	};
	//alert(objPostProperties.includeInPost);
	$.post(this.ControllerURL,objPostProperties,function(objResponse){objSelf.SubmitCommentHandler(objResponse,jForm);},"json");
};

Videos.prototype.SubmitCommentHandler = function( objResponse , jForm ){
	if (objResponse.SUCCESS){
		var newComment = $("#newComment");
		$("#post-comment-container").remove();
		newComment.html(objResponse.DATA);
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};

Videos.prototype.GetPagedComments = function( jLink ){
	var objSelf = this;
	if(objSelf.GetBusy()){ objSelf.AlertBusy(); } else { objSelf.SetBusy( true ); };
	var targetHrefArray = jLink.attr("href").split('/');
	for(var i=0;i<targetHrefArray.length;i++){
		//if(targetHrefArray[i] == 'v') {var objectid = targetHrefArray[i+1];};
		if(targetHrefArray[i] == 'page') {var page = targetHrefArray[i+1];};
	};
	var objectid = $('input#videoid').val();
	var objPostProperties = this.ControllerURL + '/do/api.forums.getRepliesAsComment/v/' + objectid + '/page/' + page;
	$.get(objPostProperties,'',function(objResponse){objSelf.GetPagedCommentsHandler(objResponse,jLink);},"json");
};

Videos.prototype.GetPagedCommentsHandler = function( objResponse , jLink ){
	if(objResponse.SUCCESS){
		$('#comment-list-container').html(objResponse.DATA);
		this.ReRegisterPagination();
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};

Videos.prototype.SubmitSubscription = function( jLink ){
	var objSelf = this;
	if(objSelf.GetBusy()){objSelf.AlertBusy();} else {objSelf.SetBusy(true);};

	/*
	var objPostProperties = {
		"do": "api.forums.updateSubscription",
		"id": jLink.attr("id")
		};
	*/
	
	var SubscriptionURL = jLink.attr("href");

	$.get(
		SubscriptionURL,
		'',
		function( objResponse ){
			objSelf.SubmitSubscriptionHandler( objResponse , jLink );
		},
		"json"
		);
};

Videos.prototype.SubmitSubscriptionHandler = function( objResponse , jLink ){
	if (objResponse.SUCCESS){
		jLink.children().remove();
		jLink.html(objResponse.DATA);
		this.SetBusy( false );
	} else { this.ShowAPIErrors( objResponse.ERRORS ); };
};
