﻿var smallDivs = Array();
var bigDivs = Array();

var channelData;

// Comments
////////////////////////////////////////////////////////
function showSmallComments()
{
	animatedcollapse.show('smallList-1');
	animatedcollapse.hide('bigList-1');
}

var _pid;

function getSmallComment(pid)
{
	_pid = pid;
	var container = $('#smallList-1');
	container.data('webMethodUrl', "/Services/Service.asmx/GetComments");
	container.data('webMethodData', "{'pid' : '" + pid + "'}");
	container.data('resultsData', null);
	container.find('#show-big-comments').show();	
	ShowChannelCommentsSmall(-1, 0);
}

function showBigComments()
{
	animatedcollapse.show('bigList-1');
	animatedcollapse.hide('smallList-1');
}

function getBigComment()
{
	var container = $('#bigList-1');
	container.data('resultsData', $('#smallList-1').data('resultsData'))
	ShowChannelComments(-1, 0);
	$('#channel-comments').show();
}

function ShowChannelCommentsSmall(index, pageIndex)
{    
	ShowChannelDataSmall(index, pageIndex, 1, ShowComment, ChangePageForCommentsSmall, NoCommentsRetrieve);
}

function ShowChannelComments(index, pageIndex)
{
	$('#bigList' + index).data("needUpdate", true);
	ShowChannelData(index, pageIndex, 5, ShowComment, ChangePageForComments, NoCommentsRetrieve);
	$('#bigList' + index).data("needUpdate", false);
}

function ShowComment(container, comment)
{
	var left = $("<div></div>").addClass("comment-left");
	left.html(comment.Text.replace(/\n/g, "<br />"));

	var right = $("<div></div>").addClass("comment-right");
	right.text(comment.UserName != null ? comment.UserName : 'undefined');

	return $("<div></div>").addClass("comment").append(left).append(right).append($("<div></div>").addClass("clear"));
}

function NoCommentsRetrieve(container)
{
	showSmallComments();
	var content = container.find("#contentHolder");
	content.append(container.find('#noItems').clone().removeClass("display-none"));
	container.find('#show-big-comments').hide();
}

function ChangePageForCommentsSmall(container)
{
	ChangePageSmall(container);
	$(".commentsTitle").text("Comments (" + container.data("resultsData").length + ")");
	getBigComment();
}

function ChangePageForComments(container)
{
	ChangePage(container);
	$(".commentsTitle").text("Comments (" + container.data("resultsData").length + ")");
}

function ShowAddNewComment()
{
	showSmallComments();
	
	$("#addNewCommentSubmitted").hide(500);
	$("#addNewCommentForm").show(500);
	
	$("#newCommentName").val($.cookie('comment_name'));
	$("#newCommentEmail").val($.cookie('comment_email'));
	$("#newCommentComment").val('');
	
	$("#addNewComment").show(500);
}

function HideAddNewComment()
{
	$("#addNewComment").hide(500);
}

function AddNewComment()
{
	var name = $("#newCommentName").val();
	var email = $("#newCommentEmail").val();
	var text = $("#newCommentComment").val();
	
	if (name == "" || text == "")
	{
		$("#addNewCommentError").fadeIn(500).animate({borderWidth:"1px"}, 2500 ).fadeOut(500);
		return;
	}
	
	$.ajax({
			type: "POST",
			url: "/Services/Service.asmx/NewComment",
			data: "{'pid' : '" + _pid + "', 'comment' : '" + text + "', 'name' : '" + name + "', 'eMail' : '" + email + "'}",
			contentType: "application/json; charset=utf-8",
			dataType: "json"
	});
	
	$.cookie('comment_name', name, { expires: 7, path: '/' });
	$.cookie('comment_email', email, { expires: 7, path: '/' });
	
	$("#addNewCommentForm").hide(500);
	$("#addNewCommentSubmitted").show(500).animate({borderWidth:"1px"}, 5000).fadeOut(500);
	$("#addNewComment").animate({borderWidth:"1px"}, 5000).fadeOut(500);
	
	UpdateCommentLists(name, email, text);
	
	$('#show-big-comments').show();
}

function UpdateCommentLists(name, email, text)
{
	var container = $('#smallList-1');
	
	var comments = container.data('resultsData');
	Array.insert(comments, 0, GetNewElement(name, email, text));
	
	container.data("needUpdate", true);
	ShowChannelCommentsSmall(-1, 0);
	container.data("needUpdate", false);
}

function GetNewElement(name, email, text)
{
	var element = new Object();
	element.Text = text;
	element.UserName = name;
	element.UserEmail = email;
	return element;
}

function CloseComment()
{
	HideAddNewComment();
}

// Content
////////////////////////////////////////////////////////
function showSmallContent(index)
{
	animatedcollapse.show('smallList' + index);
	animatedcollapse.hide('bigList' + index);

	var container = $('#smallList' + index);
	if (container.data('resultsData') == null) {
		var containerBig = $('#bigList' + index);
		
		var resultsData = containerBig.data('resultsDataNotFiltered');
		if (resultsData == null)
			resultsData = containerBig.data('resultsData');

		container.data('resultsData', resultsData)
		getSmallContent(index);
	}
}

