/*
	Expanding Boxes - Liran Oz
	Changes:
	Removed mouse listener, works now with mouseover
*/

var mainObj = new Array();
var expSize = 140;
function initBoxes(overideExpSize)
{
	mainObj[0] = new expBox('box1', 0);
	mainObj[1] = new expBox('box2', 1);
	mainObj[2] = new expBox('box3', 2);
	mainObj[3] = new expBox('box4', 3);
	if (overideExpSize)
		expSize = overideExpSize;
		
	for (i=0; i<mainObj.length; i++) {
		mainObj[i].overrideExpansion(expSize);
		mainObj[i].setAbsolute('mainObj', i);
	}
	
	window.onresize = reInit;
}

function reInit()
{
	for (i=0; i<mainObj.length; i++) {
		mainObj[i].resetStat();
		mainObj[i].setBlock();
	}
	initBoxes();
}

function findPos(obj)
{
    var curleft = curtop = 0;

    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);

        return [curleft,curtop];
    }
}

function childObj(_obj, ratio)
{
		var obj = _obj;
		var ratio = ratio;
		var elmHeight = obj.offsetWidth;

		this.keepAspect = function(containerHeight)
		{
			//ratio = elmHeight / containerHeight; => elmHeight = ratio * containerHeight;
			obj.style.height = (ratio * containerHeight) + 'px';
		}

}

function expBox(_id, _objNum)
{
	var id = _id;
	var objNum = _objNum;
	var obj = document.getElementById(id);
	if (!obj)
		return;

	var objPos = findPos(obj);
	var rect = new Array();
	var childObjects = new Array();		//will hold all the data for images inside the box, using the child object
	//build initial bounding box
	rect[0] = objPos[0];
	rect[1] = objPos[1];
	rect[2] = obj.offsetWidth;
	rect[3] = obj.offsetHeight;

	var orgRect = rect;

	var className = 'expandingBox';
	var normalClass= 'expandingBoxLayout';

	var expanded = false;
	var collapsed = true;
	var expObj = null;
	var colObj = null;
	var transSpeed = 0.2;
	var normalZ = 10;
	var middleZ = 11;
	var topZ = 150;
	var expension = 140;
	var objIndex = 0;
	var halt = false;
	var transCallback = null;

	/*
	Explorer 6 fixes
	*/
	this.haltBox = function()
	{
		halt = true;
	}

	this.unhaltBox = function()
	{
		halt = false;
	}


	//make object absolute on screen
	this.setAbsolute = function(objName, mainIndex)
	{
		obj.style.position = 'absolute';
		obj.style.top = objPos[1] + 'px';
		obj.style.left = objPos[0] + 'px';
		obj.setAttribute('class', className);
		obj.setAttribute('className', className);

		obj.setAttribute('onmouseover', objName + '[' + mainIndex + '].expandBox();');
		obj.setAttribute('onmouseout', objName + '[' + mainIndex + '].collapseBox();');
		objIndex = mainIndex;
		obj.onmouseover = function() {mainObj[objIndex].expandBox();};
		obj.onmouseleave = function() {mainObj[objIndex].collapseBox();};

		var div = obj.getElementsByTagName('div');

		var j = 0;
		for (var i=0; i<div.length; i++) {
			if (!div[i].className.match(/.*expanding.*/i) )
				continue;

			//add as a new expanding element
			childObjects[j++] = new childObj(div[i], div[i].offsetHeight / obj.offsetHeight);
		}

		/*
		This are fixes for explorer 6, which triggers mouseout events as he wishes
		*/
		var isMSIE = /*@cc_on!@*/false;
		if (isMSIE)  {
			var selects = obj.getElementsByTagName('select');

			for (var i=0; i<selects.length; i++) {
				selects[i].onmouseover = function() { mainObj[objNum].haltBox(); };
				selects[i].onmouseout = function() { event.cancelBubble = true;};
				selects[i].onchange = function() { mainObj[objNum].unhaltBox(); mainObj[objNum].collapseBox();};
			}

			var selects = obj.getElementsByTagName('form');

			for (var i=0; i<selects.length; i++) {
				selects[i].onmouseout = function() { event.cancelBubble = true; mainObj[objNum].unhaltBox();};
			}
		}

	}

	this.setBlock = function()
	{
		obj.style.position = 'static';
		obj.setAttribute('class', normalClass);
		obj.setAttribute('className', normalClass);
	}

	this.expandBox = function()
	{
		if  (expanded || !collapsed)
			return;

		expanded = true;
		collapsed = false;
		obj.style.zIndex = topZ;

		if(colObj) {
			colObj.cancel();
			colObj = null;
		}

		//how much more needed for expanding
		dx = Math.ceil(orgRect[2] * expension / 100);
		expTo = Math.floor(dx * 100 / obj.offsetWidth);


		expObj = new Effect.Scale(obj, expTo, {
			duration: transSpeed,
			scaleFromCenter: true,
			afterUpdate: updateBoundingBox
		});

	}

	this.collapseBox = function()
	{
		if  (collapsed || !expanded || halt)
			return;

		collapsed = true;
		expanded = false;
		obj.style.zIndex = middleZ;

		//calculate how much to scale back
		var percentage = 100 * orgRect[2] / obj.offsetWidth;

		if(expObj) {
			expObj.cancel();
			expObj = null;
		}

		colObj = new Effect.Scale(obj, percentage, {
			duration: transSpeed,
			scaleFromCenter: true,
			afterUpdate: updateBoundingBox,
			afterFinish: centerBox
		});
	}

	this.inBounds = function(mx, my)
	{
		if (mx < rect[0] || mx > rect[0] + rect[2])
			return false;

		if (my < rect[1] || my > rect[1] + rect[3])
			return false;

		return true;
	}

	this.isExpanded = function()
	{
		return expanded;
	}

	this.isCollapsed = function()
	{
		return collapsed;
	}

	function updateBoundingBox()
	{
		if (transCallback != null)
			transCallback();
			
		//Note - bounding box mouse checking is not used anymore
		rect = findPos(obj);
		rect[2] = obj.offsetWidth;
		rect[3] = obj.offsetHeight;

		//center it - must be done to fix glitches (working with natural numbers and not real ones)
		var dx = Math.floor((rect[2] - orgRect[2])/2);
		var dy = Math.floor((rect[3] - orgRect[3])/2);
		obj.style.top = (orgRect[1] - dy) + 'px';
		obj.style.left = (orgRect[0]  - dx) + 'px';

		//update expanding objects
		for (var i=0; i<childObjects.length; i++) {
			childObjects[i].keepAspect(obj.offsetHeight);
		}
	}

	//needed to fix minor position glitches on collapsing
	function centerBox()
	{
		obj.style.top = orgRect[1] + 'px';
		obj.style.left = orgRect[0] + 'px';
		obj.style.width = orgRect[2] + 'px';
		obj.style.height = orgRect[3] + 'px';
		colObj = null;
		obj.style.zIndex = normalZ;
	}

	this.resetStat = function()
	{
		centerBox();
	}

	this.overrideExpansion = function(newExp) {
		expension = newExp;
	}
	
	this.setTransitionCallback = function(f) {
		transCallback = f;
	}
}


function redirectPage(url)
{
	location.href = url;
}