/*
 * ciabCommons.js
 * functions needed in all channel boxes
 * copyrighted (c) christian hartmann 2009 (hartmann it-design)
 */

var firebug = false;

function InitFirebugConsole () {
	if (firebug == false) {
		if (typeof loadFirebugConsole == 'function') {
			window.loadFirebugConsole();
			firebug = true;
		}
		(firebug) && console.log("Firebug Console initialized");
	}
}

function isDefined (variable)
{
    return (typeof(window[variable]) == "undefined") ?  false : true;
}

function CallBoxFunc (frameName, functName, functArgs) {
	var _returns; // for remote procedure return values
	if (window.parent.frames.length > 0) {
		try {
			_returns = eval('window.parent.frames["' +frameName + '"].' + functName + '(' + functArgs + ')');
			return _returns;
		}
		catch (err) {
			(firebug) && console.log (err); // debug to firebug console
			return '';
		}
	}
}

function CallParentCallback(cbName, cbArgs) {
	if (window.parent) {
		try {
			eval('window.parent.' + cbName + '(' + cbArgs + ')');
		}
		catch (err) {
			(firebug) && console.log (err); // debug to firebug console
		}
	}
}

function GetThisFrameWidth() {
	var x = 0;
	if (document.documentElement && document.documentElement.clientWidth) {
		  x = document.documentElement.clientWidth;
	}
	else if (document.body) {
		  x = document.body.clientWidth;
	}
	else if (self.innerHeight) {
		  x = self.innerWidth;
	}
	return x;
}

function GetThisFrameHeight () {
	var y = 0;
	if (document.documentElement && document.documentElement.clientHeight) {
		  y = document.documentElement.clientHeight;
	}
	else if (document.body) {
		  y = document.body.clientHeight;
	}
	else if (self.innerHeight) {
		  y = self.innerHeight;
	}
	return y;
}

// GetContainerSize
// contName Id of any Element
// !!! returns currently just the height of the element
// ??? return an array
// requires the height given in px in the CSS
function GetContainerSize(contName) {
	// Example: GetContainerSize('ciabTrackSubtitelList');
	var _height;
	var _height_px;
	_height = _GetStyleById(contName).height;
	//alert('Containers ' + contName + ' height is: ' + _height);
	_height_px = _height.replace("px", ""); // UUUH quick'n dirty!
	//alert('Containers ' + contName + ' height in pixel is: ' + _height_px);
	return _height_px;
}

function _GetStyleById(elementId){
	var _elem = document.getElementById(elementId);
	try{
		return getComputedStyle(_elem, null);
	}
	catch(error){
		return _elem.currentStyle;
	}
}

function CallSiblingFrame () {
	var frameName; // name of the sibling frame
	var functName; // name of a function in that frame
	var functArgs; // remaining arguments as standard array
	var _returns;  // for remote procedure return values
	if (window.parent.frames.length > 0) {
		// Create a new array from the contents of arguments  
		var _args = Array.prototype.slice.call(arguments);
		frameName = _args.shift();
		functName = _args.shift();
		functArgs = _args;
		// get the sibling frame object
		var _sibFrameFunct = eval('window.parent.frames["' + frameName + '"].' + functName);
		_sibFrameFunct.apply(this, functArgs);
	}
}

function CallSiblingFrameFunc (frameName, functName, functArgs) {
	var _returns;  // for remote procedure return values
	if (window.parent.frames.length > 0) {
		// get the function object in the sibling frame
		var _sibFrameFunct = eval('window.parent.frames["' + frameName + '"].' + functName);
		// and call it with argument vector (aka array)
		_sibFrameFunct.apply(this, functArgs);
	}
}
