var mindroomdivs = new Array();
var playerIframes = new Array();
var selectedVideoID = -1;
var PARAM_MINDROOM_POSITION = 'mrpos';
var PARAM_MINDROOM_SEARCH = 'mrsearch';
var tab_type = 's';
var matches_over = false;

function sendMindroomSearchRequest(pattern)
{
	try
	{
		showLoader();
		function onSearchReady(resp)
	    {   
	    	try
	    	{
				tab_type = 's';
		    	div = document.getElementById(mindroomdivs[0] + '_search');	    
		    	newData = resp.responseText.split('<![CDATA[')[1];
		    	newData = newData.split(']]>')[0];
		    	div.innerHTML = newData;	
		    	hideLoader();
	    	}catch(e){}   
	    }
	    url = window.location.href;
	    if(url.indexOf('?') == -1)
	    	url += '?';
	    else
	    	url += '&';
	    	
		url += "ajaxrequest=MindroomSearchResultsAjaxAction" + "&videoid=" + selectedVideoID;
		if(pattern != null)
			url += "&pattern=" + encodeURI(pattern);
		// uzenet kikuldese
		req = AjaxService.createRequest(url, onSearchReady, null, 'GET', null);
		req.makeRequest();	
		
	}catch(e)
	{
	}
}

function sendMindroomCloudRequest()
{
	try
	{
		showLoader();
		function onCloudReady(resp)
	    {   
	    	try
	    	{
					tab_type = 'c';	
		    	div = document.getElementById(mindroomdivs[0] + '_cloud');	    	
		    	newData = resp.responseText.split('<![CDATA[')[1];
		    	newData = newData.split(']]>')[0];
		    	div.innerHTML = newData;		
		    	hideLoader();     	
	    	}catch(e){}   
	    }
	    url = window.location.href;
	    if(url.indexOf('?') == -1)
	    	url += '?';
	    else
	    	url += '&';
	    	
		url += "ajaxrequest=MindroomCloudAjaxAction" + "&videoid=" + selectedVideoID + "&limit=50";
			
		// uzenet kikuldese
		req = AjaxService.createRequest(url, onCloudReady, null, 'GET', null);
		req.makeRequest();	
		
	}catch(e)
	{
	}
}

function sendMindroomMatchRequest(pattern)
{
	try
	{
		function onMatchReady(resp)
	    {   
	    	try
	    	{
		    	div = document.getElementById(mindroomdivs[0] + '_matches');
		    	words = resp.responseXML.getElementsByTagName('word');
			if (words != null && words.length > 0)
			{
		    		ul = div.getElementsByTagName('ul')[0];
		    		ulContent = "";	    
					for(i = 0; i < words.length; i++)
					{
						text = (words[i].text == undefined) ? words[i].textContent : words[i].text;
						ulContent += '<li onclick="mindroomMatchOnClick(\'' + text + '\')">' + text + '</li>';
					}
					ul.innerHTML = ulContent;	
					div.style.display = 'block';	
					matches_over = true;   
					//div.onmouseover = mindroomMatchesOnMouseOut;	
			}
			else
			{
				div.style.display = 'none';	
			}
	    	} catch(e) {}   
	    }
	    url = window.location.href;
	    if(url.indexOf('?') == -1)
	    	url += '?';
	    else
	    	url += '&';
	    	
		url += "ajaxrequest=MindroomSearchAjaxAction" + "&videoid=" + selectedVideoID;
		if(pattern != null)
			url += "&pattern=" + pattern;
			
		// uzenet kikuldese
		req = AjaxService.createRequest(url, onMatchReady, null, 'GET', null);
		req.makeRequest();		
	} catch(e) {}
}

function showLoader()
{
	document.getElementById(mindroomdivs[0] + '_loader').style.display = 'block';
	document.getElementById(mindroomdivs[0] + '_search').style.display = 'none';
	document.getElementById(mindroomdivs[0] + '_cloud').style.display = 'none';
}

function hideLoader()
{
	document.getElementById(mindroomdivs[0] + '_loader').style.display = 'none';
	switchMindroomTab(tab_type);
}

