﻿var HarveyCategory={
    ContentClassName:{},
    Init:function(Config){
	    $(document).ready(function(){
		    HarveyCategory.ContentClassName[Config.HeaderClass]=Config.ContentClass;
		    Config.CssClass={Close: Config.ToggleClass[0], Open: Config.ToggleClass[1]} ;
		    Config.HtmlSetting={Close: Config.ToggleHtml[0], Open: Config.ToggleHtml[1]};
		    Config.Image={Open:Config.ImageHtml[0],OpenOver:Config.ImageHtml[1],Close:Config.ImageHtml[2],CloseOver:Config.ImageHtml[3]}
		    var lastexpanded={};
		    var $SubContents=$('.'+Config["ContentClass"]);
		    
		    $('.'+Config["LinkClass"]).each(function(Index){
			    $('<span class="accordprefix"></span>').prependTo(this);
			    var $SubContent=$SubContents.eq(Index);
			    $SubContent.hide();
			    var hfCategoryCurrent = document.getElementById("hfCategoryCurrent");
			    if(hfCategoryCurrent){
			        if($(this).attr('id') == "Category"+hfCategoryCurrent.value){
			            HarveyCategory.TransFormHeader($(this), Config, "Close");
			            document.getElementById($(this).attr('id') + "Submenu").style.display = 'block';
			        }else{
			            HarveyCategory.TransFormHeader($(this), Config, "Open");
			        }
			    }else{
			        HarveyCategory.TransFormHeader($(this), Config, "Open");
			    }
			    
		    })
		    $(".statusicon").each(function(index){
			    $(this).attr('headerindex', index+'h');
			    $SubContents.eq(index).attr('contentindex', index+'c');
		    })
		    $(".statusicon").click(function(){
				var $subcontent=$SubContents.eq(parseInt($(this).attr('headerindex')));
				if ($subcontent.css('display')=="none"){
					HarveyCategory.Expandit($(this), $subcontent, Config);
				}
				else{
					HarveyCategory.Collapseit($(this), $subcontent, Config);
				}
				return false;
            })
        })
	},
	Collapseit:function($TargetHeader, $TargetContent, Config){
		$TargetContent.css("display","none");
		this.TransFormHeader($TargetHeader, Config, "Open")
	},
	Expandit:function($TargetHeader, $TargetContent, Config){
		$TargetContent.css("display","block");
		this.TransFormHeader($TargetHeader, Config, "Close")
	},
	TransFormHeader:function($TargetHeader, Config, State){
		if ($TargetHeader.is("img")){
		    if($TargetHeader.attr('src') == Config.Image.Close){
		        $TargetHeader.attr('src', Config.Image.Open);
		    }else{
		        $TargetHeader.attr('src', Config.Image.Close);
		    }
		}else{
		    $TargetHeader.find('.accordprefix').html((State == "Close")? Config.HtmlSetting.Close : Config.HtmlSetting.Open);
		}
	}
}