﻿// Simple JavaScript Templating
(function () {
	var cache = {};
	this.tmpl = function tmpl(str, data) {
		var fn = !/\W/.test(str) ? cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) : new Function("obj", "var p=[],print=function(){p.push.apply(p,arguments);};" + "with(obj){p.push('" + str.replace(/\s*<!\[CDATA\[|\]\]>\s*/g, '').replace(/[\r\t\n]/g, " ").replace(/'(?=[^%]*#>)/g, "\t").split("'").join("\\'").split("\t").join("'").replace(/<#=(.+?)#>/g, "',$1,'").split("<#").join("');").split("#>").join("p.push('") + "');}return p.join('');");
		return data ? fn(data) : fn;
	};
})();

// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function (from, to) {
	var rest = this.slice((to || from) + 1 || this.length);
	this.length = from < 0 ? this.length + from : from;
	return this.push.apply(this, rest);
};
// Html for the ajax loader
(function($) {
	$.fn.showAjaxLoader = function(settings) {
		var config = {'text': 'Loading data...'};
 		if (settings) $.extend(config, settings);

		this.each(function() { $(this).html('<img src="/ui/img/load16.gif" alt="[Loading data...]" /> ' + config.text); });
		return this;
	};
})(jQuery);
// Html for the user avatar

jQuery.avatars = function() {
		
		$('body').append('<div id="userMenu" class="quickmenu">'
			+ '<div class="header">User menu</div>'
			+ '<div class="menu">'
			+ '<div class="clearfix line"><a href="javascript:void(0);" id="userMenuProfileLink"><span class="icon profile"></span>&nbsp;&nbsp;User Profile</a></div>'
			//+ '<div class="clearfix line"><a href="javascript:void(0);" id="userMenuPMLink"><span class="icon mail"></span>&nbsp;&nbsp;Send PM </a></div>'
			+ '<div class="clearfix"><a href="javascript:void(0);" id="userMenuGalleryLink"><span class="icon gallery"></span>&nbsp;&nbsp;Gallery</a></div>'
			//+ '<div class="clearfix"><a href="javascript:void(0);" id="userMenuAddFriendLink"><span class="icon add"></span>&nbsp;&nbsp;Add as friend</a></div>'
			+ '</div>'
			+ '</div>');

		$(".toolsandinfo img.countryIcon, .toolsandinfo img.mail")
			.each(function () {
				$(this).attr('title', String($(this).attr('alt')).replace(/\s-\s/g, '<br />'));
			})
			.tooltip({
				tip: '.tooltip',
				effect: 'fade',
				fadeOutSpeed: 100,
				predelay: 400,
				position: "bottom right",
				offset: [10, -10]
			});


		$('.avatar .menu')
			.bind('mouseenter', function () { $(this).addClass('selected'); })
			.bind('mouseleave', function () { $(this).removeClass('selected'); })

		$('#userMenuProfileLink').bind('click', function () { location.href = '/member/' + _selectedAvatarName + '/profile.aspx'; });
		$('#userMenuPMLink').bind('click', function () { location.href = '/member/' + _selectedAvatarName + '/send-pm.aspx'; });
		$('#userMenuGalleryLink').bind('click', function () { location.href = '/member/' + _selectedAvatarName + '/gallery.aspx'; });
		$('#userMenuAddFriendLink').bind('click', function () { location.href = '/member/' + _selectedAvatarName + '/add-as-friend.aspx'; });

		$(".toolsandinfo img.menu")
			.tooltip({
				events: { def: 'click mouseenter, mouseout' },
				tip: '#userMenu',
				effect: 'fade',
				fadeOutSpeed: 400,
				predelay: 0,
				position: "bottom right",
				offset: [0, -16],
				onShow: function () {
					_selectedAvatarName = String(this.getTrigger().closest('.avatar').attr('id')).replace('avatar_', '');
				}
			});
	};

	jQuery.googleAds = function() {
		// Ads serving...
		$('div.banner').each(function () {
			var id = $(this).attr('id').replace(/_hidden/, '');
			var pos = $("#" + id).position();

			if (pos) { // Show the banner directly over the placeholder
				$(this).css( { "left": pos.left + "px", "top": pos.top + "px" });
				$("#" + id).height($(this).height() + 5);
				$("#" + id).width($(this).width());
			}
		});

		// Repositioning the ads divs
		$(window).scroll(function () {
			$('div.banner').each(function () {
				var id = $(this).attr('id').replace(/_hidden/, '');
				var pos = $("#" + id).position();

				if (pos) { // Show the banner directly over the placeholder
					$(this).css( { "left": pos.left + "px", "top": pos.top + "px" });
				}
			});
		});
      };
