/*
 * Type: KIT
 */


//
// constructor:
//
function InSkinContent_KIT(InSkinObj) {

	this.InSkinObj = InSkinObj;

	// callbacks:
	this.onReady = null;
	this.onStart = null;
	this.onPause = null;
	this.onComplete = null;
	this.onNewItem = null;

	// specific properties for this content type:
	this.player = null;
	this.is_ready = false;
	this.is_playing = false;
	this.state = null;
	this.last_request = '';
	this.first_play = true;
}


//
// commands received from InSkin Base (Javascript):
//

// init:
InSkinContent_KIT.prototype.init = function() {
	if (this.is_ready) return;
	this.checkPlayerLoaded();
}

// start:
InSkinContent_KIT.prototype.start = function() {
	if (!this.is_ready || this.state == 'play_request' || this.state == 'playing') return;
	this.state = 'play_request';
	this.last_request = 'start';

	try {
		//alert('starting');
		this.player.dispatchEvent('ePlayVideo');
	}
	catch (e) {
	}
	//if (this.onStart) this.onStart();
}

// pause:
InSkinContent_KIT.prototype.pause = function() {
	if (!this.is_ready || this.state == 'pause_request' || this.state == 'paused') return;
	this.state = 'pause_request';
	this.last_request = 'pause';

	try {
		//alert('pausing');
		this.player.dispatchEvent('ePauseVideo');
	}
	catch (e) {
	}
	//if (this.onPause) this.onPause();
}


//
// specific methods for this content type:
//

// check if the flash object was successfully loaded:
InSkinContent_KIT.prototype.checkPlayerLoaded = function() {
	if ((this.player = this.getPlayer()) != undefined && typeof(this.player.addEventListener) != 'undefined') {
		this.addEventHandlers();
	}
	else {
		var _self = this;
		setTimeout(function() { _self.checkPlayerLoaded(); }, 250);
	}
}

// get a reference to the flash object:
InSkinContent_KIT.prototype.getPlayer = function() {
	var id = this.InSkinObj.params['plr_ContentID'];
	if (navigator.appName.indexOf('Microsoft') != -1) {
		return window[id];
	}
	else {
		return document[id];
	}
}

InSkinContent_KIT.prototype.addEventHandlers = function() {
	try {
		this.player.addEventListener('ePlayVideo', this.InSkinObj.params['plr_InSkinID'] + '.content.eventPlay');
		this.player.addEventListener('ePauseVideo', this.InSkinObj.params['plr_InSkinID'] + '.content.eventPause');
		this.player.addEventListener('eVideoClipBegin', this.InSkinObj.params['plr_InSkinID'] + '.content.eventBegin');
		this.player.addEventListener('eVideoClipComplete', this.InSkinObj.params['plr_InSkinID'] + '.content.eventComplete');
		this.playerReady();
	}
	catch (e) {
		var _self = this;
		setTimeout(function() { _self.addEventHandlers(); }, 250);
	}
}


// the player is loaded:
InSkinContent_KIT.prototype.playerReady = function() {
	//alert('ready');
	this.is_ready = true;
	if (this.onReady) this.onReady();
}

InSkinContent_KIT.prototype.eventPlay = function(obj) {
	//alert('playing');
	
	if (this.state == 'playing') return;
	this.state = 'playing';
	
	if (this.onStart) {
		var _self = this;
		setTimeout(function() { _self.onStart(); }, 25);
	}
}

InSkinContent_KIT.prototype.eventPause = function(obj) {
	//alert('paused');
	
	if (this.state == 'paused') return;
	this.state = 'paused';

	if (this.onPause) {
		var _self = this;
		setTimeout(function() { _self.onPause() }, 25);
	}
}

InSkinContent_KIT.prototype.eventBegin = function(obj) {
	//alert(this.last_request);
	//alert('begin');
	//alert(this.is_playing);
	//alert('state = ' + this.state + '; last_request = ' + this.last_request);
	if (this.last_request == 'pause') {
		this.state = '';
		this.last_request = '';
		var _self = this;
		setTimeout(function() { _self.pause(); }, 25);
		return;
	}

	if (!this.first_play) {
		if (this.onNewItem) this.onNewItem();
	}
	this.first_play = false;
	//this.is_playing = true;
}

InSkinContent_KIT.prototype.eventComplete = function(obj) {
	if (this.onComplete) this.onComplete();
	this.is_playing = false;
}
