﻿var flowli = {}

flowli.collapsed_expanded = function(){
	$(".collapsed,.expanded").live("click", function () {
		$(this).toggleClass("collapsed").toggleClass("expanded");
		$(this).nextAll(".toggle:first").slideToggle("normal");
		return false;
	});

	// collapse after page load
	$(".collapsed").each(function(){
		$(this).nextAll(".toggle:first").hide();
	});
	
	// open text by url#textid
	var open_it_ui = $(window.location.hash)

	if (open_it_ui.present())
		if (open_it_ui.hasClass("collapsed"))
			open_it_ui.click();
};

flowli.form_validation = function(){
	$("form").each(function (i, item) {
		$(item).validate({ errorPlacement: function (error, element) { } });
	});
};

flowli.activator = function () {
	var decoded_uri = decodeURI(window.location.pathname).toLowerCase().split('/');
	$("ul.activate").each(function (i, ul) {

		var url_elements = [];

		$(ul).find("li").each(function (i, li) {
			var envelope = $(li).data("activate");
			if (envelope !== undefined) {

				var url = {
					"url": envelope || "______________",
					"count": $.inArray(envelope.toLowerCase(), decoded_uri),
					"li": $(li)
				};

				url_elements.push(url);
			};
		});

		var poped = url_elements.sort(function (first, second) { return first.count - second.count; }).pop();

		if (poped.count > 0) { poped.li.addClass("active"); };

	});
};

window.log = function(obj) {
	if (window.console) {
		return window.console.log(obj);
	}
};
// usage
// var x = 42;
// var map = function (x) { return eval(x); };
// alert(f("x $x", map));
window.f = function (s, map) {
	return s.replace(/\$(\w+)/g, function (dummy, v) { return map(v);} );
};

window.flowli = flowli;

jQuery.fn.ids = function() {
	var ids;
	ids = this.map(function() {
		return $(this).attr("data-id");
	});
	return $.unique(ids.get());
};
jQuery.fn.blank = function() {
  return this.size() === 0;
};
jQuery.fn.present = function() {
  return this.size() > 0;
};

$(function () {
	flowli.collapsed_expanded();
	
	// msa Call function if plugin here
	if ($.isFunction($.fn.validate)) flowli.form_validation();
	if ($.isFunction($.fn.placeholder)) $("textarea,input").placeholder();
	if ($.isFunction($.fn.smoothScroll)) $(".smoothscroll a").smoothScroll();
	
	flowli.activator();
});
