var iRetry = 0;

function updateName(oObject)
{
	$("#labUser").html(oObject.value);

	// update length/remaining chars
	// $('#form_error').html("lenth:"+oObject.value.length);

}
function onHover(oElement, bHover)
{
	var strSrc = oElement.attr("src");
	var strNew = "/p.";

	if (bHover)
		strNew = "/o.";
	oElement.attr("src", strSrc.replace(/\/.\./, strNew));
}

function startEntry()
{
	var iRefresh = 0;
	$("#txtName").focus(function()
	{
		if ($("#txtName").val() == "" || $("#txtName").val() == m_strInputUserName)
		{
			$("#txtName").val("");
			$("#labUser").html("");
		}
		else
		{
			updateName(this);
		}		
	}).blur(function()
	{
		if ($("#txtName").val() == "" || $("#txtName").val() == m_strInputUserName)
		{
			$("#txtName").val(m_strInputUserName);
			$("#labUser").html(m_strUsrName);
		}
		else
		{
			updateName(this);
		}
	}).change(function()
	{
		updateName(this);
	}).keyup(function()
	{
		updateName(this);
	}).focus();

	$("#labSubmit").click(function()
	{
		$("#txtState").val("form_submit");
		$("#formMain").submit();
	});
	
	$("#formMain").submit(function()
	{
		//updateName($("#txtName")[0]);
		// check if button was pressed (not enter in text field)
		if ($("#txtState").val() == "form_submit")
			return true;
		return false;
	});
	
	var bOCU = true;
	var oInput = $("#OCUC input");
	//oInput.ready(function()
	oInput.load(function()
	{
		if (bOCU)
		{
			bOCU = false;
			oInput.ocupload({
				action: '',
				//params: {introState : "upload", ajaxState : 1, sid : m_strSid, webProcessState: "intro"},
				// evtl. userName hintufuegen
				params: {webProcessState : "intro", introState : "upload", ajaxState : 1, sid : m_strSid},
				onHover : onHover, 
				onComplete: function(response) {
					$('#form_error').html("&nbsp;");

					var patResult=/##1##([^:]*):(.*)##2##/gi;
					var result = patResult.exec(response);
					if (result)
					{
						if (result[1] == "url")
						{
							iRefresh = iRefresh + 1;
							$("#imgUser")[0].src = result[2] + "?refresh=" + iRefresh;
						}

						if (result[1] == "error")
						{
							$('#form_error').html(result[2]);
						}
						while (patResult.exec(response)) {}
					}
					else
					{
						$('#form_error').html("error:response:"+response);
					}
					//if (response.search(/##1##upload_error_filesize:.*##2##/) != -1)
					//{
					//	var error_txt = response.substring(response.search(/##1##upload_error_filesize:/)+27, response.search(/##2##/));
					//	$('#form_error').html(error_txt);
					//}
					//else
					//{
					//	iRefresh = iRefresh + 1;
					//	$("#imgUser")[0].src = imgName + "?refresh=" + iRefresh;
					//	$('#form_error').html('&nbsp;');
					//}
				}
			});
		}
	});	
}

function startAni(p_strAni, p_bMmute)
{
	
	var vidton = "http://media.zeddemedia.esc58.com/live/r/v/header.flv";
	//var vidton2 = "http://mvc.impossible-software.de/media/videos/e6f0de70b4d04f449b185c19efe2ba30.mp4";
	var flowPlayer = $f(p_strAni, "http://media.zedmobil.de/r/fla/flowplayer-3.1.3.swf", {
		clip:{
			url: vidton,
			autoPlay:true
		},
		onBeforeFinish:function(){
			this.mute();
			this.play();
			return false;
		},
		plugins:{
			controls:null
		},
		onLoad:function(){
			if (p_bMmute)
				this.mute();
			else
				this.unmute();
		}
	});
	$("#"+p_strAni).height("0px");
	flowPlayer.onCuepoint([500], 
		    function(clip, cuepoint) {
				//$("#tmpStatus").html($("#tmpStatus").html() + ", " + cuepoint);
				if (cuepoint > 400)
					$("#"+p_strAni).height(160 + "px");
		        //alert("cuepoint " + cuepoint + " reached"); 
		    } 
		);		

	return flowPlayer;
	
}

$(document).ready(function()
	{
	onDocumentReady();
});

function onDocumentReady()
{
	var bEntry = false;
	var bResult = false;

	bEntry = ($("#txtState").attr('name') == 'introState');
	bResult = ($("#txtState").attr('name') == 'resultState');
	
	var swfVideo = null;
	var swfAni = null;
	
	//init ani video
	if (!bResult)
	{
		swfAni = startAni("swfAni1", !bEntry);
	}
	
	if (bResult)
	{
		getVideoURL();
	}

	// only do this for entry/msisdn page
	if (bEntry)
		startEntry();	
}

