﻿var ObjectStore = {
    register: function(object) {
        if (!ObjectStore.__items) ObjectStore.__items = [];
        var result = ObjectStore.__items.length;
        ObjectStore.__items[result] = object;
        return result;
    },
    find: function(index) { return ObjectStore.__items[index]; }
}

var TrackableObject = Class.create({
    initialize: function() {
        this.__index = ObjectStore.register(this);
    }
});


function sTab(show,hide1/*,hide2*/){
	document.getElementById(show).style.display = "";
		if(show == "s" || show == "hf"){
			document.getElementById(show + "t").className = "tas";
			document.getElementById(hide1 + "t").className = "tin";
		}
		else{
			document.getElementById(show + "t").className = "ta";
			document.getElementById(hide1 + "t").className = "tins";
		}
	document.getElementById(hide1).style.display = "none";
}





function checkEnter(e)
{ //e is event object passed from function invocation
	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
	e = e;
	characterCode = e.which; //character code is contained in NN4's which property
	}
	else{
	e = event;
	characterCode = e.keyCode; //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		return true;
	}
	else{
		return false;
	}
}

var FadeRotater = Class.create(TrackableObject, {
    initialize: function($super, element, interval) {
        $super();
        this.__element = $(element);
        this.__started = false;
        this.__interval = interval;
    },
    __doSetItem: function() {
        this.setItem();
        this.__endFade();
    },
    start: function() {
        if (!this.__started) {
            this.__started = true;
            this.__startFade();
        }
    },
    stop: function() { if (this.__started) this.__started = false; },
    __startFade: function() { Effect.toggle(this.__element, "appear", { afterFinish: this.__doSetItem.bind(this) }); },
    __endFade: function() { Effect.toggle(this.__element, "appear", { afterFinish: this.__loop.bind(this) }); },
    __loop: function() { setTimeout("ObjectStore.find(" + this.__index + ").__startFade()", this.__interval); }
});

var Accordian = Class.create(TrackableObject, {
    initialize: function($super, element) {
        $super();
        this.__element = $(element);
        this.__horizontal = this.__element.hasClassName("horizontal");
        this.__counter = 0;
        this.__increment = 50;
        this.__delay = 40;
        this.__busy = false;
        this.__currentItem = null;
        this.__openingData = null;
        this.__closingData = null;
		this.__lastItem = null;
        var children = element.childElements();
        for (var i = 0; i < children.length; i++) {
			if (children[i].hasClassName)
			{
				if (children[i].hasClassName("expanded")) this.__currentItem = children[i];
			}
			else
			{
				if (children[i].className.indexOf("expanded") != -1)
				{
					this.__currentItem = children[i];
				}
			}
            children[i].observe("mousemove", this.__handler.bind(this));
            children[i].observe("click", this.__handler.bind(this));
        }
    },
    __resize: function(max, current) {
        var closeSize = max - current;
        var openSize = current;
        if (openSize > max || openSize < 0) {
            openSize = max;
            closeSize = 0;
        }
        if (this.__horizontal) {
            this.__closingData.item.setStyle({ width: (closeSize + this.__closingData.buttonSize) + "px" });
            this.__closingData.pane.setStyle({ width: closeSize + "px" });
            this.__openingData.item.setStyle({ width: (openSize + this.__openingData.buttonSize) + "px" });
            this.__openingData.pane.setStyle({ width: openSize + "px" });
        } else {
            this.__closingData.pane.setStyle({ height: closeSize + "px" });
            this.__openingData.pane.setStyle({ height: openSize + "px" });
        }
        if (closeSize > 0) setTimeout("ObjectStore.find(" + this.__index + ").__resize(" + max + "," + (current + this.__increment) + ");", this.__delay);
        else {
            this.__openingData = null;
            this.__closingData = null;
            this.__busy = false;
			if (this.__lastItem != null)
			{
				this.__OpenItem(this.__lastItem);
				this.__lastItem = null;
			}
        }
    },
    __OpenItem: function(openElem)
    {
		if (!this.__busy)
		{
			this.__busy = true;
			if (openElem != this.__currentItem) {
				this.__openingData = { "item": openElem, "pane": openElem.select("div")[0], "buttonSize": openElem.select("a")[0].getWidth() }
				this.__closingData = { "item": this.__currentItem, "pane": this.__currentItem.select("div")[0], "buttonSize": openElem.select("a")[0].getWidth() }
				var paneSize = 0;
				if (this.__horizontal) paneSize = this.__closingData.pane.getWidth();
				else paneSize = this.__closingData.pane.getHeight();
				this.__resize(paneSize, this.__increment);
				this.__currentItem.removeClassName("expanded");
				openElem.addClassName("expanded");
				this.__currentItem = openElem;
			}
		}
    },
    __handler: function(ev) {
        var item = ev.findElement("li");
        if (!this.__busy) {
            this.__busy = true;
            if (item != this.__currentItem) {
                this.__openingData = { "item": item, "pane": item.select("div")[0], "buttonSize": item.select("a")[0].getWidth() }
                this.__closingData = { "item": this.__currentItem, "pane": this.__currentItem.select("div")[0], "buttonSize": item.select("a")[0].getWidth() }
                var paneSize = 0;
                if (this.__horizontal) paneSize = this.__closingData.pane.getWidth();
                else paneSize = this.__closingData.pane.getHeight();
                this.__resize(paneSize, this.__increment);
                this.__currentItem.removeClassName("expanded");
                item.addClassName("expanded");
                this.__currentItem = item;
            } else {
                this.__busy = false;
			}
        }
		else
		{
			if (item != this.__currentItem)
			{
				this.__lastItem = item;
			}
			else
			{
				this.__lastItem = null;
			}
		}
    }
});

document.observe("dom:loaded", function() {
    var accordians = $$("ul.accordian");
    for (var i = 0; i < accordians.length; i++) new Accordian(accordians[i]);
});

function GetControl(controlText)
{
	var ele = document.forms[0].elements;
	var IDControl  = null;
	for(var i=0;i<ele.length;i++)
	{
		if(ele[i].id.search(controlText) != -1)
		{
			IDControl = ele[i];
		}
	}
	return IDControl
}

function ReadCookie(cookieName)
{
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="")
	{
		return "";
	}
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1)
	{
		ind1=theCookie.length;
	}
	return unescape(theCookie.substring(ind+cookieName.length+1,ind1));
}

function SetCookie(cookieName,cookieValue,nDays)
{
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0)
	{
		nDays=1;
	}
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

