var ua = navigator.userAgent.toLowerCase();
var ie = ( (ua.indexOf("msie") != -1) && (ua.indexOf("opera") == -1) && (ua.indexOf("webtv") == -1) );;
var movingTab = null;
var leftDiff = 0;
var topDiff = 0;
var tabAllowedMargin = 50;
var userTabStore = "tabStore";
var userTabStoreStr = "MyTabPref";

function tabSetup(cntArray)
{
	if (ie)
	{
		for (var i = 0; i < cntArray.length; i++)
		{
		    var container = document.getElementById(cntArray[i]);
		    for (var j = 0; j < container.children.length; j++)
		    {
		        var tmpObj = container.children.item(j);
		        if (tmpObj!=null)
		        {
			        if (tmpObj.className == "tab")
			        {
			            tmpObj.onmousedown = tabOnMouseDown;
			            tmpObj.onmousemove = tabOnMouseMove;
			            tmpObj.onmouseup = tabOnMouseUp;
			            tmpObj.onmouseout = tabOnMouseOut;
			        }
		        }
		    }
		    loadTabs(container);
	   }
	}
}

function tabOnMouseOut()
{
	if (window.event.srcElement.className == "tabCntHider")
	{
		saveTab(this);
	}
}

function tabOnMouseDown()
{
	if (window.event.srcElement.className == "tabMover")
    {
        movingTab = this;
         
        leftDiff = event.offsetX;
        topDiff = event.offsetY;

        movingTab.previousSibling.style.height = movingTab.offsetHeight
        movingTab.previousSibling.style.display = "inline";
        
        movingTab.style.position = "absolute";
        movingTab.style.pixelLeft = event.x - leftDiff;
        movingTab.style.pixelTop = event.y - topDiff + document.body.scrollTop;
        movingTab.style.display = "block";

        event.cancelBubble = true;
        movingTab.setCapture();
    }
    return;
}

function tabOnMouseMove()
{   
    if (!movingTab) return;

    movingTab.style.pixelLeft = event.x - leftDiff;
    movingTab.style.pixelTop = event.y - topDiff + document.body.scrollTop;
    event.cancelBubble = true;
}

function tabOnMouseUp()
{
    if (!movingTab) return;

    movingTab.releaseCapture();
    
    var tolder = movingTab.previousSibling;

    if (tabIsLeftAllowed(movingTab) && tabIsTopAllowed(movingTab)) 
    {
        tabSwap();
        saveTabs(movingTab.parentElement);
    }

    movingTab.style.position = 'static';
    tolder.style.pixelHeight = 0;
    tolder.style.display = "none";

    event.cancelBubble = true;

    movingTab = null;
}

function tabSwap()
{
    var container = movingTab.parentElement;
    var tolder = movingTab.previousSibling;
    for (var i = 0; i < container.children.length; i++)
    {
        tabO = container.children.item(i);
        if ((tabO.className == "tab") && (tabO.id != movingTab.id))
        {
            tabOTop = getAbsoluteTop(tabO);
            if (movingTab.style.pixelTop <= tabOTop)
            {
                container.insertBefore(tolder,tabO.previousSibling);
                container.insertBefore(movingTab,tabO.previousSibling);
                return;
            } 
            else if (movingTab.style.pixelTop < (tabOTop+tabO.offsetHeight))
            {
                container.insertBefore(movingTab, tabO.nextSibling);
                container.insertBefore(tolder, movingTab);
                return;
            }
        }
    }
    container.appendChild(tolder);
    container.appendChild(movingTab);
}

function tabClick(idx)
{
    var tcnt = document.getElementById("tab" + idx + "cnt");
    var tfooter = document.getElementById("tab" + idx + "footer");
    var thider = document.getElementById("tab" + idx + "hider");
    
    var orY = document.body.scrollTop;

    if (tcnt.style.display == "none")
    {
        tcnt.style.display = "block";
        tfooter.style.display = "none";
        thider.src = hiders[idx].up.src;
    } 
    else 
    {
        tcnt.style.display = "none";
        tfooter.style.display = "block";
        thider.src = hiders[idx].dw.src;
    }
    setTimeout("scrollTo(0, " + orY + ");", 1);
}

