var topComponents=15;
var butComponents=0;
var components=topComponents+butComponents;

function toggle_collapse_group(B, E) {
    if (!is_regexp) {
        return false
    }
    var D = fetch_object("collapseobj_" + B);
    var A = fetch_object("collapseimg_" + B);
    var C = fetch_object("collapsecel_" + B);
    if (!D) {
        if (A) {
            A.style.display = "none"
        }
        return false
    }
    if (D.style.display == "none" || "open" == E) {
        D.style.display = "";
        if (A) {
            img_re = new RegExp("_collapsed\\.gif$");
            A.src = A.src.replace(img_re, ".gif")
        }
        if (C) {
            cel_re = new RegExp("^(thead|tcat)(_collapsed)$");
            C.className = C.className.replace(cel_re, "$1")
        }
    } else {
        if (D.style.display != "none" || "closed" == E) {
            D.style.display = "none";
            if (A) {
                img_re = new RegExp("\\.gif$");
                A.src = A.src.replace(img_re, "_collapsed.gif")
            }
            if (C) {
                cel_re = new RegExp("^(thead|tcat)$");
                C.className = C.className.replace(cel_re, "$1_collapsed")
            }
        }
    }
    return false
}

function toolBarInit() {
    if ($("#_pagerB").length) {
		$("#_pagerB").appendTo($('#_newPagerB'));
        $("#_pagerB").css('display', 'block');
	}	
  
    if ($("#_threadNavigator").length) {
        if ($("#_newThreadNavigator")) {
			$("#_threadNavigator").appendTo($('#_newThreadNavigator'));
            $("#_threadNavigator").css('display', 'block');
		}	
	}	
  
    if ($("#_threadT").length) {
		var inner=document.getElementById("_threadT").innerHTML;

        if (inner.indexOf(' href=') > 0) {
			inner=inner.substring(inner.indexOf(' href=')+7,inner.indexOf('" rel='));

            while (inner.indexOf("&amp;") > 0) {
				inner=inner.replace(/&amp;/i,"&");
			}    

			document.getElementById("_toolA_10").href=inner;    
			document.getElementById("table_10").style.display="block";	
		}
	}	

    if (document.getElementById("_forumJump")) {
		$("#_forumJump").appendTo($('#_newForumJump'));
		document.getElementById("_forumJump").style.display="block";
	}	  

    for (var i = 1; i <= components; i++) {
        try {
            if (document.getElementById('_' + i)) {
				document.getElementById('table_'+i).style.display="block";
				inner=document.getElementById('_'+i).innerHTML;

				var k=0;
                while ((inner.indexOf("<!-- Categoty Top -->") != -1) && (k < 2)) {
                    if ((inner.indexOf("<!-- Categoty Top -->") != -1) && (inner.indexOf("<!-- /Categoty Top -->") != -1)) {
                        inner = inner.substring(0, inner.indexOf("<!-- Categoty Top -->")) +
                                inner.substring(inner.indexOf("<!-- /Categoty Top -->") +
                                        22, inner.length);
                    }
                    if ((inner.indexOf("<!-- Categoty Bottom -->") != -1) && (inner.indexOf("<!-- /Categoty Bottom -->") != -1)) {
                        inner = inner.substring(0, inner.indexOf("<!-- Categoty Bottom -->")) +
                                inner.substring(inner.indexOf("<!-- /Categoty Bottom -->") +
                                        25, inner.length);
                    }
					k++;
				}

				document.getElementById('_'+i).innerHTML=inner;
				document.getElementById('_'+i).style.display="block";

                wrapAndReturn('_' + i, i - 1);
			}
		}
        catch(err) {
	}
    }

	// now position wrapped panels
    var ppos = $('div.l-layout').offset();
    for (i = 1; i <= components; i++) {
        if ($('#wrap_' + i).length) {
            var table = $('#table_' + i);
            var pos = table.offset();

            $('#wrap_' + i).css('left', pos.left - ppos.left);
            $('#wrap_' + i).css('top', pos.top + table.height());
		}
	}

	
    $("body").click(function(event) {
        onBodyclick(event);
    });
	 window.onresize=onBodyresize;
}


$(document).ready(function(){
	setTimeout("toolBarInit()",500);
});


function wrapAndReturn(id, j) {
	var wrapid='wrap'+id;
	if(document.getElementById(wrapid)) return wrapid;
	var wraphrml=getWrapper(wrapid,j);

	$(wraphrml).appendTo($('#_toolbarDiv'));

	$('#wrap'+id+' td.r33').append( $('#'+id) );
	$('#wrap'+id).css('position','absolute');
	$('#wrap'+id).css('visibility','hidden');
	$('#wrap'+id).css('z-index','1000');
	$('#wrap'+id).css('overflow-x','auto');
	$('#wrap'+id).css('overflow-y','auto');
	document.getElementById(wrapid).style.zIndex="9999";

	return wrapid;
}


