
 /**
 * AUDI JavaScript library: 3rd-party API
 * 
 * @projectDescription	Public API
 * @namespace			api.*
 *
 * @author 				$Author: hhoettecke $
 * @version				$Revision: 10190 $
 * @copyright			NEUE DIGITALE GmbH, Berlin
 * 
 * @jslint: 2008-10-31
 * 
 * @file:				audi_ngw.api.js
 * $URL: https://svn.pvtool.org/svn/day_audi_ngw/trunk/ngw_base/frontend/js/audi/audi.api.js $
 * 
 * @TODO:
 * audi_ngw.api.track: implement proxy to tracking function
 * check advice / detailvie / login: opens, modal layers, calling different urls?!
 * 
 */

/* create namespace */
audi_ngw.namespace(audi_ngw,'api.helper');
 

audi_ngw.api.helper.aExecQueueFn = [];
audi_ngw.api.helper.aExecQueueId = [];
audi_ngw.api.helper.tExecTimer = null;
audi_ngw.api.helper.iExecutionTries = 0;
audi_ngw.api.helper.iMaxRepitions = 40;
audi_ngw.api.helper.iExecTimeoutMs = 250;
/**
 * Forces strict type 
 * @method audi_ngw.api.helper.strict
 * @param {object} Element to check
 * @param {String} Expected type (Function,Array,Boolean,String,Object,Number,Boolean)
 * @return {Void}
 */
audi_ngw.api.helper.strict = function(oElement, sType) {
	var _isType = false;
	// use jQuery in case of function
	if( sType === Function) {
		_isType = jQuery.isFunction(oElement);
	} 
	// use constructor property, don't check for object subtypes
	else {
		_isType = (oElement.constructor == sType);
	}
	// valid 
	if (_isType === true ) {
		return true;
	} 
	// invalid
	else {
		return false;
		//throw new Error ('[audi_ngw.api.helper.strict] Invalid argument type. Expected '+oElement+' / ' + sType+ ', received'+oElement+' / ' + oElement.constructor);
	}
};

/**
 * Failsafe execution of 3rd-party functions. Pass anonymus function if you relay on arguments
 * @param {Function} fnFunction
 * @param {String} Identifier
 * @return {Boolean}
 */
audi_ngw.api.helper.execute = function(fnFunction, sId) {
 	
	var _index;
	
	// use jQuery to check for function
	if (jQuery.isFunction(fnFunction)) {
		// try to execute
		sId = sId || String(fnFunction);
		try {
			// check if this function is already queued (prevent loops)
			_index = jQuery.inArray( sId, audi_ngw.api.helper.aExecQueueId);
			if (_index !== -1 ) { 
				// remove from queue
				audi_ngw.api.helper.aExecQueueFn.splice(_index,1);
				audi_ngw.api.helper.aExecQueueId.splice(_index,1);
			}
			// execute
			fnFunction();
			return true;
		} 
		// error during execution
		catch (e) {
			// check if this function ain't queued
			_index = jQuery.inArray( sId, audi_ngw.api.helper.aExecQueueId);
			if (_index === -1 ) { 
				// reappend to queue
				audi_ngw.api.helper.aExecQueueFn.push(fnFunction); 
				audi_ngw.api.helper.aExecQueueId.push(sId); 
			}

			// if no timer is set, activate
			if (!audi_ngw.api.helper.tExecTimer) {
				audi_ngw.api.helper.tExecTimer = window.setInterval(function () {
					audi_ngw.api.helper.executeQueue();
				},audi_ngw.api.helper.iExecTimeoutMs);
			}

			return false;		
		}
	} 
	// no function
	else {
		return false;
	}
};

/**
 * Executes stored funciton 
 * @return {Boolean}
 */
audi_ngw.api.helper.executeQueue = function() {

	// cycle queue, execute all functions 
	for( var _i = 0; _i < audi_ngw.api.helper.aExecQueueId.length; _i++) {
		audi_ngw.api.helper.execute(audi_ngw.api.helper.aExecQueueFn[_i],audi_ngw.api.helper.aExecQueueId[_i]);
	}
//	console.log("---> audi_ngw.api.helper.iExecutionTries",audi_ngw.api.helper.iExecutionTries)
	// stop after max repitions
	if (audi_ngw.api.helper.aExecQueueId.length === 0 || audi_ngw.api.helper.iExecutionTries++ >= audi_ngw.api.helper.iMaxRepitions ) {
		audi_ngw.api.helper.tExecTimer = audi_ngw.clearTimer(audi_ngw.api.helper.tExecTimer);
		// reset initial values
		audi_ngw.api.helper.iExecutionTries = 0;
		audi_ngw.api.helper.aExecQueueFn = [];
		audi_ngw.api.helper.aExecQueueId = [];
	}
};


/* create namespace */
audi_ngw.namespace(audi_ngw,'api');

