/* 
 * Horniman Museum
 *
 * @author		Igino Fucci
 * @copyright	Copyright (c) 2011 (Keepthinking Ltd.)
 * @link		http://keepthinking.it
 * 
 -------------------------------------------------------------------  */

(function(a){a.fn.dropdown=function(){a(".dropdown_controller").length&&(a(".dropdown_controller").find("select").each(function(){var e=a(this),f=e.parents(".dropdown_wrapper:first"),g=a("#"+e.attr("id")+" :selected").text(),c=e.find("option:selected").text()=="All"?"":"selected";dropdown=a("<div class='dropdown'><div class='dropdown_toggle "+c+"'><span>"+g+"</span></div></div>");f.append(dropdown);e.parent(".mask").hide()}),a(".dropdown_toggle").click(function(e){e.stopPropagation();var f=a(this), g=f.parents(".dropdown_controller:first"),c=f.parents(".dropdown_wrapper:first"),e=c.find(".dropdown:first"),j=c.find("select"),h=c.find(".breadcrumbs"),k=f.position(),i=j.attr("id")+"_dropdown";a(".dropdown_toggle").not(f).removeClass("focus");a(".dropdown_body").not(c.find(".dropdown_body")).fadeOut();f.addClass("focus");if(a("#"+i).length)var d=a("#"+i);else{var d=a("<div id='"+i+"' class='dropdown_body'><div class='dropdown_arrow'></div><div class='breadcrumbs'>"+h.html()+"</div></div>"),h=j.find("optgroup"), l="";c.find(".dropdown").append(d);h.each(function(){var b=a(this).find("option"),d=a("<ul></ul>");b.each(function(){l=a(this).val()==j.val()?" class='active'":"";var b=a("<li"+l+"><a href='#' title='"+a(this).text()+"'>"+a(this).text()+"</a></li>");b.data("relatedSelect",j.attr("id"));b.data("relatedOption",a(this).val());b.data("relatedOptionText",a(this).text());d.append(b)});c.find(".dropdown_body").append(d)});h=g.width()-k.left;g=g.width()-d.width();i=c.find(".dropdown_arrow");h>d.width()?d.css("left", k.left+"px"):(d.css("left",g-10+"px"),i.css("left",k.left-g+20+"px"))}d.fadeIn();a(".dropdown_body a").click(function(b){if(a(this).parents(".breadcrumbs").length==0){b.preventDefault();var b=a(this),c=b.parents(".dropdown_wrapper"),e=c.find(".dropdown_toggle"),f=b.parent("li").data("relatedSelect"),g=b.parent("li").data("relatedOption"),h=b.parent("li").data("relatedOptionText");a("#"+f).val(g);c.find(".dropdown_toggle span").html(h);e.removeClass("focus").addClass("selected");d.fadeOut();b.parents("form").submit()}}); a("html").click(function(){f.removeClass("focus");d.fadeOut()});e.click(function(a){a.stopPropagation()})}))}})(jQuery);