function logVideo(strMsg)
{
	//var info = $("#info");
	//var time = new Date();
	//info.append(time + strMsg+"<br>");	
}

function startVideo()
{
	if ($("#swfVideoCont").length > 0)
	{
		$("#swfVideoText").addClass("dnone");
		$("#swfVideoCont").removeClass("dnone");
		var iVideoHeight = 288;

		var iVideoHeight = $("#swfVideoCont").height();
		if (iVideoHeight < 1)
		{
			iVideoHeight = 225; // default value
		}
		// uncomment next line for hiding flowplayer stuff
		//$("#swfVideoCont").height("0px");
		$("#swfVideo").attr("href", m_strVideoURL);
		//var swfVideo = $f("swfVideo", "http://media.zedmobil.de/r/fla/flowplayer-3.1.3.swf");
		//var info = $("#info");
		//info.html("Init<br>");
		
		var strVideo = $("#swfVideo")[0].href;
		//var strImg = $("#swfVideo > img")[0].src;
		//var strImg = $("#wait")[0].src;
		var strImg = '/r/fla/loading.jpg';
		
		var swfVideo = $f("swfVideo", "http://media.zedmobil.de/r/fla/flowplayer-3.1.3.swf", 
		{ 
			playlist: [strImg, strVideo], 
			// debug: true,  
			//log: { level: 'debug', filter: 'org.flowplayer.controller.*' },
			//log: { level: 'debug', filter: 'org.flowplayer.rtmp.*' },  
			clip: {
				//autoPlay:false,
				//autoBuffering:true,
				onBegin: function() {
		            logVideo("Clip:Begin");
		            return true;
		        }, 
		        onStart: function() {
		        	logVideo("Clip:Start");
		        	return true;
		        },
		        onBeforeBegin: function() {
		        	logVideo("Clip:OnBeforeBegin");
		        	return true;
		        }
	    	},
	    	onBegin : function(p1, p2, p3) {
	    		logVideo("Player:Begin");
	        },  
			onLoad : function(parm){
				//$("#swfVideoCont").removeClass("dnone");
				logVideo("Player:onLoad");
				return true;
			},
			onBeforeLoad : function(parm){
				//$("#swfVideoCont").removeClass("dnone");
				logVideo("Player:onBeforeLoad");
				return true;
			},
			// uncomment next line for showing flowplayer controls
			//plugins: {controls: null} 
			// uncomment next line for autohide flowplayer controls
			plugins: {
				  controls: {autoHide: "always"}
				}

			
		});
		swfVideo.onStart(function(clip){
			this.unmute();
		});
		swfVideo.onError(function(errorCode,errorMessage){
			$("#swfVideoCont").addClass("dnone");
			$("#swfVideoError").removeClass("dnone");
			$("#swfVideoErrorDesc").append('flowplayer error:' + errorCode + ' - ' + errorMessage);
			this.hide();
		});		
		swfVideo.onCuepoint([500], 
			    function(clip, cuepoint) {
					$("#tmpStatus").html($("#tmpStatus").html() + ", " + cuepoint);
					if (cuepoint > 400)
						$("#swfVideoCont").height(iVideoHeight + "px");
			    } 
			);		
		
		swfVideo.play();
	}
}

function getVideoURL()
{
	// no error
	if (m_strVideoURL.length == 0)
	{
		m_strVideoURL = "error:timer";
		window.setTimeout("getVideoURL()", 2500);
	}
	else if (m_strVideoURL.search(/^error:/) == -1)
	{
		startVideo();
	} 
	else if (m_strVideoURL.search(/^error:refresh/) == 0)
	{
		m_strVideoURL = "error:timer";
		window.setTimeout("getVideoURL()", 5000);
	} 
	else if (m_strVideoURL.search(/^error:timer/) == 0)
	{
		iRetry = iRetry + 1;
		$.ajax({
			type:"POST",
			data:{resultState:'getVideoURL', webProcessState:'result'},
			dataType:"html",
			success:function(response){
				var patResult=/##1##(.*)##2##/gi;
				var result = patResult.exec(response);
				if (result)
				{
					//alert("getVideoURL:result:"+result);
					m_strVideoURL = result[1];
					getVideoURL();
					while (patResult.exec(response)) {}
				}
				else
				{
					//$('#form_error').html("error:response:"+response);
					alert("getVideoURL:success:"+response);
				}
			},
			error:function(xhr,err,e){
				alert("Error: getVideoURL"+err);
				}
		});
	}
	else
	{
		// alert("Error: getVideoURL:"+m_strVideoURL);
		$("#swfVideoErrorDesc").append("Error: getVideoURL:"+m_strVideoURL);
		$("#swfVideoText").addClass("dnone");
		$("#swfVideoCont").addClass("dnone");
		$("#swfVideoError").removeClass("dnone");
	}
}
