$(document).ready(function(){
	$("a.make_a_banner_link").click(tellToLoginIfNotLogged);
	
	$("#inlineMessages a.close").click(function(){
		hideInlineMessage();
		return false;
	});
	
	$.ajaxSetup({
		url: "/ajax.php",
		global: false,
		type: "get",
		dataType: "json"
	});
});

function showLoginError(err, color){
	$("#tlerr").remove();
	lf = $("#st-login-form"); 
	c = $("<div />").addClass("im").attr({id: 'tlerr'});
	l = lf.position().left; t = lf.position().top + lf.innerHeight() + 5;
	if (typeof color == 'undefined'){
		color = 'red';
	}
	c.css({position: 'absolute', top: t+'px', left: l+'px', color: color, width: '220px'});
	c.html(err).hide();
	lf.after(c); c.fadeIn("fast");
}

function getLoadingImage()
{
	$loadingImg = $('<img />').attr({src: CDNPATH+'/images/loading.gif'});
	return $loadingImg; 
}

function handleErrors(code, res){
	showErrorMessage(res);
}

var $imTimeout;
var imInited = false;
function showInlineMessage($message, $type, $right, $time, $callback)
{
	if (!imInited){
		initMessages();
		imInited = true;
	}
	
	if ($type == 'err'){
		c = $("#inlineMessages .err");
	} else {
		c = $("#inlineMessages .not");
		if ($.userApi.cookie('ignallguidlines')){
			return;
		}
	}
	clearTimeout($imTimeout);
	c.find("div.right").html($right);
	c.find("div.left").html($message);
	
	if (typeof $message == 'object'){
		c.find("div.left").html($message);
		$message.show();
	} else {
		c.find("div.left").html($message);
	}
	
	if (c.css("display") == 'none'){
		c.slideDown(200);
	}
	c.find("a.close").click(_closeInlineMessage);
	if (c.find("a.ignore_all").length == 1){
		c.find("a.ignore_all").click(_ignoreAllGuidlines);
	}
	if (parseInt($time) > 0){
		$imTimeout = setTimeout("hideInlineMessage()",$time);
	}
	if ($(window).scrollTop() > $("#inlineMessages").position().top){
		$('html,body').animate({scrollTop: $("#inlineMessages").offset().top}, 300);
	}
	
	if (typeof $callback == 'function'){
		$callback(im);
	}
}
function _ignoreAllGuidlines(){
	$.userApi.cookie('ignallguidlines', true, { expires: 10*365, path: '/' });
	hideInlineMessage($(this).parent().parent().parent().attr("class"), true);
	return false;
}
function _closeInlineMessage(){
	hideInlineMessage($(this).parent().parent().parent().attr("class"), true);
	return false;
}

function initMessages(){
	if ($("#inlineMessages").length >= 1){
		im = $("#inlineMessages"); 
	} else {
		im = $("<div />").attr({id: 'inlineMessages'});
		$("#master").before(im);
	}
	
	cont = $("<div />").addClass("cont");
	cont.append($("<div />").addClass("left"));
	cont.append($("<div />").addClass("right"));
	cont.append($("<br />").addClass("clear"));
	contErr = cont.clone();
	not = $("<div />").addClass("not").append(cont);
	err = $("<div />").addClass("err").append(contErr);
	im.html("");
	im.append(not).append(err);
}

function hideInlineMessage(t,userClicked)
{
	if (t && t == 'not') {
		if (typeof onGuidlineClose == 'function'){
			r = onGuidlineClose($("#inlineMessages .not"), userClicked);
			if (r == true){
				$("#inlineMessages .not").slideUp(200);
			}
		} else {
			$("#inlineMessages .not").slideUp(200);
		}
	} else {
		if (typeof onNoticeClose == 'function'){
			r = onNoticeClose($("#inlineMessages .not"), userClicked);
			if (r == true){
				$("#inlineMessages .not").slideUp(200);
			}
		} else {
			$("#inlineMessages .err").slideUp(200);
		}
	}
}
function hideGuidline()
{
	hideInlineMessage('not');
}
function showNotice(message, timeout, callback)
{
	showErrorMessage(message, timeout, callback);
}
function hideNotice()
{
	hideInlineMessage('err');
}
function hideAllMessages()
{
	hideInlineMessage('not');
	hideInlineMessage('err');
}
if (typeof TEXTS == 'undefined'){
	TEXTS = {};
}
function showErrorMessage(message, timeout, callback)
{
	var tdismiss =  TEXTS.notice_dismiss || 'Dismiss';
	right = $("<a>").addClass("close").text(tdismiss).attr({href: '#'});
	showInlineMessage(message, 'err', right, timeout, callback);
}

function showMessage(message, timeout, callback)
{
	var tdismiss = TEXTS.notice_dismiss || 'Dismiss';
	var tignore_all = TEXTS.notice_ignore_all || 'Ignore all';
	
	right = '<a href="#" class="close">' + tdismiss + '</a> <span style="color: #D2D2D2">|</span> <a href="#" class="ignore_all">' + tignore_all + '</a>'; 
	showInlineMessage(message, 'not', right, timeout, callback);
}

function validEmail(email)
{
	var regMail = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (regMail.test(email)){
		return true;
	}
	return false;
}

function validScreenName(name)
{
	var regName = /^[0-9A-Za-z_\- ]+$/;
	if (regName.test(name)){
		return true;
	}
	return false;
}

function aUA_onError(err){
	showErrorMessage(err);
}
function aUA_onNotice(err){
	showMessage(err);
}
function aUA_onHideError(){
	hideInlineMessage();
}

UAjustRegistered = false;
function UA_onRegister(user)
{
	UAjustRegistered = true;
}

function UA_onRegisterContinue()
{
	if (LOGGED == true){
		if (goToPageAfterLogin != ''){
			location.href = goToPageAfterLogin; 
		}
	} else {
		location.href = '/';
	}
}

function UA_onLogout()
{
	$.ajax({
		data: {page: 'logout'},
		success: function(){
			LOGGED = false;
			if (typeof(UA_onLogout2) == 'function') {
				UA_onLogout2();
			}
			if (typeof REQUIRE_LOGGED_IN != 'undefined' && REQUIRE_LOGGED_IN){
				location.href = $.userApi.getLangPre();
			}
		}
	});
}
function UA_onLogin(sId)
{
	$.ajax({
		data: {page: 'login', sessionId: sId},
		success: function(){
			LOGGED = true;
			if (goToPageAfterLogin != '' && !UAjustRegistered) {
				location.href = goToPageAfterLogin;
			}
		}
	});
}
function UA_ifLogged(sId)
{
	UA_onLogin(sId);
}

function tellToLoginIfNotLogged()
{
	if (!LOGGED){
		location.href = $.userApi.loginPage + '?next=' + $(this).attr("href"); 
		return false;
	}
}

function doActionOnBuyPoints()
{
	$.userApi.buyPoints();
}



function hasFlash()
{
	var plugin = (navigator.mimeTypes && navigator.mimeTypes["application/x-shockwave-flash"]) ? navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin : 0;
	if (plugin){
		return true;
	}
	try {
		var a = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
		if (a){
			return true;
			}
	} catch(e) {}
	return false;
}


