var jsv = 0;
var winIe = 0;
var macIe = 0;
/*@cc_on jsv = @_jscript_version; @if (@_win64 || @_win32 || @_win16) winIe = 1; @elif (@_mac && (@_PowerPC || @_mc680x0)) macIe = 1; @end @*/

var procStatus = 0;

window.onbeforeunload = function(event)
{
	if (procStatus == 1)
	{
		event = event || window.event; 
		event.returnValue = "It's still processing the conversion.";
	}
}

///////////////////////////////////////////////////////////////////////////////////////
function get_hex2(val)
{
	val = parseInt(val);
	if (val == 0) return '00';

	var out='';
	while (val) 
	{
		out = '0123456789abcdef'.substr(val % 16, 1) + out;
		val = parseInt(val / 16);
    }
	
	if (val.length == 1) return '0' + out;
	return out;
}

///////////////////////////////////////////////////////////////////////////////////////
function FlvdlBase(url)
{
	this.id = '';
	this.url = url;
	this.ext = '.mov';
	this.is_fecthing = false;
	this.size = 0;
	this.ui = null;
	this.dl_base_url = null;
}

FlvdlBase.prototype =
{
	set_ui : function(ui)
	{
		this.ui = ui;
	},

	set_ext : function(ext)
	{
		this.ext = ext;
	},

	_get_dl_url : function()
	{
		return this.dl_base_url + '?c=dl&id='+this.id;
	},

	_get_urls : function()
	{
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open('GET', svurls_url, false);//sync
		xmlhttp.send(null);
		return eval("(" + xmlhttp.responseText + ")");
		
	},
	
	_get_process_frame_url : function()
	{
		var svurls = this._get_urls().svurls;
		var svurl = svurls[Math.floor(Math.random() * svurls.length)];
		return svurl + '?c=process&u=' + escape(this.url) + '&e='+this.ext;
	},
	
	kick_cancel : function()
	{
		set_process_frame(null);
	},
	
	kick_process : function()
	{
		set_process_frame(this._get_process_frame_url());
	},
	
	on_recieve_json : function(obj)
	{
		var s = (obj.status == 'ok');
		switch(obj.process)
		{
		case 'connected':
			this.ui.on_connected();
			break;
		case 'initial':
			if (s)
			{
				this.size = obj.size;
				this.url = obj.url;
				this.id = obj.id;
				this.ui.on_file_info_end(obj.size, null);
			}
			else this.ui.on_file_info_end(0, obj.reason);
			break;
		case 'start':
			if (!s)
			{
				this.ui.on_start_failed(obj.reason);
			}
			break;
		case 'fetching':
			this.ui.on_fetching(s, obj.size==undefined?0:obj.size, this.size);
			break;
		case 'fetched':
			this.ui.on_fetch_end(s);
			break;
		case 'converted':
			this.dl_base_url = obj.dl_base_url;
			this.ui.on_convert_end(s, this._get_dl_url());
			break;
		}
	}
}

/////

function FlvdlUI()
{
	this.base = null;
}

