var Options = {

    /** Hide the parleys pictures and set a cookie */
    hideAll:function(cookieName, parent, child, id, text) {
        var expdate = new Date();
	    expdate.setTime(expdate.getTime() + (24 * 60 * 60 * 1000) * 180);
	    Memory.storeValue(cookieName, "true" , expdate, "/", null, false);
    },

    showAll:function(cookieName, parent, child, id, text) {
        Memory.forgetValue(cookieName, "/", null);
    },

    /** Visibility switch so the same HREF can be used when pictures are turned off without page refresh! */
    visible:function(cookieName, parent, child, id, text) {
        if (this.isCookieEnabled(cookieName)){
           this.hideAll(cookieName, parent, child, id, text);
        }else{
           this.showAll(cookieName, parent, child, id, text);
        }
        this.changeState(cookieName,parent,child);
        this.showUrl(cookieName, parent, child, id, text);
    },

    pictures:function() {
        var st = Memory.recallValue("picturesDisabled");
        if (st == null || st == "false") {
            document.write("<a href=\"javascript:Options.visible('picturesDisabled', 'picture', 'pict', 'hidePictures', 'Show Pictures');\" id=\"hidePictures\">Hide Pictures</a>");
        } else if (st == "true"){
        	document.write("<a href=\"javascript:Options.visible('picturesDisabled', 'picture', 'pict', 'hidePictures', 'Hide Pictures'); \" id=\"hidePictures\">Show Pictures</a>");
        }
    },

    tags:function() {
        var st = Memory.recallValue("tagsDisabled");
        if (st == null || st == "false") {
        	document.write("<a href=\"javascript:Options.visible('tagsDisabled', 'tags', 'tag', 'hideTags', 'Show Tags');\" id=\"hideTags\">Hide Tags</a>");
        } else if (st == "true"){
        	document.write("<a href=\"javascript:Options.visible('tagsDisabled', 'tags', 'tag', 'hideTags', 'Hide Tags');\" id=\"hideTags\">Show Tags</a>");
        }
    },

    postBy:function() {
        var st = Memory.recallValue("postByDisabled");
        if (st == null || st == "false") {
        	document.write("<a href=\"javascript:Options.visible('postByDisabled', 'postByParent', 'postByChild', 'hidePostBy', 'Show PostBy');\" id=\"hidePostBy\">Hide Postby</a>");            
        } else if (st == "true"){
        	document.write("<a href=\"javascript:Options.visible('postByDisabled', 'postByParent', 'postByChild', 'hidePostBy', 'Hide PostBy');\" id=\"hidePostBy\">Show PostBy</a>");
        }
    },
    
    showUrl:function(cookieName, parent, child, id, text) {
    	var url = dojo.byId(id);
    	url.setAttribute("id", id);
        if (this.isCookieEnabled(cookieName)){
       		text = "Hide" + text.substring(4);
        } else{
        	text = "Show" + text.substring(4);
        }
        url.setAttribute("href","javascript:Options.visible('" + cookieName + "', '" + parent + "', '" + child + "', '" + id + "', '" + text + "');");
        url.innerHTML = text;
     },
     
     changeState:function(cookieName,parent,child) {
            var divChild;
            for (var i=1; i < 20; i++) {
            	var divParent = parent + i;
            	var d = dojo.byId(divParent);
            	if (d != null) {
               		divChild = child + i;
            		var olddiv = dojo.byId(divChild);
            		if (!this.isCookieEnabled(cookieName)) {
        				dojo.lfx.html.fadeOut(olddiv, 200).play();
						setTimeout('Options.fadeWrapper(' + divChild +')', 200);
        			} else {
        				olddiv.style.display='';                        
                        dojo.lfx.html.fadeIn(olddiv, 200).play();
        			}
            	} else {
            		break;
            	}
        	}     
     },
     
     isCookieEnabled:function(cookieName) {
     	var isDisabled = Memory.recallValue(cookieName);
     	return isDisabled == null || isDisabled == "false";
     },
     
	toggleDiv:function(divId, check, controlId, prefix) {
		var panel = dojo.byId(divId);
		var link = dojo.byId(controlId);
        var icon = dojo.byId("optarrow");
        link.setAttribute("id",controlId);
        if (check == 'Y'){
            icon.src = prefix + "/download/resources/parleys.extra.plugins:parleystoc/gfx/list/close.gif";
            panel.style.display='';
			dojo.lfx.html.fadeIn(panel, 200).play();
			link.setAttribute("href","javascript:void(Options.toggleDiv('" + divId + "','N','" + controlId + "', '" + prefix + "'))");
		} else {
            icon.src = prefix + "/download/resources/parleys.extra.plugins:parleystoc/gfx/list/open.gif";
			dojo.lfx.html.fadeOut(panel, 200).play();
            setTimeout('Options.fadeWrapper(' + divId +')', 200);
			link.setAttribute("href","javascript:void(Options.toggleDiv('" + divId + "','Y','" + controlId + "', '" + prefix + "'))");
		}
	},

	fadeWrapper:function(divId) {
		dojo.byId(divId).style.display='none';
	}
};