function refreshMindroomSearch(videoID)
{
	selectedVideoID = videoID;
	sendMindroomCloudRequest();
	switchMindroomTab('c');

	// turn off search tab and div	
	s_div = document.getElementById(mindroomdivs[0] + '_search');
	s_tabdiv = document.getElementById(mindroomdivs[0] + '_searchtab');
	s_div.style.display = 'none';
	s_tabdiv.className = 'MRTabButton';
	s_tabdiv.style.display = 'none';
}

function switchMindroomTab(type)
{
	s_div = document.getElementById(mindroomdivs[0] + '_search');
	c_div = document.getElementById(mindroomdivs[0] + '_cloud');

	s_tabdiv = document.getElementById(mindroomdivs[0] + '_searchtab');
	c_tabdiv = document.getElementById(mindroomdivs[0] + '_cloudtab');

	tab_type = type;
	
	if (type == 's')
	{
		s_div.style.display = 'block';
		c_div.style.display = 'none';
		s_tabdiv.className = 'MRTabButton active';
		s_tabdiv.style.display = 'block';
		c_tabdiv.className = 'MRTabButton';
	}
	else
	{
		s_div.style.display = 'none';
		c_div.style.display = 'block';
		s_tabdiv.className = 'MRTabButton';
		c_tabdiv.className = 'MRTabButton active';
	}
}

function expandList(id)
{
	table = document.getElementById(id);
	for(var rowid=0; rowid < table.rows.length; rowid++)
	{
		try
		{
			row = table.rows[rowid];
			if(row.style.display == 'none')
				row.style.display = '';
		}
		catch(e){}
	}
	document.getElementById('expandbtn_' + id).style.display = 'none';
}

function registerMindroomSearch(iframeid, id)
{
	mindroomdivs.push(id);
	playerIframes.push(iframeid);
}

function setPlayerCurrentPosition(position)
{
	try
	{
		iframe = document.getElementById(playerIframes[0]);
		var oDoc = iframe.contentWindow || iframe.contentDocument;
		if (oDoc.document)
        		oDoc = oDoc.document;

		player = oDoc.getElementById('player');
		player.controls.currentPosition = Math.max(0, (parseFloat(position) / 1000) - 2);
		player.controls.play();
	}catch(e){alert(e);}
	return false;
}

function mindroomSearchOnChange(e, field_id)
{
	if (!e) e = window.event;
  	var key = e.keyCode ? e.keyCode : e.which;
	if (key == '13')
	{
		mindroomSearchOnClick(field_id);
		return true;
	}


	field = document.getElementById(field_id);
	if(field.value.length > 1)
	{
		sendMindroomMatchRequest(field.value);
	}
	else
	{
		div = document.getElementById(mindroomdivs[0] + '_matches');
		div.style.display = 'none';
	}
}

function mindroomMatchOnClick(value)
{
	field = document.getElementById(mindroomdivs[0] + '_field');
	field.value = value;
	mindroomSearchOnClick(field.id);
	div = document.getElementById(mindroomdivs[0] + '_matches');
	div.style.display = 'none';
	switchMindroomTab('s');
}

function mindroomDoSearch(pattern)
{
	field = document.getElementById(mindroomdivs[0] + '_field');
	field.value = pattern;
	mindroomSearchOnClick(field.id);
	switchMindroomTab('s');
	if (pageTracker != undefined) 
		pageTracker._trackEvent('mindroom', 'search');
}

function mindroomSearchOnClick(field_id)
{
	field = document.getElementById(field_id);
	sendMindroomSearchRequest(field.value);
}

function mindroomOnLoad()
{
	params = window.location.href.split('?');
	if(params.length > 1)
	{
		params = params[1];
		params = params.split('&');
		for(var p = 0; p < params.length; p++)
		{
			param = params[p];
			p_name = param.split('=')[0];
			p_value = param.split('=')[1];
			if(p_name == PARAM_MINDROOM_POSITION)
			{
				setPlayerCurrentPosition(parseFloat(p_value));
			}
			else if(p_name == PARAM_MINDROOM_SEARCH)
			{
				mindroomDoSearch(p_value);
			}
		}
	}
	document.getElementById('portal').onmouseup = function()
	{
		if(matches_over)
		{
			try{
			document.getElementById(mindroomdivs[0] + '_matches').style.display = 'none';
			} catch(e) {
				alert(e);
			}
		}
	}
}

window.onload = mindroomOnLoad;

