// this overrides a extension in prototype.js so that we can do better trapping and debugging in ie.
window.onerror = function (err, file, line) {
	var s_ERROR = "";

	s_ERROR += 'The following error occured:\n';
	s_ERROR += 'Error [' + err + ']\n';
	s_ERROR += 'File  [' + file + ']\n';
	s_ERROR += 'Line  [' + line + ']\n';
	if (window.onerror.caller === null){
		s_ERROR += 'CallLevel was called from the top level\n';
	}
	else{
		s_ERROR += 'CallLevel was called by another function.' + window.onerror.caller;
	}
	alert(s_ERROR);
	return true;
};

// this overrides a extension in prototype.js so that we can do better trapping and debugging in ie.
Object.extend(
	String.prototype, {
		evalScripts: function() {
			return this.extractScripts().map(
				function(script) {
					var s_ERROR = "";
					var o_res;
					try {
						o_res = eval(script);
					}
					catch (e) {
						s_ERROR += "Exception Caught:eval(script)\n";
						s_ERROR += "Exception:" + e + "\n";
						if (script.match(/[\s\t\r\n]*<!--/)){
							s_ERROR += "[There is a <!-- --> Comment in the javascript]\n";
							s_ERROR += "[IE Does not like to eval() that]\n";
						}
						for (var i in e) {
							s_ERROR += i + ' = ' + e[i] +'\n';
						}
						alert(s_ERROR + "\n--------------------------------------------------------\n" + script);
					}

					//finally {
							//alert("Application failed - Sorry");
					//}
					return o_res;
				}
			);
		}
	}
);


function deliver_smartads(s_ads_destination, i_ads, s_location_type, i_location_id){
	Event.observe(
		window,
		'load',
		function(){
			_deliver_smartads(s_ads_destination, i_ads, s_location_type, i_location_id);
		}// End Function
	); //End Observe
}


function _deliver_smartads(s_ads_destination, i_ads, s_location_type, i_location_id){
	var b_failed = false;
	var h_parameters = $H({
		'g': 's', // Smart Ads
		'd': s_ads_destination,
		'n': i_ads,
		'l': s_location_type,
		'id': i_location_id
	});

	new Ajax.Request(
		'/proxy.aspx',
		{
			method:'get',
			//asycnhronous:false,
			asycnhronous:true,
			parameters: h_parameters,

			onCreate: function(transport) {},
			onException: function(transport, exception) {
				b_failed = true;
				//$('pnl_smart_ads_error').update("Exception: Sorry, Exception occured when fetching ads from /proxy.aspx?" + h_parameters.toQueryString());
			},
			onFailure: function(transport) {
				b_failed = true;
				//$('pnl_smart_ads_error').update("Failure: Sorry, Failure occured when fetching ads from /proxy.aspx?" + h_parameters.toQueryString());
			},
			onSuccess: function(transport) {
				$('pnl_smart_ads').update(transport.responseText);
			},
			onComplete: function(transport) {

				// --------------------------------------------------------------------
				// Observer to watch clicks
				// --------------------------------------------------------------------
				var a_sad_links = $$('a.sad_link');
				$A(a_sad_links).each(
					function(o_element){
						o_element.observe('click',
							function(event){
								sad_click(event, o_element);
							} // End Function
						); // End Observe
					} // End Function
				); // End Each

			}
		}
	);
}

function deliver_ads(i_zone, i_ads){
	// Call to deliver ads
	Event.observe(
		window,
		'load',
		function(){
			_deliver_ads(i_zone, i_ads);
		}// End Function
	); //End Observe
}


function _deliver_ads(i_zone, i_ads){
	var b_failed = false;
	var h_parameters = $H({
		'g': 'p', // Regular Ads
		'z': i_zone,
		'n': i_ads
	});

	new Ajax.Request(
		'/proxy.aspx',
		{
			method:'get',
			asycnhronous:true,
			parameters: h_parameters,

			onCreate: function(transport) {},
			onException: function(transport, exception) {
				b_failed = true;
				//$('pnl_ads_error').update("Exception: Sorry, Exception occured when fetching ads from /proxy.aspx?" + h_parameters.toQueryString());
			},
			onFailure: function(transport) {
				b_failed = true;
				//$('pnl_ads_error').update("Failure: Sorry, Failure occured when fetching ads from /proxy.aspx?" + h_parameters.toQueryString());
			},
			onSuccess: function(transport) {
				$('pnl_ads').update(transport.responseText);
			},
			onComplete: function(transport) {

				// --------------------------------------------------------------------
				// Observer to watch clicks
				// --------------------------------------------------------------------
				var a_pad_links = $$('a.pad_link');
				//$A(a_pad_links).each(
				//alert("a_pad_links " + a_pad_links);
				a_pad_links.each(
					function(o_element){
						//alert("adding observer to " + o_element.id);
						//alert("adding observer to " + o_element.innerHTML);
						o_element.observe('click',
							function(event){
								pad_click(event, o_element);
							} // End Function
						); // End Observe
					} // End Function
				); // End Each

			} // End onComplete

		} // End Options
	); // End Ajax.Updater

} // End function



// pad_click
// Tracks clicks on paid ads
function pad_click(event, o_element){

//Event.cancelBubble;
//Event.returnValue = false;

//alert("pad_click");










	var s_id = o_element.id;
	var i_id = o_element.id.match(/^bid([0-9]+)$/)[1];
	//alert("Click on Banner s_id:" + s_id + ", idP:" + i_id + " detected");
	
	var hashParams = $H(
	{ 
				'g': 'p',                   // Paid Ads
				'z': i_zone,                // This is the Zone of the ad displayed: from this we can determine the site of the origin of the click
				'c': 1,                     // Boolean - This is a click (not an impression request)
				'b': i_id                   // This is the ID of the banner
	}
	); 
	/*
	var strUrl = '/proxy.aspx?' + hashParams.toQueryString();
	window.location = strUrl;
	return false;
	*/

	new Ajax.Request(
		'/proxy.aspx',
		{
			method:'get',
			asycnhronous:false,
			parameters: hashParams//,
		}
	);
	return true;
}



// sad_click
// Tracks clicks on Smart ads
function sad_click(event, o_element){
	var s_id = o_element.id;
	var i_id = o_element.id.match(/^mid([0-9]+)$/)[1];
	new Ajax.Request(
		'/proxy.aspx',
		{
			method:'get',
			asycnhronous:false,
			parameters: {
				'g': 's',                   // Smart Ads
				'd': s_ads_destination,     // This is the site of the origin of the click
				'c': 1,                     // Boolean - This is a click (not an impression request)
				'm': i_id                   // This is the ID of the Member
			}
		}
	);
	return true;
}