function getAbsoluteLeft(o)
{
	var leftPosition = 0;
	while (o)
	{
		if (o.tagName.toUpperCase()  ==  'BODY') break;
		leftPosition +=  o.offsetLeft;
		o = o.offsetParent;
	}
	return leftPosition;
}

function getAbsoluteTop(o)
{
	var topPosition = 0;
	while (o)
	{
		if (o.tagName.toUpperCase()  ==  'BODY') break;
		topPosition +=  o.offsetTop;
		o = o.offsetParent;
	}
	return topPosition;
}

function tabIsLeftAllowed(o)
{
	if ((o.style.pixelLeft + o.offsetWidth) < getAbsoluteLeft(o.parentElement))
		return false;

	if (o.style.pixelLeft > (getAbsoluteLeft(o.parentElement) + o.offsetWidth))
		return false;

	return true;
}

function tabIsTopAllowed(o) 
{
    if ((o.style.pixelTop+o.offsetHeight+tabAllowedMargin) < getAbsoluteTop(o.parentElement) 
            || (o.style.pixelTop > (getAbsoluteTop(o.parentElement)+o.parentElement.offsetHeight+tabAllowedMargin)))
        return false;


    return true;
}

function savedTab(id) {
    this.id = id
    this.cnt = "inline";
    this.pos = -1;
    return this;
}

function loadSavedTab(id) 
{
    var loadedTab = null;
    var tStore = document.getElementById(userTabStore);
    tStore.load(userTabStoreStr);
    var sAttr = tStore.getAttribute(id);
    if (sAttr) {
        loadedTab = new savedTab(id);
        if (sAttr.split(',')[0]) loadedTab.cnt = sAttr.split(',')[0];
        if (sAttr.split(',')[1]) loadedTab.pos = sAttr.split(',')[1];
    }
    return (loadedTab);
}   

function saveTab(o,p) 
{
    var sTab = loadSavedTab(o.id);
    if (!sTab) sTab = new savedTab(o.id);
    sTab.cnt = document.getElementById(o.id+"cnt").style.display;
    if (sTab.cnt == "")
    	sTab.cnt = "block";
    if (p) sTab.pos = p;
    var tStore = document.getElementById(userTabStore);
    tStore.setAttribute(sTab.id, sTab.cnt + "," + sTab.pos);
    tStore.save(userTabStoreStr);
}

function saveTabs(o) 
{
    for (var i = 0;i<o.children.length;i++)
    {
        var sTab = o.children.item(i);
        if (sTab.className == "tab") saveTab(sTab,i);
    }
}

function resetTabs(un) 
{
   var tStore = document.getElementById(userTabStore);
   for (var i = 0;i<=un;i++)
   {
   	tStore.removeAttribute("tab"+i);
   	tStore.save(userTabStoreStr);
   }
   location.reload();
}

function loadTabs(o)
{
    var orderedTabs = new Array();
    for (var i = 0; i < o.children.length; i++)
    {
        var tTab = o.children.item(i);
        if (tTab!=null)
        {
	        if (tTab.className == "tab")
	        {
	            var lTab = loadSavedTab(tTab.id);
	            if (lTab!=null)
	            {
		            if (lTab)
		            {
		                var tcnt = document.getElementById(tTab.id + "cnt");
		                if (tcnt!=null)
		                {
			                if (tcnt.style.display == "" || tcnt.style.display == "inline")
			                	tcnt.style.display = "block";
			                if (tcnt.style.display != lTab.cnt)
			                {
			                	tabClick(tTab.id.substring(3));
			                }
			                if (lTab.pos!= -1)
			                	orderedTabs[lTab.pos] = lTab.id;                    
			            }
		            }
		        }
	        }
        }
    }
    for (var i = 0; i < orderedTabs.length; i++)
    {
        if(orderedTabs[i])
        {    
            var tab2move = document.getElementById(orderedTabs[i]);
            if (tab2move!=null)
            {
            	o.appendChild(tab2move.previousSibling);
            	o.appendChild(tab2move);
            }
        }
    }
}
