﻿/* 
 * The following code is copyright (c)2009, POP World Media, LLC.
 * and is subject to the license agreement at http://www.popforums.com.
 */

/// <reference name="MicrosoftAjax.debug.js" />

Type.registerNamespace("PopForums.UI");

PopForums.UI.UserBlockSwitch = function(boxID, userID, urlBase, selectorText) {
	this._boxID = boxID;
	this._userID = userID;
	this._urlBase = urlBase;
	this._selectorText = selectorText;
}

PopForums.UI.UserBlockSwitch.prototype = {
	executeSwitch : function() {
		var box = $get(this._boxID);
		var ExpandContract = new PopForums.UI.ExpandContract(this._boxID, this._selectorText);
		ExpandContract.executeExpandContract();
		if (box.style.display == "none" || box.style.display == "") {
			box.innerHTML = "Loading...";
			var request = new Sys.Net.WebRequest();
			request.set_url(this._urlBase+"?id="+this._userID);
			request.add_completed(Function.createDelegate(this, this.populateResponse));
			request.invoke();
		}
	},
	
	populateResponse : function(executor, eventArgs) {
		var box = $get(this._boxID);
		box.innerHTML = executor.get_responseData();
	}
}

PopForums.UI.UserBlockSwitch.registerClass("PopForums.UI.UserBlockSwitch");

PopForums.UI.UserBlockSwitch.execute = function(boxID, userID, urlBase, selectorText) {
	var userBlockSwitch = new PopForums.UI.UserBlockSwitch(boxID, userID, urlBase, selectorText);
	userBlockSwitch.executeSwitch();
}



PopForums.UI.ExpandContract = function(boxID, selectorText) {
	PopForums.UI.ExpandContract.initializeBase(this);
    this._boxID = boxID;
    this._selectorText = selectorText;
    this._inertiabase = 5;
    this._interval = 10;
    this._inertiabaseoriginal = this._inertiabase;
    this._slideinterval = 0;
    this._newHeight = 0;
    this._box = $get(boxID);
    this._events = null;
}

PopForums.UI.ExpandContract.prototype = {
	initialize: function() {
		PopForums.UI.ExpandContract.callBaseMethod(this, "initialize");
		this._events = new Sys.EventHandlerList();
	},
	
	executeExpandContract : function() {
		this._targetHeight = this._getHeight().replace("px", "");
		if (this._box.style.display == "none" || this._box.style.display == "") {
			// expand
			this._slideinterval = setInterval(Function.createDelegate(this, this.expand), this._interval);
		}
		else {
			// shrink
			this._newHeight = this._targetHeight;
			this._slideinterval = setInterval(Function.createDelegate(this, this.shrink), this._interval);
		}
	},

	shrink : function() {
		if (this._newHeight > 1) {
			this._newHeight = this._newHeight - this._inertiabase;
			this._inertiabase += 1;
			this._box.style.height = (this._newHeight > 1) ? this._newHeight + "px" : "1px";
		}
		else {
			clearInterval(this._slideinterval);
			this._box.style.display = "none";
			this._raiseEvent("shrinkComplete", Sys.EventArgs.Empty);
		}
	},

	expand : function() {
		if (this._newHeight < this._targetHeight) {
			this._newHeight = this._newHeight + this._inertiabase;
			this._inertiabase += 1;
			this._box.style.height = (this._newHeight < this._targetHeight) ? this._newHeight + "px" : this._targetHeight + "px";
			this._box.style.display = "block";
		}
		else {
			clearInterval(this._slideinterval);
			this._raiseEvent("expandComplete", Sys.EventArgs.Empty);
		}
	},

	_getHeight : function() {
		var result;
		if (document.styleSheets[0].cssRules) {
			for (var i = 0; i < document.styleSheets.length; i++) {
				var sheetHref = document.styleSheets[i].href;
				if (sheetHref && (sheetHref.indexOf(window.location.host) > 0 || sheetHref.indexOf("/") == 0 || sheetHref.indexOf("../") == 0)) {
					for (var j = 0; j < document.styleSheets[i].cssRules.length; j++) {
						var ss = document.styleSheets[i];
						var rule = ss.cssRules[j];
						if (this._selectorText == rule.selectorText)
							result = rule.style.getPropertyValue('height');
					}
				}
			}
		}

		if (document.styleSheets[0].rules) {
			for (var i = 0; i < document.styleSheets.length; i++) {
				var sheetHref = document.styleSheets[i].href;
				if (sheetHref && (sheetHref.indexOf(window.location.host) > 0 || sheetHref.indexOf("/") == 0 || sheetHref.indexOf("../") == 0)) {
					for (var j = 0; j < document.styleSheets[i].rules.length; j++) {
						var ss = document.styleSheets[i];
						var rule = ss.rules[j];
						if (this._selectorText == rule.selectorText)
							result = rule.style.height;
					}
				}
			}
		}

		return result;
	},

	add_expandComplete : function(handler) {
		this.get_events().addHandler("expandComplete", handler);
	},

	remove_expandComplete : function(handler) {
		this.get_events().removeHandler("expandComplete", handler);
	},

	add_shrinkComplete : function(handler) {
		this.get_events().addHandler("shrinkComplete", handler);
	},

	remove_shrinkComplete : function(handler) {
		this.get_events().removeHandler("shrinkComplete", handler);
	},

	_raiseEvent : function(eventName, eventArgs) {
		var handler = this.get_events().getHandler(eventName);
		if (handler) {
			if (!eventArgs)
				eventArgs = Sys.EventArgs.Empty;
			handler(this, eventArgs);
		}
	},
	
	dispose : function() {
		PopForums.UI.ExpandContract.callBaseMethod(this, "dispose");
	}
}

