﻿
var CustomizePlayerSeed = new Class({

	initialize: function(playerSeedIndex, ddlAutoStart,
                         controlCoverID, chkTextOnHeaderID, txtTextOnHeaderID, chkAllowOverridesID, divOverridesID) {

		this.generateCodePlayerSeedObj = eval('generateCodePlayerSeedObj' + playerSeedIndex);

		var classRef = this;
		this.playerSeedIndex = playerSeedIndex;
		this.ddlAutoStart = $(ddlAutoStart);		
		this.controlCoverID = controlCoverID;

		this.chkTextOnHeader = $(chkTextOnHeaderID);
		this.txtTextOnHeader = $(txtTextOnHeaderID);
		this.chkAllowOverrides = $(chkAllowOverridesID);
		this.divOverrides = $(divOverridesID);

		this.chkAllowOverrides.addEvent('click', function(e) {
			this.divOverrides.style.display = this.chkAllowOverrides.checked ? '' : 'none';
		} .bind(this));

		this.chkTextOnHeader.addEvent('click', function(e) {

			var text;
			var height = parseInt(playerCustomization.txtPlayerHeight.value);

			if (this.chkTextOnHeader.checked) {
				text = this.txtTextOnHeader.value;
				height += 20;
				this.txtTextOnHeader.disabled = false;
			}
			else {
				text = '';
				height -= 20;
				this.txtTextOnHeader.disabled = true;
			}
			playerCustomization.txtPlayerHeight.value = height;
			this.ChangeTopHeader(text, height);

		} .bind(this));

		var _writeHeaderTimeout = null;

		this.txtTextOnHeader.addEvent('keyup', function(e) {

			if (this.chkTextOnHeader.checked) {

				//set delay because if user write quickly - the browser crash (because playerSeed.Load(); write to DB)
				var text = this.txtTextOnHeader.value;

				if (_writeHeaderTimeout)
					_writeHeaderTimeout = $clear(_writeHeaderTimeout);

				_writeHeaderTimeout = (function() {
					classRef.ChangeTopHeader(text);
				}).delay(500);
			}
		} .bind(this));

		this.ddlAutoStart.addEvent('change', function(e) {
			psFiveMinCounter = this.playerSeedIndex;
			playerSeed.autoStart = classRef.ddlAutoStart.value;
			classRef.generateCodePlayerSeedObj.generate();
			playerSeed.Load();
			window.focus();
		});
	},	

	ChangeTopHeader: function(text, height) {

		if (height)
			playerSeed.height = height;

		playerSeed.topHeader = text;
		playerSeed.Load();
		this.generateCodePlayerSeedObj.generate();
	},

	isNumeric: function(numstr) {
		if (numstr >= 48 && numstr <= 57
            || numstr >= 96 && numstr <= 105) {
			return true;
		}
		else {
			return false;
		}
	}
});


var skinType = null;

var GenerateCodePlayerSeed = new Class({

	initialize: function(playerSeedIndex, textareaCodeID, sid, fiveminsite, pageContainer) {

		//variables values should be like the enum Enums.SeedContainer
		this.WIDGET = 1;
		this.WIDGETYOURPICK = 2;

		this.playerSeedIndex = playerSeedIndex;
		this.textareaCode = $(textareaCodeID);
		this.sid = sid;
		this.pageContainer = pageContainer;

		this.Categories = null;
		this.fiveminsite = fiveminsite;
		this.playList = '';
	},

	generate: function() {
		var customizePlayerSeedObj = eval('customizePlayerSeedObj' + this.playerSeedIndex);
		
		var width = '';
		var height = '';
		//in case user select customize ratio but not insert width/height - don't write width/height to script because playerseed.js has our default values(var PLAYERSEED_WIDTH = '400'; var PLAYERSEED_HEIGHT = '255';)
		if (playerCustomization.txtPlayerWidth.value != '' && playerCustomization.txtPlayerHeight.value != '') {

			width = " playerSeed.width = '" + Trim(playerCustomization.txtPlayerWidth.value) + "';\n";

			height = " playerSeed.height = '" + Trim(playerCustomization.txtPlayerHeight.value) + "';\n";
		}

		var currentCategories = '';
		if (this.Categories) {
			currentCategories = " playerSeed.categories = '" + this.Categories + "';\n";
		}

		var skinTypeFiveMin = '';
		if (playerSeed.skinType > 0) {
			skinTypeFiveMin = " playerSeed.skinType = '" + skinType + "';\n";
		}

		var featured = "";
		if (this.pageContainer == this.WIDGET) {
			featured = "playerSeed.featured = true; \n ";
		}

		var playList = "";
		if (this.pageContainer == this.WIDGETYOURPICK && this.playList != "") {
			playList = "playerSeed.playList = '" + this.playList + "';\n ";
		}

		var topHeader = "";
		if (customizePlayerSeedObj.chkTextOnHeader.checked)
			topHeader = "playerSeed.topHeader = '" + customizePlayerSeedObj.txtTextOnHeader.value + "';\n ";

		var videoControlDisplayColor = " playerSeed.videoControlDisplayColor = '" + playerCustomization.txtColor.value + "';\n";

		this.textareaCode.value = "<div id='SmartPlayer'></div>\n<script type='text/javascript'>\n var playerSeed = new PlayerSeed('SmartPlayer');\n playerSeed.sid = " + this.sid + ";\n"+ width + height +videoControlDisplayColor + currentCategories + skinTypeFiveMin + " playerSeed.autoStart= " + customizePlayerSeedObj.ddlAutoStart.value + ";\n " + featured + playList + topHeader + "playerSeed.Load();\n</script>";
	}

});
