﻿var Mouse = { X: 0, Y: 0 };
var brousertype = navigator.userAgent.indexOf('Chrome') != -1 ? "Chrome" : "NoneChrome";

if (parent && parent != window) {
    $(document).mousemove(function(e) {
        if (brousertype == "Chrome") {
            Mouse.Y = e.pageY;
        } else {

            Mouse.Y = e.pageY + document.body.scrollTop;
        }
    });

}

function ToggleShow(show) {
    $(show).css("display", "");
    $(show).siblings().css("display", "none");
}
function ToggleCurClass(cur) {
    $(cur).siblings().removeClass("c");
    $(cur).addClass("c");
}
var ModuleCurrentTopicCategory;
var Hash = {
     _lastHash : "",
     _lastHashArray : [],
     Read : function() {
          if(this._lastHash!=window.location.hash) {          
               var result = [];
               var array = window.location.hash.replace(/^#\//,"").replace(/\/$/,"").split("/");
               for(var arrayIndex in array) {
                    result.push(array[arrayIndex]);
               }
               this._lastHash = window.location.hash;
               this._lastHashArray = result;
          }
          return this._lastHashArray;
     },
     Write : function() {
          var hash = "/";
          for (var i = 0; i < arguments.length; i++)
               if (arguments[i] != undefined && arguments[i] != null)
                   hash += arguments[i] + "/";
           window.location.hash = hash;
           if (parent != window) {
               parent.syncURL(window.location.href);
           }
     }
}
// Tolea Part

MVCAction = function(Controller, Action) {
    var Result = BaseURL + Controller + "/" + Action + "/";
    for (var i = 2; i < arguments.length; i++)
        if (arguments[i] != undefined)
        Result += arguments[i] + "/";
    return Result;
}

MVCGo = function(Controller, Action) {
    
    var Result = BaseURL + Controller + "/" + Action + "/";
    for (var i = 2; i < arguments.length; i++)
        if (arguments[i] != undefined)
        Result += arguments[i] + "/";
    
    window.location.href = Result;
}

SetCookie = function(Name, Value, DateString) {
    document.cookie = Name + "=" + Value +
        ";path=/"+
        (DateString != null ? ";expires=" + new Date(DateString).toGMTString() : "");
}

GetCookie = function(Name) {
    var Cookies = document.cookie.split(";");
    for (var CookieIndex in Cookies) {
        var Cookie = Cookies[CookieIndex].split("=");
        if (Cookie[0] == Name)
            return Cookie[1];
    }
    return null;
}

var Page = {
	// Modules
	Module: {
		Toggle: function (SystemName) {
			if ($("#Module_Container_" + SystemName).hasClass("hidden")) {
				if (Trim($("#Module_Content_" + SystemName).html()) == "") {
					$("#Module_Container_" + SystemName).find(".ModuleMoveHandler").addClass("ModuleLoading");
					this.Load(SystemName, function () { });
				} else {
					$("#Module_Container_" + SystemName).removeClass("hidden");
					$("#Module_" + SystemName).attr("State", "1");
					Page.ModuleCollection.SaveState();
				}
			} else {
				$("#Module_Container_" + SystemName).addClass("hidden")
				$("#Module_" + SystemName).attr("State", "0");
				Page.ModuleCollection.SaveState();
			}
			Page.ModuleCollection.InitializeMove();
		},
		Load: function (SystemName, CallBack) {
			//            $.get(MVCAction("Module", SystemName) + $("#Module_" + SystemName).attr("Params"), {}, function(result) {
			//                $("#Module_Content_" + SystemName).html(result);
			//                CallBack();
			//            });
			LoadScript(MVCAction("Module", SystemName) + $("#Module_" + SystemName).attr("Params"));
		},
		LoadComplete: function (SystemName, result) {
			$("#Module_Container_" + SystemName).find(".ModuleMoveHandler").removeClass("ModuleLoading");
			$("#Module_Container_" + SystemName).removeClass("hidden");
			$("#Module_Content_" + SystemName).html(result);
			$("#Module_" + SystemName).attr("State", "1");
			Page.ModuleCollection.SaveState();
		},
		Enter: function (Sender) {
			$(Sender).find(".ModuleMoveHandler").css("display", "block");
		},
		Leave: function (Sender) {
			$(Sender).find(".ModuleMoveHandler").css("display", "none");
		}
	},
	// ModuleCollection
	ModuleCollection: {
		CookiePrefix: "Forum.Modules.",
		Load: function (Address) {
			$.get(Address, {}, function (result) {
				$("#RightModules").html(result);
				Page.ModuleCollection.InitializeMove();
			});
		},
		SaveState: function () {
			var CollectionName = $("#RightModules").attr("CollectionName");
			var NewStateString = "";
			$("#RightModules").find("[Module]").each(function () {
				NewStateString += $(this).attr("Module") + "=" + $(this).attr("State") + ",";
			});
			NewStateString = NewStateString.substr(0, NewStateString.length - 1);
			SetCookie(Page.ModuleCollection.CookiePrefix + CollectionName, NewStateString, null);
		},
		InitializeMove: function () {
			$("#RightModules").sortable({
				items: ".Module",
				handle: ".ModuleMoveHandler",
				axis: 'y',
				stop: function (event, ui) {
					Page.ModuleCollection.SaveState();
				}
			});
		}
	},
	// Theme

	//Main
	Main: {
		GetFriendsActivity: function (sender) {
			$("#loading").css("display", "");
			$.post(MVCAction("Main", "GetFriendsActivity"), {}, function (result) {
				$("#LeftContainer").css("display", "none");
				$("#LeftOverContent").css("display", "");
				$("#LeftOverContent").html(result);
				$(".lefttabs").removeClass("sel");
				$(sender).addClass("sel");
				$("#loading").css("display", "none");
			});
		},
		ShowTopTopics: function (sender) {
			$("#LeftContainer").css("display", "");
			$("#LeftOverContent").css("display", "none");
			$(".lefttabs").removeClass("sel");
			$(sender).addClass("sel");
		}
	},

	// Discuss
	Discuss: {
		Pin: function (sender, DiscussId) {
			var action = $(sender).attr("param");
			if (action == 1) {
				$(sender).attr("param", 0);
				$(sender).removeClass("mag");
				$(sender).addClass("mag2");
			}
			else {
				$(sender).attr("param", 1);
				$(sender).removeClass("mag2");
				$(sender).addClass("mag");
			}
			$.post(MVCAction("Discuss", "Pin", DiscussId, action), {}, function (result) {

			});
		},
		Block: function (NodeId) {
			$.post(MVCAction("Discuss", "BlockDiscuss", NodeId), {}, function (result) {
				window.location.reload(true);
			});
		},
		UnBlock: function (NodeId) {
			$.post(MVCAction("Discuss", "UnBlockDiscuss", NodeId), {}, function (result) {
				window.location.reload(true);
			});
		},
		ToFavorities: function (NodeId) {
			$("#tof").css("display", "none");
			$("#fromf").css("display", "");
			$.post(MVCAction("Discuss", "AddToFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#tof").css("display", "");
					$("#fromf").css("display", "none");
				}
			});
		},
		FromFavorities: function (NodeId) {
			$("#tof").css("display", "");
			$("#fromf").css("display", "none");
			$.post(MVCAction("Discuss", "RemoveFromFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#tof").css("display", "none");
					$("#fromf").css("display", "");
				}
			});
		},
		ToFavoritiesByNode: function (NodeId) {
			$("#tof" + NodeId).css("display", "none");
			$("#fromf" + NodeId).css("display", "");
			$.post(MVCAction("Discuss", "AddToFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#tof" + NodeId).css("display", "");
					$("#fromf" + NodeId).css("display", "none");
				}
			});
		},
		FromFavoritiesByNode: function (NodeId) {
			$("#tof" + NodeId).css("display", "");
			$("#fromf" + NodeId).css("display", "none");
			$.post(MVCAction("Discuss", "RemoveFromFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#tof" + NodeId).css("display", "none");
					$("#fromf" + NodeId).css("display", "");
				}
			});
		},
		Rate: function (Sender, DiscussId, OwnerUserId, MD5, DiscussTitleRu, DiscussTitleRo, TopicId, TopicTitleRu, TopicTitleRo, isAlbum) {
			var Value = $(Sender).attr("param");
			$("#loading").css("display", "");
			$.ajax({
				url: MVCAction("Discuss", "Rate"),
				data: { DiscussId: DiscussId, OwnerUserId: OwnerUserId, Value: Value, MD5: MD5, DiscussTitleRu: DiscussTitleRu, DiscussTitleRo: DiscussTitleRo, TopicId: TopicId, TopicTitleRu: TopicTitleRu, TopicTitleRu: TopicTitleRo, isAlbum: ((isAlbum != null) ? isAlbum : "") },
				success: function (result) {
					if (result.status == '1') {
						var RatioContainer = $("#Ratio" + DiscussId);
						var RatioCountContainer = $("#DiscussRatioCount");
						RatioContainer.text(parseInt(RatioContainer.text()) + parseInt(result.value));
						RatioCountContainer.text(parseInt(RatioCountContainer.text()) + 1);
					}
					else
						alert(result.value);
				},
				dataType: "json",
				type: "POST"
			});
			$("#loading").css("display", "none");
		},
		GetComments: function (TopicId, DiscussId, Page, Comment) {
			$("#loading").css("display", "");
			$.post(MVCAction("Comment", "List", TopicId, DiscussId, Page), {}, function (result) {
				$("#ListComments").html(result);
				$("#loading").css("display", "none");
				if (Comment != null) {
					$.scrollTo('#Comment' + Comment, 0)
				} else if (Page == 0)
				{ }
				else if (Page != null) {
					$.scrollTo('#up_paginator', 0)
				}
			});
		},
		Load: {
			Process: function (TopicId, DiscussId) {
				hash = Hash.Read();
				if (hash.length > 0) {
					if (hash[1] != null) {
						Page.Discuss.GetComments(TopicId, DiscussId, hash[0], hash[1]);
					}
					else {
						Page.Discuss.GetComments(TopicId, DiscussId, hash[0]);
					}
				}
				else
					Page.Discuss.GetComments(TopicId, DiscussId);
			},
			Comments: function (DiscussId, Page) {
				Hash.Write(Page != 1 ? Page : 1);
				this.Process(DiscussId);
			}

		},
		CheckIncoming: function () {

		}
	},
	//Topics
	Topics: {
		GetTopics: function (Page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Themes", "GetTopics", Page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				$("#categoriesdiv > *").removeClass("cur");
				$("#C0").addClass("cur");
				if (Page != 1) {
					$.scrollTo({ top: "1px", left: "1px" }, 0)
				}
			});
		},
		GetCategoryTopics: function (CategoryName, Page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Themes", "GetCategoryTopics", CategoryName, Page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				$("#categoriesdiv > a").removeClass("cur");
				$("#C" + CategoryName).addClass("cur");
				ModuleCurrentTopicCategory = CategoryName;
				if (Page != 1) {
					$.scrollTo({ top: "1px", left: "1px" }, 0)
				}
			});
		},
		Load: {
			Process: function () {
				hash = Hash.Read();
				if (hash.length > 0)
					if (hash[0] == "All")
						Page.Topics.GetTopics(hash[1], hash[2]); // hash[1] = Page , hash[2] = Order
					else
						Page.Topics.GetCategoryTopics(hash[0], hash[1], hash[2]); // hash[0] = Page
				else
					Page.Topics.GetTopics(1, null);
			},
			Topics: function (Page, Order, Category) {
				if (!Category) Category = "All";
				Hash.Write(Category, Page != 1 ? Page : (Order != null ? Page : null), Order);
				this.Process();
			}
		}
	},
	//Topic
	Topic: {
		ToFavorities: function (NodeId) {
			$("#ttof").css("display", "none");
			$("#tfromf").css("display", "");
			$.post(MVCAction("Topic", "AddToFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#ttof").css("display", "");
					$("#tfromf").css("display", "none");
				}
			});
		},
		FromFavorities: function (NodeId) {
			$("#ttof").css("display", "");
			$("#tfromf").css("display", "none");
			$.post(MVCAction("Topic", "RemoveFromFavorites", NodeId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#ttof").css("display", "none");
					$("#tfromf").css("display", "");
				}
			});
		},
		ToFavoritiesByTopicId: function (TopicId) {
			$("#ttof" + TopicId).css("display", "none");
			$("#tfromf" + TopicId).css("display", "");
			$.post(MVCAction("Topic", "AddToFavorites", TopicId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#ttof" + TopicId).css("display", "");
					$("#tfromf" + TopicId).css("display", "none");
				}
			});
		},
		FromFavoritiesByTopicId: function (TopicId) {
			$("#ttof" + TopicId).css("display", "");
			$("#tfromf" + TopicId).css("display", "none");
			$.post(MVCAction("Topic", "RemoveFromFavorites", TopicId), {}, function (result) {
				if (result == 1) {
					Page.Module.Load("Favorities");
				}
				else {
					$("#ttof" + TopicId).css("display", "none");
					$("#tfromf" + TopicId).css("display", "");
				}
			});
		},
		CurrentTopic: null,
		GetTopic: function (Topic, Page) {
			$("#loading").css("display", "");
			$.post(MVCAction("Themes", "GetTopic", Topic, Page), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				if (Page != 1) {
					$.scrollTo({ top: "1px", left: "1px" }, 0)
				}
			});
		},
		GetCategoryTopic: function (Topic, Category, Page) {
			$("#loading").css("display", "");
			$.post(MVCAction("Themes", "GetTopicByCategory", Topic, Category, Page), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				if (Page != 1) {
					$.scrollTo({ top: "1px", left: "1px" }, 0)
				}
			});
		},
		Load: {
			Process: function () {
				hash = Hash.Read();
				if (hash.length > 0)
					if (hash[0] == "Authors")
						Page.People.GetTopicPeople(Page.Topic.CurrentTopic, hash[1], hash[2]); // hash[1] = Page , hash[2] = Order
					else
						Page.Topic.GetTopic(Page.Topic.CurrentTopic, hash[0]); // hash[0] = Page
				else
					Page.Topic.GetTopic(Page.Topic.CurrentTopic, 1);
			},
			ProcessCategory: function (Category) {
				hash = Hash.Read();
				if (hash.length > 0)
					Page.Topic.GetCategoryTopic(Page.Topic.CurrentTopic, Category, Hash[0]);
				else
					Page.Topic.GetCategoryTopic(Page.Topic.CurrentTopic, Category, 1);
			},
			Authors: function (Page, Order) {
				Hash.Write("Authors", Page != 1 ? Page : (Order != null ? Page : null) ? Page : null, Order);
				this.Process();
			},
			Discusses: function (Page) {
				Hash.Write(Page != 1 ? Page : null);
				this.Process();
			},
			Settings: function () {
				Hash.Write("Settings");
				this.Process();
			}
		}
	},
	//Tags
	Tags: {
		GetDiscussions: function (Tag, Page) {
			$("#loading").css("display", "");
			$.post(MVCAction("Tags", "GetDiscussions", Tag, Page), {}, function (result) {
				$("#ListDiscussions").html(result);
				$("#loading").css("display", "none");
			});
		}
	},
	//Authors
	Authors: {
		Highlite: function (id) {
			$(".amenu").removeClass("b");
			$("#" + id).addClass("b");
		},
		GetUserInfo: function (NickName) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetUserInfo", NickName), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				Page.Authors.Highlite("info");
			});
		},
		GetMain: function (NickName) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetMain", NickName), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
			});
		},
		GetFriends: function (NickName, page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetFriends", NickName, page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				Page.Authors.Highlite("Friends");
			});
		},
		GetFriendsOfFriends: function (NickName, page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetFriendsOfFriends", NickName, page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				Page.Authors.Highlite("Friends");
			});
		},
		GetCommonFriends: function (NickName, page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetCommonFriends", NickName, page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				Page.Authors.Highlite("Friends");
			});
		},
		GetWall: function (NickName, page) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetWall", NickName, page), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				Page.Authors.Highlite("userwall");
			});
		},
		GetNewPost: function (NickName, Category) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetAddBlogPost", NickName, Category), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				//Page.Authors.Highlite("userwall");
			});
		},
		Load: {
			Process: function (NickName) {
				hash = Hash.Read();
				if (hash.length > 0) {
					if (hash[0] == "NewPost") {
						Page.Authors.GetNewPost(NickName, Page.Authors.CurrentSubCat);
					}
					if (hash[0] == "info") {
						Page.Authors.GetUserInfo(NickName);
					}
					if (hash[0] == "Wall") {
						Page.Authors.GetWall(NickName, hash[1]);
					}
					if (hash[0] == "Friends") {
						Page.Authors.GetFriends(NickName, hash[1], hash[2]);
					}
					if (hash[0] == "FriendsOfFriends") {
						Page.Authors.GetFriendsOfFriends(NickName, hash[1], hash[2]);
					}
					if (hash[0] == "CommonFriends") {

						Page.Authors.GetCommonFriends(NickName, hash[1], hash[2]);
					}
				}
				else {
					Page.Authors.GetMain(NickName);
				}
			},
			Info: function (NickName, sender) {
				Hash.Write("info");
				this.Process(NickName);
			},
			NewPost: function (NickName, sender) {
				Hash.Write("NewPost");
				this.Process(NickName);
			},
			Wall: function (NickName, Page, sender) {
				Hash.Write("Wall", Page != 1 ? Page : null);
				this.Process(NickName);
			},
			Friends: function (NickName, Page, Order) {
				Hash.Write("Friends", Page != 1 ? Page : (Order != null ? Page : null) ? Page : null, Order);
				this.Process(NickName);
			},
			FriendsOfFriends: function (NickName, Page, Order) {
				Hash.Write("FriendsOfFriends", Page != 1 ? Page : (Order != null ? Page : null) ? Page : null, Order);
				this.Process(NickName);

			},
			CommonFriends: function (NickName, Page, Order) {
				Hash.Write("CommonFriends", Page != 1 ? Page : (Order != null ? Page : null) ? Page : null, Order);
				this.Process(NickName);

			}
		}
	},
	//EditAuthor
	EditAuthor: {
		Highlite: function (Id) {
			$(".edita").removeClass("c_");
			$("#" + Id).addClass("c_");
		},
		GetMain: function () {
			$("#loading").css("display", "");
			$.post(MVCAction("People", "GetEditMain"), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditAuthor.Highlite("Main");
				$("#loading").css("display", "none");
			});
		},
		GetContactInfo: function () {
			$("#loading").css("display", "");
			$.post(MVCAction("People", "GetEditContactInfo"), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditAuthor.Highlite("ContactInfo");
				$("#loading").css("display", "none");
			});
		},
		GetChangePassword: function () {
			$("#loading").css("display", "");
			$.post(MVCAction("People", "GetEditChangePassword"), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditAuthor.Highlite("ChangePassword");
				$("#loading").css("display", "none");
			});
		},
		GetChangeStyle: function () {
			$("#loading").css("display", "");
			$.post(MVCAction("People", "GetEditChangeStyle"), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditAuthor.Highlite("ChangeStyle");
				$("#loading").css("display", "none");
			});
		},
		Load: {
			Process: function () {
				hash = Hash.Read();
				if (hash.length > 0) {
					switch (hash[0]) {
						case "ContactInfo":
							Page.EditAuthor.GetContactInfo();
							break;
						case "ChangePassword":
							Page.EditAuthor.GetChangePassword();
							break;
						case "ChangeStyle":
							Page.EditAuthor.GetChangeStyle();
							break;
						default:
							Page.EditAuthor.GetMain();
					}
				}
				else {
					Page.EditAuthor.GetMain();
				}
			},
			Main: function () {
				Hash.Write("Main");
				this.Process();
			},
			ContactInfo: function () {
				Hash.Write("ContactInfo");
				this.Process();
			},
			ChangePassword: function () {
				Hash.Write("ChangePassword");
				this.Process();
			},
			ChangeStyle: function () {
				Hash.Write("ChangeStyle");
				this.Process();
			}
		}

	},
	//EditTopic
	EditTopic: {
		Highlite: function (Id) {
			$(".editm").removeClass("c_");
			$("#" + Id).addClass("c_");
		},
		GetMain: function (TopicId) {
			$("#loading").css("display", "");
			$.post(MVCAction("Topic", "GetEditMain", TopicId), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditTopic.Highlite("Main");
				$("#loading").css("display", "none");
			});
		},
		GetCensors: function (TopicId) {
			$("#loading").css("display", "");
			$.post(MVCAction("Topic", "GetEditCensors", TopicId), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditTopic.Highlite("Censors");
				$("#loading").css("display", "none");
			});
		},
		GetAccess: function (TopicId) {
			$("#loading").css("display", "");
			$.post(MVCAction("Topic", "GetEditAccess", TopicId), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditTopic.Highlite("Access");
				$("#loading").css("display", "none");
			});
		},
		GetOwner: function (TopicId) {
			$("#loading").css("display", "");
			$.post(MVCAction("Topic", "GetEditOwner", TopicId), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditTopic.Highlite("Owner");
				$("#loading").css("display", "none");
			});
		},
		GetRubrics: function (TopicId) {
			$("#loading").css("display", "");
			$.post(MVCAction("Topic", "GetEditRubrics", TopicId), {}, function (result) {
				$("#ListContainer").html(result);
				Page.EditTopic.Highlite("Rubrics");
				$("#loading").css("display", "none");
			});
		},
		Load: {
			Process: function (TopicId) {
				hash = Hash.Read();
				if (hash.length > 0) {
					switch (hash[0]) {
						case "Censors":
							Page.EditTopic.GetCensors(TopicId);
							break;
						case "Access":
							Page.EditTopic.GetAccess(TopicId);
							break;
						case "Owner":
							Page.EditTopic.GetOwner(TopicId);
							break;
						case "Rubrics":
							Page.EditTopic.GetRubrics(TopicId);
							break;
						default:
							Page.EditTopic.GetMain(TopicId);
					}
				}
				else {
					Page.EditTopic.GetMain(TopicId);
				}
			},
			Main: function (TopicId) {
				Hash.Write("Main");
				this.Process(TopicId);
			},
			Censors: function (TopicId) {
				Hash.Write("Censors");
				this.Process(TopicId);
			},
			Access: function (TopicId) {
				Hash.Write("Access");
				this.Process(TopicId);
			},
			Owner: function (TopicId) {
				Hash.Write("Owner");
				this.Process(TopicId);
			},
			Rubrics: function (TopicId) {
				Hash.Write("Rubrics");
				this.Process(TopicId);
			}
		}

	},
	//People
	People: {
		GetAllPeople: function (page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Authors", "GetAllPeople", page, Order), {}, function (result) {
				$("#ListPeople").html(result);
				$("#loading").css("display", "none");
			});
		},
		GetTopicPeople: function (TopicId, Page, Order) {
			$("#loading").css("display", "");
			$.post(MVCAction("Themes", "GetTopicPeople", TopicId, Page, Order), {}, function (result) {
				$("#ListContainer").html(result);
				$("#loading").css("display", "none");
				$("#People").addClass("c");
			});
		},
		Load: {
			Process: function () {
				hash = Hash.Read();
				if (hash.length > 0)
					Page.People.GetAllPeople(hash[0], hash[1]);
				else
					Page.People.GetAllPeople(1, null);
			},
			Authors: function (Page, Order) {
				Hash.Write(Page != 1 ? Page : (Order != null ? Page : null) ? Page : null, Order);
				this.Process();
			}
		},
		CurrentRequester: null,
		GetFriendsRequests: function () {
			$.post(MVCAction("People", "GetFriendRequests"), {}, function (result) {

				for (var i = 0; i < result.length; i++) {
					Page.People.CurrentRequester = result[i].Sender;
					$("#friendrequests").css("display", "");
					$("#senderNick").html("<a href=\"javascript:void(0);\" onclick=\"MVCGo('Authors','" + result[i].RecieverNick + "');\">" + result[i].RecieverNick + "</a>");
				}

			});

			// setTimeout(Page.People.GetFriendsRequests(), 5000000);
		},
		ProcessFriendRequest: function (status) {

			var RequesterUserid = Page.People.CurrentRequester;
			$.post(MVCAction("People", "ProcessRequest", RequesterUserid, status), {}, function (result) {

				window.location.reload(true);
			});
		},
		BreakFrienship: function (UserId, NickName) {
			$("#friendbut").css("display", "none");
			if (confirm("Вы действительно хотите разорвать дружбу ?")) {
				$.post(MVCAction("People", "DeleteFriendship", UserId, NickName), {}, function () {

					window.location.reload(true);
				});
			}
			else {
				$("#friendbut").css("display", "");
			}

		}

	},
	// Comment
	Comment: {
		LastComments: function (PageNumber) {
			$.post(MVCAction("Main", "GetLastComments", PageNumber), {}, function (result) {
				$("#LastCommentsContainer").html(result);
			});
		},
		Bubble: function (Sender, Order) {
			var comment = (Order == 0)
				? $("#Discussion")
				: $("#Comments > [Order = " + Order + "]");
			if (Order == 0)
				$("#cclass").addClass("rep_v2");
			else
				$("#cclass").removeClass("rep_v2");

			comment = comment.length > 0 ? comment[0] : null;
			if (comment != null) {
				$("#ccontent").html($(comment).html());
				$("#CommentBox").css({ display: "", top: ($(Sender).offset().top + 30) + "px" });
			} else {
				$("#CommentBox").css({ display: "", top: ($(Sender).offset().top + 30) + "px" });
				$("#ccontent").html("Loading...");
				$.post(MVCAction("Comment", "Get", TopicId, DiscussId, Order), {}, function (result) {
					$("#ccontent").html(result);
				});
			}

			Bubble = true;
		},
		Moderate: function (TopicId, CommentId, Action) {
			var comment = $("#Comments > [CommentId = " + CommentId + "]");
			comment = comment.length > 0 ? comment[0] : null;

			$(comment).css({ height: $(comment).height() + "px", overflow: "hidden" })
			$(comment).animate({ opacity: "0" }, 100, function () {
				$(comment).html("");
				$.post(MVCAction("Comment", "Moderate", TopicId, CommentId, Action), {}, function (result) {
					var resultdiv = $("<div></div>").html(result);
					$(comment).append(resultdiv);
					$(comment).animate({ height: $(resultdiv).height() + "px", opacity: "1" }, 500);
				});
			});
		},
		Rate: function (Sender, TopicId, CommentId, CommentUserId, MD5, CText, Corder, CDiscussId, DTitleRu, DTitleRo) {
			var Value = $(Sender).attr("param");
			$.ajax({
				url: MVCAction("Comment", "Rate", TopicId, CommentId, CommentUserId, MD5, Value, Corder, CDiscussId),
				data: { CText: CText, DTitleRu: DTitleRu, DTitleRo: DTitleRo },
				success: function (result) {
					if (result.status == "1") {
						var RatioContainer = $("#CommentRatio" + CommentId);
						RatioContainer.text(parseInt(RatioContainer.text()) + parseInt(result.value));
					}
					else
						alert(result.value);
				},
				dataType: "json",
				type: "POST"
			});
		},
		Add: function () {
			if (navigator.userAgent.match(/iPhone|iPod/i)) {

			} else {
				FCKeditorAPI.GetInstance('Text').UpdateLinkedField();
			}
			if ($("#Text").val() != "") {
				$("#AddCommentForm").submit();
			}
		}
	}
}

function LoadScript() {
    var arg = LoadScript.arguments;
    var url = arg[0];
    if (arg.length > 1) var onload = arg[1];
    if (document.createElement && document.getElementsByTagName) {
        var s = document.createElement('script');
        var h = document.getElementsByTagName('head');
        if (s && h.length) {
            s.src = url;
            //if ((type == 'defer') && (s.defer)) s.defer = true;
            //if ((type == 'async') && (s.async)) s.async = true;
            h[0].appendChild(s);
        }
    }
}

function InitWatermark() {
    $("[watermark]").each(function() {
        if ($(this).val() == "")
            $(this).val($(this).attr("watermark"));
        $(this).focus(function() {
            if ($(this).val() == $(this).attr("watermark"))
                $(this).val("");
        });
        $(this).blur(function() {
            if ($(this).val() == "")
                $(this).val($(this).attr("watermark"));
        });
    });
}