var _obj_ch = false;
var _obj_vid = false;
var curr_rUrl = false;
var curr_url = false;
var curr_flv = false;
var active_color = '#FFFFFF';

var handleSuccess = function(o){
	if(o.responseText !== undefined) {
		var _div = document.getElementById(o.argument.divID);

		_div.innerHTML = o.responseText;

		if (o.argument.flv) {
			SWF_load(o);
		}

		if (o.argument._aID) {
			updLayers(o);
		}
	}
}


var handleFailure = function(o){
	if(o.responseText !== undefined) {
		location.href = o.argument.url;
	}
}

var SWF_load = function(o) {
	if (o.argument.template == 'news_item') {
		w = 492; h = 282; c = "#FFFFFF"; flv_player_src = '/swf/flv_player_rounded_corners_492x282.swf';
	} else if (o.argument.template == 'conf_items' || typeof o.argument.template == "undefined"){
		return;
	}
	else {
		w = 360; h = 206; c = "#CCCCCC"; flv_player_src = '/swf/flv_player_rounded_corners_360x206.swf';
	}

	var so = new SWFObject(flv_player_src, 'video', w, h, '8', c);
	so.useExpressInstall('/swf/expressinstall.swf');
	so.addParam('wmode', 'transparent');
	so.addParam('align', 'middle');
	so.addParam("swliveconnect", "true");
	so.addParam("allowscriptaccess", "always");
	so.addParam('salign', 'tl');
	so.addParam('quality', 'high');
	so.addParam('menu', 'false');
	so.addVariable("videoURL", o.argument.flv);
	so.write('videoplayer');
}

var updLayers = function(o) {
	obj = document.getElementById(o.argument._aID);

	if (o.argument.item == 'ch') {
		obj.style.backgroundColor = active_color;

		if (_obj_ch) {
			_obj_ch.style.backgroundColor = '';
			_obj_ch.setAttribute('class', '');
		}

		_obj_ch = obj;

		// use flv as first vid id
		if (o.argument.flv)
			_obj_vid = document.getElementById('vID' + o.argument.flv);
	}

	if (o.argument.item == 'vid') {
		if (o.argument.template == 'news_item') {

			obj.style.backgroundColor = '#F9B6BD';

			if (_obj_vid) {
				_obj_vid.style.backgroundColor = '';
				_obj_vid.setAttribute('class', '');
			}
			_obj_vid = obj;

		} else {
			obj.style.backgroundColor = active_color;

			if (_obj_vid)
				_obj_vid.style.backgroundColor = '#FFFFFF';

			_obj_vid = obj;
		}
	}
}

function loadPage(id, item, rUrl, template, divID, flv) {
	sUrl = '/templates/ajax/' + template + '.tpl.php' + rUrl;

	url = '/' + rUrl;
	curr_sUrl = sUrl;
	curr_flv = flv;

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + '#com', {success:handleSuccess, failure:handleFailure, argument: {flv:flv, _aID:id, url:url, divID:divID, item:item, template:template }} );
}


function loadPage2(id, item, rUrl, sUrl, divID, flv) {
	//sUrl = sUrl + rUrl;

	url = '/' + rUrl;
	curr_sUrl = sUrl;
	curr_flv = flv;

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + '#com', {success:handleSuccess, failure:handleFailure, argument: {flv:flv, _aID:id, url:url, divID:divID, item:item }} );
}

function loadPageConf(id, item, rUrl, template, divID, flv) {
	sUrl = '/templates/ajax/' + template + '.tpl.php' + rUrl;

	url = '/' + rUrl;
	curr_sUrl = sUrl;
	curr_flv = flv;

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + '#com', {success:handleSuccess, failure:handleFailure, argument: {flv:flv, _aID:id, url:url, divID:divID, item:item, template:template }} );
}

function loadPageHeader(type, template) {

	if (template == 'award_item') {
		location.href="/conference/?e=0&show=comment#com";
	}

	id = false;
	sUrl = curr_sUrl + '&show=' + type;
	url = curr_url;
	flv = curr_flv;
	divID = 'content_item';

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl + '#com', {success:handleSuccess, failure:handleFailure, argument: {flv:flv, _aID:id, divID:divID, url:url, template:template}} );
}

var handlePostSuccess = function(o) {
	if(o.responseText !== undefined) {
		var _div = document.getElementById(o.argument.divID);

		_div.innerHTML = o.responseText;

		if (o.argument.flv) {
			SWF_load(o);
		}

	}
}

var handlePostFailure = function(o) {
	alert('Failure');
}

function postForm(id, template, postData, q, thisform) {
	template = '/templates/' + template + '.ajax.php';

	if (postData == 'login') {
		postData = 'iLogin=' + document.getElementById('iLogin').value;
		postData += '&iUsername=' + document.getElementById('iUsername').value;
		postData += '&iPassword=' + document.getElementById('iPassword').value;
	}

	if (postData == 'registerform') {
		postData = 'iRegisterForm=1';
	}

	if (postData == 'showlogin') {
		postData = '';
	}

	if (postData == 'register') {
		postData = 'iRegister=1';
		postData += '&iFirstName=' 		+ document.getElementById('iFirstName').value;
		postData += '&iLastName=' 		+ document.getElementById('iLastName').value;
		postData += '&iOrganisation=' 	+ document.getElementById('iOrganisation').value;
		postData += '&iEmail=' 			+ document.getElementById('iEmail').value;
		postData += '&iUsername=' 		+ document.getElementById('iUsername').value;
		postData += '&iPassword=' 		+ document.getElementById('iPassword').value;
		postData += '&iC_Password=' 	+ document.getElementById('iC_Password').value;
		postData += '&iNewsletter=' 	+ document.getElementById('iNewsletter').value;
		postData += '&iTerms=' 			+ document.getElementById('iTerms').checked;
	}

	if (postData == 'submitCompetition') {
		postData = 'submitCompetition=1';
		postData += '&uid=' + document.getElementById('comp_uid').value;
		postData += '&cid=' + document.getElementById('comp_cid').value;

		var slitq = q.split(",");

		for(n = 0; n < slitq.length; n++){
			for( i = 0; i < document.q['comp_'+slitq[n]].length; i++ ) {
				if( document.q['comp_'+slitq[n]][i].checked == true ) {
					postData += "&comp["+slitq[n]+"]=" + document.q['comp_'+slitq[n]][i].value;
	}	}	}	}

	postData += '&btn_colour=' + document.getElementById('btn_colour').value;


	var request = YAHOO.util.Connect.asyncRequest('POST', template, {success:handlePostSuccess, failure:handlePostFailure, argument: {divID:id}}, postData );
}

function postVote(id, sUrl, flv){
	postData = 'emailaddress=' + document.getElementById('emailaddress').value;
	sUrl = '/templates/ajax/award_item.tpl.php' + sUrl;
	var request = YAHOO.util.Connect.asyncRequest('POST', sUrl, {success:handlePostSuccess, failure:handlePostFailure, argument: {divID:id, flv:flv}}, postData );

}