Downloads = function() {
	this._url = "/service/download";
	this._confirmUrl = "/service/download/confirm";
	
	this._smsInfo = [];
	this._smsInfo["ru"] = new Object();
	this._smsInfo["ru"].name = "Россия";
	this._smsInfo["ru"].text = "<b>1151</b> Стоимость SMS 10 RUR без НДС";
	this._smsInfo["kir"] = new Object();
	this._smsInfo["kir"].name = "Киргизия";
	this._smsInfo["kir"].text = "<b>1131</b> Стоимость SMS 0.3 USD без НДС";	
	this._smsInfo["lat"] = new Object();
	this._smsInfo["lat"].name = "Латвия";
	this._smsInfo["lat"].text = "<b>1871</b> Стоимость SMS 0.3 LVL без НДС";	
	this._smsInfo["lit"] = new Object();
	this._smsInfo["lit"].name = "Литва";
	this._smsInfo["lit"].text = "<b>1381</b> Стоимость SMS 0.85 LTL без НДС";	
	this._smsInfo["est"] = new Object();
	this._smsInfo["est"].name = "Эстония";
	this._smsInfo["est"].text = "<b>17010</b> Стоимость SMS 4.24 EEK без НДС";	
	this._smsInfo["bel"] = new Object();
	this._smsInfo["bel"].name = "Бельгия";
	this._smsInfo["bel"].text = "<b>7796</b> Стоимость SMS 1.58 EUR без НДС";	
	this._smsInfo["fr"] = new Object();
	this._smsInfo["fr"].name = "Франция";
	this._smsInfo["fr"].text = "<b>83868</b> Стоимость SMS 2.41 EUR без НДС";	
	this._smsInfo[""] = new Object();
	this._smsInfo[""].name = "Австрия";
	this._smsInfo[""].text = "<b>0930399999</b> Стоимость SMS 1.60 EUR без НДС";	
	this._smsInfo["de"] = new Object();
	this._smsInfo["de"].name = "Германия";
	this._smsInfo["de"].text = "<b>44442</b> Стоимость SMS 0.83 EUR без НДС";	
	this._smsInfo["gol"] = new Object();
	this._smsInfo["gol"].name = "Голландия";
	this._smsInfo["gol"].text = "<b>7117</b> Стоимость SMS 1.26 EUR без НДС";	
	this._smsInfo["esp"] = new Object();
	this._smsInfo["esp"].name = "Испания";
	this._smsInfo["esp"].text = "<b>5339</b> Стоимость SMS 1.20 EUR без НДС";	
	this._smsInfo["nr"] = new Object();
	this._smsInfo["nr"].name = "Норвегия";
	this._smsInfo["nr"].text = "<b>2322</b> Стоимость SMS 16.00 NOK без НДС";	
	this._smsInfo["pol"] = new Object();
	this._smsInfo["pol"].name = "Польша";
	this._smsInfo["pol"].text = "<b>7910</b> Стоимость SMS 9.00 PLN без НДС";	
	this._smsInfo["por"] = new Object();
	this._smsInfo["por"].name = "Португалия";
	this._smsInfo["por"].text = "<b>4565</b> Стоимость SMS 1.65 EUR без НДС";	
	this._smsInfo["fin"] = new Object();
	this._smsInfo["fin"].name = "Финляндия";
	this._smsInfo["fin"].text = "<b>179479</b> Стоимость SMS 1.64 EUR без НДС";	
	this._smsInfo["csh"] = new Object();
	this._smsInfo["csh"].name = "Чехия";
	this._smsInfo["csh"].text = "<b>9090150</b> Стоимость SMS 42.20 CZK без НДС";	
	this._smsInfo["shv"] = new Object();
	this._smsInfo["shv"].name = "Швеция";
	this._smsInfo["shv"].text = "<b>72170</b> Стоимость SMS 16.00 SEK без НДС";	
	this._smsInfo["bol"] = new Object();
	this._smsInfo["bol"].name = "Болгария";
	this._smsInfo["bol"].text = "<b>1098</b> Стоимость SMS 2.00 BGN без НДС";	
	this._smsInfo["ven"] = new Object();
	this._smsInfo["ven"].name = "Венгрия";
	this._smsInfo["ven"].text = "<b>90645045</b> Стоимость SMS 550.00 HUF без НДС";	
	this._smsInfo["dn"] = new Object();
	this._smsInfo["dn"].name = "Дания";
	this._smsInfo["dn"].text = "<b>1945</b> Стоимость SMS 16.00 DKK без НДС";
}

