Object.createPackage = function(path) {
	var root = window, parts = path.split('.'), part;

	for (var i = 0; i < parts.length; i++) {
		part = parts[i];
		if (!root[part]) {
			root[part] = {};
		}
		root = root[part];
	}
	
	root.getInstance = function() {
		return this;
	}

	return root;
}

Object.findObject = function(path) {
	var root = window, parts = path.split('.'), part;
	
	for (var i = 0; i < parts.length; i++) {
		part = parts[i];
		root = root[part];
		if (!root) {
			return null;
		}
	}
	
	return root;
}

Function.createCallback = function(method, context) {
	return function(args) {
		return method(args, context);
	}
}

String.prototype.endsWith = function(suffix) {
	return (this.substr(this.length - suffix.length) == suffix);
}
String.prototype.startsWith = function(prefix) {
	return (this.substr(0, prefix.length) == prefix);
}
String.prototype.trimLeft = function() {
	return this.replace(/^\s*/, '');
}
String.prototype.trimRight = function() {
	return this.replace(/\s*$/, '');
}
String.prototype.trim = function() {
	return this.trimRight().trimLeft();
}

if (!Array.prototype.push) {
	Array.prototype.push = function() {
		var numargs = arguments.length;
		for (var i=0; i<numargs; i++) {
			this[this.length] = arguments[i];
		}
		return this.length;
	}
}

document.setCookie = function(name, value) {
	document.cookie = name + '=' + escape(value) + '; expires=Mon, 31 Dec 2010 23:59:59 UTC';
}

document.getCookie = function(name) {
	var cookies = document.cookie.split('; ');
	for (var i = 0; i < cookies.length; i++) {
		var pair = cookies[i].split('=');
		if (name == pair[0]) {
			return unescape(cookies[1]);
		}
	}

	return null;
}

document.deleteCookie = function(name) {
	document.cookie = name + '=' + escape(' ') + '; expires=Fri, 31 Dec 1999 23:59:59 GMT;';
}

with (Object.createPackage('js')) {
	getInstance().EventContext = function (src, args) {
		this.src = src;
		this.args = args;		
	}
}

Object.createPackage('js.vars');

Object.createUniqueId = function() {
	return Math.random().toString().substring(2,12);
}

if (!Function.prototype.apply) {
	Function.prototype.apply = function(instance, argArray) {
		if (instance) {
			var id = Object.createUniqueId();
			eval('instance.' + id + ' = this;');
			eval('instance.' + id + '(' + argArray.join(',') + ');');
			eval('delete instance.' + id + ';');
		}
	}
}

Function.createDelegate = function(instance, method){
  return function(args) {
    method.apply(instance, args);
  }
}

document.findElement = function(id) {
	return document.all ? document.all[id] : document.getElementById(id);
}

Number.parse = function(value) {
		if (!value || (value.length == 0)) {
			return 0;
		}
		return parseFloat(value);
}

