/*
 * ciabControl.js
 * functions needed in the embedding web page
 * copyrighted (c) christian hartmann 2009 (hartmann it-design)
 * 2008-10-26 CH initial for trendcharts.de
 * 2008-11-19 CH didi some work on the actual remote procedure caller function
 */



/*
 * start playing video
 * state: WORKING
 */
function CiabPlay() {
    _CallCiabBoxFuncByControlBox('CiabPlay');
	window.status = 'CiabPlay() called';
}
/*
 * pause playing video
 * state: WORKING
 */
function CiabPause() {
	_CallCiabBoxFuncByControlBox('CiabPause');
	window.status = 'CiabPause() called';
}
/*
 * start playing video if stopped or vice versa
 * state: WORKING
 */
function CiabTogglePlay() {
   _CallCiabBoxFuncByControlBox('CiabTogglePlay');
	window.status = 'CiabTogglePlay() called';
}
/*
 * stop playing video
 * state: NOT WORKING (API BROKEN?)
 */
function CiabStop() {
    _CallCiabBoxFuncByControlBox('CiabStop');
	window.status = 'CiabStop() called';
}
/*
 * turn players sound off
 * state: WORKING (by pausing in the current version)
 */
function CiabMute() {    _CallCiabBoxFuncByControlBox('CiabMute');
	window.status = 'CiabMute() called';
}
/*
 * turn players sound on
 * state: WORKING (by playing in the current version)
 */
function CiabUnmute() {    _CallCiabBoxFuncByControlBox('CiabUnmute');
	window.status = 'CiabUnmute() called';
}
/*
 * skip to a track if playing or not
 * state: WORKING
 */
function CiabSkipTo(trackNum) {	_CallCiabBoxFuncByControlBox('CiabSkipTo',trackNum);
	window.status = 'CiabSkipTo(' + trackNum + ') called';
}
/*
 * get current track number
 * state: NOT TESTED
 */
function CiabGetCurrentTrackNum() {	return _CallCiabBoxFuncByControlBox('CiabGetCurrentTrackNum');	window.status = 'CiabGetCurrentTrackNum() called';
}

/*
 * _CallCiabBoxFunc
 * this is intended to be privat
 * provides generic communication with and to CiaB boxes
 */
function _CallCiabBoxFuncByControlBox (functName, functArgs) {
	// build the query string
	var query_string;
	//alert('typeof(functArgs) = ' + typeof(functArgs));
	if (typeof(functArgs) == 'undefined') {
		query_string = '?function=' + functName;
	} else {
		query_string = '?function=' + functName + '&arguments=' + functArgs;
	}
	// get local body element
	localbody = document.getElementsByTagName('body')[0];
	
	var cif; // control IFrame
	// remove the element if it has been created before
	if (document.getElementById('ciabRemoteControlBox')) {
		cif = document.getElementById('ciabRemoteControlBox');
		localbody.removeChild(cif);
		cif = null;
	}
	// create an invisible or nearly invidibel (1px size) iframe inside
	// local document and point it's url to: http://ciab.imuse.tv/ciabControl.php
	// append arguments to ciab boxes as standard GET parameters
	cif = document.createElement('iframe');
	cif.setAttribute('id','ciabRemoteControlBox');
	cif.setAttribute('name','ciabRemoteControlBox');
	cif.setAttribute('width','600');
	cif.setAttribute('height','200');
	cif.setAttribute('frameborder','1');
	//alert('http://ciab.imuse.local/ciabControl.php' + query_string);
	cif.setAttribute('src','http://ciab.imuse.tv/ciabControl.php' + query_string);
	localbody.appendChild(cif);
}
