

jQuery.fn.extend({
	
	nextUntil: function(expr) {
	    var match = [];

	  
	    this.each(function(){
	      
	        for( var i = this.nextSibling; i; i = i.nextSibling ) {
	    
	            if ( i.nodeType != 1 ) continue;

	         
	            if ( jQuery.filter( expr, [i] ).r.length ) break;

            
	            match.push( i );
	        }
	    });

	    return this.pushStack( match );
	},

	Accordion: function(settings) {
		
		settings = jQuery.extend({}, jQuery.Accordion.defaults, {
			
			header: jQuery(':first-child', this)[0].tagName 
		}, settings);
    if ( settings.menu ) {
			var current = this.find("a").filter(function() {
			  return (location.href == this.href);
		  });
		  
		  if (current.length == 0) {
			    current = this.find("a").filter(function() {

			      if (location.href.match("/KS/haberler/") && this.href.match("/KS/haberler/arsiv.php")) {
			          return true;
			      }
				  
				   if (location.href.match("/KS/istatistik/sezon.php") && this.href.match("/KS/istatistik/sezon.php")) {
			          return true;
			      }
				  
			      if (location.href.match("/KS/sezon/consult/buteur.php") && this.href.match("/KS/sezon/consult/istatistikler.php")) {
			          return true;
			      }
				  if (location.href.match("/KS/sezon/consult/compo.php") && this.href.match("/KS/sezon/consult/istatistikler.php")) {
			          return true;
			      }
				  if (location.href.match("/KS/sezon/consult/compo2.php") && this.href.match("/KS/sezon/consult/istatistikler.php")) {
			          return true;
			      }
				  if (location.href.match("/KS/sezon/consult/compoCJ.php") && this.href.match("/KS/sezon/consult/istatistikler.php")) {
			          return true;
			      }
				  if (location.href.match("/KS/sezon/consult/compoCR.php") && this.href.match("/KS/sezon/consult/istatistikler.php")) {
			          return true;
			      }
			      if (location.href.match("/KS/takimlar/A_Takim/futbolcular") && this.href.match("/KS/takimlar/A_Takim/futbolcular")) {
			          return true;
			      }
				  if (location.href.match("/KS/takimlar/A_Takim/futbolcular") && this.href.match("/KS/takimlar/A_Takim/futbolcular")) {
			          return true;
			      }
				   if (location.href.match("/KS/takimlar/A_Takim/teknik-kadro") && this.href.match("/KS/takimlar/A_Takim/teknik-kadro")) {
			          return true;
			      }
				   if (location.href.match("/KS/altyapi/U17/U17_Puan_Fikstur") && this.href.match("/KS/altyapi/U17/U17_Puan_Fikstur")) {
			          return true;
			      }
				   if (location.href.match("/KS/altyapi/U17/index.php") && this.href.match("/KS/altyapi/U17/index.php")) {
			          return true;
			      }
			      if (location.href.match("/KS/image_gallery") && this.href.match("/KS/image_gallery")) {
			          return true;
			      }
			      if (location.href.match("/tickets/matchdayinfo/") && this.href.match("/tickets/ticketinfo_latest.html")) {
			          return true;
			      }
			      if (location.href.match("/academy/first_year/") && this.href.match("/academy/academy_academyplayers.html")) {
			          return true;
			      }
			      if (location.href.match("/academy/second_year/") && this.href.match("/academy/academy_academyplayers.html")) {
			          return true;
			      }
			      if (location.href.match("/history/hall_of_fame/") && this.href.match("/history/history_hof.html")) {
			          return true;
			      }
			      if (location.href.match("/history/legends/") && this.href.match("/history/history_legends.html")) {
			          return true;
			      }
			      if (location.href.match("/history/managers/") && this.href.match("/history/history_managers.html")) {
			          return true;
			      }
			      if (location.href.match("/history/great_games/") && this.href.match("/history/history_great_games.html")) {
			          return true;
			      }
			      if (location.href.match("/soccerschools/news/") && this.href.match("/soccerschools/coaching_zone.html")) {
			          return true;
			      }
			      if (location.href.match("/academy/news/") && this.href.match("/academy/academy_latestnews.html")) {
			          return true;
			      }
			      if (location.href.match("/foundation/news/") && this.href.match("/foundation/foundation_news.html")) {
			          return true;
			      }
			      if (location.href.match("/events/suites/") && this.href.match("/events/conference_suites.html")) {
			          return true;
			      }
			      if (location.href.match("/news/archive/") && this.href.match("/news/news_papers.html")) {
			          return true;
			      }
			      if (location.href.match("/foundation/") && this.href.match("/foundation/spurslearningzone.html")) {
			          return true;
			      }

			      return false;

			    });
		  }
			if ( current.length ) {
			  var nodes = current.parents().filter(function() {
			      return $("li", this);
			    }).children("a.head");
			    settings.active = current.add(nodes);
  				current.addClass(settings.selectedClass);
			}
		}
		
		var container = this,
			active = settings.active
				? jQuery(settings.active, this)
				: settings.active === false
					? jQuery("<li>")
					: jQuery(settings.header, this).eq(0),
			running = 0;

		var headers = container.find(settings.header);
		headers
			.not(active || "")
			.nextUntil(settings.header)
			.hide();
		active.addClass(settings.selectedClass);
		var toShow = active.nextUntil(settings.header);
		toShow.slideDown(settings.showSpeed);

    function toggle(toShow, toHide, data, clickedActive) {
			var finished = function(cancel) {
				running = cancel ? 0 : --running;
				if ( running )
					return;
				
				container.trigger("change", data);
			};

			
			running = toHide.size() + toShow.size();

			if ( settings.animated ) {
				if ( !settings.alwaysOpen && clickedActive ) {
					toShow.slideToggle(settings.showSpeed);
					finished(true);
				} else {
					toHide.filter(":hidden").each(finished).end().filter(":visible").slideUp(settings.hideSpeed, finished);
					toShow.slideDown(settings.showSpeed, finished);
				}
			} else {
				if ( !settings.alwaysOpen && clickedActive ) {
					toShow.toggle();
				} else {
					toHide.hide();
					toShow.show();
				}
				finished(true);
			}
		}

		function clickHandler(event) {
			if ( !event.target && !settings.alwaysOpen ) {
				active.toggleClass(settings.selectedClass);
				var toHide = active.nextUntil(settings.header);
				var toShow = active = jQuery([]);
				toggle( toShow, toHide );
			}

			
			var clicked = jQuery(event.target);

      if (clicked.length > 0 && !clicked[0].href.match("javascript:")) {
          if (clicked[0].target != "_blank") {
            location.href = clicked[0].href;
          }
          return;
      }

			
			if ( clicked.parents(settings.header).length )
				while ( !clicked.is(settings.header) )
				{
					clicked = clicked.parent();
				}

			var clickedActive = clicked[0] == active[0];

			
			if(running || (settings.alwaysOpen && clickedActive) || !clicked.is(settings.header))
				return;

			
			active.not(clicked.parents().filter(function() {
			      return $("li", this);
			    }).children("a.head")).toggleClass(settings.selectedClass);
			if ( !clickedActive ) {
				clicked.addClass(settings.selectedClass);
			} else {
  			clicked.toggleClass(settings.selectedClass);
	    }
			
			var toShow = clicked.nextUntil(settings.header),
			  // hide the active ul
				toHide = active.nextUntil(settings.header).add(
				// now add parents of the active ul for removal
				  active.parents().filter(
				    // but leave out level1 list elements
				    function() {
			        return $("li.level1", this) == 0;
			      }
			    ).children("a.head")
			  
			  ).not(clicked.parents()),
				data = [clicked, active, toShow, toHide];
		  active = clickedActive ? jQuery([]) : clicked;
		  toggle( toShow, toHide, data, clickedActive );

			return !toShow.length;
		};
		function activateHandler(event, index) {
			if ( index == null )
				return;
			
			clickHandler({
				target: index >= 0
					? jQuery(settings.header, this)[index]
					: typeof index == "string"
						? jQuery(index, this)[0]
						: null
			});
		};

		container.bind("activate", activateHandler);
		return container.bind(settings.event, clickHandler)
	},
	
	activate: function(index) {
		return this.trigger('activate', [index]);
	}
});

jQuery.Accordion = {};
jQuery.extend(jQuery.Accordion, {
	defaults: {
		selectedClass: "on",
		showSpeed: 'fast',
		hideSpeed: 'fast',
		alwaysOpen: true,
		animated: true,
		event: "click"
	}
});
