/**************************************************************************************
* dreamportal.js                                                                      *
***************************************************************************************
* Dream Portal                                                                        *
* Forum Portal Modification Project founded by ccbtimewiz (ccbtimewiz@ccbtimewiz.com) *
* =================================================================================== *
* Software by:                  Dream Portal Team (http://dream-portal.net/team/)     *
* Software for:                 Simple Machines Forum                                 *
* Copyright 2009-2010 by:       Dream Portal Team (http://dream-portal.net/team/)     *
* Support, News, Updates at:    http://dream-portal.net/                              *
**************************************************************************************/

var start = 0;
var colLStart = 0;
var colRStart = 0;
var rlit = 0;
var rrit = 0;
var mHeights = new Array();
var cWidthLeft = null;
var cWidthRight = null;
var guest = null;
var imageurl = null;
var sessid = null;

function loadInfo(user, imageDir, session)
{
	if (!guest)
		guest = user;
	if (!imageurl)
		imageurl = imageDir;
	if (!sessid)
		sessid = session;
}

function toggleModule(targetId)
{
	var test = null;
	var dpheader = null;
	var dpmodule = "module_" + targetId;
	var dpmoduleimage = "modulecollapse_" + targetId;

	// Block Style
	if (document.getElementById("dpmodule_" + targetId) != test)
		dpheader = document.getElementById("dpmodule_" + targetId);
	
	mode = document.getElementById(dpmodule).style.display == "" ? 0 : 1;

	if (parseInt(guest) == 0)
		document.cookie = dpmodule + "=" + (mode ? 0 : 1);
	else
		smf_setThemeOption(dpmodule, mode ? 0 : 1, null, sessid);
	
	if(dpheader != test)
	{
		if (mode == 0)
			dpheader.style.height = "31px";	
		else
			dpheader.style.height = "25px";		
	}
	document.getElementById(dpmoduleimage).src = imageurl + "/" + (mode ? "collapse.gif" : "expand.gif");
	document.getElementById(dpmodule).style.display = mode ? "" : "none";
}

function toggleModuleAnim(targetId, speed)
{
	if (start != 0)
		return;

	var dpmodule = "module_" + targetId;

	if (document.getElementById(dpmodule).style.display == "none")
		expandModuleAnim(parseInt(targetId), dpmodule, parseInt(speed), guest, imageurl, sessid);
	else
		collapseModuleAnim(parseInt(targetId), dpmodule, parseInt(speed), guest, imageurl, sessid);
}
	
function collapseModuleAnim(targetId, dpmodule, speed)
{
	var dpmoduleimage = "modulecollapse_" + targetId;
	var dpheader = null;
	var test = null;

	// Block Style
	if (document.getElementById("dpmodule_" + targetId) != test)
		dpheader = document.getElementById("dpmodule_" + targetId);

	var module = document.getElementById(dpmodule);
	var modHeight = module.offsetHeight;
	module.style.overflowY = "hidden";

	if (!mHeights[targetId] && start == 0)
	{
		document.cookie = "dpmodule_height_" + targetId + "=" + modHeight;
		mHeights[targetId] = modHeight;	
	}

	var minHeight = 0;
	var moveBy = Math.round(speed * 10);
	var intId = setInterval(function() {
		var curHeight = module.offsetHeight;
		var newHeight = curHeight - moveBy;
		if (newHeight > minHeight)
		{
			start = 1;
			module.style.height = newHeight + "px";
		}
		else {
			clearInterval(intId);
			module.style.height = "0px";
			if(dpheader != test)
				dpheader.style.height = "31px";

			start = 0;
			module.style.display = "none";
			document.getElementById(dpmoduleimage).src = imageurl + '/expand.gif';
			if (parseInt(guest) == 0)
				document.cookie = dpmodule + "=1";
			else
				smf_setThemeOption(dpmodule, 1, null, sessid);
			
		}
	}, 30);
}
	