function getWrapper(id, t) {
   var htmlCode= '<table id='+id; 
    if (id == "wrap_1") {
        htmlCode += ' width=670';
    }

   htmlCode+=' cellpadding="0" cellspacing="0" >'+
          '<tr height="10">'+
                '<td width="10"><img width="10" src="/forum/toolBar/images/module-3-tl.png"/></td>' +
                '<td class="module-t"><img width="118px" height="10px" src="/forum/toolBar/images/module-3-t.png"></td>' +
                '<td width="10"><img  width="10" src="/forum/toolBar/images/module-3-tr.png"/></td>' +
		  '</tr>'+	
		  '<tr>'+
                '<td class="module-l" width="10"></td>' +
                '<td class="r33" style="background:#E1E6EE"></td>' +
                '<td class="module-r" width="10"></td>' +
		  '</tr>'+
          '<tr height="10">'+
                '<td width="10"><img width="10" src="/forum/toolBar/images/module-3-bl.png"/></td>' +
                '<td class="module-b"></td>' +
                '<td width="10"><img width="10" src="/forum/toolBar/images/module-3-br.png"/></td>' +
		  '</tr>'+		   
          '</table>';
	
	return htmlCode;		
}


function onBodyclick(evt) {
	var obj=new Object();
	obj.id="";
	obj=evt.target || evt.srcElement || null;

    if ((obj.className.indexOf("toolA") == -1) && (obj.className.indexOf('tItem') == -1) && (obj.id.indexOf("wrap") == -1) && (obj.className.indexOf('selected') == -1)) {
		flag=true;
		
        while (obj.offsetParent) {
			if (obj.id.indexOf("wrap")!=-1) flag=false;
			obj=obj.offsetParent;
		}	

        if (flag != false) {
            for (var i = 1; i <= components; i++) {
                if (document.getElementById('table_' + i)) {
					$('#wrap_'+i).css('visibility','hidden');
					deselectTab('_'+i);
				}
			} 

			selectedObj="_0";  
			pressed=-1;
			lastPressed=0;
		}
	}
}


function onBodyresize() {
    for (var i = 1; i <= components; i++) {
        var table = $('table_' + i);
        if (table.length > 0) {
            var wrap = $('#wrap_' + i);

            wrap.css('visibility', 'hidden');
			deselectTab('_'+i);
            var pos = table.offset();
            wrap.css('left', pos.left);
            wrap.css('top', pos.top + table.height());
		}
	} 

	selectedObj="_0";  
	pressed=-1;
	lastPressed=0;  
}


function onToolbarMouseOver(obj) {
    if (obj != selectedObj) {
	var tableObj=document.getElementById("table"+obj); 
        if (obj.substring(1, obj.length) <= topComponents) {
            tableObj.className = "tItemSel";
        } else {
            tableObj.className = "bItemSel";
	} 
} 
}


function onToolbarMouseOut(obj) {
    if (obj != selectedObj) {
	var tableObj=document.getElementById("table"+obj); 	
        if (obj.substring(1, obj.length) <= topComponents) {
            tableObj.className = "tItem";
        } else {
            tableObj.className = "bItem";
	}
} 
}


function deselectTab(obj) {
	var tableObj=document.getElementById("table"+obj); 	
	
    if (obj.substring(1, obj.length) <= topComponents) {
        tableObj.className = "tItem";
    } else {
        tableObj.className = "bItem";
} 
}


function selectTab(obj) {
	var tableObj=document.getElementById("table"+obj); 

    if (obj.substring(1, obj.length) <= topComponents) {
        tableObj.className = "tItemSel";
    } else {
        tableObj.className = "bItemSel";
} 
}


var selectedObj;
var pressed=-1;
var lastPressed=0;
	
	
function onToolbarClick(item) {
	selectedObj=item;

	pressed=parseInt(item.substring(1,item.length)); 

    for (var i = 1; i <= components; i++) {
        if (i != pressed) {
			$('#wrap_'+i).css('visibility','hidden');

            if ($('#table_' + i).length) {
				onToolbarMouseOut('_'+i);
			}  
		}
	}

	selectTab(item);

    var wrapper = $('#wrap' + item);

    if (lastPressed != pressed) {
        wrapper.css('visibility', 'visible');
    } else {
        wrapper.css('visibility', 'hidden');
		
		selectedObj="";
        onToolbarMouseOut(item);
		pressed=0;		  
	}
	lastPressed=pressed;
}

function addBookmark(url, title) {
	if (!url) url = location.href;
	if (!title) title = document.title;

	//Gecko
	if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel (title, url, "");
	//IE4+
	else if (typeof window.external == "object") window.external.AddFavorite(url, title);
	//Opera7+
    else if (window.opera && document.createElement) {
		var a = document.createElement('A');
		if (!a) return false; //IF Opera 6
		a.setAttribute('rel','sidebar');
		a.setAttribute('href',url);
		a.setAttribute('title',title);
		a.click();
	}
	else return false;

	return true;
}


function searchThread(searchWord) {
	document.getElementById("newQuery").value=searchWord;
	document.forms['_5-searchThread'].submit();
}


function s() {
	document.forms['f'].submit();
}

