//menu Accordion
//author: Marghoob Suleman
//Date: 05th Aug, 2009
//Version: 1.0
//web: www.giftlelo.com | www.marghoobsuleman.com
;(function(jQuery){
	jQuery.fn.msAccordion = function(options) {
		options = jQuery.extend({
					currentDiv:'1',
					previousDiv:'',
					previousTitle:'',		
					defaultid:0,
					currentcounter:0,
					intervalid:0,
					autodelay:0,
					event:"click",
					alldivs_array:new Array()
			}, options);
		jQuery(this).addClass("accordionWrapper");
		jQuery(this).css({overflow:"hidden"});
		var elementid = jQuery(this).attr("id");
		var allDivs = this.children();
		if(options.autodelay > 0)  {
			jQuery("#"+ elementid +" > div").bind("mouseenter", function(){
														   pause();
														   });
			jQuery("#"+ 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 = jQuery(currentDiv).find("div.title");
			titleDiv.attr("id", sTitleID);
			var contentDiv = jQuery(currentDiv).find("div.acc_content");
			contentDiv.attr("id", sContentID);
			options.alldivs_array.push(sTitleID);
			//jQuery("#"+sTitleID).click(function(){openMe(sTitleID);});
			jQuery("#"+sTitleID).bind(options.event, function(){pause();
			openMe(sTitleID);});
		});
		
		//open default
		openMe(elementid+"_msTitle_"+options.defaultid);
		if(options.autodelay>0) {startPlay();};
		function openMe(id) {
			if(options.previousTitle == ''){
				options.previousTitle = id;
			}
			var sTitleID = id;
			var iCurrent = sTitleID.split("_")[sTitleID.split("_").length-1];
			processImg(iCurrent);
			options.currentcounter = iCurrent;
			var sContentID = id+"_msContent_"+iCurrent;
			if(jQuery("#"+sContentID).css("display")=="none") {
				if(options.previousDiv != "") {
					closeMe(options.previousDiv);
				};
	
				jQuery("#"+sContentID).show("slow");
	
				options.currentDiv = sContentID;
				options.previousDiv = options.currentDiv;
				//if(id != options.previousTitle){
				//	jQuery('#' + options.previousTitle).show();
				//}
				showAll(iCurrent);
				options.previousTitle = sTitleID;
			};
			jQuery('#' + id).fadeOut('slow');
		};

		function showAll(current) {
			for(i = 0; i < allDivs.length; i++){
				//ko cần if này vẫn chạy ngon
				if(i != current){
					jQuery('#' + elementid + '_msTitle_' + i).show();
				}				
			}
		};
		
		//xử lí ảnh trái phải
		function processImg(current) {
			for(i = 0; i < allDivs.length; i++){
				if(i < current){
					jQuery('#' + elementid + '_msTitle_' + i).addClass('title_left');
				}else if(i > current){
					jQuery('#' + elementid + '_msTitle_' + i).removeClass('title_left');
				}
			}
		};

		function closeMe(div) {
			jQuery("#"+div).hide("slow");
		};
		
		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);
