//menu Accordion
//author: Marghoob Suleman
//Date: 05th Aug, 2009
//Version: 1.0
//web: www.giftlelo.com | www.marghoobsuleman.com
// Version 1.5 for Kakanien Reloaded == janos.bekesi@kakanien.ac.at 2009-10-28
;(function($j){
	$j.fn.msAccordion = function(options) {
		options = $j.extend({
					currentDiv:'1',
					previousDiv:'',
					vertical: false,
					defaultid:0,
					currentcounter:0,
					intervalid:0,
					autodelay:0,
					event:"click",
					alldivs_array:new Array()
			}, options);
		$j(this).addClass("accordion_wrapper");
		$j(this).css({overflow:"hidden"});
		//alert(this);
		var elementid = $j(this).attr("id");
		var allDivs = this.children();
        var bgimg = undefined;
		if(options.autodelay>0)  {
			$j("#"+ elementid +" > div").bind("mouseenter", function(){
														   pause();
														   });
			$j("#"+ elementid +" > div").bind("mouseleave", function(){
																  startPlay();
																  });
		}
		//set ids
		allDivs.each(function(current) {
								 var iCurrent = current;
								 var sTitleID = elementid+"_msTitle_"+(iCurrent);
								 var sContentID = sTitleID+"_msContent_"+(iCurrent);
								 var currentDiv = allDivs[iCurrent];
								 var totalChild = currentDiv.childNodes.length;
								 var titleDiv = $j(currentDiv).find("div.title");
								 titleDiv.attr("id", sTitleID);
								 var contentDiv = $j(currentDiv).find("div.content");
								 contentDiv.attr("id", sContentID);
								 options.alldivs_array.push(sTitleID);
								 //$j("#"+sTitleID).click(function(){openMe(sTitleID);});
								 $j("#"+sTitleID).bind(options.event, function(){pause();openMe(sTitleID);});
								 });
		
		//make vertical
		if(options.vertical) {makeVertical();};
		//open default
		openMe(elementid+"_msTitle_"+options.defaultid);
		if(options.autodelay>0) {startPlay();};
		//alert(allDivs.length);
		function openMe(id) {
			var sTitleID = id;
			var iCurrent = sTitleID.split("_")[sTitleID.split("_").length-1];
            // var bgimg = $j("#"+ sTitleID).css( 'background-image');
			options.currentcounter = iCurrent;
			var sContentID = id+"_msContent_"+iCurrent;
			if($j("#"+sContentID).css("display")=="none") {
				if(options.previousDiv!="") {
					closeMe(options.previousDiv);
				};
				if(options.vertical) {
                    $j("#"+ sTitleID).find("div.acc_plus_minus").css({ 'background-position': "0px -34px"});
                    // alert("baclgrpimd image: " + $j("#"+ sTitleID).css( 'background-image'));
                    $j("#"+ sTitleID).css( 'background-image', 'none');
					$j("#"+sContentID).slideDown("slow");
				} else {
					$j("#"+sContentID).show("slow");
				}
				options.currentDiv = sContentID;
				options.previousDiv = options.currentDiv;
			} else {
                closeMe(sContentID, sTitleID);
            };
		};
		function closeMe(div, title) {
			if(options.vertical) {
                $j("#"+ title).find("div.acc_plus_minus").css({ 'background-position': "0px 0px"});
				$j("#"+div).slideUp("slow", resetBackground);
                if (bgimg) {
                    // $j("#"+ title).css( {'background-image': bgimg});
                    // alert("bakground image: " + $j("#"+ title).css( 'background-image') +" -- " + bgimg);
                }
			} else {
				$j("#"+div).hide("slow");
			};
		};	
        function resetBackground() {
           $j(this).parent().find("div.title").css({'background-image': bgimg});  
        }; // 
		function makeVertical() {
			$j("#"+elementid +" > div").css({display:"block", float:"none", clear:"both"});
			$j("#"+elementid +" > div > div.title").css({display:"block", float:"none", clear:"both"});
			$j("#"+elementid +" > div > div.content").css({clear:"both"});
            if (! bgimg)
                bgimg = $j("#"+elementid +" > div > div.title").css('background-image');
                
		};
		function startPlay() {
			options.intervalid = window.setInterval(play, options.autodelay*1000);
		};
		function play() {
			var sTitleId = options.alldivs_array[options.currentcounter];
			openMe(sTitleId);
			options.currentcounter++;
			if(options.currentcounter==options.alldivs_array.length) options.currentcounter = 0;
		};
		function pause() {
			window.clearInterval(options.intervalid);
		};
		}
})(jQuery);