/**
 * Ã–ffnet die angegebene Seite in einem modalen Layer. Als Parameter kÃ¶nnen Callbacks fÃ¼r open/close angegeben werden.
 * @method audi_ngw.api.openModal 
 * @param {String} URL 
 * @param {Object} Parameters: {Function} Callback .onOpen, {Function} Callback .onClose
 * @return {Void}
 */
audi_ngw.api.openModal = function(sUrl, oParam) {
	// check types
	//audi_ngw.api.helper.strict(sUrl,String);
	audi_ngw.api.helper.strict(oParam,Object);
	
	// execute callback
	audi_ngw.api.helper.execute(oParam.onOpen);
	// clear memory
	oParam.onOpen = null;

	// @TODO open  layer
	audi_ngw.api.helper.execute(function(){
		audi_ngw.layer.modal.api.show(sUrl, oParam);
	},'audi_ngw.layer.modal.api.show');

	// clear memory
	oParam.onClose = null;

};


/**
 * SchlieÃŸt einen geÃ¶ffneten modalen Layer.
 * @method audi_ngw.api.closeModal 
 * @return {Void}
 */
audi_ngw.api.closeModal = function () {
	// rollback page title on modal close
	audi_ngw.navigation.title = audi_ngw.navigation.baseTitle;
	audi_ngw.navigation.doSetTitle();
	
	audi_ngw.layer.modal.hide();
}

/**
 * @projectDescription	Public API for iframe interaction
 * @namespace api.iframe
 */

/* create namespace */
audi_ngw.namespace(audi_ngw,'api.iframe');

/**
 * Diese Funktion kann aufgerufen werden, wenn sich der Seiteninhalt in einem iFrame geÃ¤ndert hat, um die HÃ¶he des iFrames daran anzupassen.
 * @method audi_ngw.api.iframe.updated 
 * @return {Void}
 */
audi_ngw.api.iframe.updated = function (){
	audi_ngw.iframe.onUpdate();
};

/**
 *
 * @method audi_ngw.api.iframe.loaded
 * @param {Function} Callback
 * @return {Void}
 */
audi_ngw.api.iframe.loaded = function () {
	audi_ngw.iframe.onLoad();

};

/**
 * Diese Funktion kann aufgerufen werden, wenn ein Seitenwechsel im iFrame stattfindet, um Speicher freizugeben.
 * @method audi_ngw.api.iframe.unloaded
 * @return {Void}
 */
audi_ngw.api.iframe.unloaded = function () {
	audi_ngw.iframe.onUnload();
};

/**
 * Setzt die HÃ¶he des iFrames auf den angegebenen Wert.
 * @method audi_ngw.api.iframe.height
 * @param {Number} New iframe height 
 * @return {Void}
 */
audi_ngw.api.iframe.height = function (iHeight) {
	if (!audi_ngw.api.helper.strict(iHeight,Number)) { return; }
	audi_ngw.iframe.setHeight(iHeight);
};



/**
 * @projectDescription	Public API for flash interaction
 * @namespace api.flash
 */
/* create namespace */
audi_ngw.namespace(audi_ngw,'api.flash');


/**
 * Setzt die Breite eines Flashplayers auf den angegebenen Wert. 
 * Als id muss das ID- bzw. Name-Attribut des Object/Embed-Tags Ã¼bergeben werden, 
 * das den jeweiligen Flashplayer einbindet. 
 * Dieser Wert kann in Flash Ã¼ber ExternalInterface.objectID ausgelesen werden.
 * @method audi_ngw.api.flash.width
 * @param {String} ID / NAME of OBJECT / EMBED
 * @param {Number} New iframe height 
 * @return {Void}
 */
audi_ngw.api.flash.width = function (id, width) {
	width = parseInt(width, 10);
	try { audi_ngw.flash.setWidth(id, width);}
	catch(e) {return false;}
};





/**
 * Setzt die HÃ¶he eines Flashplayers auf den angegebenen Wert. 
 * Als id muss das ID- bzw. Name-Attribut des Object/Embed-Tags Ã¼bergeben werden, 
 * das den jeweiligen Flashplayer einbindet. 
 * Dieser Wert kann in Flash Ã¼ber ExternalInterface.objectID ausgelesen werden.
 * @method audi_ngw.api.flash.height
 * @param {String} ID / NAME of OBJECT / EMBED
 * @param {Number} New iframe height 
 * @return {Void}
 */
audi_ngw.api.flash.height = function (id, height) {
	height = parseInt(height, 10);
	try{audi_ngw.flash.setHeight(id, height);}
	catch(e) {return false;}
};


/**
 * Ersetzt das gegebenen DOM-Element mit einer sIFR-Headline
 *
 * @method audi_ngw.api.flash.sifr
 * @param {String} sSelectorWrapper / Wrapper-DOM-Element
 * @param {String} sSelectorElement / DOM-Element to be replaced by sIFR-Headline
 * @param {Number} sFontSize / Size for the sFIR-Font
 * @param {String} sTextColor / Color for the text
 * @return {Void}
 */