function getSmallContent(index)
{
	ShowChannelResultsPageSmall(index, 0);
}

function showBigContent(index)
{
	for (i in smallDivs)
	{
		if (smallDivs[i] == 'smallList' + index)
		{
			animatedcollapse.hide(smallDivs[i]);
			$('#bigList' + index).show();
			// cant calc real container height to show "loading please wait" while it is expanding
			//animatedcollapse.show(bigDivs[i]);
		}
		else
		{
			animatedcollapse.show(smallDivs[i]);
			animatedcollapse.hide(bigDivs[i]);
		}
	}
	
	var container = $('#bigList' + index);
	if (container.data('resultsData') == null) {
		container.data('resultsData', $('#smallList' + index).data('resultsData'))
		getBigContent(index);
	}
}

function getBigContent(index)
{
	ShowChannelResultsPage(index, 0);
}

function getBigContentByTerCat(index, terCat)
{
	var container = $('#bigList' + index);
	
	var resultsDataNotFiltered = container.data('resultsDataNotFiltered');
	if (resultsDataNotFiltered != null) container.data('resultsData', resultsDataNotFiltered.slice());
	else container.data('resultsDataNotFiltered', container.data('resultsData').slice());
	
	var jsonData = container.data('resultsData');
	if (jsonData != null)
	{
		if (terCat != "")
		{
			for (var i=0; i<jsonData.length;i++)
			{
				var terCats = jsonData[i].TerCategories;
				if (!Array.contains(terCats, terCat))
					Array.remove(jsonData, i--);
			}
		}
			
		getBigContent(index);
	}

    // gray selected button
	var buttons = container.find(".terMenu A").each(function(i, domEl) {

	    var button = $(domEl);
	    if (button.attr("name") == terCat) {
	        button.removeClass("roundedLightBlueGrayBg");
	        button.addClass("roundedGrayBg");
	    }
	    else {
	        button.removeClass("roundedGrayBg");
	        button.addClass("roundedLightBlueGrayBg");
	    }
	    
	});
}

function getBigContentBySort(index, sortBy)
{
	var container = $('#bigList' + index);
	var jsonData = container.data('resultsData');
	if (jsonData != null)
	{
		if (sortBy == "0")
			jsonData.sort(function(video1, video2) { return video1.Position - video2.Position; });
		else if (sortBy == "1")
			jsonData.sort(function(video1, video2) { return video2.Video.NumberOfViews - video1.Video.NumberOfViews; });
		else if (sortBy == "2")
			jsonData.sort(function(video1, video2) { return video2.Video.Rating - video1.Video.Rating; });
		
		getBigContent(index);
	}
}

function ShowChannelResultsPageSmall(index, pageIndex)
{
	$('#smallList' + index).data("needUpdate", true);
	ShowChannelDataSmall(index, pageIndex, 5, ShowClipWithTertCat, ChangePageSmall, NoVideosRetrieve);
	$('#smallList' + index).data("needUpdate", false);
}

function ShowChannelResultsPage(index, pageIndex)
{
	$('#bigList' + index).data("needUpdate", true);
	ShowChannelData(index, pageIndex, 10, ShowClipWithTertCat, ChangePage, NoVideosRetrieve);
	$('#bigList' + index).data("needUpdate", false);
}

function ShowClipWithTertCat(container, item)
{
	return ShowClip(container, item.Video);
}

// Other
////////////////////////////////////////////////////////
function ShowChannelDataSmall(index, pageIndex, pageSize, showContentFunc, afterDataProcessFunc, noItemsRetrieveFunc)
{
	var container = $('#smallList' + index);
	container.data("pageIndex", pageIndex);
	container.data("pageSize", pageSize);
	container.data("index", index);
	container.data("onclickFuncStr", "ShowChannelDataSmall(" + index + ",#currentPage," + pageSize + "," + getFunctionName(showContentFunc) + "," + getFunctionName(afterDataProcessFunc) + "," + getFunctionName(noItemsRetrieveFunc) + ");return false;");

	if (container.data("needUpdate") != true || container.data("resultsData") == null) 
	    GetResults(container, showContentFunc, null, afterDataProcessFunc, noItemsRetrieveFunc);	
	else
	    ShowResults(container, container.data("resultsData"), showContentFunc, null, afterDataProcessFunc, noItemsRetrieveFunc);
}

function ShowChannelData(index, pageIndex, pageSize, showContentFunc, afterDataProcessFunc, noItemsRetrieveFunc)
{
	var container = $('#bigList' + index);
	container.data("pageIndex", pageIndex);
	container.data("pageSize", pageSize);
	container.data("onclickFuncStr", "ShowChannelData(" + index + ",#currentPage," + pageSize + "," + getFunctionName(showContentFunc) + "," + getFunctionName(afterDataProcessFunc) + "," + getFunctionName(noItemsRetrieveFunc) + ");return false;");

	if (container.data("needUpdate") != true || container.data("resultsData") == null)
		GetResults(container, showContentFunc, null, afterDataProcessFunc, noItemsRetrieveFunc);
	else
		ShowResults(container, container.data("resultsData"), showContentFunc, null, afterDataProcessFunc, noItemsRetrieveFunc);
}