/**
 * @copyright  Copyright(c) 2005-2010, IC Zones
 * @author     Michael Jolin
 * @since      2010,02,03
 * @package		JS
**/

var iczones = {
	security: {
			activeX: true,
			transparency: true,
			popupBlocker: true,
			video: { h264: false, ogg: false },
			audio: { mp3: false, ogg: false, acc: false, wav: false }
		},
	config: {},
	labels: {},
	messages: {},
	object: {},
	widget: {},
	module: {},
	modCustomer: {},
	lbl: {},
	tinyMCE: {},
	swfUpload: {},
	swfUploadWindow: null,
	javaDownload: null,
	loadingError: null,
	interval: {},
	addOnLoad: null,
	addOnStart: null,

	/**
	 * Return the number of element in object
	 *
	 * @author		Michael Jolin
	 * @since 		2010,01,12
	**/
	count: function( _obj ) {
		var nb = 0;
		for( var i in _obj ) {
			nb++;
		}

		return nb;
	},

	/**
	 * Merge each object/array in only one object
	 *
	 * @author		Michael Jolin
	 * @since 		2010,01,12
	**/
	merge: function() {
		var obj = {};
		var j = 0;

		for( var i=0; i<arguments.length; i++ ) {
			if( Ext.isArray( arguments[i] ) ) {
				for( j=0; j<arguments[i].length; j++ ) {
					obj[iczones.count( obj )] = arguments[i][j];
				}
			} else {
				for( j in arguments[i] ) {
					if( j.isNumeric() ) {
						obj[iczones.count( obj )] = arguments[i][j];
					} else {
						obj[j] = arguments[i][j];
					}
				}
			}
		}
 
		return obj;
	},

	/**
	 * Test some browser security
	 *
	 * @author		Michael Jolin
	 * @since		2010,05,04
	**/
	testSecurity: function() {
		// Check if active X is activated
		try {
			var obj = ( Ext.isIE ? new ActiveXObject( 'Microsoft.XMLHTTP' ) : new XMLHttpRequest() );
		} catch( e ) {
			this.security.activeX = false;
		}

		// Check if transparency is available on browser
		if( typeof( document.getElementById( 'x-desktop' ).style.opacity ) == 'undefined' &&
				typeof( document.getElementById( 'x-desktop' ).filters ) != 'object' ) {
			this.security.transparency = false;
		}

		// Check if the popup blocker is activated
		/*var mypopup = window.open( '', 'win', 'width=10,height=10' );
		if( mypopup ) {
			mypopup.close();
			this.security.popupBlocker = false;
		}*/

		// Check if H264/Ogg video tag is available
		var v = document.createElement( 'video' );
		if( typeof( v.canPlayType ) == 'function' ) {
			this.security.video.h264 = ( v.canPlayType( 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"' ) ? true : false );
			this.security.video.ogg = ( v.canPlayType( 'video/ogg; codecs="theora, vorbis"' ) ? true : false );
		}
		
		// Check if Mp3/Ogg/Acc/Wav video tag is available
		var a = document.createElement( 'audio' );
		if( typeof( v.canPlayType ) == 'function' ) {
			this.security.audio.mp3 = ( a.canPlayType( 'audio/mpeg3;' ) ? true : false );
			this.security.audio.ogg = ( a.canPlayType( 'audio/ogg; codecs="vorbis"' ) ? true : false );
			this.security.audio.acc = ( a.canPlayType( 'audio/x-m4a;' ) ? true : false );
			this.security.audio.wav = ( a.canPlayType( 'audio/wav; codecs="1"' ) ? true : false );
		}
	}
};

/**
 * Object constructor
 *
 * @author		Michael Jolin
 * @since		2008,04,01
**/
iczones.object.main = {
	/**
	 * Post page with right parameter
	 *
	 * @author		Michael Jolin
	 * @since		2008,04,01
	**/
	doAction: function( _action ) {
		var an_action = document.getElementById( 'mvc_action' );
		if( an_action ) {
			an_action.value = ( _action ? _action : '' );
		}

		if( _action.indexOf( '->' ) >= 0 ) {
			this.postPage( '', '' );
		} else {
			this.postPage( _action, '' );
		}
	},

	/**
	 * Place the good target and action to the form send submit it
	 *
	 * @author		Michael Jolin
	 * @since      2008,04,01
	**/
	postPage: function( _action, _target ) {
		var form = document.getElementById( 'webForm' );
		if( _action != '' ) {
			form.action = _action;
		}

		form.target = _target;
	
		if( document.all && form.onsubmit ) {
			form.onsubmit();
		}
		form.submit();
	},

	/**
	 * Delay for a number of milliseconds
	 *
	 * @author		Michael Jolin
	 * @since		2009,06,30
	**/
	sleep: function( _msecs ) {
		if( iczones.security.activeX ) {
			var obj = ( Ext.isIE ? new ActiveXObject( 'Microsoft.XMLHTTP' ) : new XMLHttpRequest() );
			var start = new Date().getTime();
	
			while( new Date().getTime() < start + _msecs ) {
				obj.open( 'GET', iczones.config.url + '/source/connect.php?wait=' + _msecs, false );
				obj.send( null );
			}
		}
	},

	/**
	 * Check if the application is in iframe
	 *
	 * @author		Michael Jolin
	 * @since		2009,10,07
	**/
	checkIframe: function() {
		if( window.parent ) {
			if( window.parent.location.href != location.href ) {
				window.parent.location.href = location.href;
			}
		}
	}
};