function expandModuleAnim(targetId, dpmodule, speed)
{
	var dpmoduleimage = "modulecollapse_" + targetId;
	var dpheader = null;
	var test = null;

	// Block Style
	if (document.getElementById("dpmodule_" + targetId) != test)
		dpheader = document.getElementById("dpmodule_" + targetId);

	var module = document.getElementById(dpmodule);
	document.getElementById(dpmoduleimage).src = imageurl + '/collapse.gif';
	if(dpheader != test)
		dpheader.style.height = "25px";

	module.style.height = "0px";
	module.style.overflowY = "hidden";
	module.style.display = "";
	if (!mHeights[targetId])
		var match = getCookie("dpmodule_height_" + targetId);
	else
		var match = mHeights[targetId];

	var modHeight = parseInt(match);
	var moveBy = Math.round(speed * 10);

	var intId = setInterval(function() {
		var curHeight = module.offsetHeight;
		var newHeight = curHeight + moveBy;
		if (newHeight < modHeight)
		{
			module.style.height = newHeight + "px";
			start = 1;
		}
		else {
			clearInterval(intId);
			if(dpheader != test)
				module.style.overflowY = "hidden";
			else
				module.style.overflowY = "auto";

			module.style.height = "";
			start = 0;
			if(parseInt(guest) == 0)
				document.cookie = dpmodule + "=0";
			else
				smf_setThemeOption(dpmodule, 0, null, sessid);
		}
	}, 30);
}

function dp_collapseCalendar(id)
{
	new_day = "dp_calendar_" + id;

	if (new_day == current_day)
		return false;

	document.getElementById(current_day).style.display = "none";
	document.getElementById(new_day).style.display = "";
	current_day = new_day;
	return true;
}

function getCookie(c_name)
{
	if (document.cookie.length > 0)
	{
		var c_start = document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{
			c_start = c_start + c_name.length + 1;
			var c_end = document.cookie.indexOf(";", c_start);
			if (c_end == -1) c_end = document.cookie.length;
			return unescape(document.cookie.substring(c_start, c_end));
		}
	}
	return "";
}

// Hide overflows when collapsing/expanding columns
function modulesOverflow(column)
{
	var test = null;
	for (var i = 0; i < column.childNodes.length; i++)
	{
		if(column.childNodes[i].nodeName=='#text') continue;
		var getId = column.childNodes[i].getAttribute('id');
			// Handle the modular layout style!
			if (getId === test)
			{
				var modular = column.childNodes[i].getElementsByTagName("div");
				for (var x = 0; x < modular.length; x++)
				{
					getId = modular[x].id;
					if (getId.indexOf('module_') != 0) continue;
					modular[x].style.overflowX = "hidden";
					var parentDiv = modular[x].parentNode.parentNode.parentNode.parentNode;
					if(parentDiv !== test)
						parentDiv.style.overflow = "hidden";
				}
				continue;
			}
			if (getId.indexOf('module_') != 0) continue;
				column.childNodes[i].style.overflowX = "hidden";
	}
}

function right_columnCollapseAnim(expand, speed)
{
	var rightColumn = document.getElementById("dp_right");
	var colWidth = rightColumn.offsetWidth;
	rrit = 1;

	modulesOverflow(rightColumn);
	rightColumn.style.whiteSpace = "nowrap";
	rightColumn.style.overflowX = "hidden";

	if (!cWidthRight && colRStart == 0)
	{
		document.cookie = "dpRightWidth=" + colWidth;
		cWidthRight = colWidth;	
	}

	var minWidth = 0;
	var moveBy = Math.round(speed * 10);
	var intId = setInterval(function() {
		var curWidth = rightColumn.offsetWidth;
		var newWidth = curWidth - moveBy;
		if (newWidth > minWidth)
		{
			colRStart = 1;
			rightColumn.style.width = newWidth + "px";
		}
		else {
			clearInterval(intId);
			rightColumn.style.width = "0px";
			rightColumn.style.display = "none";
			
			if (parseInt(guest) == 0)
				document.cookie = "dp_right=1";
			else
				smf_setThemeOption("dp_right", 1, null, sessid);

			setInnerHTML(document.getElementById("dp_right_collapse"), expand);
			colRStart = 0;
		}
	}, 30);	
}

function right_columnExpandAnim(collapse, speed)
{
	var rightColumn = document.getElementById("dp_right");

	rightColumn.style.width = "0px";
	
	if (rrit == 0)
	{
		modulesOverflow(rightColumn);
		rightColumn.style.whiteSpace = "nowrap";
	}

	rightColumn.style.overflowX = "hidden";

	rightColumn.style.display = "";
	if (!cWidthRight)
		var match = getCookie("dpRightWidth");
	else
		var match = cWidthRight;

	var colWidth = parseInt(match);
	var moveBy = Math.round(speed * 10);

	var intId = setInterval(function() {
		var curWidth = rightColumn.offsetWidth;
		var newWidth = curWidth + moveBy;
		if (newWidth < colWidth)
		{
			rightColumn.style.width = newWidth + "px";
			colRStart = 1;
		}
		else {
			clearInterval(intId);
			modulesOverflow(rightColumn);
			rightColumn.style.width = "";
			rightColumn.style.whiteSpace = "normal";
			colRStart = 0;
			if(parseInt(guest) == 0)
				document.cookie = "dp_right=0";
			else
				smf_setThemeOption("dp_right", 0, null, sessid);

			setInnerHTML(document.getElementById("dp_right_collapse"), collapse);
		}
	}, 30);
}

