// Javascript for EinsteinVideo
// 
// Author: 	Brian Smith (bsmith@einsteinindustries.com)
// Date: 	Sept 2007

/************************************************************************

EII.Video.Player wraps the player.swf flash object

************************************************************************/
EII.namespace("Video");
EII.Video.Player = function (videoPlayerId, locale, videoArray, currentIndex) {

	var _locale = locale;
	var _playerObjectId = videoPlayerId;
	var _videoArray = videoArray;
	var _currentVideoIndex;
	var that = this;
	
	if(currentIndex == null) {
		_currentVideoIndex = _videoArray.length == 0 ? null : 0;
	} else {
		_currentVideoIndex = currentIndex;
	}
	
	
	function getPlayerObject() {
	    if (navigator.appName.indexOf("Microsoft") != -1) {
	        return window[_playerObjectId];
	    } else {
	        return document[_playerObjectId];
	    }
	}
	
	function registerCallbacks() {
		getCurrent = that.getCurrentVideo;
		getNext = that.nextVideo;
		getPrev = that.previousVideo;
	}
	
	this.updatePlayer = function(videoArray, currentIndex) {
		_videoArray = videoArray;
		if(currentIndex == null) {
			_currentVideoIndex = _videoArray.length == 0 ? null : 0;
		} else {
			_currentVideoIndex = currentIndex;
		}
	}
		
	// Sets the current video (by index) and plays
	//
	this.play = function(videoIndex) {
		_currentVideoIndex = videoIndex;
		getPlayerObject().playVid(_videoArray[_currentVideoIndex].url);
	};
	
	this.getCurrentVideoId = function() {
		return _videoArray[_currentVideoIndex].id;
	};
	
	this.getCurrentCategoryId = function() {
		return _videoArray[_currentVideoIndex].category_id;
	};
	
	this.getCurrentVideo = function() {
		if(_currentVideoIndex != null) {
			return _videoArray[_currentVideoIndex].url;
		} else {
			return false;
		}
	};
	
	// Moves playback to the previous video and returns the URL for the video
	//
	this.afterNextVideo = function() {};
	this.nextVideo = function() {
		if(_currentVideoIndex != null) {
			_currentVideoIndex = (_currentVideoIndex == (_videoArray.length-1)) ? 0 : _currentVideoIndex + 1;
			new Ajax.Request('/video/' + _locale + '/player/' + _videoArray[_currentVideoIndex].slug, {asynchronous:true, evalScripts:true});
      this.afterNextVideo();
		} else {
			return false;
		}
	};
	
	// Moves playback to the next video and returns the URL for the video
	//
	this.previousVideo = function() {
		if(_currentVideoIndex != null) {
			_currentVideoIndex = (_currentVideoIndex == 0) ? _videoArray.length - 1 : _currentVideoIndex - 1;
			new Ajax.Request('/video/' + _locale + '/player/' + _videoArray[_currentVideoIndex].slug, {asynchronous:true, evalScripts:true});
		} else {
			return false;
		}
	};
	
	// Register global callbacks for the flash player
	//
	// Note: This used to happen in window.onload, but it was too late!
	registerCallbacks();
	
};


//-----------------------------------------------------------------
//
//  These are the callbacks that the flash video player requires
//
//-----------------------------------------------------------------
var getCurrent = function () {};
var getPrev = function () {};
var getNext = function () {};


EII.namespace("FormHandler");

EII.FormHandler.focus = function() {
	if (typeof this.default_value == 'undefined') {
		this.default_value = $F(this);
	}
	if (this.value == this.default_value) {
		this.value = '';
	}
}

EII.FormHandler.blur = function() {
	tmp = $F(this);
	if (tmp == '') {
		this.value = this.default_value;
	}
}

EII.FormHandler.clearDefaultValues = function() {
	elms = Form.getElements(this);
	elms.each(function(elm) {
		if (elm.type == "text" || elm.tagName == "TEXTAREA") {
			if (typeof elm.default_value == "undefined" || $F(elm) == elm.default_value) {
				elm.value = ""
			}
		}
	});
}

EII.FormHandler.clearDefaultValuesOnForm = function(form) {
	elms = Form.getElements(form);
	elms.each(function(elm) {
		if (elm.type == "text" || elm.tagName == "TEXTAREA") {
			if (typeof elm.default_value == "undefined" || $F(elm) == elm.default_value) {
				elm.value = ""
			}
		}
	});
}

EII.FormHandler.setup = function() {
//This handles having the form names inside all input and textarea fields, selects the whole label on focus and if hte field is empty re populates the label
	$$('input, textarea').each(function(elm) {
		Event.stopObserving(elm, 'focus', EII.FormHandler.focus.bind(elm));
		Event.observe(elm, 'focus', EII.FormHandler.focus.bind(elm));
		
		Event.stopObserving(elm, 'blur', EII.FormHandler.blur.bind(elm));
		Event.observe(elm, 'blur', EII.FormHandler.blur.bind(elm));
	});
	
//If the form elements were never changed, remove their default values
	$$('form').each(function(f) {
		
		f.clear = EII.FormHandler.clearDefaultValues.bind(f);
		
		f.oldsubmit = f.onsubmit
		f.removeAttribute('onsubmit');
		
		f.onsubmit = function() {
      this.clear();
			this.oldsubmit();
			return false;
		};
		
		/*
		Event.stopObserving(f, 'submit', EII.FormHandler.clearDefaultValues.bind(f));
		
		Event.observe(f, 'submit', function() {
			this.clear();
			alert('cleared');
			this.oldsubmit();
			return false;
		}.bind(f));
		*/
	});

}

