$.validator.addMethod('positive',
    function (value) {
    	return Number(value) > 0;
    }, "");
    $.validator.addMethod('human',
    function (value, element) {
		return !(value == $(element).attr("placeholder"));
    }, "");

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 = $(this).find(".app");
		_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();
	}
} 
