
function numericFilter(field)
{
	if (typeof(field) == 'object' && field != null)
	{
		var value = '';
		var changed = false;
		for (var i=0;i<field.value.length;i++)
		{
			if (field.value[i] > '9' || field.value[i] < '0')
			{
				changed = true;
			}
			else
			{
				value += field.value[i];
			}
		}
		if (changed)
		{
			field.value = value;
		}
	}
}

function onBlurNumericFilter(field, defValue)
{
	if (typeof(field) == 'object' && field != null)
	{
		var value = '';
		var changed = false;
		for (var i=0;i<field.value.length;i++)
		{
			if (field.value[i] > '9' || field.value[i] < '0')
			{
				changed = true;
			}
			else
			{
				value += field.value[i];
			}
		}

		if (changed || value == '' || value == '0')
		{
			field.value = (value == '' || value == '0' ? defValue : value);
		}
	}
}

function InformeVideo()
{
	this.window = false;
	this.container = false;
	this.saveField = false;

	this.parentConstrunct = InformeObject;
	this.parentConstrunct();

	this.bind();

	this.logoPath = '/common/style/img/logos/';
	this.videoSystems = new Array();

	this.videoSystems[0] = new Object();
	this.videoSystems[0].name = 'youtube';
	this.videoSystems[0].title = 'YouTube';
	this.videoSystems[0].url = 'www.youtube.com';
	this.videoSystems[0].logo = 'youtube.png';

	this.videoSystems[1] = new Object();
	this.videoSystems[1].name = 'yahoo';
	this.videoSystems[1].title = 'Yahoo Video';
	this.videoSystems[1].url = 'video.yahoo.com';
	this.videoSystems[1].logo = 'yahoo.png';

	this.videoSystems[2] = new Object();
	this.videoSystems[2].name = 'metacafe';
	this.videoSystems[2].title = 'Metacafe';
	this.videoSystems[2].url = 'www.metacafe.com';
	this.videoSystems[2].logo = 'metacafe.png';

	this.videoSystems[3] = new Object();
	this.videoSystems[3].name = 'msn';
	this.videoSystems[3].title = 'MSN Video';
	this.videoSystems[3].url = 'video.msn.com';
	this.videoSystems[3].logo = 'msn.png';

	this.videoSystems[4] = new Object();
	this.videoSystems[4].name = 'rambler';
	this.videoSystems[4].title = 'Rambler Vision';
	this.videoSystems[4].url = 'vision.rambler.ru';
	this.videoSystems[4].logo = 'rambler.png';

	this.videoSystems[5] = new Object();
	this.videoSystems[5].name = 'reuters';
	this.videoSystems[5].title = 'Reuters';
	this.videoSystems[5].url = 'www.reuters.com/news/video';
	this.videoSystems[5].logo = 'reuters.png';

	this.videoSystems[6] = new Object();
	this.videoSystems[6].name = 'rutube';
	this.videoSystems[6].title = 'Ru Tube';
	this.videoSystems[6].url = 'rutube.ru';
	this.videoSystems[6].logo = 'rutube.png';
	
	this.videoSystems[7] = new Object();
	this.videoSystems[7].name = 'myspace';
	this.videoSystems[7].title = 'MySpace';
	this.videoSystems[7].url = 'myspacetv.com';
	this.videoSystems[7].logo = 'myspace.png';
	
	this.videoSystems[8] = new Object();
	this.videoSystems[8].name = 'mailru';
	this.videoSystems[8].title = 'Video Mail.Ru';
	this.videoSystems[8].url = 'video.mail.ru';
	this.videoSystems[8].logo = 'mail_ru.png';
	
	this.videoSystems[9] = new Object();
	this.videoSystems[9].name = 'tutv';
	this.videoSystems[9].title = 'Tu TV';
	this.videoSystems[9].url = 'tu.tv';
	this.videoSystems[9].logo = 'tutv.png';
	
	this.videoSystems[10] = new Object();
	this.videoSystems[10].name = 'smotri';
	this.videoSystems[10].title = 'Smotri Com';
	this.videoSystems[10].url = 'smotri.com';
	this.videoSystems[10].logo = 'smotri.jpg';

	this.initContainer = function ()
	{
		if (typeof(this.window) == 'object' && this.window != null)
		{
			this.container = this.window.getContainer();
			return (typeof(this.container) == 'object' && this.container != null);
		}
		return false;
	}

	this.show = function ()
	{
		if (typeof(this.container) != 'object' || this.container == null)
		{
			this.initContainer();
		}

		if (typeof(this.container) == 'object' && this.container != null)
		{
			this.window.setTitle('Video adding');
			this.container.innerHTML =
				'<form name="form_' + this.id + '" onsumbit="return false;">\n' +
				'<table border="0" width="100%" height="370px">\n' +
				' <tr>\n' +
				'  <td colspan="2" align="center"><div class="iwin-scrolled" id="video-sys-list"></div>\n' +
				'  </td>\n' +
				' </tr>\n' +
				' <tr>\n' +
				'  <td class="iwin-text" width="40px" style="text-align: right;">URL:</td>\n' +
				'  <td class="iwin-text">\n' +
				'   <input class="iwin-input" type="text" name="url" value="" style="width: 350px;" title="URL of the page with video"><br /><small>please fill this field with a link to the page with video</small>\n' +
				'  </td>\n' +
				' </tr>\n' +
				' <tr>\n' +
				'  <td colspan="2" class="iwin-text" style="text-align: center;">\n' +
				'   W:<input class="iwin-input" type="text" name="width"  value="400" style="width: 40px;" title="The Width of your video object at the post" maxlength="4" onKeyUp="numericFilter(this);" onKeyPress="numericFilter(this);" onKeyup="numericFilter(this);" onBlur="onBlurNumericFilter(this, 400);" />px\n' +
				'   H:<input class="iwin-input" type="text" name="height" value="300" style="width: 40px;" title="The Height of your video object at the post" maxlength="4" onKeyUp="numericFilter(this);" onKeyPress="numericFilter(this);" onKeyup="numericFilter(this);" onBlur="onBlurNumericFilter(this, 300);" />px\n' +
				'  </td>\n' +
				' </tr>\n' +
				' <tr>\n' +
				'  <td colspan="2" style="text-align: center;">\n' +
				'   <input class="iwin-button" type="button" value="Set" onClick="' + this.getBindPath() + '.save();" />\n' +
				'   <input class="iwin-button" type="button" value="Cancel" onClick="' + this.getBindPath() + '.cancel();" />\n' +
				'  </td>\n' +
				' </tr>\n' +
				'</table>\n' +
				'</form>\n';
			setTimeout(this.getBindPath() + ".showVideoSystems();", 200);
		}
	}

	this.showVideoSystems = function()
	{
		var field = document.getElementById('video-sys-list');
		if (typeof(field) == 'object' && field != null)
		{
			var html = '<table border="0">\n';
			for (var i=0;i<this.videoSystems.length;i++)
			{
				html += ' <tr>\n' +
					'  <td><input type="radio" name="systemId" id="check_' + this.id + '_' + i + '" value=' + i + '></td>\n' +
					'  <td><div class="ivideo-sys-name"><label for="check_' + this.id + '_' + i + '">' + this.videoSystems[i].title + '</label></div></td>\n' +
					'  <td><a href="http://' + this.videoSystems[i].url + '" target="_blank" class="ivideo-sys-name"><img src="' + this.logoPath + this.videoSystems[i].logo + '" border="0" /></a></td>\n' +
	                                ' </tr>\n';
			}
                        html += '</table>\n';
			field.innerHTML = html;
		}
	}

	this.save = function ()
	{
		var close = false;
		if (typeof(document.forms['form_' + this.id]) == 'object' && document.forms['form_' + this.id] != null)
		{
			var form = document.forms['form_' + this.id];
			var systemId = false;
			var url = form.elements.url.value;
			var width = form.elements.width.value;
			var height = form.elements.height.value;
			close = true;

			for (var i=0;i<form.elements.systemId.length;i++)
			{
				if (form.elements.systemId[i].checked)
				{
					systemId = Math.floor(form.elements.systemId[i].value);
				}
			}

			if (typeof(systemId) != 'number' || isNaN(systemId) || systemId < 0 || systemId >= this.videoSystems.length)
			{
				close = false;
				alert('You haven\'t selected video system, please do it.');
			}

			if (close && url == '')
			{
				close = false;
				alert('You haven\'t set URL of the page with video, please do it.');
			}
			/*else if (close && (url.indexOf('http://' + this.videoSystems[systemId].url) < 0 || url.length < 15))
			{
				close = false;
				alert('Sorry, but you\'ve entered a wrong URL of the page with video, please enter a valid URL.');
			}*/

			if (close && typeof(this.saveField) == 'object' && this.saveField != null)
			{
				var code = '[video=' + this.videoSystems[systemId].name + ',' + width + ',' + height + ']' + url + '[/video]';
				if (typeof(this.saveField.value) == 'string')
				{
					this.saveField.value += code;
				}
				else if (typeof(this.saveField.innerHTML) == 'string')
				{
					this.saveField.innerHTML += code;
				}
			}
		}

		if (close && typeof(this.window) == 'object' && this.window != null)
		{
			this.window.hide();
		}
	}

	this.cancel = function ()
	{
		if (typeof(this.window) == 'object' && this.window != null)
		{
			this.window.hide();
		}
	}
}
