var currentopened = 1;
;(function($){
	$.fn.msAccordion = function(options) {
		options = $.extend({
					currentDiv:'1',
					previousDiv:'',
					defaultid:0,
					currentcounter:0,
					intervalid:0,
					autodelay:0,
					event:"click",
					alldivs_array:new Array()
			}, options);
		$(this).addClass("accordionWrapper");
		$(this).parent().css({overflow:"hidden"});
        $(this).css({width:10000});
		var elementid = $(this).attr("id");
		var allDivs = this.children();
        
		if(options.autodelay>0)  {
			$("#"+ elementid +" > div").bind("mouseenter", function(){pause();});
			$("#"+ elementid +" > div").bind("mouseleave", function(){startPlay();});
		}
		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 = $(currentDiv).find("div.title");
								 titleDiv.attr("id", sTitleID);
								 var contentDiv = $(currentDiv).find("div.content");
								 contentDiv.attr("id", sContentID);
								 options.alldivs_array.push(sTitleID);
								 $("#"+sTitleID).bind(options.event, function(){
                                        pause();
                                        openMeCustom(current+1);
                                    });
                                 $("#"+sTitleID).bind("click", function(){pause();openMeCustom(current+1);});
								 });
        $(".set"+currentopened).animate({width: [820, 'swing']});
        $("#accordion1_msTitle_3_msContent_3").css({display:"block"});
        
		if(options.autodelay>0) {startPlay();};

        function openMeCustom(id) {
            $(".set").stop(true, true);
            if (!$(".set").is(":animated") ) {
                if (currentopened != id){
                    // Показать
                    $(".set"+id).animate({width: [820, 'swing']});
                    // Спрятать
                    $(".set"+currentopened).animate({width: [20, 'swing']});
                    currentopened = id;
                }
            }
        };
		function startPlay(){options.intervalid = window.setInterval(play, options.autodelay*1000);};
        
		function play() {
            console.info(currentopened);
            var next = currentopened + 1;
            if (next == 5) {
                next = 1;
            }
            openMeCustom(next);
            console.info(currentopened);
        };
		function pause() {window.clearInterval(options.intervalid);};
		}
})(jQuery);
