/**
*	Global variables
**/

// Var to hold list of persons
var persons;

// Vars to hold index and data of current person
var cur_person;
var cur_person_data;

// URL for list of persons
var url_person_list = 'Person.aspx?lang=nl';

// URL for person details
var url_person_details = 'Person.aspx';

// Show transscript in lightbox
var trans_in_lb;

// Paths
var flv_path = '/assets/videoplayer/flv/';
var xml_path = '/assets/videoplayer/xml';
var img_path = '/assets/images/';

// Language for subtitles
var sub_lang = '';
var sub_vis = '';

// Position adjustments
var x_pos_adjust = new Object;
x_pos_adjust.Annemiek = 40;
x_pos_adjust.Dia = 62;
x_pos_adjust.Genevieve = 76;
x_pos_adjust.Jeroen = 53;
x_pos_adjust.Johan = 68;
x_pos_adjust.Michael = 55;
x_pos_adjust.Nicky = 68;
x_pos_adjust.Philippe = 65;
x_pos_adjust.Simone = 47;

// -----------------------------------------------------------------------

function getURLParam(strParamName){
  var strReturn = "";
  var strHref = window.location.href;
  if ( strHref.indexOf("?") > -1 )
  {
    var strQueryString = strHref.substr(strHref.indexOf("?"));
    var aQueryString = strQueryString.split("&");
    for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
      if (aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 )
      {
        var aParam = aQueryString[iParam].split("=");
        strReturn = aParam[1];
        break;
      }
    }
  }
  return unescape(strReturn);
}

// -----------------------------------------------------------------------

/**
*	Prepare page
**/
function InitPersonPage() 
{
	var person_id = getURLParam('person_id');
	if (person_id == 0)
	{
		person_id = null;
	}

	if (!$$('box_person'))
	{
		return false;
	}

	// Look for show_lightbox class on transscript box
	if ($('#box_transcript').hasClass('show_lightbox'))
	{
		trans_in_lb = true;
		$('#box_transcript').addClass('jqmWindow');
	}

	// Setup HTML
	LoadHtml();

	// Get list of persons + load first person
	$.getJSON(url_person_list, function(data)
	{
		persons = data;
		
		// If just 1 person, remove select & nav
		if (persons.length == 1)
		{
			$('#box_person_select, #box_person_nav').remove();
		}
		else
		{
			// Fill select with persons
			FillPersonSelect();
		}

		// Pick random person
		//cur_person = Math.floor(Math.random() * persons.length);

		// Take first person
		cur_person = 0;
		if (person_id)
		{
			// Find person in list
			for (var i = 0; i < persons.length; i++)
			{
				if (persons[i].id.toLowerCase() == person_id.toLowerCase())
				{
					cur_person = i;
					break;
				}
			}
		}
		
		// Load person
		LoadPerson(cur_person);
		
		// Show the whole box
		$('#box_person').fadeIn('slow');
	});
}

// -----------------------------------------------------------------------

/**
*	LoadHtml
*
*	Sets up the HTML necessary for the script
**/
function LoadHtml() 
{
	var str = '<h4 id="person_name"></h4><h5 id="person_position"></h5><div class="hr" />';
	str += '<div class="clearfix"><div id="box_person_select"><select id="select_person"></select></div>';
	str += '<div id="box_person_nav" class="clearfix">';
	str += '<a href="#" id="show_prev" class="nav_link"><img src="' + img_path + 'btn_prev.gif" /></a>';
	str += '<a href="#" id="show_next" class="nav_link"><img src="' + img_path + 'btn_next.gif" /></a>';
	str += '</div></div>';
	str += '<div id="box_person_content"></div>';
	$('#box_person').html(str);
	
	// Blur hack
	$('#box_person a#show_prev, #box_person a#show_next').focus(function()
	{
		if (this.blur)
		{
			this.blur();
		}
	});

	// Add event handler for prev
	$('#box_person a#show_prev').click(function()
	{
		// Previous = current - 1
		var prev = cur_person_data.index - 1;
		
		// Go to last one if clicked on first one
		if (prev < 0)
		{
			prev = persons.length - 1;
		} 
		LoadPerson(prev);
		return false;
	});

	// Add event handler for next
	$('#box_person a#show_next').click(function()
	{
		// Next = current + 1
		var next = cur_person_data.index + 1;
		
		// Go to first one if clicked on last one
		if (next == persons.length)
		{
			next = 0;
		} 
		LoadPerson(next);
		return false;
	});
	
	// Add event handler for select onchange
	$('#box_person #select_person').change(function()
	{
		LoadPerson($$('select_person').selectedIndex);
		this.blur();
	});
}

// -----------------------------------------------------------------------

/**
*	FillPersonSelect
*
*	Adds options for persons to the select
**/
function FillPersonSelect() 
{
	$$('select_person').options.length = 0;
	//$$('select_person').options[0] = new Option('Kies medewerker', 'choose_person');
	for (var i = 0; i < persons.length; i++)
	{
		$$('select_person').options[i] = new Option(persons[i].name, persons[i].id);
	}
}

