function attachBanner(id, obj)
{
	screenWidth = screen.width ? screen.width : false;

	for(i = 0; i < obj.length; i++)
	{
		if(obj[i])
		{
			if(screenWidth >= parseInt(obj[i].minWidth) && screenWidth <= parseInt(obj[i].maxWidth))
			{
				$('#' + id).html($.urldecode(obj[i].bannersrc));
				poststr = '/ajax/metrics/' + obj[i].id + '/' + obj[i].rid;

				$.ajax({
					url: '/ajax/metrics/' + obj[i].id + '/' + obj[i].rid,
					dataType: 'json',
					success: function(data)
					{
						if(data.response === true)
						{
							if(typeof data.message === 'string')
							{
								alert(data.message);
							}
							if(typeof data.callback === 'string')
							{
								eval(data.callback);
							}
							if(typeof data.html === 'string')
							{
								$('#' + data.id).html($.urldecode(data.html));								
							}
						}

					}
				});
				//$.ajax('/ajax/metrics/' + obj[i].id + '/' + obj[i].rid, '', '$.jSON');
			}
		}
	}

}

function attachBanners(event)
{
	if(typeof(banners) != "undefined")
	{
		attachBanner('site_header_main_banner', banners);
	}

	if(typeof(bannerside) != "undefined")
	{
		attachBanner('sidebar_banners', bannerside);
	}

}

jQuery.extend({
	_utf8_decode : function() { // utf8_decode a string.
		var str 	= arguments[0];
		var string 	= '';
		var i		= 0;

		while(i < str.length)
		{
			c = str.charCodeAt(i);

			if(c < 128)
			{
				string += String.fromCharCode(c);
				i++;
			}
			else if ( (c > 191) && (c < 224) )
			{
				c2 = str.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else
			{
				c2 = str.charCodeAt(i+1);
				c3 = str.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		};

		return string;
	},

	urldecode : function() {
		var utf8str = this._utf8_decode(arguments[0]);
		var string	= '';
		var i = 0;

		while(i < utf8str.length)
		{
			c	= utf8str.charCodeAt(i);

			if(c == 0x2b)
			{
				string += ' ';
			}
			else if (c == 0x25)
			{

				string += unescape(utf8str.substr(i,3));
				i += 2;
			}
			else
			{
				string += String.fromCharCode(c);
			}
			i++;
		};

		return string;
	}
});



$(function() {
	$('a.en_fancy_gal').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false	
	});	
});

$(document).ready(function(){
	attachBanners();
});