//menu Accordion
//author: Marghoob Suleman
//Date: 05th Aug, 2009
//Version: 1.0
//web: www.giftlelo.com | www.marghoobsuleman.com
;(function($){
	$.fn.msAccordion = function(options) {
		var curDiv = -1;
		var hash = window.location.hash;
		var anchorId = hash.substr(7);
		if(anchorId != "") {
			curDiv = $("a[name=anchor"+anchorId+"]").attr("alt");
		}

		options = $.extend({
					currentDiv:'1',
					previousDiv:'',
					isopen:'',
					vertical: false,
					defaultid:curDiv,
					currentcounter:0,
					intervalid:0,
					autodelay:0,
					event:"click",
					alldivs_array:new Array()
			}, options);
		$(this).addClass("accordionWrapper");
		$(this).css({overflow:"hidden"});
		//alert(this);
		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();
																  });
		}
		//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 = $(currentDiv).find("div.title");
								 titleDiv.attr("id", sTitleID);
								 var contentDiv = $(currentDiv).find("div.content");
								 contentDiv.attr("id", sContentID);
								 options.alldivs_array.push(sTitleID);
								 //$("#"+sTitleID).click(function(){openMe(sTitleID);});
								 
								 var imagePreview = $(currentDiv).find("div.content_top div.content_top_left");
								 
								 imagePreview.bind('mouseover', function(){
									imagePreview.prepend('<div class="portfolio_project_title"><span class="link_project">Нажмите для того, чтобы посмотреть работу</span></div>');
								 });
								 imagePreview.bind('mouseleave', function(){
									$(".portfolio_project_title").remove();
								 });
								 
								 imagePreview.bind(options.event, function(){
										if(options.isopen != sTitleID) {
											titleDiv.html('<img src="/;-)/slide_up.jpg" width="140" height="52" class="slide_down" />');
										} else {
											titleDiv.html('<img src="/;-)/slide_down.jpg" width="251" height="52" class="slide_down" />');
										}
										pause();
										openMe(sTitleID);
								 });
								 
								 var titleLink = $(currentDiv).find("div.content_top_right h2.title");
								 
								 titleLink.bind(options.event, function(){
										if(options.isopen != sTitleID) {
											titleDiv.html('<img src="/;-)/slide_up.jpg" width="140" height="52" class="slide_down" />');
										} else {
											titleDiv.html('<img src="/;-)/slide_down.jpg" width="251" height="52" class="slide_down" />');
										}
										pause();
										openMe(sTitleID);
								 });
								 
								 $("#"+sTitleID).bind(options.event, function(){
										if(options.isopen != sTitleID) {
											titleDiv.html('<img src="/;-)/slide_up.jpg" width="140" height="52" class="slide_down" />');
										} else {
											titleDiv.html('<img src="/;-)/slide_down.jpg" width="251" height="52" class="slide_down" />');
										}
											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];
			options.currentcounter = iCurrent;
			var sContentID = id+"_msContent_"+iCurrent;
			if($("#"+sContentID).css("display")=="none") {
				if(options.previousDiv!="") {
					closeMe(options.previousDiv);
					var pTitleID = options.previousDiv;
					var pCurrent = pTitleID.split("_")[pTitleID.split("_").length-1];
					var pDiv = allDivs[pCurrent];
					var ptitleDiv = $(pDiv).find("div.title");
					ptitleDiv.html('<img src="/;-)/slide_down.jpg" width="251" height="52" class="slide_down" />');
					options.isopen = '';
				};
				if(options.vertical) {
					$("#"+sContentID).slideDown("slow");
				} else {
					$("#"+sContentID).show("slow");
				}
				options.currentDiv = sContentID;
				options.previousDiv = options.currentDiv;
				options.isopen = sTitleID;
				
			} else {
				if(sTitleID == options.isopen) {
					options.isopen = '';
					closeMe(sContentID);
				}
			};
		};
		function closeMe(div) {
			if(options.vertical) {
				//options.isopen = '';
				$("#"+div).slideUp("slow");
			} else {
				$("#"+div).hide("slow");
			};
		};	
		function makeVertical() {
			$("#"+elementid +" > div").css({display:"block", float:"none", clear:"both"});
			$("#"+elementid +" > div > div.title").css({display:"block", float:"none", clear:"both"});
			$("#"+elementid +" > div > div.content").css({clear:"both"});
		};
		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);