function left_columnCollapseAnim(expand, speed)
{
	var leftColumn = document.getElementById("dp_left");
	var colWidth = leftColumn.offsetWidth;
	rlit = 1;

	modulesOverflow(leftColumn);
	leftColumn.style.whiteSpace = "nowrap";
	leftColumn.style.overflowX = "hidden";

	if (!cWidthLeft && colLStart == 0)
	{
		document.cookie = "dpLeftWidth=" + colWidth;
		cWidthLeft = colWidth;	
	}

	var minWidth = 0;
	var moveBy = Math.round(speed * 10);
	var intId = setInterval(function() {
		var curWidth = leftColumn.offsetWidth;
		var newWidth = curWidth - moveBy;
		if (newWidth > minWidth)
		{
			colLStart = 1;
			leftColumn.style.width = newWidth + "px";
		}
		else {
			clearInterval(intId);
			leftColumn.style.width = "0px";

			colLStart = 0;
			leftColumn.style.display = "none";
			
			if (parseInt(guest) == 0)
				document.cookie = "dp_left=1";
			else
				smf_setThemeOption("dp_left", 1, null, sessid);

			setInnerHTML(document.getElementById("dp_left_collapse"), expand);
		}
	}, 30);	
}

function left_columnExpandAnim(collapse, speed)
{
	var leftColumn = document.getElementById("dp_left");

	leftColumn.style.width = "0px";

	if (rlit == 0)
	{
		modulesOverflow(leftColumn);
		leftColumn.style.whiteSpace = "nowrap";
		rlit = 1;
	}

	leftColumn.style.overflowX = "hidden";
	leftColumn.style.display = "";
	if (!cWidthLeft)
		var match = getCookie("dpLeftWidth");
	else
		var match = cWidthLeft;

	var colWidth = parseInt(match);
	var moveBy = Math.round(speed * 10);

	var intId = setInterval(function() {
		var curWidth = leftColumn.offsetWidth;
		var newWidth = curWidth + moveBy;
		if (newWidth < colWidth)
		{
			leftColumn.style.width = newWidth + "px";
			colLStart = 1;
		}
		else {
			clearInterval(intId);
			modulesOverflow(leftColumn);
			leftColumn.style.width = "";
			leftColumn.style.whiteSpace = "normal";
			colLStart = 0;
			if(parseInt(guest) == 0)
				document.cookie = "dp_left=0";
			else
				smf_setThemeOption("dp_left", 0, null, sessid);

			setInnerHTML(document.getElementById("dp_left_collapse"), collapse);
		}
	}, 30);
}

function toggle_left_columnAnim(collapse, expand, speed)
{
	if (colLStart != 0)
		return;

	if (document.getElementById("dp_left").style.display == "")
		left_columnCollapseAnim(expand, speed);
	else
		left_columnExpandAnim(collapse, speed);
}

function toggle_right_columnAnim(collapse, expand, speed)
{
	if (colRStart != 0)
		return;

	if (document.getElementById("dp_right").style.display == "")
		right_columnCollapseAnim(expand, speed);
	else
		right_columnExpandAnim(collapse, speed);	
}

function toggle_left_column(collapse, expand)
{
	mode = document.getElementById("dp_left").style.display == "" ? 0 : 1;
	if (parseInt(guest) == 0)
		document.cookie = "dp_left=" + (mode ? 0 : 1);
	else
		smf_setThemeOption("dp_left", mode ? 0 : 1, null, sessid);

	setInnerHTML(document.getElementById("dp_left_collapse"), mode ? collapse : expand);
	document.getElementById("dp_left").style.display = mode ? "" : "none";
}


function toggle_right_column(collapse, expand)
{
	mode = document.getElementById("dp_right").style.display == "" ? 0 : 1;
	if (parseInt(guest) == 0)
		document.cookie = "dp_right=" + (mode ? 0 : 1);
	else
		smf_setThemeOption("dp_right", mode ? 0 : 1, null, sessid);

	setInnerHTML(document.getElementById("dp_right_collapse"), mode ? collapse : expand);
	document.getElementById("dp_right").style.display = mode ? "" : "none";
}