
$(document).ready(function()
{
	onDocumentReady();
});

//var g_strGegen = ' gegen '; 
var g_strGegen = ' VS. ';
var g_strTeamFilter = "0,90";
var g_strTeam1 = "0";
var g_strTeam2 = "0";

var g_strState = "init";

var g_strStoryboard = "";

var g_strAktItem = "sb_0_0";
var g_strAktGroup = 10;
var m_iMode = -1;
var m_iMaxScenes = 4;
var m_iScenes = 0;
var m_swfVideo = null;

var g_strSpeed = 'slow';

var m_strVideoURL = '';

var m_iMaxAutoTaf = 2;

var g_bSubscriptionFirst = false;
var g_bSubscriptionDone = false;
var g_bViewVideo = false;
var g_bViewVideoStart = false;
var g_bSubscriptionNone = false;

function onDocumentReady()
{
	var bEntry = false;
	var bResult = false;

	bEntry = ($("#txtState").attr('name') == 'introState');
	bResult = ($("#txtState").attr('name') == 'resultState');
	
	if (bEntry)
	{
		if (g_bSubscriptionFirst && g_bSubscriptionDone)
		{
			initEntryDlg(0);
		}
		else
		{
			if (g_bViewVideo && g_bViewVideoStart)
			{
				initEntryDlg(4);
			}
			else
			{
				initEntryDlg(3);
			}
		}
			
	}

	if (bResult)
	{
		if (g_bSubscriptionNone)
		{
			$("#taf_handy").addClass("dnone");
		}
		//startVideo(10);
		getVideoURL(10);
	}
	if (($("#txtState").attr('name') == 'msisdnState'))
	{
		initSelectVideos();
	}
}

function initSelectVideos()
{
	$('#sg > input').unbind('change click');
	$('#sg > ul').unbind('mousedown');

	$('#sg > ul').bind('mousedown', function(){
		//$(this).prev().attr("checked", "checked");
		$(this).prev().click();
	});

	
	$('#sg > input').bind('change click', function (){
		$('#txtUsrHash').val($(this).prev().val());
		m_strVideoURL = $(this).val();
		startVideo(20);
	});
	
	$('#sg > ul:first').prev().click();
	
}

function toggleVideo(p_iPlay)
{
	if (m_swfVideo != null)
	{
		switch(p_iPlay)
		{
			case 0:m_swfVideo.pause(); break;
			case 1:m_swfVideo.play(); break;
		}
	}
}

