//document.write("<script type='text/javascript' src='http://apism.mediastream.cl/_js/jquery-1.4.2.js'></script>");

var _img_video_no = 'http://apism.mediastream.cl/_img/video_no.jpg';
var _flash_str = '';
_flash_str += '<object id="smp_object_id_&&video_id&&" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" name="smp_object_name_&&video_id&&" width="&&width&&" height="&&height&&"> ';
_flash_str += '<param name="movie" value="http://apism.mediastream.cl/_mediaplayer_old/player-licensed.swf" /> ';
_flash_str += '<param name="allowfullscreen" value="true" /> ';
_flash_str += '<param name="allowscriptaccess" value="always" /> ';
_flash_str += '<param name="flashvars" value="file=&&file&&&image=&&preview&&&skin=http://apism.mediastream.cl/_mediaplayer/skins/atv/player.swf" /> ';
_flash_str += '<embed ';
_flash_str += 'type="application/x-shockwave-flash" ';
_flash_str += 'id="smp_embed_id_&&video_id&&" ';
_flash_str += 'name="smp_embed_name_&&video_id&&" ';
_flash_str += 'src="http://apism.mediastream.cl/_mediaplayer_old/player-licensed.swf" ';
_flash_str += 'width="&&width&&" ';
_flash_str += 'height="&&height&&" ';
_flash_str += 'allowscriptaccess="always" ';
_flash_str += 'allowfullscreen="true" ';
_flash_str += 'flashvars="file=&&file&&&image=&&preview&&&&skin=http://apism.mediastream.cl/_mediaplayer/skins/atv/player.swf" ';
_flash_str += '/> ';
_flash_str += '</object> ';

function _getVideoList (proxy, key, search) {
	var _url = proxy;
	var _result;
	
	var a = $.ajax({
		url: _url,
		type: 'GET',
		async: false,
		data: {
			'url':'http://apism.mediastream.cl/video/getVideoUrl.php',
			'action': 'getVideoList',
			'k': key,
			's': search},
		dataType: 'json',
		cache: false,
		success: function(_data, _textStatus, _XMLHttpRequest) {
			_result = _data;
		}, 
		error: function(_XMLHttpRequest, _textStatus, _errorThrown) {
			_result = '{"result": "00", "msg": "Ha ocurrido un error al obtener los resultados."}';
		}, 
		complete: function(_XMLHttpRequest, _textStatus) {
		}
	});
	return _result;
}

function _getVideo(proxy, id_video, w, h, skin) {
	var _url = proxy;
	var _htmlStr = '';
	var obj = { player:'', id:'', title:'', description:'', images:'', tags:'' };
	
	$.ajax({
		url: _url,
		type: 'GET',
		data: "url=http://apism.mediastream.cl/video/getVideoUrl.php&action=getVideo&i=1&v="+id_video,
		cache: false,
		async: false,
		dataType: 'json',
		success: function(_data, _textStatus, _XMLHttpRequest) {
			_htmlStr = _flash_str;
			
			if(_data != null) {
				if(_data.fields.length > 0) {
					_htmlStr = _htmlStr.replace(/&&video_id&&/gi, _data.fields[0].id_video);
					_htmlStr = _htmlStr.replace(/&&file&&/gi, _data.fields[0].url);
					_htmlStr = _htmlStr.replace(/&&preview&&/gi, _data.fields[0].images[0]);
				} else {
					_htmlStr = _htmlStr.replace(/&&video_id&&/gi, 'no_video');
					_htmlStr = _htmlStr.replace(/&&file&&/gi, _img_video_no);
					_htmlStr = _htmlStr.replace(/&&preview&&/gi, _img_video_no);
				}
				
				if(skin != '') {
					_htmlStr = _htmlStr.replace(/&&skin&&/gi, '&skin='+skin);
				} else {
					_htmlStr = _htmlStr.replace(/&&skin&&/gi, '');
				}
				
				_htmlStr = _htmlStr.replace(/&&width&&/gi, w);
				_htmlStr = _htmlStr.replace(/&&height&&/gi, h);

				obj.player = _htmlStr;
				obj.id = _data.fields[0].id_video;
				obj.title = _data.fields[0].title;
				obj.description = _data.fields[0].description;
				obj.images = _data.fields[0].images[0];
				obj.tags = '';
				
				if(_data.fields[0].tags.length > 0) {
					for(_t=0;_t<_data.fields[0].tags.length;_t++) {
						obj.tags += _data.fields[0].tags[_t] + ', ';
					}
				}
			
			} else {
				_htmlStr = _htmlStr.replace(/&&video_id&&/gi, 'no_video');
				_htmlStr = _htmlStr.replace(/&&file&&/gi, _img_video_no);
				_htmlStr = _htmlStr.replace(/&&preview&&/gi, _img_video_no);

				_htmlStr = _htmlStr.replace(/&&width&&/gi, w);
				_htmlStr = _htmlStr.replace(/&&height&&/gi, h);

				if(skin != '') {
					_htmlStr = _htmlStr.replace(/&&skin&&/gi, '&skin='+skin);
				} else {
					_htmlStr = _htmlStr.replace(/&&skin&&/gi, '');
				}

				obj.player = _htmlStr;
				obj.id = null;
				obj.title = null;
				obj.description = null;
				obj.images = null;
				obj.tags = '';
			}

		}, 
		error: function(_XMLHttpRequest, _textStatus, _errorThrown) {
			_htmlStr = _flash_str;
			_htmlStr = _htmlStr.replace(/&&video_id&&/gi, 'no_video');
			_htmlStr = _htmlStr.replace(/&&file&&/gi, _img_video_no);
			_htmlStr = _htmlStr.replace(/&&preview&&/gi, _img_video_no);
			_htmlStr = _htmlStr.replace(/&&width&&/gi, w);
			_htmlStr = _htmlStr.replace(/&&height&&/gi, h);
			
			obj.player = _htmlStr;
			obj.id = '';
			obj.title = 'No existe';
			obj.description = 'El video solicitado no existe';
			obj.images = _img_video_no;
			obj.tags = '';
		}, 
		complete: function(_XMLHttpRequest, _textStatus) {
			//alert(_textStatus);
			obj.player = _htmlStr;
		}
	});
	
	return obj;
}