FlvdlUI.prototype =
{
	_is_valid_url : function(url) 
	{
	    return url.match(/^http:\/\//);
	},
	
	_inputs_disabled : function(dis)
	{
		$('start_button').disabled = dis;
	},
	
	_html_reset : function()
	{
		Element.hide('process');
		Element.hide('download');
		Element.hide('input-button-back');
		Element.show('input-button');
		Element.show('inputbox');
		this._set_process_msg(TITLE_INIT);
		this._show_process_progress(false);
		this._set_progress(0,0,0);
		this._inputs_disabled(false);
		set_download_frame(null);
	    $('url').focus();
		procStatus = 0;
	},
	
	_show_initialize : function()
	{
		this._set_process_msg(TITLE_CONNECTING);
		this._show_process_progress(true);
		$('source-url').innerHTML = $('url').value;
		$('conv-format').innerHTML = $('format').value;
	},
	
	_show_process_progress : function(show)
	{
		if (show)
		{
			Element.show('panim');
		}
		else
		{
			Element.hide('panim');
		}
	},
		
	_set_process_msg : function(msg)
	{
		$('title').innerHTML = msg;
	},
	
	_set_progress : function(percent, size, totalsize)
	{
		var ps = String(percent) + '%';
		var ptag = $('progress');
		if (percent > 10)
		{
			ptag.innerHTML = ps;
		}
		else
		{
			ptag.innerHTML = '';
		}
		
		ptag.style.width = ps;
		
		var cs;
		if (totalsize > 0)
		{
			cs = this._kb(size) + '/' + this._kb(totalsize) + 'KB';
		}
		else
		{
			cs ='-/-KB';
		}
		
		$('conv-size').innerHTML = cs;
	},
	
	_send_cancel : function()
	{
		if (this.base)
		{
			this.base.kick_cancel();
			this.base = null;
		}
	},

	_clear : function()
	{
		this.base = null;
		this._html_reset();
	},
	
	_kb : function(size)
	{
		return String(Math.floor(Number(size) / 1024));
	},
	
	_start_download : function(url)
	{
		set_download_frame(null);

		if (winIe)
		{
			this._set_process_msg(TITLE_COMPL_DL);
		}
		else
		{
			this._set_process_msg(TITLE_COMPL_AT);
			set_download_frame(url);
		}
		
		Element.hide('process');
		Element.hide('input-button');
		Element.hide('inputbox');

		$('download_a').href = url;
		
		//insert_468x60_cjad('download_ad');

		Element.show('download');
	
		if(navigator.userAgent.indexOf("Safari")!=-1)
		{
			// for safari
			$('input-button-back').innerHTML = '<p style="text-align:center;"><a href="http://vixy.net/">Back</a></p>';
		}

		Element.show('input-button-back');
	
		$('url').value = '';
		
//		urchinTracker('/flvtc_trakcing/dl/'+escape(url));
		procStatus = 2;
	},
		
	/////
	
	// from base
	on_connected : function()
	{
		this._set_process_msg(TITLE_RESLV);
	},
	
	on_file_info_end : function(size, err)
	{
		if (size > 0)
		{
			this._set_process_msg(TITLE_STARTING);
		}
		else
		{
			this._show_process_progress(false);
			var html = "<span style='color:red;'>Error: " + err + "</span>";
			this._set_process_msg(html);
			procStatus = 0;
		}
	},
	
	on_start_failed : function(err)
	{
		this._show_process_progress(false);
		var html = "<span style='color:red;'>Error: " + err + "</span>";
		this._set_process_msg(html);
		procStatus = 0;
	},
	
	on_fetching : function(suceeded, size, filesize)
	{
		if (suceeded)
		{
			this._set_process_msg(TITLE_PROCESS);
			var p = Math.floor(size * 100 / filesize);
			this._set_progress(p, size, filesize);
		}
	},

	on_fetch_end : function(succeeded)
	{
		if (succeeded)
		{
		}
		else
		{
			this._show_process_progress(false);
			this._set_process_msg(TITLE_PROC_FAILED);
			procStatus = 0;
		}
	},
	
	on_convert_end : function(suceeded, dlurl)
	{
		this._show_process_progress(false);
		if (suceeded)
		{
			this._set_progress(100, 0, 0);
			this._start_download(dlurl);
		}
		else
		{
			this._set_process_msg(TITLE_CONV_FAILED);
			procStatus = 0;
		}
	},
	
	/////
	// from html
	on_load : function()
	{
	},
	
	on_unload : function()
	{
		this._send_cancel();
	},
	
	on_start : function()
	{
		this.base = null;

		var url = $('url').value;
		url = url.replace(/\s/ig,'');
		if (this._is_valid_url(url)) 
		{
			this._inputs_disabled(true);
			
			Element.hide('inputbox');
			Element.show('process');

			this.base = new FlvdlBase(url);
			this.base.set_ui(this);
			this.base.set_ext($('format').value);
			this.base.kick_process();

			this._show_initialize();
			
//			urchinTracker('/flvtc_trakcing/start/'+escape(url));
			procStatus = 1;
	    } 
		else 
		{
			this._html_reset();
	    }
	},
	
	on_cancel : function()
	{
		this._send_cancel();
		this._clear();
	},
	
	on_back : function()
	{
		location.href="http://vixy.net/";
	},
	
	/////
	// from process_frame
	on_recieve_json : function(obj)
	{
		if (this.base)
		{
			this.base.on_recieve_json(obj);
		}
	}
	
}

/////

function get_query_url()
{
	var q = String(location.href).split('?');
	if (q[1] == undefined) return '';

	var qs = q[1].split('&');
	for(var i = 0; i < qs.length; i++)
	{
		var qu = qs[i].split('=');
		if (qu[0] == 'u')
		{
			return unescape(qu[1]);
		}
	}
	return '';
}

var flvdlui = new FlvdlUI();

function on_body_load() 
{
    $('url').focus();
	var url = get_query_url();
	if (url != '') $('url').value = url;
	
	flvdlui.on_load();
}

function on_body_unload()
{
	flvdlui.on_unload();
}

function set_process_frame(url)
{
	if (url)
	{
		$('process_frame').src = url;
	}
	else
	{
		$('process_frame').src = 'about:blank';
	}
}

function set_download_frame(url)
{
	if (url)
	{
		$('download_frame').src = url;
	}
	else
	{
		$('download_frame').src = 'about:blank';
	}
}

var recieve_json = function(obj)
{
	flvdlui.on_recieve_json(obj);
}