PopForums.UI.ExpandContract.execute = function(id, selector) {
	var expander = new PopForums.UI.ExpandContract(id, selector);
	expander.executeExpandContract();
}

PopForums.UI.ExpandContract.registerClass("PopForums.UI.ExpandContract", Sys.Component);



PopForums.UI.ScrollToReply = function(replyBoxID) {
	var y = PopForums.UI.FindScrollPosition(replyBoxID);
	scrollTo(0, y);
}

PopForums.UI.FindScrollPosition = function(id) {
	var e = document.getElementById(id);
	var curtop = 0;
	if (e.offsetParent) {
		while (e.offsetParent) {
			curtop += e.offsetTop
			e = e.offsetParent;
		}
	}
	else if (e.y) {
		curtop += e.y;
	}
	return curtop;
}

PopForums.UI.LoadQuote = function(postID, titleID, boxID, replyBoxID) {
	this._boxID = boxID;
	this._titleID = titleID;
	this._replyBoxID = replyBoxID;
	this._postID = postID;
}

PopForums.UI.LoadQuote.prototype = {
	executeLoad : function() {
		var box = $get(this._boxID);
		var boxComponent = $find(this._boxID);
		box.value = "Loading...";
		PopForums.UI.WebServices.GetQuote(this._postID, boxComponent.get_isRichTextEnabled(), Function.createDelegate(this, this.successfulCall), Function.createDelegate(this, this.failedCall), this);
		PopForums.UI.ScrollToReply(this._replyBoxID);
	},
	
	successfulCall : function(result, context) {
		var box = $get(this._boxID);
		box.value = result.FullText;
		var boxComponent = $find(this._boxID);
		if (boxComponent.get_isRichTextEnabled())
			boxComponent.initialize();
		var box = $get(this._titleID);
		box.value = result.Title;
	},
	
	failedCall : function(result, context) {
		var box = $get(this._boxID);
		box.value = "Quote load failed.";
	}
}

PopForums.UI.LoadQuote.execute = function(postID, titleID, boxID, urlBaseQuote, urlBaseTitle, replyBoxID) {
	var loadQuote = new PopForums.UI.LoadQuote(postID, titleID, boxID, urlBaseQuote, urlBaseTitle, replyBoxID);
	loadQuote.executeLoad();
}

PopForums.UI.LoadQuote.registerClass("PopForums.UI.LoadQuote");

PopForums.UI.PreviewTopic = function(topicID, boxID, textID) {
	this._topicID = topicID;
	this._boxID = boxID;
	this._textID = textID;
}

PopForums.UI.PreviewTopic.prototype = {
	executeLoad : function() {
		var box = $get(this._textID);
		var expander = new PopForums.UI.ExpandContract(this._boxID, ".topicPreview");
		expander.executeExpandContract();
		var container = $get(this._boxID);
		if (container.style.display == "none" || container.style.display == "") {
			box.innerHTML = "Loading...";
			PopForums.UI.WebServices.GetFirstPost(this._topicID, Function.createDelegate(this, this.successfulCall), Function.createDelegate(this, this.failedCall), this._textID);
		}
	},
	
	successfulCall : function(result, context) {
		var box = $get(context);
		box.innerHTML = result.FullText;
	},
	
	failedCall : function(result, context) {
		var box = $get(context);
		box.innerHTML = "Error getting first post.";
	}
}

PopForums.UI.PreviewTopic.execute = function(topicID, boxID, textID) {
	var loadPreview = new PopForums.UI.PreviewTopic(topicID, boxID, textID);
	loadPreview.executeLoad();
}

PopForums.UI.PreviewTopic.registerClass("PopForums.UI.PreviewTopic");