/**
* JS for MissionControl API
* @version $Id: base.js 1 2009-02-09 15:18:22Z tacker $
* @author Global Media GmbH | http://www.global-media.de/
*
* Important changes in API:
*  - r162: Explicit referer tracking has been removed. Referers are now tracked alongside the access track.
*/
var MissionControl = new Class({
	Implements: Events,
	STATUS_DATA_ERROR: 1,
	apiurl: null,
	trackPid: false,
	pid: null,
	language: null,
	trackData: new Object,
	country: null,
	lottery: null,
	fetchEcards: true,
	defaultEcard: null,
	ecards: null,
	enableCookieTest: false,
	cookieTestName: 'cookiecheck',
	cookieTest: null,
	initialize: function( myOptions )
	{
		for ( var i in myOptions ) {
			this[ i ] = myOptions[ i ];
		}
		
		// detect pid
		if( this.trackPid ) {
			if( this.getCookieData( 'pid' ) ) {
				this.pid = this.getCookieData( 'pid' );
			}
			if( this.pid !== null && this.pid.length > 0 ) {
				this.setCookieData( 'pid', this.pid );
				this.trackData[ 'pid' ] = this.pid;
			}
		}

		// detect referer
		var referer = '';
		if( this.getCookieData( 'referer' ) ) {
			referer = this.getCookieData( 'referer' );
		} else {
			if ( document.referrer.length > 0 && document.referrer.search( window.location.hostname ) < 0 ) {
				referer = document.referrer;
			}
		}
		if( referer.length > 0 ) {
			this.trackData[ 'referer' ] = referer;
			this.setCookieData( 'referer', referer );
		}

		// Preload ecards
		if ( this.fetchEcards ) {
			new Request.JSON({
				'url': this.apiurl + 'ecard',
				'onComplete': function( response ) {
					if ( this.checkApiResponse( response ) ) {
						this.ecards = response.result;
						if ( response.length === 1 ) this.defaultEcard = response.result[ 0 ].id;
					}
				}.bind( this )
			}).get();
		}

		// Check for cookies
		if ( this.enableCookieTest ) {
			if( this.getCookieData( this.cookieTestName ) === this.cookieTestName ) {
				this.track( 'cookies_enabled' );
				this.cookieTest = true;
			} else {
				this.track( 'cookies_disabled' );
				this.cookieTest = false;
			}
		}
	},
	track: function( trackname, myData )
	{
		if ( typeof myData === 'undefined' ) myData = {};
		myData = $merge( myData, this.trackData );
		if ( this.language !== null && typeof myData.language === 'undefined' ) myData.language = this.language;
		if ( this.country !== null && typeof myData.country === 'undefined' ) myData.country = this.country;
		new Request.JSON({
			'url': this.apiurl + 'track/' + trackname
		}).post( myData );
	},
	addLotteryParticipation: function( email, custom, pollId, pollChoices )
	{
		myData = { 'email': email };
		myData = $merge( myData, this.trackData );
		if ( typeof custom !== 'undefined' ) myData.custom = custom;
		if ( this.language !== null ) myData.language = this.language;
		if ( this.country !== null ) myData.country = this.country;
		
		if( typeof pollId !== 'undefined' && typeof pollChoices !== 'undefined' ) {
			myData.poll_id = pollId;
			myData.poll_choice = pollChoices;
		}		
		new Request.JSON({
			'url': this.apiurl + 'lottery/' + this.lottery + '/participate',
			'onComplete': function( response ) {
				if ( this.checkApiResponse( response ) ) {
					this.fireEvent( 'lotteryparticipation', new Array( true, response ) );
				} else {
					this.fireEvent( 'lotteryparticipation', new Array( false, response ) );
				}
			}.bind( this )
		}).post( myData );
	},
	getPollChoices: function( poll ) {
		new Request.JSON({
			'url': this.apiurl + 'poll/' + poll,
			'onComplete': function( response ) {
				this.fireEvent( 'pollchoices', new Array( this.checkApiResponse( response ), response ) );
			}.bind( this )
		}).get();
	},
	sendEcard: function( from, to, message, ecard, custom )
	{
		if ( ( typeof ecard === 'undefined' || ecard === null ) && this.defaultEcard !== null ) {
			ecard = this.defaultEcard;
		} else {
			this.fireEvent( 'sendecard', false );
			return false;
		}
		myData = { 'from': from, 'to': to };
		myData = $merge( myData, this.trackData );
		if ( this.language !== null ) myData.language = this.language;
		if ( this.country !== null ) myData.country = this.country;
		if ( typeof message !== 'undefined' ) myData.message = message;
		if ( typeof custom !== 'undefined' ) myData.custom = custom;
		new Request.JSON({
			'url': this.apiurl + 'ecard/' + ecard,
			'onComplete': function( response ) {
				if ( this.checkApiResponse( response ) ) {
					this.fireEvent( 'sendecard', new Array( true, response ) );
				} else {
					this.fireEvent( 'sendecard', new Array( false, response ) );
				}
			}.bind( this )
		}).post( myData );
	},
	confirmEcard: function( ecard, token )
	{
		new Request.JSON({
			'url': this.apiurl + 'ecard/' + ecard + '/confirm',
			'onComplete': function( response ) {
				if ( this.checkApiResponse( response ) ) {
					this.fireEvent( 'confirmecard', new Array( true, response ) );
				} else {
					this.fireEvent( 'confirmecard', new Array( false, response ) );
				}
			}.bind( this )
		}).post( { 'token': token } );
	},
	confirmLottery: function( lottery, token )
	{
		new Request.JSON({
			'url': this.apiurl + 'lottery/' + lottery + '/confirm',
			'onComplete': function( response ) {
				if ( this.checkApiResponse( response ) ) {
					this.fireEvent( 'confirmlottery', new Array( true, response ) );
				} else {
					this.fireEvent( 'confirmlottery', new Array( false, response ) );
				}
			}.bind( this )
		}).post( { 'token': token } );
	},
	isDataError: function( response )
	{
		if ( typeof response === 'undefined' || response === null ) return false;
		return ( typeof response.code !== 'undefined' && response.code === this.STATUS_DATA_ERROR );
	},
	getErrorFields: function( response )
	{
		var fields = {};
		for( var i = 0; i < response.result.length; i++ ) {
			fields[ response.result[ i ].param ] = true;
		}
		return fields;
	},
	checkApiResponse: function( response )
	{
		if ( typeof response === 'undefined' || response === null ) return false;
		if ( typeof response.code === 'undefined' ) return false;
		if ( typeof response.status === 'undefined' ) return false;
		if ( response.code !== 0 ) return false;
		if ( response.status !== 'OK' ) return false;
		return true;
	},
	getCookieData: function( ident ) {
		return Cookie.read( 'missioncontrol[' + ident + ']' );
	},
	setCookieData: function( ident, value ) {
		return Cookie.write( 'missioncontrol[' + ident + ']', value );
	},
	getCookieCheckResult: function()
	{
		if ( this.cookieTest === null ) {
			return 'unchecked';
		} else if ( this.cookieTest === false ) {
			return 'failed ' + this.getCookieData( this.cookieTestName );
		} else {
			return 'passed';
		}
	},
	getPid: function()
	{
		return this.pid;
	}
});