Downloads.prototype = {
	download: function(downloadId) {
		this._downloadId = downloadId;
		$("span#link").hide();
		
		$.getJSON(this._url, this.getParameters(), function(data) {
			try {
				if(data.success) {
					$("span#link").html("<a href=\"" + data.url + "\" title=\"Скачать " + data.label + "\">" + data.label + "</a>");
					$("span#downloadCount").html(data.count);
					if(data.free) {
						$("div#paymentPanel").show();
					}
				}
			} catch (e) { // ignore
			} finally {
				$("span#link").show();
			}
		});
	},
	pay: function(token) {
	},
	getParameters: function() {
		return "downloadId=" + this._downloadId + mjs.getUtils().randomize();
	},
	getConfirmParameters: function() {
		return "downloadId=" + this._downloadId + "&" + $("input#token").serialize() + mjs.getUtils().randomize();
	},
	selectCountry: function() {
		$("#countries").slideToggle(1000);
		return false;
	},
	onSelect: function(country) {
		var info = this._smsInfo[country];
		if(info != null) {
			$("#countries").slideUp(1000, function() {
				$("a#country").html(info.name);
				$("span#info").html(info.text);
			});
		}
		return false;
	},
	notifyToken: function() {
		return true;
	},
	confirmToken: function() {
		$.getJSON(this._confirmUrl, this.getConfirmParameters(), function(data) {
			try {
				if(data.success) {
					$("span#link").html("<a rel=\"nofollow\" href=\"" + data.url + "\" title=\"Скачать " + data.label + "\">" + data.label + "</a>");
					$("#paymentPanel").slideUp(1300, function() {
						$("li#warn").slideUp(600);
					});
				} else {
					$("span#error").show();
				}
			} catch (e) { // ignore
			} finally {
				$("span#link").show();
			}
		});
		
		return false;
	}
}

// support service
SupportService = function() {
	
}

SupportService.prototype = {
	url: "/service/support",
	addMessage: function() {
		if(mjs.getUtils().isEmpty(this.getName().val())) {
			return this.error("name");
		} else {
			this.info("name");
		}
		if(mjs.getUtils().isEmpty(this.getEmail().val())) {
			return this.error("email");
		} else {
			this.info("email");
		}
		if(mjs.getUtils().isEmpty(this.getMessage().val())) {
			return this.error("message");
		} else {
			this.info("message");
		}
		// send message to support 
		$.get(this.url, $("div#addMessage form").serialize() + mjs.getUtils().randomize(), function(data, textStatus) {
			mjs.getSupportService().getMessage().val("");
			mjs.getSupportService().getSuccessMessage().show("slide");
			return false;
		}, "text");
		return false;
	},
	notify: function(field) {
		if("name" == field) {
			if(mjs.getUtils().isEmpty(this.getName().val())) {
				this.error("name")
			} else {
				this.info("name");
			}
		} else if("email" == field) {
			if(mjs.getUtils().isEmpty(this.getEmail().val())) {
				this.error("email");
			} else {
				this.info("email");
			}
		} else if("message" == field) {
			if(mjs.getUtils().isEmpty(this.getMessage().val())) {
				this.error("message");
			} else {
				this.info("message");
			}
		}
	},
	info: function(field) {
		if("name" == field) {
			this.getName().removeClass("error");
			this.getNameValidator().hide("slide");
		} else if("email" == field) {
			this.getEmail().removeClass();
			this.getEmailValidator().hide("slide");
		} else if("message" == field) {
			this.getMessage().removeClass("error");
			this.getMessageValidator().hide("slide");
		}
		return false;
	},
	error: function(field) {
		if("name" == field) {
			var name = this.getName();
			name.addClass("error");
			name.focus();
			this.getNameValidator().show("slide");
		} else if("email" == field) {
			var email = this.getEmail();
			email.addClass("error");
			email.focus();
			this.getEmailValidator().show("slide");
		} else if("message" == field) {
			var message = this.getMessage();
			message.addClass("error");
			message.focus();			
			this.getMessageValidator().show("slide");
		}
		return false;
	},
	getName: function() {
		return $("[name=name]");
	},
	getEmail: function() {
		return $("[name=email]");
	},
	getMessage: function() {
		return $('[name=message]');
	},
	getNameValidator: function() {
		return $("span#nameValidator");
	},
	getEmailValidator: function() {
		return $("span#emailValidator");
	},
	getMessageValidator: function() {
		return $('span#messageValidator');
	},
	getSuccessMessage: function() {
		return $("#messageSuccess");
	}
}

// FAQ
FAQ = function() {
	this.pq = null;
	this.psq = null;
	this.reh = true;
	this.resh = true;
}