audi_ngw.api.flash.sifr = function (sSelectorWrapper, sSelectorElement, sFontSize, sTextColor) {

	if (!audi_ngw.api.helper.strict(sSelectorElement, String)) { return; }
	sSelectorWrapper = (sSelectorWrapper) ? sSelectorWrapper : 'body';	
	sTextColor = (sTextColor) ? sTextColor : '#fff';
	sFontSize = (sFontSize) ? sFontSize : 12;
	sFontSize = parseInt(sFontSize, 10) + 'px';

	try {
		audi_ngw.flash.replace({sSelectorWrapper: sSelectorWrapper, sSelectorElement: sSelectorElement, sFontSize: sFontSize, sTextColor: sTextColor});
	} catch(e) {
		return false;		
	}
	
	
};



/*********************************
* DEPRECATED FUNCTIONS
**********************************/




/**
 * Ã–ffnet die angegebene Seite als Detail-View. Als Parameter kÃ¶nnen Callbacks fÃ¼r open/close angegeben werden.
 * @method audi_ngw.api.openDetail 
 * @param {String} URL 
 * @param {Object} Parameters: {Function} Callback .onOpen, {Function} Callback .onClose
 * @return {Void}
 * @deprecated
 */
audi_ngw.api.openDetail = function(sUrl, oParam) {
	// check types
	audi_ngw.api.helper.strict(sUrl,String);
	audi_ngw.api.helper.strict(oParam,Object);
	
	// execute callback
	audi_ngw.api.helper.execute(oParam.onOpen);
	// clear memory
	oParam.onOpen = null;

	// @TODO open  layer
	audi_ngw.api.helper.execute(function(){
		audi_ngw.layer.modal.api.show(sUrl, oParam);
	},'audi_ngw.layer.modal.api.detail');

	// clear memory
	oParam.onClose = null;
};



/**
 * Ã–ffnet die angegebene Seite im Advice-Modul. Als Parameter kÃ¶nnen Callbacks fÃ¼r open/close angegeben werden.
 * @method audi_ngw.api.openAdvice 
 * @param {String} URL 
 * @param {Object} Parameters: {Function} Callback .onOpen, {Function} Callback .onClose
 * @return {Void}
 * @deprecated
 */
audi_ngw.api.openAdvice = function(sUrl, oParam){
	// check types
	audi_ngw.api.helper.strict(sUrl,String);
	audi_ngw.api.helper.strict(oParam,Object);
	
	// execute callback
	audi_ngw.api.helper.execute(oParam.onOpen);
	// clear memory
	oParam.onOpen = null;

	// @TODO open  layer
	audi_ngw.api.helper.execute(function(){
		audi_ngw.layer.modal.api.show(sUrl, oParam);
	},'audi_ngw.layer.modal.api.detail');

	// clear memory
	oParam.onClose = null;
};


/**
 * Ã–ffnet den Login-Layer. Als Parameter kÃ¶nnen Callbacks fÃ¼r open/close angegeben werden.
 * @method audi_ngw.api.openLogin 
 * @param {String} URL 
 * @param {Object} Parameters: {Function} Callback .onOpen, {Function} Callback .onClose
 * @return {Void}
 * @deprecated 
 */
audi_ngw.api.openLogin = function(sUrl, oParam){
	// check types
	audi_ngw.api.helper.strict(sUrl,String);
	audi_ngw.api.helper.strict(oParam,Object);
	
	// execute callback
	audi_ngw.api.helper.execute(oParam.onOpen);
	// clear memory
	oParam.onOpen = null;

	// @TODO open  layer
	audi_ngw.api.helper.execute(audi_ngw.layer.modal.api.login(sUrl,oParam));

	// clear memory
	oParam.onClose = null;
};


/**
 * Leitet Tracking-Aufrufe an den Tracking-Provider weiter. 
 * Diese Methode funktioniert als Wrapper und gewÃ¤hrleistet schnelle Anpassungen an die Schnittstelle des Tracking-Anbieters.
 * @method audi_ngw.api.track 
 * @param {String} Pagetitle 
 * @param {String} EventType 
 * @param {Object} [customData] custom data as object
 * @return {Void}
 * @deprecated
 */
audi_ngw.api.track = function(sPageTitle, sEventType, customData) {
	// check types
	audi_ngw.api.helper.strict(sPageTitle,String);
	audi_ngw.api.helper.strict(sEventType,String);
	
	// @TODO implement proxy to tracking function

};

/**
 * Bindet die Klick-Events fÃ¼r den angegebenen DOM-Bereich (neu)
 * @param {jQuery-Object} $scope
 * @method audi_ngw.api.bindClickEvents 
 * @return {Void}
 */
audi_ngw.api.bindClickEvents = function () {
	return;	
};
