/**
*	@name							Accordion
*	@descripton						This Jquery plugin makes creating accordions pain free
*	@version						1.3
*	@requires						Jquery 1.2.6+
*
*	@author							Jan Jarfalk
*	@author-email					jan.jarfalk@unwrongest.com
*	@author-website					http://www.unwrongest.com
*
*	@licens							MIT License - http://www.opensource.org/licenses/mit-license.php
*/

(function(jQuery){
     jQuery.fn.extend({  
         accordion: function() {       
            return this.each(function() {
            	
            	var $ul = $(this);
            	
				if($ul.data('accordiated'))
					return false;
											
				//$.each($ul.find('ul, li>div'), function(){
				$.each($ul.find('ul ul'), function(){
					$(this).data('accordiated', true);
					$(this).hide();
				});
				
				//$.each($ul.find('a'), function(){
				$.each($ul.find('a:not(.foo)'), function(){
					$(this).click(function(e){
						e.preventDefault();
						/* // ovo je da osigura da je bar jedan otvoren(kad se klikne na otvoreni da ga ne zatvori
							var current = $(this)['parent']('li').attr('class');
							if (!(current == 'current active' || current == 'active')) { activate(e.target); }
						// ----- */
						
						/*
						// DODANO ZA HASH ---
						// zapiši hash
						document.location.hash=$(this).attr("href"); 
						// ------------------
						*/
						
						activate(this);
						return void(0);
					});
				});
				
				var active = (location.hash)?$(this).find('a[href=' + location.hash + ']')[0]:'';

				if(active){
					activate(active, 'toggle');
					$(active).parents().show();
				}
				
				function activate(el,effect){
					$(el).parent('li').toggleClass('active').siblings().removeClass('active').children('ul, div').slideUp('slow');
					$(el).siblings('ul, div')[(effect || 'slideToggle')]((!effect)?'slow':null);
					/*// DODANO ZA HASH ---
					if(!($(el).parent('li').hasClass('active'))) {
						if ("pushState" in history)
							history.pushState("", document.title, window.location.pathname);
						else
							window.location.hash = "";
					}
					// ------------------
					*/
				}
				
            });
        } 
    }); 
})(jQuery);