Number.prototype.toFormattedString = function(format) {
	var _percentPositivePattern = ['n %', 'n%', '%n' ];
	var _percentNegativePattern = ['-n %', '-n%', '-%n'];
	var _numberNegativePattern = ['(n)','-n','- n','n-','n -'];
	var _currencyPositivePattern = ['$n','n$','$ n','n $'];
	var _currencyNegativePattern = ['($n)','-$n','$-n','$n-','(n$)','-n$','n-$','n$-','-n $','-$ n','n $-','$ n-','$ -n','n- $','($ n)','(n $)'];
	
	function expandNumber(number, precision, groupSizes, sep, decimalChar) {
		var curSize = groupSizes[0];
		var curGroupIndex = 1;
		var numberString = '' + number;
		var decimalIndex = numberString.indexOf('.');
		var right = '';
		
		if (decimalIndex > 0) {
			right = numberString.slice(decimalIndex + 1);
			numberString = numberString.slice(0, decimalIndex);
		}
		
		if (precision > 0) {
			var rightDifference = right.length - precision;
			if (rightDifference > 0) {
				right = right.slice(0, precision);
			} else if (rightDifference < 0) {
				for (var i=0; i<Math.abs(rightDifference); i++) {
					right += '0';
				}
			}
			
			right = decimalChar + right;
		} else {
			right = '';
		}
		
		var stringIndex = numberString.length - 1;
		var ret = '';
		while (stringIndex >= 0) {
			if (curSize == 0 || curSize > stringIndex) {
				if (ret.length > 0) {
					return numberString.slice(0, stringIndex + 1) + sep + ret + right;
				} else {
					return numberString.slice(0, stringIndex + 1) + right;
				}
			}
			
			ret = ret.length > 0 ?
				numberString.slice(stringIndex - curSize + 1, stringIndex+1) + sep + ret :
				numberString.slice(stringIndex - curSize + 1, stringIndex+1);
				
			stringIndex -= curSize;
			if (curGroupIndex < groupSizes.length) {
				curSize = groupSizes[curGroupIndex];
				curGroupIndex++;
			}
		}
		
		return numberString.slice(0, stringIndex + 1) + sep + ret + right;
	}

	var nf = js.CultureInfo.NumberFormat;
	var number = Math.abs(this);
	
	if (!format) {
		format = 'D';
	}
	
	var precision = -1;
	if (format.length > 1) {
		precision = parseInt(format.slice(1));
	}
	
	var pattern;
	switch (format.charAt(0)) {
		case 'd':
		case 'D':
			pattern = 'n';
			
			if (precision != -1) {
				var numberStr = '' + number;
				var zerosToAdd = precision - numberStr.length;
				if (zerosToAdd > 0) {
					for (var i = 0; i < zerosToAdd; i++) {
						numberStr = '0' + numberStr;
					}
				}
				number = numberStr;
			}
			
			if (this < 0) {
				number = -number;
			}
			break;

		case 'c':
		case 'C':
				pattern = this < 0 ?
					_currencyNegativePattern[nf.CurrencyNegativePattern] :
					_currencyPositivePattern[nf.CurrencyPositivePattern];

				if (precision == -1) {
					precision = nf.CurrencyDecimalDigits;
				}
				number = expandNumber(Math.abs(this), precision, nf.CurrencyGroupSizes, nf.CurrencyGroupSeparator, nf.CurrencyDecimalSeparator);
				break;

		case 'n':
		case 'N':
				pattern = this < 0 ?
					_numberNegativePattern[nf.NumberNegativePattern] :
					'n';
        
				if (precision == -1) {
					precision = nf.NumberDecimalDigits;
				}
				number = expandNumber(Math.abs(this), precision, nf.NumberGroupSizes, nf.NumberGroupSeparator, nf.NumberDecimalSeparator);
				break;

		case 'p':
		case 'P':
				pattern = this < 0 ?
					_percentNegativePattern[nf.PercentNegativePattern] :
					_percentPositivePattern[nf.PercentPositivePattern];

				if (precision == -1) {
					precision = nf.PercentDecimalDigits;
				}
				number = expandNumber(Math.abs(this), precision, nf.PercentGroupSizes, nf.PercentGroupSeparator, nf.PercentDecimalSeparator);
				break;
		default:
	}

	var regex = /n|\$|-|%/g;

	var ret = '';

	for (;;) {
		var index = regex.lastIndex;
		var ar = regex.exec(pattern);
		ret += pattern.slice(index, ar ? ar.index : pattern.length);
		
		if (!ar) {
			break;
		}

		switch (ar[0]) {
			case 'n':
					ret += number;
					break;
			case '$':
					ret += nf.CurrencySymbol;
					break;
			case '-':
					ret += nf.NegativeSign;
					break;
			case '%':
					ret += nf.PercentSymbol;
					break;
			default:
		}
	}

	return ret;
}

String.format = function(format) {
	var result = '';
	
	for (var i=0;;) {
		var next = format.indexOf('{', i);
		if (next < 0) {
			result += format.slice(i);
			break;
		}
		
		result += format.slice(i, next);
		i = next + 1;
		
		if (format.charAt(i) == '{') {
			result += '{'; i++; continue;
		}
		
		var next = format.indexOf('}', i);
		var brace = format.slice(i, next).split(':');
		
		var argNumber = Number.parse(brace[0]) + 1;
		var arg = arguments[argNumber];
		if (arg == null) {
			arg = '';
		}
		
		if (arg.toFormattedString) {
			result += arg.toFormattedString(brace[1] ? brace[1] : '');
		} else {
			result += arg.toString();
		}
		
		i = next+1;
	}
	
	return result;
}