FAQ.prototype = {
	faqExpandHeader: function (q) {
		if(!this.reh) {
			return;
		}
		this.reh = false;
		if(this.pq != null && q != this.pq) {
			$(this.pq).slideUp(700, function() {
				$(q).slideToggle(700, function() {
					mjs.getFaq().reh=true;
				});
			});
		} else {
			$(q).slideToggle(700, function() {
				mjs.getFaq().reh=true;
			});
		}
		this.pq = q;
	},
	faqExpandSubHeader: function (q) {
		if(!this.resh) {
			return;
		}
		this.resh = false;
		if(this.psq != null && q != this.psq) {
			$(this.psq).slideUp(700, function() {
				$(q).slideToggle(700, function() {
					mjs.getFaq().resh=true;
				});
			});
		} else {
			$(q).slideToggle(700,function() {
				mjs.getFaq().resh=true;
			});
		}
		this.psq = q;
	}
}

// Sidebars
Sidebar = function() {
}

Sidebar.prototype = {
	toggleSidebar: function (btn,el) {
		el.slideToggle(700, function() {
			if(el.is(":visible")) {
				btn.removeClass("closed");
			} else {
				btn.addClass("closed");
			}
		});
	}
}

// Comments
Comments = function() {
	this._separatorHTML = "<div class=\"hr\"></div>";
	this._titleHTML = "<h3><span id=\"titleCount\">:count</span> для сериала <span class=\"colour\">:title</span></h3>";
	this._commentHTML = "<div id=\":id\" class=\"comment\"><p class=\"author colour\">:name</p><p class=\"date\">:date</p><p class=\"message\">:message</p></div>";
	this._paginationHTML = "<div id=\"pagination\"><div class=\"left\"><a href=\":url?page=1#comments\" class=\"f\">Первая</a><a href=\":url?page=:prev#comments\" class=\"p\">Предыдущая</a></div><div class=\"pages\"><a href=\":url?page=:prev3#comments\" class=\"colour\">&hellip;</a><a href=\":url?page=:page1#comments\" class=\"colour :on1\">:page1</a><a href=\":url?page=:page2#comments\" :visible2 class=\"colour :on2\">:page2</a><a href=\":url?page=:page3#comments\" :visible3 class=\"colour\">:page3</a><a href=\":url?page=:next3#comments\" class=\"colour\">&hellip;</a></div><div class=\"right\"><a href=\":url?page=:next#comments\" class=\"n on colour\">Следующая</a><a href=\":url?page=:last#comments\" class=\"l on colour\">Последняя</a></div></div>";
	this._urlComments = "/service/comments";
	this._urlAddComment = "/service/comments/new";
}

