try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {}

/* Browser Detection
----------------------------------------------------------------------------- */
var _BROWSER_IS_IE =
    (document.all
     && window.ActiveXObject
     && navigator.userAgent.toLowerCase().indexOf("msie") > -1
     && navigator.userAgent.toLowerCase().indexOf("opera") == -1);

/**
 * I hate navigator string based browser detection too, but when Opera alone
 * chokes on cookies containing double quotes...
 */
var _BROWSER_IS_OPERA =
    (navigator.userAgent.toLowerCase().indexOf("opera") != -1);

/* CookieManager Object
----------------------------------------------------------------------------- */
/**
 * Provides a simple interface for creating, retrieving and clearing cookies.
 *
 * @author Jonathan Buchanan
 * @version 0.8
 * @dependencies $() in Core.js
 */
CookieManager = Class.create();
CookieManager.prototype =
{
    /**
     * Determines if this object will use IE's proprietary userData behaviour
     * instead of cookies for storage.
     */
    userDataForIE: false,

    initialize: function(userDataForIE)
    {
        this.cookieShelfLife = 365;
        this.userDataForIE = userDataForIE;

        // Internet Explorer has a cookie handling bug - if the *combined size*
        // of all cookies stored for a given domain is greater than 4096 bytes,
        // document.cookie will return an empty string. Until this is fixed , we
        // will fall back on IE's proprietary userData behaviour.
        if (_BROWSER_IS_IE && this.userDataForIE)
        {
            this.IE_CACHE_NAME = "storage";
            if ($(this.IE_CACHE_NAME) == null)
            {
                var div = document.createElement("DIV");
                div.id = this.IE_CACHE_NAME;
                document.body.appendChild(div);
            }
            this.store = $(this.IE_CACHE_NAME);
            this.store.style.behavior = "url('#default#userData')";
        }
    },

    /**
     * Returns the value of a cookie with the given name, or <code>null</code>
     * if no such cookie exists.
     */
    getCookie: function(aCookieName)
    {
        var result = null;
        if (_BROWSER_IS_IE && this.userDataForIE)
        {
            this.store.load(this.IE_CACHE_NAME);
            result = this.store.getAttribute(aCookieName);
        }
        else
        {
            for (var i = 0; i < document.cookie.split('; ').length; i++)
            {
                var crumb = document.cookie.split('; ')[i].split('=');
                if (crumb[0] == aCookieName && crumb[1] != null)
                {
                    result = crumb[1];
                    break;
                }
            }
        }

        if (_BROWSER_IS_OPERA && result != null)
        {
            result = result.replace(/%22/g, '"');
        }
        return result;
    },

    /**
     * Sets a cookie with the given name and value.
     */
    setCookie: function(aCookieName, aCookieValue)
    {
        if (_BROWSER_IS_IE && this.userDataForIE)
        {
            this.store.setAttribute(aCookieName, aCookieValue);
            this.store.save(this.IE_CACHE_NAME);
        }
        else
        {
            if (_BROWSER_IS_OPERA)
            {
                aCookieValue = aCookieValue.replace(/"/g, "%22");
            }
            var date = new Date();
            date.setTime(date.getTime() + (this.cookieShelfLife * 24*60*60*1000));
            var expires = '; expires=' + date.toGMTString();
            document.cookie = aCookieName + '=' + aCookieValue + expires + '; path=/';
        }
    },

    /**
     * Clears the cookie with the given name.
     */
    clearCookie: function(aCookieName)
    {
        if (_BROWSER_IS_IE && this.userDataForIE)
        {
            this.store.load(this.IE_CACHE_NAME);
            this.store.removeAttribute(aCookieName);
            this.store.save(this.IE_CACHE_NAME);
        }
        else
        {
            document.cookie =
                aCookieName + '=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/';
        }
    }
}

var DB=function(val){$("header_links").innerHTML += ":"+val}

/*
	左ナビ開閉
*/


new (function (optionObj){
	this.listElems=[]
	this.linkElems=[]
	this.if_cookie_enable=false
	this.cookie=null;
	this.instance_of_animations=[];
	
	this.ANIMATIONs={
		if_animation:false,
		if_solo_open:false
	}
	this.IMAGEs={
		preload_list:[]
	}
	this.COOKIEs={
		 enable:false
	}

	
	this.setStyle=function(){
		document.write("<style id='"+this.IDs.initStyle+"'>#"+this.IDs.wrapper+" ul{visibility:hidden}<\/style>");
	}
	this.imagePreload=function(){
		this.IMAGEs.preload_list.each(function(imagePath){
			var imgObj = new Image()
			imgObj.src=imagePath;
		})
		
	}
	
	this.listInitialize=function(listElem,key){
		
		var childElem=	listElem.down("ul",0);
		var linkElem=	listElem.down("a",0);
		if(typeof(linkElem)=="undefined" || typeof(childElem)=="undefined") {
			listElem.removeClassName(this.CLASSes.list.opened)
			listElem.removeClassName(this.CLASSes.list.closed)
			listElem.addClassName(this.CLASSes.list.noChild)
			return false;
		}
		
		var listObject={}
		listObject["childElem"] = childElem
		listObject["linkElem"]  = linkElem
		listObject["listElem"]  = listElem
		this.listElems.push(listObject)
		this.linkElems.push(linkElem)
		
		var childLinks=$A(childElem.getElementsByTagName("a"))
		
		var current_key=this.listElems.length-1
		
		if(this.ANIMATIONs.if_solo_open){
			if(this.openStatus==String(current_key)){
				var defaultOpen="1"
			}else {
				var defaultOpen="0"
			}
		} else {
			if(typeof this.openStatuses[current_key]!="undefined")
				var defaultOpen=this.openStatuses[current_key];
			else
				var defaultOpen="0";
			
			this.openStatuses[current_key]=defaultOpen
		}
		Event.observe(linkElem,"click",this.clickFunc.bind(this),false);
		Event.observe(linkElem,"dblclick",function(e){
			e.stop();
		},false);
		
		if(defaultOpen=="1"){
			listElem.removeClassName(this.CLASSes.list.closed)
			listElem.addClassName(this.CLASSes.list.opened)
			childElem.style.display="block"
			
		} else {
			listElem.removeClassName(this.CLASSes.list.opened)
			listElem.addClassName(this.CLASSes.list.closed)
			childElem.style.display="none"
		}
		
		if(this.ANIMATIONs.if_animation==true) this.instance_of_animations[current_key]={state:"idle"}
	}
	this.clickFunc=function(e){
		e.stop();
		
		var elem=e.element()
		while(elem.tagName!="A" && elem.tagName!="BODY"){
			elem=elem.parentNode
		}
		if(elem.tagName=="BODY") return null;
		
		var key=this.linkElems.indexOf(elem);
		if(key!=-1){
			if(typeof(this.instance_of_animations[key])!="undefined"){
				if(this.instance_of_animations[key].state!="running"){
					this.toggleStart(key)
				} //else {alert("run!")}
			}
		}
	}
	this.toggleStart=function(key){
		if(typeof this.listElems[key]["childElem"]=="undefined") return false;
		
		var ifVisibleState = (this.listElems[key]["childElem"].visible()==true) ? true : false;
		
		if(this.ANIMATIONs.if_animation==true){
			if(ifVisibleState){
				this.instance_of_animations[key] = new Effect.BlindUp(this.listElems[key]["childElem"],{duration:this.ANIMATIONs.duration,afterFinish:this.afterToggle.bind(this,key,ifVisibleState)})
			} else {
				this.instance_of_animations[key] = new Effect.BlindDown(this.listElems[key]["childElem"],{duration:this.ANIMATIONs.duration,afterFinish:this.afterToggle.bind(this,key,ifVisibleState)})
			}
		} else {
			if(ifVisibleState){
				this.listElems[key]["childElem"].hide()
			} else {
				this.listElems[key]["childElem"].show()
			}
			this.afterToggle(key,ifVisibleState);
		}		

		if(this.ANIMATIONs.if_solo_open){
			if(ifVisibleState){
				this.openStatus="null"
			} else {
				if(this.openStatus!="null"){
					this.closeStart(Number(this.openStatus))
				}
				this.openStatus=String(key)
			}
			
			this.saveCookie()
		}			
	}
	this.closeStart=function(key){
		if(typeof this.listElems[key]["childElem"]=="undefined") return false;
		
		var ifVisibleState = true;
		
		if(this.ANIMATIONs.if_animation==true){
			
			this.instance_of_animations[key] = new Effect.BlindUp(this.listElems[key]["childElem"],{duration:this.ANIMATIONs.duration,afterFinish:this.afterToggle.bind(this,key,ifVisibleState)})
		} else {
			this.listElems[key]["childElem"].hide()
			this.afterToggle(key,ifVisibleState);
		}		

	}
	
	
	this.afterToggle=function(key,ifVisibleState){
		if(ifVisibleState){
			this.listElems[key]["listElem"].addClassName(this.CLASSes.list.closed);
			this.listElems[key]["listElem"].removeClassName(this.CLASSes.list.opened);
			if(!this.ANIMATIONs.if_solo_open)
				this.openStatuses[key]="0"
			
		} else {
			this.listElems[key]["listElem"].addClassName(this.CLASSes.list.opened);
			this.listElems[key]["listElem"].removeClassName(this.CLASSes.list.closed);
			if(!this.ANIMATIONs.if_solo_open){
				this.openStatuses[key]="1"
			}
			
		}
		//alert(this.instance_of_animations[key].state)
		if(!this.ANIMATIONs.if_solo_open) this.saveCookie();
	}
	this.loadFunc=function(){
		$A($$("#"+this.IDs.wrapper+">ul>li")).each(this.listInitialize.bind(this))
		//$(this.IDs.initStyle).disabled=true;
		$(this.IDs.initStyle).remove()
		
		if(this.if_cookie_enable) this.saveCookie();
		
		/*var stack=""
		this.instance_of_animations.each(function(ins,key){stack+="[keytype:"+typeof(key)+";key:"+key+";ins:"+ins.state+";]"})
		
		alert(stack)*/
		
	}
	this.initCookie=function(){
		if(!document.cookie) return false;
		if(typeof this.COOKIEs=="undefined") return false;
		if(this.COOKIEs.enable!==true) return false;
		
		this.Cookie = eval("new "+this.COOKIEs.constructor)
		
		var cookie_value=this.Cookie.getCookie(this.COOKIEs.name)
		
		if(cookie_value!="" || cookie_value!=null){
			if(this.ANIMATIONs.if_solo_open){
				
				if(cookie_value=="null")
					this.openStatus="null"
				else if( /[^0-9]/.test( cookie_value ))
					this.openStatus="null"	
				else
					this.openStatus = cookie_value

			} else {
				this.openStatuses=(this.Cookie.getCookie(this.COOKIEs.name)).split(",");
			}
		} else {
			if(this.ANIMATIONs.if_solo_open)
				this.openStatus="null"
			else
				this.openStatuses=[]
		}
		return true;
	}
	this.saveCookie=function(){
		if(this.ANIMATIONs.if_solo_open)
			this.Cookie.setCookie(this.COOKIEs.name ,this.openStatus);
		else
			this.Cookie.setCookie(this.COOKIEs.name ,this.openStatuses.join(","));
	}
	
	// main	
	for(var prop in optionObj){
		this[prop]=optionObj[prop]
	}
	this.setStyle()
	this.if_cookie_enable=this.initCookie()
	
	this.imagePreload()
	Event.observe(document,"dom:loaded",this.loadFunc.bind(this),false);
	
})({
	 IDs:{
		wrapper:"navigation2",
		initStyle:"leftMenuInitStyle"
	}
	,CLASSes:{
		 list:{
			opened:"opened"
			,closed:"closed"
			,noChild:""
		}
		,lastLink:"opened_last"
	}
	,ANIMATIONs:{
		if_animation:true,
		if_solo_open:true,
		duration:0.6
	}
	,IMAGEs:{
		preload_list:[
			"/images/menu_closed.gif","/images/menu_opened.gif","/images/menu_noChild.gif"
		]
	}
	,COOKIEs:{
		 enable:true
		,constructor:"CookieManager"
		,name:"leftMenuOpen"
	}
})
