// Ajax object
var AjaxObject = {

	handleSuccess:function(o) {
		// This member handles the success response
		// and passes the response object o to AjaxObject's
		// processResult member.
		this.processResult(o);
		pageTracker._trackPageview(o.argument.uri);

		if (document.getElementById('search'))
			document.getElementById('search').style.backgroundImage = 'url(/images/search_bg.gif)';

	},

	handleFailure:function(o) {
		// Failure handler
		logMsg(o);
		alert('Page error\r\n - Page not found or time limit exceeded\r\n\r\nPlease try again later');
	},

	processResult:function(o) {
		// This member is called by handleSuccess

		/* adam's part */
		var i;
		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);

		}
		// change HTML on selected layer
		o.argument.eleItem.innerHTML = o.responseText;

		// Update flash player
		if (typeof o.argument.flv == 'string' && o.argument.flv.lenght > 4) {
			this.SWFLoad(o);
		}

		// Change colours in list
		if (typeof o.argument.eleList == 'object') {
			this.updList(o);
		}
		try {
			if (s2) eval(s2); // here we run all inline scripts at once
		} catch(e) {}

		/* adam's part end */


	},

	startRequest:function(method, uri, callback, postData) {
		s = window.location.href;
		s = s.substr(s.indexOf("/",7));
		if (s.indexOf("#")>-1) s = s.substr(0,s.indexOf("#"));
		if (s.indexOf("/",1)>-1) s = s.substr(0,s.indexOf("/",1));
		if (s=="/") s="/news";
		s+=uri.substr(uri.indexOf('/',1));

		YAHOO.util.Connect.asyncRequest(method, uri, callback, postData);
	},

	SWFLoad:function(o) {
		// This member reloads Flash video
		if (o.argument.section.indexOf('menu') != -1 || typeof o.argument.section == "undefined") {
			return false;
		}

		// config player and print
		var so = new SWFObject(weObj.get('player_src'),
								'video',
								weObj.get('player_w'),
								weObj.get('player_h'),
								'8',
								weObj.get('player_c'));
		so.useExpressInstall('/swf/expressinstall.swf');
		so.addParam('wmode', 'window');
		so.addParam('align', 'middle');
		so.addParam("swliveconnect", "true");
		so.addParam("allowfullscreen", "true");
		so.addParam("allowscriptaccess", "always");
		so.addParam('salign', 'tl');
		so.addParam('quality', 'high');
		so.addParam('menu', 'false');
		so.addVariable("autostart", "0");
		so.addVariable("videoURL2", o.argument.flv);
		so.write('videoplayer');

		// save last url
		weObj.set('curr_flv', o.argument.flv);
	},

	updList:function(o) {
		// This member updates className on list items
		if (weObj.get('curr_eleList') != null)
			weObj.get('curr_eleList').className = '';
		o.argument.eleList.className = 'active';
		weObj.set('curr_eleList', o.argument.eleList);
	}
};

var openSearch = function(uri, postData){

	var id = 'searchResults';
	var _divID = document.getElementById(id);
	var _current_display = YAHOO.util.Dom.getStyle(id, 'display');

	if ( _current_display == 'block' )
		return;

	var handleSearchSuccess = function(o) {
		YAHOO.util.Dom.setStyle(id, 'display', 'block');
		YAHOO.util.Dom.setStyle(id, 'visibility', 'hidden');
		_divID.innerHTML = o.responseText;
		var toHeight = _divID.offsetHeight;
		YAHOO.util.Dom.setStyle(id, 'height', '0px');
		YAHOO.util.Dom.setStyle(id, 'visibility', 'visible');
		var attributesSearch = {
			height: { to: toHeight }
		};
		var myHandler = function(type, args) { 	YAHOO.util.Dom.setStyle(id, 'height', 'auto'); }

		var animSearch = new YAHOO.util.Anim(id, attributesSearch, 0.3, YAHOO.util.Easing.easeOut);
		animSearch.animate();
		animSearch.onComplete.subscribe(myHandler);
	}

	var handleSearchFailure = function(o) {
		logMsg(o);
		alert('Page error\r\n - Page not found or time limit exceeded\r\n\r\nPlease try again later');
	}

	var search_callback = {
			success:handleSearchSuccess,
			failure:handleSearchFailure,
			timeout:30000
	};

	YAHOO.util.Connect.asyncRequest('POST', uri, search_callback, postData);
}

/* DEBUG */
var logMsg = function(o){
	var _divID = document.getElementById('ajaxResponse');
	_divID.innerHTML = '';
	if(o.responseText !== undefined){
			var _divID = document.getElementById('ajaxResponse');
			_divID.innerHTML = '';
			_divID.innerHTML += "<strong>responseText</strong>" + " : " + o.responseText + "<br>";
			for ( i in o.argument )
				if ( i == '_params' ){
					_divID.innerHTML += '<strong>Params:</strong>';
					for ( n in o.argument._params )
						_divID.innerHTML += "<blockquote><strong>"+n+"</strong>" + " : " + o.argument._params[n] + "<br></blockquote>";
				}
				else
						_divID.innerHTML += "<strong>"+i+"</strong>" + " : " + o.argument[i] + "<br>";
		}

}