Comments.prototype = {		
	load: function(contentUrl, contentId, page) {
		this._contentUrl = contentUrl;
		this._contentId = contentId;
		this._page = page;		
	},
	add: function() {
		if(mjs.getUtils().isEmpty(this.getName().val())) {
			return this.error("name");
		} else {
			this.info("name");
		}
		if(mjs.getUtils().isEmpty(this.getEmail().val())) {
			return this.error("email");
		} else {
			this.info("email");
		}
		if(mjs.getUtils().isEmpty(this.getMessage().val())) {
			return this.error("message");
		} else {
			this.info("message");
		}
		// send comment
		$.getJSON(this._urlAddComment, $("div#addMessage form").serialize() + "&contentId=" + this._contentId + mjs.getUtils().randomize(), function(data) {
			if(data.success) {
				$("span#numberOfComments").html(data.count);
				$("span#titleCount").html(mjs.getUtils().getDeclensionString(data.count, ["комментарий", "комментариев", "комментария"]));
				if(mjs.getComments()._page == 1) {
					mjs.getComments().addComment(data.id, mjs.getComments().getName().val(), data.date, mjs.getComments().getMessage().val());
				}
				mjs.getComments().getMessage().val("");
			}
		});
		return false;
	},
	addComment: function(id, name, date, message) {
		$("div#comments h3").next().after(this._commentHTML.replace(/:id/, id).replace(/:name/, name).replace(/:date/, date).replace(/:message/, message) + "<div class=\"hr\"></div>")
	},
	info: function(field) {
		if("name" == field) {
			this.getName().removeClass("error");
			this.getNameValidator().hide("slide");
		} else if("email" == field) {
			this.getEmail().removeClass();
			this.getEmailValidator().hide("slide");
		} else if("message" == field) {
			this.getMessage().removeClass("error");
			this.getMessageValidator().hide("slide");
		}
		return false;
	},
	error: function(field) {
		if("name" == field) {
			var name = this.getName();
			name.addClass("error");
			name.focus();
			this.getNameValidator().show("slide");
		} else if("email" == field) {
			var email = this.getEmail();
			email.addClass("error");
			email.focus();
			this.getEmailValidator().show("slide");
		} else if("message" == field) {
			var message = this.getMessage();
			message.addClass("error");
			message.focus();			
			this.getMessageValidator().show("slide");
		}
		return false;
	},
	getName: function() {
		return $("[name=name]");
	},
	getEmail: function() {
		return $("[name=email]");
	},
	getMessage: function() {
		return $('[name=message]');
	},
	getNameValidator: function() {
		return $("span#nameValidator");
	},
	getEmailValidator: function() {
		return $("span#emailValidator");
	},
	getMessageValidator: function() {
		return $('span#messageValidator');
	},
	notify: function(field) {
		if("name" == field) {
			if(mjs.getUtils().isEmpty(this.getName().val())) {
				this.error("name")
			} else {
				this.info("name");
			}
		} else if("email" == field) {
			if(mjs.getUtils().isEmpty(this.getEmail().val())) {
				this.error("email");
			} else {
				this.info("email");
			}
		} else if("message" == field) {
			if(mjs.getUtils().isEmpty(this.getMessage().val())) {
				this.error("message");
			} else {
				this.info("message");
			}
		}
	},
	getParameters: function() {
		return "contentId=" + this._contentId + "&page=" + this._page + mjs.getUtils().randomize();
	},
	getCommentHtml: function(comment) {
		return this._commentHTML.replace(/:id/, comment.id).replace(/:name/, comment.name).replace(/:date/, comment.date).replace(/:message/, comment.message);
	},
	getTitle: function(title, count) {
		return this._titleHTML.replace(/:count/, mjs.getUtils().getDeclensionString(count, ["комментарий", "комментариев", "комментария"])).replace(/:title/, title);
	},
	getSeparator: function() {
		return this._separatorHTML;
	},
	getPagination: function(count, perPage) {
		var pageCount = Math.ceil(count / perPage);
		var page1 = "";
		var page2 = "";
		var page3 = "";
		
		var visible2 = "style=\"display: none;\"";
		var visible3 = "style=\"display: none;\"";
		
		var on1 = "";
		var on2 = "";
		
		if (this._page == 1) {
			page1 = 1;
			page2 = 2;
			page3 = 3;
			on1 = "on";
			if(pageCount > 1) {
				visible2 = "";
			}
			if(pageCount > 2) {
				visible3 = "";
			}
		} else {
			on2 = "on";
			page1 = this._page - 1;
			page2 = this._page;
			page3 = this._page + 1;
			visible2 = "";
			if(page3 <= pageCount) {
				visible3 = "";
			}
		}
		return this._paginationHTML
			.replace(/:url/g, this.getContentUrl())
			.replace(/:prev/,Math.max(1, this._page - 1))
			.replace(/:prev3/,Math.max(1, this._page - 3))
			.replace(/:page1/g, page1)
			.replace(/:page2/g,page2)
			.replace(/:page3/g,page3)	
			.replace(/:on1/, on1)
			.replace(/:on2/, on2)
			.replace(/:visible2/, visible2)
			.replace(/:visible3/, visible3)
			.replace(/:next3/,Math.min(pageCount, this._page + 3))
			.replace(/:next/, Math.min(pageCount, this._page + 1))
			.replace(/:last/,pageCount);
	},
	getContentUrl: function() {
		return this._contentUrl;
	}
}

// Utils
Utils = function() {
	
}

Utils.prototype = {
	splitTime: function(second) {
		var h=Math.floor(second/3600);
		var m=Math.floor(second/60)-(h*60);
		if(h > 0) {
			return (h < 10 ? "0" : "") + h + ":" + (m < 10 ? "0" : "") + m;
		} else {
			var s=second-(h*3600)-(m*60);
			return (m < 10 ? "0" : "") + m + ":" + (s < 10 ? "0" : "") + s;
		}
	},
	isEmpty: function(text) {
		return text == null || text.length == 0;
	},
	randomize: function() {
		return "&r=" + String((new Date()).getTime()).replace(/\D/gi,"");
	},
	getDeclensionString: function(number, words) {
		var digit = (number < 0 ? (number - number * 2) : number) % 100;
		digit = digit > 20 ? digit % 10 : digit;
		return number + " " + (digit == 1 ? words[0] : (digit > 4 || digit < 1 ? words[1] : words[2]));
	}
}

MJS = function() {	
}

MJS.prototype = {
	getDownloads: function() {
		if(this.downloads == null) {
			this.downloads = new Downloads();
		}
		return this.downloads;
	},
	getSupportService: function() {
		if(this.supportService == null) {
			this.supportService = new SupportService();
		}
		return this.supportService;
	},
	getUtils: function() {
		if(this.utils == null) {
			this.utils = new Utils();
		}
		return this.utils;
	},
	getFaq: function() {
		if(this.faq == null) {
			this.faq = new FAQ();
		}
		return this.faq;
	},
	getSidebar: function() {
		if(this.sidebar == null) {
			this.sidebar = new Sidebar();
		}
		return this.sidebar;
	},
	getComments: function() {
		if(this.comments == null) {
			this.comments = new Comments();
		}
		return this.comments;
	}
}

// Mnogoserialov JS global service
var mjs = new MJS();





