// -----------------------------------------------------------------------

/**
*	LoadPerson
*
*	Gets the details of a person and sets up the html/flash.
*
*	- index: index in persons array
**/
function LoadPerson(index) 
{
	var person_id = persons[index].id;
	
	// Get details
	$.getJSON(url_person_details, { id: person_id }, function(pers_data)
	{
		cur_person_data = pers_data;
		cur_person_data.id = person_id;
		cur_person_data.index = index;
		
		$('#select_person').val(person_id);

		ShowCurrentPerson();
	});
}

// -----------------------------------------------------------------------

/**
*	ShowCurrentPerson
*
*	Shows the name, position and video or image of the current person.
**/
function ShowCurrentPerson() 
{
	$('#box_transcript').hide();

	$('#box_person #person_name').html(cur_person_data.name);
	$('#box_person #person_position').html(cur_person_data.position);

	// Build HTML for transscript 
	// Transscript is shown 1) when user clicks on button in Flash animation
	// 2) when there's no flash, by the javascript included with the alternative content
	var trans_html = '<h2>' + cur_person_data.name + '</h2>';
	trans_html += '<h3>' + cur_person_data.position + '</h3>';
	trans_html += '<div id="transscript_text"><img src="' + img_path + 'speech.gif" style="float: left; margin-right: 10px; border: none;"/> ' + cur_person_data.transcript;
	if (trans_in_lb)
	{
		trans_html += '<a href="#" id="close_transscript"></a>';
	}
	trans_html += '</div>';
	$('#box_transcript').html(trans_html);
	
	// Prepare HTML for alternate content (image)
	var person_html = '<div id="box_person_inside"><img id="person_image" src="assets/videoplayer/jpg/' + cur_person_data.id + '.jpg"/>';
	$('#box_person #box_person_content').html(person_html);
		
	// Load Flash using SwfObject
	var flashvars = { 
		naam: cur_person_data.id,
		flv_path: flv_path,
		xml_path: xml_path,
		sub_lang: sub_lang,
		sub_vis: sub_vis,
		plaats: x_pos_adjust[cur_person_data.id]
	};
	var params = flashvars;
	params.wmode = 'opaque';
	params.swliveconnect = 'true';
	params.allowscriptaccess = 'always';
	var attr = {};
	attr.id = "person_movie";
	attr.name = "person_movie";
	swfobject.embedSWF('assets/videoplayer/mediaPlayer.swf', 'box_person_inside', '256', '351', '8.0.0', '', flashvars, params, attr);

	// Close function
	var close_func = function(hash)
	{
		hash.w.fadeOut('', function() { 
			hash.o.remove(); 
		});
	};
	$('#box_transcript').jqm({ onHide: close_func });
	
	// Check if person image is still there; if it is, it means there's no Flash, 
	// so we show the transscript right away
	if ($('#box_person #person_image').length == 1)
	{
		if (trans_in_lb)
		{
			$('#box_transcript').jqmShow();
		}
		else
		{
			$('#box_transcript').show();
		}
	}

	// Event handler for close link
	$('#box_transcript a#close_transscript').click(function()
	{
		$('#box_transcript').jqmHide();
		this.blur();
		return false;
	});
}

// -----------------------------------------------------------------------

/**
*	ShowTransscript
*
*	Container function for Flash animation, used to toggle transscript.
**/
function ShowTransscript()
{
	if (trans_in_lb)
	{
		$('#box_transcript').jqmShow();
	}
	else
	{
		$('#box_transcript').show();
	}
	return false;
}

// -----------------------------------------------------------------------

/**
*	RememberSubLang
*
*	Sets the subtitle language variable.
**/
function RememberSubLang(lang)
{
	sub_lang = lang;
}

// -----------------------------------------------------------------------

/**
*	RememberSubVis
*
*	Sets the subtitle visible variable.
**/
function RememberSubVis(vis)
{
	sub_vis = vis;
}

// -----------------------------------------------------------------------

/**
*	RegisterPlay
*
*	Adds an image to the body for Nedstat statistics.
**/
function RegisterPlay()
{
	// Build nedstat URL
	var dO = new Date();
	var timeStamp = "&amp;ns__t=" + dO.getTime();
	var counter = 'nl.werken_bij_equens.ontmoet_je_collegas.flash.' + cur_person_data.id.toLowerCase();
	var ns_img = 'http://nl.sitestat.com/interpay/werkenbij/s?' + counter + '&amp;ns_type=flash' + timeStamp;
	$('body').append('<img src="' + ns_img + '" height="1" width="1" alt="" />');
}

// -------------------------------------------------------------------

/**
*	$$
*
*	Returns an object reference
*	for the given object id
**/
function $$(obj_id)
{
	return document.getElementById(obj_id);
}

// -------------------------------------------------------------------

/**
*	log
*
*	Logs to the console
**/
function log(str, to_div)
{
	if (console)
	{
		console.log(str);
	}
}
