$.validator.addMethod('positive',
	 function (value) {
		return Number(value) > 0;
	 }, "");

$.validator.addMethod('human',
	 function (value, element) {
		return !(value == $(element).attr("placeholder"));
	 }, "");

$.validator.addMethod("completeUrl", function (val, elem) {
	// if no url, don't do anything
	if ($(elem).val().length == 0) { return true; }

	// if user has not entered http:// https:// or ftp:// assume they mean http://
	if (!/^(https?|ftp):\/\//i.test(val)) {
		val = 'http://' + val; // set both the value
		$(elem).val(val); // also update the form element
	}
	// now check if valid url
	// http://docs.jquery.com/Plugins/Validation/Methods/url
	// contributed by Scott Gonzalez: http://projects.scottsplayground.com/iri/
	return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&amp;'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(val);
});


$.validator.addMethod('uniqueEmail',
	 function (value, element) {
		var result = $.ajax({
			url: '/service/exists/member',
			data: { parameter: value },
			async: false
		}).responseText;

		return !(result === "true");
	 }, "");

$.validator.addMethod('registeredEmail',
	 function (value, element) {
		var result = $.ajax({
			url: '/service/exists/member',
			data: { parameter: value },
			async: false
		}).responseText;

		return (result === "true");
	 }, "");

$.validator.addMethod('accountUnlocked',
	 function (value, element) {
		var result = $.ajax({
			url: '/service/accountUnlocked',
			data: { email: value },
			async: false
		}).responseText.toLowerCase() === 'true';

		if (result)
			$(element).removeClass("error_lockout");
		else
			$(element).addClass("error_lockout");

		return result;
	 }, "");


String.prototype.capitalize = function () {
	return this.charAt(0).toUpperCase() + this.slice(1);
};
String.prototype.toTitleLogin = function () {
	if (this.length > 16) return this.substring(0, 16) + "..";
	else return this;
};
String.prototype.sameas = function (str) {
	if (str != null && str != "") {
		return this.toLowerCase() === str.toLowerCase();
	}
};
function getQueryVariable(variable) {
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	for (var i = 0; i < vars.length; i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return "";
};
//Array.prototype.unique = function () {
//	var a = [];
//	var l = this.length;
//	for (var i = 0; i < l; i++) {
//		for (var j = i + 1; j < l; j++) {
//			if (this[i] === this[j])
//				j = ++i;
//		}
//		a.push(this[i]);
//	}
//	return a;
//};

if (!Array.prototype.forEach) {
	Array.prototype.forEach = function (fun /*, thisp*/) {
		var len = this.length;
		if (typeof fun != "function")
			throw new TypeError();

		var thisp = arguments[1];
		for (var i = 0; i < len; i++) {
			if (i in this)
				fun.call(thisp, this[i], i, this);
		}
	};
};

$(document).ready(function () {
	// generate tags
	$('#tag_service').click(function () {
		var text = $("textarea[name=body]").val();
		$("this").html("Retrieving"); var tag_holder = $(this).parents("table").next();
		$.post("/Service/Tag", { "body": text }, function (data) {
			var tags = data.split(',');
			tag_holder.html("");
			for (var t = 0; t < tags.length; t++) {
				tag_holder.append("<li class='tag'>" + tags[t].capitalize() + " <a href=" + tags[t] + " class=\"remove\">Remove</a><input type='hidden' name='tags' value='" + tags[t] + "' /></li>")
			}
		});

		return false;
	});
	$(".add_tag_action").click(function () {
		var type = $(this).parents(".toggle").attr("id");
		var new_tag = $(this).parents("td").prev().find("input.add_tag_text").val();
		$(this).parents("table").next().prepend("<li class='tag'>" + new_tag + " <a href=" + new_tag + " class=\"remove\">Remove</a><input type='hidden' name='" + type + "' value='" + new_tag + "' /></li>");
		return false;
	});

	$(".tag_container li a.remove").live("click", function () {
		$(this).parent().remove();
		return false;
	});
	/*
	$("li.collapsed > a").click(function () {
	$("li").removeClass("expanded").addClass("collapsed");
	$(this).parent("li").removeClass("collapsed").addClass("expanded");
	return false;
	});*/
	$("li div>a:first-child", ".views").live("click", function (e) {
		e.preventDefault();
		var target = $(this).parent("div");
		if (target.hasClass("expanded")) {
			target.removeClass("expanded").addClass("collapsed");
		} else {
			target.removeClass("collapsed").addClass("expanded");
		}
		return false;
	});
	$("li div>a:first-child", ".menus").live("click", function (e) {
		e.preventDefault();
		$(this).closest("li").siblings().removeClass("active");
		$(this).closest("li").addClass("active");
		return false;

	});
	$("form.validate").validate({ errorPlacement: function (error, element) { } });
	$("ul.highlight li a").each(function (i, item) {
		if ($(item).text().sameas(Env.parameter)) {
			$(item).parent("li").addClass("active");
		}
	});

	$({}).ready(function () {
		var _self = $(".app", this); // perfomance
		_self.each(function (i, item) {
			render(item);
		});
	});

	function render(element) {
		var _title = $(element).attr("data-title");
		if (_title == "dynamic") _title = Env.parameter;
		var _query = {
			title: _title,
			categories: $(element).attr("data-categories"),
			tags: $(element).attr("data-tags")
		}
		if (_query.title || _query.categories || _query.tags) {
			$.get("/service/api", _query, function (json) {
				$(element).parseTemplate(json);
			});
		}
	}

	$("select[name=version]").change(function () {
		$(this).parent("form").submit();
	});
});
function showSidePanel(onoff, ispicture) {
	if (ispicture) {
		if (onoff === true) {
			$("#side_panel_3").show();
			return;
		}
		if (onoff === false) {
			$("#side_panel_3").hide();
			return;
		}
	}
	if (onoff === true) {
		$("#listofviews").show();
		$("#side_panel_2").show();
	}
	if (onoff === false) {
		$("#listofviews").hide();
		$("#side_panel_2").hide();
	}
} 
