var _obj = false;
var curr_rUrl = false;
var curr_url = false;
var curr_flv = false;

var handleSuccess = function(o){
	if(o.responseText !== undefined) {
			// in this section we look for script tags to handle them separately

		s2 = "";
		//we look here for all script tags in a loop, and remove them so after this loop there'll be no script tags in string
		while ( (i= o.responseText.indexOf("<script"))> -1) {  // first we take positions in string of all neccessary elements opening tag
		   i2 = o.responseText.indexOf("</script>",i); //closing tag
		   i5 = o.responseText.indexOf("src",i); // scr attribute
		   i6 = o.responseText.indexOf("\"",i5); // opening double quote directly after src attribute
		   i7 = o.responseText.indexOf("\"",i6+1); // closing double quote directly after src attribute
		   i3 = o.responseText.indexOf(">",i);  // last character of opening tag
		   i4=i2-i;
		   i2=i2-i3;

			if (i5>i && i5<i3) {  // if script tag has src attribute we're adding it to head of document
				s3 = o.responseText.substr(i6+1,i7-i6-1); // src attribute value taken from string
				var headTag = document.getElementsByTagName('head')[0];
				script = document.createElement('script');
				script.type = 'text/javascript';
				script.src = s3;
				headTag.appendChild(script);
			} else  // else we add it to s2 string to handle them all at once
		  	  s2 += o.responseText.substr(i3+1,i2-1);
		   o.responseText = o.responseText.substr(0,i)+o.responseText.substr(i+i4+9);

		}

		try {
			if (s2) eval(s2); // here we run all inline scripts at once
		} catch(e) {}


		if (typeof o.argument.template != 'undefined') {

			if (o.argument.template.substr(0,5) == 'news_') {
				var _div = document.getElementById('aNews');
			}
			else {
				var _div = document.getElementById('content_item');
			}
		}
		else {
			var _div = document.getElementById('content_item');
		}

		_div.innerHTML = o.responseText;

		if (o.argument.flv) {
			SWF_load(o);
		}

		if (o.argument.a_ID) {
			updLayers(o);
		}
	}

}


var handleFailure = function(o){
	if(o.responseText !== undefined) {
		location.href = o.argument.url;
	}
}

var SWF_load = function(o) {
	if (flv_player_src == '/webmedia/flv_player_rounded_corners_492x282.swf') {
		w = 492; h = 282;
	}
	else {
		w = 360; h = 240;
	}
	var so = new SWFObject(flv_player_src, "flashplayer", w, h, "8", "#CCCCCC");
	so.addVariable("videoURL", o.argument.flv);
	so.addParam('wmode', 'window');
	so.addParam('align', 'middle');
	so.addParam('allowScriptAccess', 'sameDomain');
	so.addParam('salign', '');
	so.write("videoplayer");
}

var updLayers = function(o) {
	obj = document.getElementById(o.argument.a_ID);

	// change colour
	//obj.style.backgroundColor = (o.argument.template == 'arts') ? '#FFE4E4' : '#EAEBF3';
	obj.className = 'active';

	// remove styls from prev item
	if (_obj != null) {
		_obj.className = '';
		_obj.style.backgroundColor = '';
	}

	// temp store for next click
	_obj = obj;
}

function loadPage(id, rUrl, template, flv) {
	sUrl = '/templates/ajax/' + template + '.tpl.php' + rUrl;

	url = '/' + rUrl;
	curr_sUrl = sUrl;
	curr_flv = flv;

	location.href = '#' + rUrl;

	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:handleSuccess, failure:handleFailure, argument: {flv:flv, a_ID:id, url:url, template:template}} );
}

function loadPageHeader(type) {
	id = false;
	_tmp = (curr_sUrl.indexOf('?') != -1) ? '&' : '?';
	sUrl = curr_sUrl + _tmp + 'show=' + type;
	sUrl = sUrl.replace('/comment', '');
	sUrl = sUrl.replace('/register', '');
	sUrl = sUrl.replace('/sendtofriend', '');
	sUrl = sUrl.replace('&amp;', '&');
	url = curr_url;
	flv = curr_flv;
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, {success:handleSuccess, failure:handleFailure, argument: {flv:flv, a_ID:id, url:url}} );
}