Object.createPackage('js.CultureInfo');
js.CultureInfo = {'Name':'en-US','NumberFormat':{'CurrencyDecimalDigits':2,'CurrencyDecimalSeparator':'.','IsReadOnly':false,'CurrencyGroupSizes':[3],'NumberGroupSizes':[3],'PercentGroupSizes':[3],'CurrencyGroupSeparator':',','CurrencySymbol':'$','NaNSymbol':'NaN','CurrencyNegativePattern':0,'NumberNegativePattern':1,'PercentPositivePattern':0,'PercentNegativePattern':0,'NegativeInfinitySymbol':'-Infinity','NegativeSign':'-','NumberDecimalDigits':2,'NumberDecimalSeparator':'.','NumberGroupSeparator':',','CurrencyPositivePattern':0,'PositiveInfinitySymbol':'Infinity','PositiveSign':'+','PercentDecimalDigits':2,'PercentDecimalSeparator':'.','PercentGroupSeparator':',','PercentSymbol':'%','PerMilleSymbol':'','NativeDigits':['0','1','2','3','4','5','6','7','8','9'],'DigitSubstitution':1}};

js.vars.ua = navigator.userAgent.toLowerCase();

Object.createPackage('js.ui.BrowserCaps');
js.ui.BrowserCaps.isIE = (js.vars.ua.indexOf('msie') != -1);
js.ui.BrowserCaps.isOpera = (js.vars.ua.indexOf('opera') != -1);
js.ui.BrowserCaps.isIE = js.ui.BrowserCaps.isIE && !js.ui.BrowserCaps.isOpera;
js.ui.BrowserCaps.isGecko = (js.vars.ua.indexOf('gecko') != -1);

js.ui.Image = new function() {
	this.create = function() {
		var res = [];
		for (var i = 0; i < arguments.length; i++) {
			var img = new Image();
			img.src = arguments[i];
			res[res.length] = img;
		}
		return res;
	}
}

Object.createPackage('js.ui.WebForms');
js.ui.WebForms.GetScrollY = function() {
  if (js.ui.BrowserCaps.isIE) {
      if (document.documentElement && document.documentElement.scrollTop) {
          return document.documentElement.scrollTop;
      } else if (document.body) {
          return document.body.scrollTop;
      }
  } else {
      return window.pageYOffset;
  }
  return 0;
}
js.ui.WebForms.RestoreScrollPos = function(x, y) {
	if (window.scrollTo) {
		window.scrollTo(x, y);
	}
}

js.Url = new function() {
	this.combine = function(u1, u2) {
		if (u1.indexOf('?') != -1) {
			return u1 + '&' + u2;
		} else {
			return u1 + '?' + u2;
		}
	}
}

js.HttpReq = new function() {	
	this.perform = function(url, data, context, okfn, errfn) {
		var req = false;
		
		if(window.XMLHttpRequest) {
			try {
				req = new XMLHttpRequest();
			} catch(e) {
				req = false;
			}
		} else if(window.ActiveXObject) {
			try {
				req = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try {
					req = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					req = false;
				}
			}
		}

		if (!req) {
			return false;
		}

		context = context || null;
		okfn = okfn || function() {};
		errfn = errfn || function() {};

		req.onreadystatechange = function() {
			if (req.readyState == 4) {
				if (req.status == 200) {
					okfn(req, context);
				} else {
					errfn(req, context);
				}
			}
		};
		
		if (req && typeof(req.setRequestHeader) != 'undefined') {
			req.open('POST', url, true);
			req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			req.send(data);
		} else {
			req.open('GET', js.Url.combine(url, data), true);
			req.send();
		}

		return true;
	}
}


