Event.onDOMReady(function() {
	new WWESL.Landing();
});

if (! WWESL) {
	var WWESL = {};
}

WWESL.MINIMUM_SILVERLIGHT_VERSION = '1.0';

WWESL.Landing = function() {
	this.link = $('launchButton');
	this.classicLink = $('classicLink');
	this.classicText = $('classicText');
	this.installDiv = $('wwesl_installation');
	this.notAvailableDiv = $('wwesl_notAvailable');
	this.classicWindowParams = 'status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no,scrollbars=no,height=525,width=800';
	this.silverlightWindowParams = 'status=no,toolbar=no,location=no,menubar=no,directories=no,resizable=no,scrollbars=no,height=650,width=978';
	this.newWindowParams = this.classicWindowParams;
	this.getPreference();
	this.silverlightCheck();
	this.main();
};
WWESL.Landing.prototype = {
	getPreference : function() {
		this.preference = visitordata.videopref;
	},
	
	setPreference : function(type) {
		if (visitordata) {
			visitordata.videopref = type;
			visitordata.store();
		}
		this.preference = type;
	},
	
	silverlightCheck : function() {
		this.supportsSilverlight = Silverlight.ua.Browser !== 'Unsupported';
		this.hasSilverlight = Silverlight.isInstalled(WWESL.MINIMUM_SILVERLIGHT_VERSION);
	},
	
	main : function() {
		if (! this.supportsSilverlight) { // use case 2
			this.useClassic();
		} else {
			if (this.hasSilverlight) { // use cases 1 and 5
				this.useSilverlight();
			} else {
				if (this.preference == 'classic') { // use case 4
					this.useClassic();
				} else { // use case 3
					this.showInstallation();
				}
			}
		}
	},
	
	useClassic : function() {
		this.newWindowParams = this.classicWindowParams;
		
		if (WWESL.DeepLinkType !== 'video') {
			this.classicNotAvailable();
			return;
		}
		if (this.link.href.indexOf('view=silverlight') != -1) {
			this.link.href = this.link.href.replace('view=classic', 'view=silverlight');
		}
		this.setPreference('classic');
		this.initLink();
	},
	
	useSilverlight : function() {
		this.newWindowParams = this.silverlightWindowParams;
		
		if (this.link.href.indexOf('view=classic') != -1) {
			this.link.href = this.link.href.replace('view=classic', 'view=silverlight');
		}
		this.setPreference('silverlight');
		this.initLink();
	},
	
	classicNotAvailable : function() {
		this.notAvailableDiv.style.display = 'block';
	},
		
	spawnPopup : function(event) {
		var href = (event && event.target && event.target.href) ? event.target.href : this.link.href;
		var params = href.indexOf('view=silverlight') !== -1 ? this.silverlightWindowParams : this.classicWindowParams;
		if(!window.open(href, 'WWESLPopup', params)) {
			// show popup error message?
		}
		if (event) {
			event.preventDefault();
		}
	},
	
	initLink : function() {
		this.link.observe('click', this.spawnPopup.bind(this))
	},
	
	showInstallation : function() {
		this.installDiv.style.display = 'block';
		Silverlight.InstallAndCreateSilverlight(WWESL.MINIMUM_SILVERLIGHT_VERSION, 'wwesl_installPrompt', 'silverlightHost', function() {});
	}
};