
var filteredKeywords = {};

(function (a) { a.fn.extend({ easySelectBox: function (b) { var c = { className: "easy-select-box", speed: 0 }; var b = a.extend(c, b); return this.each(function () { var j = b; var k = a(this); if ("select" != k[0].nodeName.toLowerCase()) { return false } var d = k.children("option"); var h = null; var i = k.val(); var g = "easy-select-box-disp"; var e = 0; var f = ""; a.each(d, function (l, m) { f += '<li><a href="#" rel="' + a(m).val() + '">' + a(m).text() + "</a></li>"; if (i == a(m).val()) { e = l } }); f = '<div class="' + j.className + '"><a class="' + g + '" href="#">' + d.eq(e).text() + "</a><ul>" + f + "</ul></div>"; h = a(f).insertAfter(k); k.hide(); h.data("o", j); easySelectRegistry = a(document).data("easySelect"); if (easySelectRegistry == null) { easySelectRegistry = new Array() } easySelectRegistry.push(h); a(document).data("easySelect", easySelectRegistry); h.click(function (l) { if (a(h).children("ul").is(":visible")) { a(h).children("ul").slideUp(j.speed); h.css("z-index", 99) } else { easySelectRegistry = a(document).data("easySelect"); if (easySelectRegistry != null) { a.each(easySelectRegistry, function () { opts = a(this).data("o"); a(this).children("ul").slideUp(opts.speed); a(this).css("z-index", 99) }) } a(h).children("ul").slideDown(j.speed); h.css("z-index", 100) } l.stopPropagation(); return false }); a(document).click(function () { easySelectRegistry = a(document).data("easySelect"); if (easySelectRegistry != null) { a.each(easySelectRegistry, function () { if (a(this).children("ul").is(":visible")) { opts = a(this).data("o"); a(this).children("ul").slideUp(opts.speed) } }) } }); h.children("ul").children("li").click(function () { h.children("." + g).html(a(this).children("a").html()); k.children("option").removeAttr("selected"); k.find("option").eq(a(this).index()).attr("selected", "selected");/*$(k).change() */}) }) } }) })(jQuery);

var getQueryString_reg = /[\?|\&](\w+)=(\w+)/g;
var hashSplitterReg = /\/((\w+):((\w+)[=<>]?(\w+)?))/g
var curretnHash = "";

function getQueryString(){
    var obj = {}; 
    var res;
    while(res = getQueryString_reg.exec(window.location.href)){
        obj[res[1]] = res[2];
    }
    
    return obj;
}

$(document).ready(function(){
   
   (function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
   
	$(".filterSelector").easySelectBox();
	$(".filterSelector").change(function(event){
        var ref = event.target.options[event.target.selectedIndex].value;
        changeReferanceFilter(ref);
    });
    $(".easy-select-box li a").click(function(){
        changeReferanceFilter($(this).attr("rel"), $(this).parent().parent().parent().prev().attr("id"));
    });
	
	$("a", ".referancesItem").click(function(){
		window.location.hash = curretnHash;
	});
	
	$(window).hashchange(function(){
		parseHash();
	});
	
	parseHash();
/*
	var gs = getQueryString();

	if(gs.tag)
	{

		for(i in gs){
			var optionTag = $('.filterSelector option[value="'+gs[i]+'"]');
			var selectTag = optionTag.parent();
			$(selectTag.next().children()[0]).html(optionTag.html());
			changeReferanceFilter(gs[i], selectTag.attr("id"));
		}
	}
	*/
});

function parseHash(){
	/*
	$(".filter").each(function(index, element){
		element.checked = false;
	});
	*/
	var hash = location.hash;
	if(hash !== ""){
		var hashResults;
		while(hashResults = hashSplitterReg.exec(hash)){
			var element = $("#"+hashResults[2])[0];
			switch(element.nodeName){
				case "SELECT":
					//dett er funcked up
					$($(element).next().find("a")[0]).html($(element).next().find("a[rel="+hashResults[3]+"]").html());
				break;
				case "INPUT":
					element.value = hashResults[5];
				break;
			}
			changeReferanceFilter(hashResults[3], hashResults[2]);
		}
	}else{
		filteredKeywords = {};
		changeReferanceFilter("*", "_");
	}

}
	
function changeReferanceFilter(ref, rel){
    
    filteredKeywords[rel] = ref;

    var classSearch = "";

    for(var i in filteredKeywords){
        if(filteredKeywords[i] != "*"){
            if(classSearch.length != 0){
                classSearch += " ";
            }

            classSearch += filteredKeywords[i];
        }
    }
    
	curretnHash = "#/";
	var filteredKeywordsIsEmpty = true;
	for(var key in filteredKeywords){
		if(filteredKeywords[key] !== "*"){
			curretnHash += key+":"+filteredKeywords[key]+"/";
		}
	   // break;
	}
	
    if(classSearch  === ""){
		$(".referanceList .referancesItem").slideDown(200);
		$(".referanceList .referancesItem").addClass("youcanseeme");
	}else{
		$(".referanceList .referancesItem").each(function(index, element){
			
			var showThis = true;
			
			for(var i in filteredKeywords){
				if(filteredKeywords[i] != "*" && !$(element).hasClass(filteredKeywords[i])){
					showThis = false;
				}
			}

			if(showThis){
				$(element).slideDown(200);
				$(element).addClass("youcanseeme");
			}else{
				$(element).slideUp(200);
				$(element).removeClass("youcanseeme");
			}
		});
	}
	setTimeout(reflowSplitters, 200);
}

function reflowSplitters(){
	var counter = 0;
	
	$(".rowSplitter").remove();
	$(".youcanseeme").each(function(index, element){
		counter ++;
		if(counter % 3 == 0){
			$(this).after('<div class="rowSplitter"></div>');
		}
	});
}

