﻿var hide;
var show;
var xmlHttp;
var page = null;

var StudioPage = new Class({
	studioOwnerID: null,
	loadingPane: null,

	initialize: function(studioOwnerID, loadingPaneID) {
		this.studioOwnerID = studioOwnerID;
		this.loadingPane = $(loadingPaneID);
	},

	sort: function(pageNumber, sortType) {
		if (page.pagerClient != null && page.pagerClient != 'undefined') {
			page.pagerClient.loadPage(pageNumber, sortType);
		} else {
			initPaginated(); //call initPaginated() instead of PagerClient.ascx will call it
			this.loadPage(pageNumber, sortType);
		}
	},

	newSort: function() {
		var s_cat = "", s_sort = "";

		var o_cat = $("cmbMyVideosCategories");
		if (o_cat)
			s_cat = o_cat.get("value");

		var o_sort = $("cmbMyVideosSorting");
		if (o_sort)
			s_sort = o_sort.get("value");

		this.sort(1, s_sort + "_" + s_cat);
	},

	loadPage: function(pageNumber, sortType) {
		var pageData = 'method=' + page.myVideos.method + '&studioOwnerID=' + this.studioOwnerID + '&retrieveTotalCount=' + true;
		if (this.loadingPane)
			this.loadingPane.setStyle("display", "block");
		this.http = new Request({
			method: 'post',
			url: 'Handlers/PagingHandler.aspx',
			data: pageData + '&pageNumber=1&numInPage=' + page.myVideos.numInPage + '&sortType=' + sortType,
			onSuccess: this.loadPaginatedObjectsSucceeded.bind(this),
			onFailure: this.loadPaginatedObjectsFailed.bind(this)
		}).send();
	},

	loadPaginatedObjectsSucceeded: function() {
		page.myVideos.divPaginatedObjetsContainer.innerHTML = "";
		if (this.http.response != null && this.http.response.text != null) {
			page.myVideos.divPaginatedObjetsContainer.innerHTML = this.http.response.text;
		}
		if (this.loadingPane)
			this.loadingPane.setStyle("display", "none");
	},

	loadPaginatedObjectsFailed: function() {
		page.myVideos.divPaginatedObjetsContainer.innerHTML = "";
		if (this.loadingPane)
			this.loadingPane.setStyle("display", "none");
	}
});

var Paginated = new Class({
	initialize: function(divPaginatedObjetsContainerID, method, numInPage) {
		this.divPaginatedObjetsContainer = $(divPaginatedObjetsContainerID);
		this.method = method;
		this.numInPage = numInPage;
	}
});

function Subscribe(studio, member, _hide, _show, action) {
	//create xml object
	xmlHttp = GetXmlHttpObject();

	//if unsuccessful - alert member
	if (xmlHttp == null) {
		alert("Browser does not support HTTP Request");
		return;
	}

	hide = document.getElementById(_hide);
	show = document.getElementById(_show);

	//set the url for the request and assign a handler
	var url = "Handlers/xmlSubscribe.aspx?memberID=" + member + "&studioID=" + studio + "&action=" + action;

	RunAJAXRequest(url, xmlHttp, xmlSubscribedChanged);
}

//function is called when the xml response
//changes state
function xmlSubscribedChanged() {
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var reply = xmlHttp.responseText;
		if (reply == "OK") {
			hide.style.display = 'none';
			show.style.display = '';
		} else {
			alert("Error contacting server");
		}
	}
}