function startVideo(p_iMode)
{
	if (m_swfVideo)
	{
		// m_swfVideo.hide();
		m_swfVideo.setClip(m_strVideoURL);
		m_swfVideo.play();
		$("#swfVideoCont").removeClass("dnone");
		$("#swfVideoError").addClass("dnone");
		m_swfVideo.show();
		return;
	}
	if ($("#swfVideoCont").length > 0)
	{
		var iMode = p_iMode;
		$("#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 strVideo = $("#swfVideo")[0].href;
		var strImg = '/r/fla/loading.jpg';
		
		var oPlayer;
		if (iMode == 10)
			oPlayer = "http://media.zedmobil.de/r/fla/flowplayer-3.2.2.swf";
		else
			oPlayer = {src: "http://media.zedmobil.de/r/fla/flowplayer-3.2.2.swf", wmode: "transparent"};

		//oPlayer = {src: "/r/fla/flowplayer-3.2.2.swf", wmode: "transparent"};
		
		var oPlugins = { controls: {autoHide: "always"} };
		if (iMode == 20)
		{
			oPlugins = {controls: null};
		}
		
		m_swfVideo = $f("swfVideo", 
				// player unter die sb Leiste
				oPlayer,
				{ 
					//debug: true,
					//log: { level: 'debug', filter: 'org.flowplayer.controller.*' },

					playlist: [strVideo], 
					//playlist: [strVideo],
					plugins: oPlugins
				}
		);
		// m_swfVideo.addParam('wmode','transparent');
		m_swfVideo.onError(function(errorCode,errorMessage){
			$("#swfVideoCont").addClass("dnone");
			$("#swfVideoError").removeClass("dnone");
			$("#swfVideoErrorDesc").append('flowplayer error:' + errorCode + ' - ' + errorMessage);
			this.hide();
		});		
		m_swfVideo.onStart(function(clip){
			this.unmute();
		});

		if (!(iMode == 10 && typeof g_iFormState != "undefined" && g_iFormState == 11))
		{
			m_swfVideo.play();
		}
		if (iMode == 3)
		{
			m_swfVideo.onPause(function(){
				$("#swfVideoText").removeClass("dnone");
				$("#swfVideoCont").addClass("dnone");
				initEntryDlg(0);
			});
			m_swfVideo.onFinish(function(p1,p2,p3) {
				if (p1.url == strVideo)
				{
					$("#swfVideoText").removeClass("dnone");
					$("#swfVideoCont").addClass("dnone");
					initEntryDlg(0);
				}
			});
			$('#ataf').bind('click', function()
			{
				$(this).unbind('click');
				initEntryDlg(0);
				return false;
			});
		}
		if (iMode == 10 && typeof g_iFormState != "undefined" && g_iFormState == 11)
		{
			switchTaf();
		}
		if (iMode == 10)
		{
			m_swfVideo.onFinish(function(p1,p2,p3) {
				if (p1.url == strVideo && m_iMaxAutoTaf > 0)
				{
					m_iMaxAutoTaf = m_iMaxAutoTaf - 1;
					switchTaf();
				}
			});
			
		}
		if (iMode == 40)
		{
			m_swfVideo.onFinish(function(p1,p2,p3) {
				if (p1.url == strVideo)
				{
					switchMsisdn();
				}
			});
		}
	}
}

function getVideoURL(p_iMode)
{
	// no error
	if (m_strVideoURL.length == 0)
	{
		m_strVideoURL = "error:timer";
		window.setTimeout("getVideoURL("+p_iMode+")", 2500);
	}
	else if (m_strVideoURL.search(/^error:/) == -1)
	{
		startVideo(p_iMode);
	} 
	else if (m_strVideoURL.search(/^error:refresh/) == 0)
	{
		m_strVideoURL = "error:timer";
		window.setTimeout("getVideoURL("+p_iMode+")", 5000);
	} 
	else if (m_strVideoURL.search(/^error:timer/) == 0)
	{
		$.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(p_iMode);
					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");
	}
}
function switchTaf()
{
	if ($('#div_taf').css('visibility') != 'hidden')
	{
		$('#div_taf').css('visibility', 'hidden');
		$('#playerbck').css('visibility', 'visible');
		if ($('#div_taf').size() > 0)
			toggleVideo(1);
	}
	else
	{
		$('#div_taf').css('visibility', 'visible');
		$('#playerbck').css('visibility', 'hidden');
		toggleVideo(0);
	}
}

function switchMsisdn()
{
	$("#txtState").val("form_submit");	
	$("#formMain").submit();
}

function logVideo(strMsg)
{
	//var info = $("#info");
	//var time = new Date();
	//info.append(time + strMsg+"<br>");	
}


function getSplitID(p_strItem)
{
	var aItem = p_strItem.split("_");
	var strItem = aItem[1]; 
	return strItem;
}

function initEntryDlg(p_iMode)
{
	// show dialog and hide video on click or if video is complete
	if (p_iMode == 0)
	{
		if (g_bSubscriptionFirst && !g_bSubscriptionDone)
		{
			$('#formEntry').submit();
		}
		else
		{
			// das video wurde gerade fertig abgespielt
			if (g_bViewVideo && g_bViewVideoStart) 
			{
				$("#txtState").val("form_submit");
				$("#txtSbInfo").val(g_strTeam1+","+g_strTeam2);
				$("#formMain").submit();
			}
			else
			{
				m_iMode = p_iMode;
				if (m_swfVideo)
				{
					m_swfVideo.pause();
				}
				
				$('#playerbck,#dtaf').fadeOut(g_strSpeed);
				$('#dlg,#sb,#sg,#appbck').fadeIn(g_strSpeed);
				$('#dintro').hide();
			}
		}
	}

	// set timer for hiding dlg after x seconds
	if (p_iMode == 1)
	{
		window.setTimeout("initEntryDlg(2)", 4000);
	}

	// hide dlg after x seconds
	if (p_iMode == 2)
	{
		if (m_iMode < 0)
		{
			$('#dlg,#sb,#sg,#appbck,#dintro').fadeOut(g_strSpeed);
			$('#playerbck,#dtaf').fadeIn(g_strSpeed, function()
			{
				startVideo(p_iMode);
			});
			
			//window.setTimeout("initEntryDlg(0)", 5000);
			//startVideo(p_iMode);
		}
	}

	// start without dialog
	if (p_iMode == 3)
	{
		if (m_iMode < 0)
		{
			$('#dlg,#sg,#appbck').fadeOut(0);
			$('#playerbck,#dtaf').show();
			if (m_iMode < 0)
			{
				startVideo(p_iMode);
				m_iMode = p_iMode; 
			}
		}
	}
	
	// start without dialog and load video
	if (p_iMode == 4)
	{
		$('#dlg,#sg,#appbck').fadeOut(0);
		//$('#playerbck,#dtaf').show();
		$('#playerbck').show();
		getVideoURL(40);
	}
	

	if (m_iScenes == 0)
	{
		$('#vp_199').hide();
	}
	$("#dlg ul li").unbind('click');
	$("#dlg ul li").bind('click', clickSbItem);
}

function clickSbItem(p_evt)
{
	$("#dlg ul li").unbind('click');
	//var bInFunction = false;
	
	//if (bInFunction) 
	//	return false;
	
	//bInFunction = true;
	if (m_iMode < 0)
		m_iMode = 0;

	var strNextGroup = this.value;
	
	switch (g_strState)
	{
	case "init":
		var strValue = $(this).text();
		if (strNextGroup == 1140)
		{
			$("#dintro").hide();
			//$("#game").append(strValue);
			g_strTeam1 = getSplitID(this.id); 
		}
		if (strNextGroup == 1200)
		{
			$("#game").append('<div>'+g_strGegen+'</div>').css('cursor', 'default');
			//$("#game").append('<li>'+g_strGegen+'</li>' + strValue);
			g_strTeam2 = getSplitID(this.id);
			strNextGroup = 90;
			g_strState = "default";
		}
		$(this).clone().appendTo('#game').width("auto").removeClass('flw').css('cursor', 'default');
		g_strAktGroup = strNextGroup;
		g_strTeamFilter = g_strTeamFilter + ", " + $(this).attr('name');
		break;
	default:
		setNextSbItem($(this).text(), getSplitID(this.id), strNextGroup);
		break;
	}

	$.ajax({
		type:"POST",
		data: {
			webProcessState : "intro", 
			ajaxState : 1, 
			nextGroup:g_strAktGroup, 
			teamFilter:g_strTeamFilter, 
			team1:g_strTeam1,
			team2:g_strTeam2,
			sb:g_strStoryboard
			},
		dataType:"html",
		success:function(response){
			//alert("introdlg:success:"+response);
			$("#dlg").html(response);
			initEntryDlg(3);
		},
		error:function(xhr,err,e){
			alert("Error: introdlg"+err);
			}
	});	
	return false;
}

function setNextSbItem(p_strValue, p_iID, p_strNextGroup)
{
	var aAktItem = g_strAktItem.split("_");
	switch(p_strNextGroup)
	{
	case 10000:
	case "10000":
		p_strNextGroup = 0;
		$("#txtSb").val(p_strValue);
		if (g_bViewVideo)
		{
			$("#txtState").val("view_video");
			g_bViewVideoStart = true;
		}
		else
		{
			$("#txtState").val("form_submit");
		}
		$("#txtSbInfo").val(g_strTeam1+","+g_strTeam2);

		//$('#dlg').fadeOut(g_strSpeed);
		//$('#appbck').fadeIn(g_strSpeed);

		if (m_iScenes < m_iMaxScenes)
		{
			$("#formMain").submit();
		}
		else
		{
			$("#formMain").submit();
			//$("#labSubmit").removeClass('dnone');
			//$("#labSubmit").click(function()
			//{
			//	$("#formMain").submit();
			//});
		}
		return true;
		break;
	case 10:
	case "10":
		m_iScenes = m_iScenes + 1;
		// next row
		aAktItem[1] = parseInt(aAktItem[1]) + 1;
		aAktItem[2] = 0;
		g_strStoryboard = g_strStoryboard + "," + p_iID + '';
		if (m_iScenes >= m_iMaxScenes)
		{
			// jetzt kommt das Fazit
			//g_strTeamFilter = g_strTeamFilter.replace(/90/g, "91");
			// jetzt kommt das Ende
			p_strNextGroup = 0;
		}
			
		break;
	case 1200:
	case "1200":
		aAktItem[2] = parseInt(aAktItem[2]) - 1;
		g_strAktItem = aAktItem.join("_");
		//p_strValue = $("#"+g_strAktItem).text() + g_strGegen + p_strValue;
		p_strValue = $("#"+g_strAktItem).text() + ' gegen ' + p_strValue;
		aAktItem[2] = parseInt(aAktItem[2]) + 1;

		g_strStoryboard = g_strStoryboard + "+" + p_iID + '';
		break;
	default: // next column
		if ((g_strAktGroup != 90) && (g_strAktGroup != 10))
		{
			aAktItem[2] = parseInt(aAktItem[2]) + 1;
			g_strStoryboard = g_strStoryboard + "," + p_iID + '';
		}
		break;

	}
	if ((g_strAktGroup == 90) || (g_strAktGroup == 10))
		p_strValue = "";

	if ((g_strAktGroup == 90) && (m_iScenes > 0) && (p_strNextGroup != 0))
		g_strStoryboard = g_strStoryboard + "," + '100'; // anschlusseement ende => neuer anfang
	
	//$("#"+g_strAktItem).html(p_strValue);
	$("#sbcontent").append(" " + p_strValue);
	
	g_strAktItem = aAktItem.join("_");
	g_strAktGroup = p_strNextGroup;
	$("#txtSb").val(g_strStoryboard);	
}