with (Object.createPackage('js.ui')) {
	getInstance().Player = function (cid) {
		//
		// private vars
		//
		var me = this;
		var _dlList = []; // downloading
		var _dlManager = null; // download manager
		var _dlType = 'real time'; // regime "real time" / "background", second is a BITS (Windows XP required)
		var _progresses = [];
		var _progressTimer = null;

		//
		// setters and getters
		//
		this.getId = function() {
			return cid;
		}

		var _drm = null;
		this.getDRM = function() {
			return _drm;
		}

		var _wmp = null;
		this.getWMP = function() {
			return _wmp;
		}
		
		var _dlsCountCookieName = 'MEDIASTOREDLCCNT';
		this.getDlsCountCookieName = function() {
			return _dlsCountCookieName;
		}
		this.setDlsCountCookieName = function(name) {
			_dlsCountCookieName = name;
		}
		
		var _dlCookieNamePrefix = 'MEDIASTOREDLC';
		this.DlCookieNamePrefix = function() {
			return _dlCookieNamePrefix;
		}
		this.DlCookieNamePrefix = function(name) {
			_dlCookieNamePrefix = name;
		}

		this.getCurrentMedia = function() {
			return _wmp.currentMedia;
		}

		//
		// events
		//		
		var peclbks = [];
		this.addPlayerErrorCallback = function(callback) {
			return peclbks.push(callback) - 1;
		}
		this.removePlayerErrorCallback = function(index) {
			delete peclbks[index];
		}
		function _firePlayerErrorEvent(args) {
			for (var i=0; i<peclbks.length; i++) {
				if (peclbks[i] != null) {
					peclbks[i](args);
				}
			}
		}
		
		var stleclbks = [];
		this.addStoreLicensesErrorCallback = function(callback) {
			return stleclbks.push(callback) - 1;
		}
		this.removeStoreLicensesErrorCallback = function(index) {
			delete stleclbks[index];
		}
		function _fireStoreLicensesErrorEvent(args) {
			for (var i=0; i<stleclbks.length; i++) {
				if (stleclbks[i] != null) {
					stleclbks[i](args);
				}
			}
		}

		var scclbks = [];
		this.addStateChangedCallback = function(callback) {
			return scclbks.push(callback) - 1;
		}
		this.removeStateChangedErrorCallback = function(index) {
			delete scclbks[index];
		}
		function _fireStateChangedEvent(args) {
			for (var i=0; i<scclbks.length; i++) {
				if (scclbks[i] != null) {
					scclbks[i](args);
				}
			}
		}
		
		var cicclbks = [];
		this.addCurrentItemChangedCallback = function(callback) {
			return cicclbks.push(callback) - 1;
		}
		this.removeCurrentItemChangedCallback = function(index) {
			delete cicclbks[index];
		}
		function _fireCurrentItemChangedEvent(args) {
			for (var i=0; i<cicclbks.length; i++) {
				if (cicclbks[i] != null) {
					cicclbks[i](args);
				}
			}
		}

		//
		// private methods
		//
		function _getPendingDlCount() {
			var cookieVal = document.getCookie(_dlsCountCookieName);
			return cookieVal == null ? 0 : cookieVal.valueOf();
		}
		
		function _createNewDownloadCollection() {
			try {
				var dc = _dlManager.createDownloadCollection();
				_dlList.push(dc);
				return dc;
			} catch(err) {
				return null;
			}
		}
		
		function _initProgress(dlc, callback) {
			_progresses.push(new function () {
				var _state = -1;
				this.checkProgress = function() {
					var dli = dlc.item(0);
					if (_state != dli.downloadState) {
						callback(new js.EventContext(me, dli.downloadState));
						_state = dli.downloadState;
					}
				}
				
				callback(new js.EventContext(me, _state));
			});

			if (_progressTimer == null) {
				var onProgress = function() {
					for (var i=0; i<_progresses.length; i++) {
						_progresses[i].checkProgress();
					}

					_progressTimer = window.setTimeout(onProgress, 1000);
				}
				
				onProgress();
			}
		}

		//
		// public methods
		//
		this.emitPlayerObject = function() {
			var id = cid + '_wmp';
			document.write('<object id="' + id + '" classid="clsid:6bf52a52-394a-11d3-b153-00c04f79faa6" height="0" width="0" style="display: none;"><param name="autoStart" value="true" /><param name="uiMode" value="invisible" /><embed type="application/x-mplayer2" pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/" name="' + id + '" height="0" width="0" autostart="true"></embed></object>');
			_wmp = document.getElementById(id);
			if (_wmp && !_wmp.versionInfo) {
				_wmp = null;
			}
		}
		
		this.attachEvents = function() {
			if (_wmp) {
				_wmp.attachEvent('Error', function() {
					var max = _wmp.error.errorcount;
					var strerror = '';
					for (var i = 0; i < max; i++) {
						strerror += 'error: ' + _wmp.error.item(i).errordescription + '\n';
					}
					
					_firePlayerErrorEvent(new js.EventContext(this, strerror));

					_wmp.error.clearerrorqueue();
				});
				
				_wmp.attachEvent('PlayStateChange', function(newState) {
				
					//_fireStateChangedEvent(new js.EventContext(this, newState));

				});

				_wmp.attachEvent('CurrentItemChange', function() {

					//_fireCurrentItemChangedEvent(new js.EventContext(this, me.getCurrentMedia()));

				});
			}
		}
		
		this.emitDRMObject = function() {
			var id = cid + '_drm';
			document.write('<object id="' + id + '" classid="clsid:a9fc132b-096d-460b-b7d5-1db0fae0c062" height="0" width="0" viewastext="true" style="display: none;"><embed mayscript type="application/x-drm-v2" hidden="true" /></object>');
			_drm = document.getElementById(id);
			if (_drm && typeof(_drm.GetSystemInfo) == 'undefined') {
				_drm = null;
			}
		}
		
		this.storeLicenses = function(lics) {
			if (_drm) {
				/*@cc_on @*/
				/*@if (@_jscript_version >= 5)
				if (lics) {
					for (var i = 0; i < lics.length; i++) {
						try {
							_drm.StoreLicense(lics[i]);
						} catch (e) {
							_fireStoreLicensesErrorEvent(new js.EventContext(this, e));
						}
					}
				}
				@end @*/
			}
		}
		
		this.getLicenseChallenge = function() {
			return _drm ? escape(_drm.GetSystemInfo()) : '';
		}

		this.isValidPlayerVersion = function(version) {
			return !(_wmp == null || _wmp.versionInfo.length == 0 || _wmp.versionInfo.substr(0, 2) < version);
		}

		this.setTaskPane = function(pane) {
			if (window.external && window.external.SelectedTaskPane) {
				window.external.SelectedTaskPane = pane;
			}
		}
		
		var _withEmbed = false;
		this.setPlaybackWithEmbed = function(flag) {
			_withEmbed = flag;
		}
		
		this.isPlaybackWithEmbed = function() {
			return _withEmbed;
		}
		
		this.play = function(url, withbreak) {
			if (typeof(withbreak) == 'undefined') {
				withbreak = true;
			}
			
			if (_withEmbed) {
				_playWithEmbed(url);
			} else if (_wmp) {
				if (_wmp.URL != url || withbreak) {
					_wmp.URL = url;
				}
				return true;
			} else {
				return false;
			}
		}
		
		var _playerSpanId = 'id' + Object.createUniqueId();
		
		function _playWithEmbed(url) {
			var span = document.findElement(_playerSpanId);
			if (span == null) {
				span = document.createElement('span');
				with(span) {
					id = _playerSpanId; style.position = 'absolute'; style.visibility = 'hidden';
				}
				document.body.appendChild(span);
			}
			
			span.innerHTML = '<embed src="' + url + '" hidden="true" autostart="true" loop="false">';
			return true;
		}
		
		function _stopEmbedPlaying(url) {
			var span = document.findElement(_playerSpanId);
			if (span != null) {
				span.innerHTML = '';
			}
		}

		this.stop = function() {
			if (_withEmbed) {
				_stopEmbedPlaying();
			} else if (_wmp) {
				_wmp.controls.stop();
			}
		}

		this.initDownload = function() {
			if (!window.external) {
				return '';
			}

			if (_dlManager) return '';
			var pending = _getPendingDlCount();
			try {
				_dlManager = window.external.DownloadManager;

				var name, value;
				for(var i=0; i<pending; i++) {
					name = _dlCookieNamePrefix + i.toString(10);
					value = document.getCookie(name);
					document.deleteCookie(name);
					_dlList.push(_dlManager.getDownloadCollection(value.valueOf()));
				}
			} catch(err) {
				return err.Description;
			}

			return '';
		}

		//var el = document.createElement('span');
		//el.innerHTML = 'error';
		//document.body.appendChild(el);

		this.shutdownDownload = function() {
			if (!_dlManager) return;

			var name, pending;
			for (var i=0; i<_dlList.length; i++) {
				name = _dlCookieNamePrefix + i.toString(10);

				document.setCookie(name, _dlList[i].id);
				pending++;
			}

			document.setCookie(_dlsCountCookieName, i.toString(10)); // record counter
		}

		this.startDownload = function(url, dlStateChangedCallback) {
			if (!window.external) {
				return false;
			}
			
			try {
				var dlc = _createNewDownloadCollection();
				var dli = dlc.startDownload(url, _dlType);
				_initProgress(dlc, dlStateChangedCallback);
				return true;
			} catch(err) {
				return false;
			}
		}
		
		this.getItemInfoByType = function(media, infoName) {
			try {
				return _wmp.currentMedia.getItemInfoByType(infoName, '', 0);
			} catch (e) {
				return null;
			}
		}
	}
	
	getInstance().PlayingMonitor = function (player, loadingImgUrl, playingImgUrl, stoppedImgUrl) {
		var _stateImg = null, _playing = false;	
		player.addStateChangedCallback(function(e) {
			var state = e.args;
			switch (state) {
				case 1: // Stopped
					_playing = false;
					if (_stateImg) _stateImg.src = stoppedImgUrl;
					break;
				case 2: // Paused
					break;
				case 3: // Playing
					_playing = true;
					if (_stateImg) _stateImg.src = playingImgUrl; //stop
					break;
				case 6: // Buffering
					break;
				case 7: // Waiting
					break;
				case 8: // Media Ended
					_playing = false;
					if (_stateImg) _stateImg.src = stoppedImgUrl;
					break;
				case 9: // Transitioning
					break;
				case 10: // Ready
					break;
				default: // Other
			}
		});

		this.isPlaying = function() {
			return _playing;
		}
		
		this.getCurrentStateImg = function() {
			return _stateImg;
		}

		this.play = function(url, img) {
			var withEmbed = player.isPlaybackWithEmbed();
			if (!withEmbed && !player.getWMP()) {
				return false;
			}

			if (!_playing) {
				player.play(url);
				_stateImg = img;
				_playing = true;
				img.src = withEmbed ? playingImgUrl : loadingImgUrl;
			} else {
				player.stop();
				if (withEmbed && _stateImg) {
					 _stateImg.src = stoppedImgUrl;
				}
				if (_stateImg != img) {
					player.play(url);
					_stateImg = img;
					_playing = true;
					img.src = withEmbed ? playingImgUrl : loadingImgUrl;
				} else {
					_stateImg = null;
					_playing = false;
				}
			}
			
			return true;
		}		
	}	
}

function change_color (id, on) {

		if(on == "1") {		
			//eval("var previous_color_1=bt_1.style.backgroundColor");
			eval("previous_color_" + id + "=bt_" + id + ".style.backgroundColor;");
			eval("bt_" + id + ".style.backgroundColor = '#aaaaaa';");
			eval("previous_color_1" + id + "=bt_1" + id + ".style.backgroundColor;");
			eval("bt_1" + id + ".style.backgroundColor = '#F29200';");
			//window.alert('');
			//status_menu_line.innerHTML='bla';
		}
		else {
			eval("bt_" + id + ".style.backgroundColor = previous_color_" + id);
			eval("bt_1" + id + ".style.backgroundColor = previous_color_1" + id);
			//status_menu_line.innerHTML='&nbsp;';
		}

}