var notification = new Notification();

function clean(field, text){
	if ($("#" + field).val() == text ){
		$("#" + field).val('');
		
	}
}

function restore(field, text){
	if ($("#" + field).val() == '' ){
		writeState = 0;
		$("#" + field).val(text);
	} 
}

function futboleaSendReport( type, key ){
	var reason = $("#report_reason").val();	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/member_report.php",
		async: false,
		data: ({ key: key, type: type, reason: reason}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done"){
				$.facebox(futboleaOkBox(json.msg),'',0);
			} else {
				$.facebox(futboleaErrorBox(json.msg),'',0);
			}
			
			futboleaCloseBoxTimer();
		}
	});	
}

function futboleaInviteFan(fid){
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "ajax/member_invite_fan.php",
			async: false,
			data: ({ fid : fid }),	
			success: function(json){
				/* evaluate result */
				if ( json.result == "done") {
					$.facebox.settings.opacity = 0;
					$.facebox.settings.modal = false;
					$.facebox(futboleaOkBox(json.msg),'',0);
					$("#addFan-"+fid).remove();
				} else {
					$.facebox(futboleaErrorBox(json.msg),'',0);
				}
				
				futboleaCloseBoxTimer();
			}
		});	
}

function futboleaEvaluateThinking() {
//	$('#thinkingButton').toggleClass('disabled');
//	$('#thinkingButton').attr("href","javascript:futboleaUpdateThinking();");
}

function futboleaUpdateThinking()
{	
	var data_thinking = $('#thinkingInput').attr('value');
	data_thinking = data_thinking.substring(0,220);
	if(data_thinking != "" && writeState != 0){
		//disabled the wall content
		$('#thinkingInput').attr('disabled', true);
		$('#thinkingButton').toggleClass('disabled');
		$('#thinkingButton').attr("href","javascript:void(0)");
	
		var encoded = encodeURIComponent(data_thinking);
		$.ajax({
			type: "POST",
			dataType: "json",
			url: "ajax/update_thinking.php",
			data: ({
				data_thinking 	: encoded
			}),	
			success: function(json){
				/* evaluate result */
				if ( json.result == 'done'){
					$('#publishThinking').text(data_thinking).html(); // security!
					$('#publishThinking').html( $('#publishThinking').html() +'<span>'+json.date+' | <a href="javascript:futboleaDeleteThinking();">'+langVars['HOME_DELETE']+'</a> </span>');	
				}
				else
				{
					alert(json.result);
				}

				//enable the wall content
				$('#thinkingInput').removeAttr('disabled');

				//clean wall message
				$('#thinkingInput').val('');
				$('#thinkingButton').toggleClass('disabled');
				$('#thinkingButton').attr("href","javascript:futboleaUpdateThinking()");	
			}
		});
	}
}

function futboleaDeleteThinking()
{
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/update_thinking.php",
		data: ({
			mode : 1
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == 'done'){
				$('#publishThinking').html('');	
			}
			else
			{
				alert(json.result);
			}
		}
	});
}

function futboleaAddFriendBox(friendId){
	var HTML = '';
	$.facebox.settings.opacity = 0.5;
	$.facebox.settings.modal = true;
	futboleaLoadingBox();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: 'ajax/member_get_data.php',
		async: true,
		data: ({
			data_friend_id	: friendId
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				HTML += '<div id="image" class="popup">';
				HTML += '	<h4>'+langPopup['POPUP_ADD_FRIEND']+'</h4>';
				HTML += '	<div class="inner img">';
				HTML += '			<div class="avatar">';
				HTML += '			<p style="background-image: url(\'images/teams/crests/21x21/'+json.team_crest+'\');">';
				HTML += '				<a href="profile.php?pid='+json.member_id+'">';
				HTML += '					<img src="images/members/pictures/66x66/'+json.member_picture+'" width="66" height="66" alt="" />';
				HTML += '				</a>';
				HTML += '			</p>';
				HTML += '		</div>';
				HTML += '		<div class="inner"><p class="msg"><a href="profile.php?pid='+json.member_id+'" >'+json.member_name+' '+json.member_lastname+'</a> '+langPopup['POPUP_MUST_CONFIRM_REQUEST_BEFORE']+'</p></div>';
				HTML += '	</div>';
				HTML += '	<ul class="footer">';
				HTML += '		<li><a class="ok" href="javascript:futboleaAddFriend('+json.member_id+',2);">'+langPopup['POPUP_SEND_REQUEST']+'</a></li>';
				HTML += '		<li><a href="javascript:futboleaCloseBox();">'+langPopup['POPUP_CANCEL']+'</a></li>';
				HTML += '	</ul>';
				HTML += '</div>';
				$.facebox(HTML,'',1);
			}
			else
			{
				$.facebox(futboleaErrorBox(json.msg));
			}
		}
	});
}

function futboleaAddFanBox(teamId, currentTeamName, teamName, teamCountry, switchBtn){
	var HTML = '';
	
	HTML += '<div id="simpatizante" class="popup">' +
				'<h4>'+langPopup['POPUP_BECAME_FAN']+'</h4>' +
				'<div class="inner"><p>'+langPopup['POPUP_BECAME_FAN_OF']+' <b>' + teamName + '</b> <span>' + teamCountry +'</span> '+langPopup['POPUP_BECAME_FAN_OF_MUST_LEAVE']+' <b>' + currentTeamName + '</b> <span>' + teamCountry + '</span></p>' +
				'<p class="extra">&iquest;'+langPopup['POPUP_BECAME_WANT_LEAVE']+' ' +currentTeamName+ ' '+langPopup['POPUP_BECAME_WANT_LEAVE_AND_MAKE']+' ' +teamName+ '?</p>' +
				'</div>' +
				'<ul class="footer">' +
					'<li><a class="ok" href="javascript:futboleaAddFan(' +teamId+ ',\'\',\'\',1,'+switchBtn+');">'+langPopup['POPUP_BECAME_YES_WANT']+' ' + teamName+ '</a></li>' +
					'<li><a href="javascript:futboleaCloseBox();">'+langPopup['POPUP_CANCEL']+'</a></li>' +
				'</ul>' +
				'</p>' +
			'</div>';
	$.facebox(HTML,'', 1);
}

function futboleaCloseBox(){
	jQuery(document).trigger('close.facebox');
}

function futboleaCloseBoxTimer(){
	setTimeout(function(){
		jQuery(document).trigger('close.facebox');
	}, 1000);
}

function futboleaLoadingBox(){
	var html = '';
	
	html += '<div id="progress" class="popup">';
	html += '<p class="inner"><span class="progress">'+langPopup['POPUP_LOADING']+'</span></p>';
	html += '</div>';
	
	$.facebox(html);
}

function futboleaLoadingBoxNew(){
	var html = '';
	
	html += '<div id="progress" class="popup">';
	html += '<p class="inner"><span class="progress">'+langPopup['POPUP_LOADING']+'</span></p>';
	html += '</div>';
	
	$.facebox(html,'',1);
}

function futboleaOkBox(msg){
	var html = '';
	
	html += '<div id="msgok" class="popup">'
	//html += '	<h4></h4>'
	html += '	<p class="success">'+msg+'</p>'
	html += '</div>'
	
	return html;
}

function futboleaErrorBox(msg){
	var html = '';
	
	html += '<div id="error" class="popup alternative">'
	html += '	<h4>Error</h4>'
	html += '	<p class="warning">'+msg+'</p>'
	html += '	<div class="close"><a href="javascript:futboleaCloseBox();">'+langPopup['POPUP_CLOSE_WINDOW']+'</a></div>'
	html += '</div>'

	return html;
}

function futboleaNewReportBox(type, key ) {
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/report_getbox.php",
		data: ({ type : type, key: key}),	
		success: function(data){
			/* evaluate result */
			if ( data.result == 'done') {
				$.facebox(data.html);	
			}
		}
	});
}

function futboleaAddFriend( friendId, mode ){
	var page;
	
	if (mode == 2){
		futboleaLoadingBoxNew();
		page = "ajax/member_add_friend.php" ;
	}else{
		futboleaLoadingBox();
		page = "ajax/member_accept_friend.php" ;
	}
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: page,
		async: true,
		data: ({
			data_friend_id	: friendId
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				$.facebox.settings.opacity = 0;
				$.facebox.settings.modal = false;
				if ( mode == 2 ){
					// new friend
					$.facebox(futboleaOkBox(json.msg),'',0);
					$("#f-"+friendId).after('<span>'+langPopup['POPUP_WAITING_CONFIRMATION']+'</span>');
					$("#f-"+friendId).remove();
				}else{
					$.facebox(futboleaOkBox(json.msg),'',0);					
					// friend accepted
					$("#f-"+friendId).remove(); 
				}
			}
			else
			{
				$.facebox(futboleaErrorBox(json.msg));
			}
			
			futboleaCloseBoxTimer();
		}
	});
}

function futboleaAddFan( teamId, teamName, teamCountry, mode, switchBtn ) {
	$.facebox.settings.opacity = 0.5;
	$.facebox.settings.modal = true;
	futboleaLoadingBox();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/member_add_fan.php",
		async: true,
		data: ({
			tid	: teamId,
			override: mode
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				$.facebox.settings.opacity = 0;
				$.facebox.settings.modal = false;
				$.facebox(futboleaOkBox(json.msg),'', mode);
				$("#addFan-" +teamId).hide();
				futboleaCloseBoxTimer();
				if ( switchBtn == true ) {
						$("#addFan-" +teamId).after('<a id="rmvFan-'+teamId+'" href="javascript:futboleaRemoveFan('+teamId+',\''+teamName.replace("'","\\'")+'\', \''+teamCountry.replace("'","\\'")+'\', '+mode+');" class="ico_remove">'+langPopup['POPUP_LEAVE_FAN']+'</a>');
				} else {
					$("#addFan-" +teamId).parent().remove();
				}
					
				$("#addFan-" +teamId).remove();
			}
			else
			{
				if ( json.result == 'support_error' ) {
					$.facebox(futboleaErrorBox(json.msg));
				}
				else if ( json.result == 'fan_error' ) {
					futboleaAddFanBox(teamId, json.team_name, teamName, teamCountry, switchBtn);					
				}
			}
		}
	});	
}

function futboleaRemoveFan(teamId, teamName, countryName, deleteMode) {
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/member_remove_fan.php",
		async: false,
		data: ({ tid : teamId }),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				$.facebox.settings.opacity = 0;
				$.facebox.settings.modal = false;
				$.facebox(futboleaOkBox(json.msg),'', 0);
				futboleaCloseBoxTimer();
				if (deleteMode == 1) {
					offset = 0;
					showPage();
				}else{
					$("#rmvFan-" +teamId).hide();
					$("#rmvFan-" +teamId).after('<a id="addFan-'+teamId+'" href="javascript:futboleaAddFan('+teamId+',\''+teamName.replace("'","\\'")+'\', \''+countryName.replace("'","\\'")+'\', 0, true);" class="squadButton ico_follower">'+langPopup['POPUP_BECAME_FAN']+'</a>');
					$("#rmvFan-" +teamId).remove();
				}
			}
			else
			{
				$.facebox(futboleaErrorBox(json.msg));
				futboleaCloseBoxTimer();
			}
		}
	});	
}

function futboleaRemoveFriend(friendId, deleteMode)  
{
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/member_remove_friend.php",
		async: false,
		data: ({
			data_friend_id	: friendId
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				$.facebox.settings.opacity = 0;
				$.facebox.settings.modal = false;
				$.facebox(futboleaOkBox(json.msg),'', 1);
				
				if (deleteMode == 1) {
					offset = 0;
					showPage();
				}else{
					$("#rf-" +friendId).remove();
					document.location.href = "profile.php?pid="+friendId;
				}
			}
			else
			{
				$.facebox(futboleaErrorBox(json.msg));
				futboleaCloseBoxTimer();
			}
		}
	});	
}

function futboleaRemoveFriendBox(friendId, deleteMode){
	var HTML = '';
	futboleaLoadingBox();
	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: 'ajax/member_get_data.php',
		async: false,
		data: ({
			data_friend_id	: friendId
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done")
			{
				HTML += '<div id="image" class="popup">';
				HTML += '	<h4>'+langPopup['POPUP_DELETE_FRIEND']+'</h4>';
				HTML += '	<div class="inner img">';
				HTML += '			<div class="avatar">';
				HTML += '			<p style="background-image: url(\'images/teams/crests/21x21/'+json.team_crest+'\');">';
				HTML += '				<a href="profile.php?pid='+json.member_id+'">';
				HTML += '					<img src="images/members/pictures/66x66/'+json.member_picture+'" width="66" height="66" alt="" />';
				HTML += '				</a>';
				HTML += '			</p>';
				HTML += '		</div>';
				HTML += '		<div class="inner"><p class="msg">'+langPopup['POPUP_ARE_SURE_REMOVE_FRIEND']+' <a href="profile.php?pid='+json.member_id+'" >'+json.member_name+' '+json.member_lastname+'</a> '+langPopup['POPUP_ARE_SURE_REMOVE_FRIEND_OF_LIST']+'.</p></div>';
				HTML += '	</div>';
				HTML += '	<ul class="footer">';
				HTML += '		<li><a class="ok" href="javascript:futboleaRemoveFriend('+json.member_id+','+deleteMode+');">'+langPopup['POPUP_DELETE_FRIEND']+'</a></li>';
				HTML += '		<li><a href="javascript:futboleaCloseBox();">'+langPopup['POPUP_CANCEL']+'</a></li>';
				HTML += '	</ul>';
				HTML += '</div>';
				$.facebox(HTML,'',1);
			}
			else
			{
				$.facebox(futboleaErrorBox(json.msg));
			}
		}
	});
}

function showTeamInfo(mode, wallType)
{
	if ( wallType == 2 || wallType == 5 ){
		if ( mode == 'Wide_Wall' ) {
			// show wall
			$("#Wide_History").hide();
			$("#Wide_Wall").show();
			$("#lwall").toggleClass('current');
			$("#lhis").toggleClass('current');		
		} else {
			//show history
			$("#Wide_Wall").hide();
			$("#Wide_History").show();
			$("#lwall").toggleClass('current');
			$("#lhis").toggleClass('current');
		}
	}
}

function email_validation(email){
        var b=/^[^@\s]+@[^@\.\s]+(\.[^@\.\s]+)+$/
        return b.test(email)
}

function validateEmail(elementValue){  
    var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
    return emailPattern.test(elementValue);  
}

function futboleaLogin() {
	var formOk = true;
	
	if ( email_validation($("#lgn_email").val()) == false ){
		formOk = false;
	}
	if ( $("#lgn_password").val().length < 3 ){
		formOk = false;
	}
	if (formOk == true){
		$("#login").submit();
	}
}

function futboleaLoginchangeSelected(val) {
	
		$("#"+val).toggleClass("selected");
		if($("#"+val).is(".selected")){
			$("#"+val).css({"font-style":"normal", "color":"#000"});
		}else{
			if($("#"+val).val() == '')
				$("#"+val).css({"font-style":"italic", "color":"#999"});
		}
};

/* REGISTER */
function futboleaRegisterToStepOne(){
	var formOk = true;
	
	if ( email_validation($("#reg_email").val()) == false ){
		formOk = false;
	}else{
		$("#reg_email_error").hide();
	}
	if ( $("#reg_password").val().length < 6 || $("#reg_password").val() == langPopup['POPUP_MIN_CHARACTERS']){
		formOk = false;
	}else{
		$("#reg_password_error").hide();
	}
	
	if (formOk == true){
		$("#register").submit();
	}
}

function futboleaWelcomeDeleteMessage(messageType)
{	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/home_delete_message.php",
		data: ({
			data_home_type_message		: messageType
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done"){
			//	alert("#"+messageType);
				$("#welcome_"+messageType).fadeOut(500);
			}
			else
			{
				alert(json.result);
			}
		}
	});
}

function closePromo()
{	
	$.ajax({
		type: "POST",
		dataType: "json",
		url: "ajax/home_delete_message.php",
		data: ({
			data_home_type_message : 'pronostic_promo'
		}),	
		success: function(json){
			/* evaluate result */
			if ( json.result == "done"){
			//	alert("#"+messageType);
				$("#PencaPromo").fadeOut(500);
			}
			else
			{
				alert(json.result);
			}
		}
	});
}

var writeState = 0;
var e;
function blurCheck (target, value) {
   e = setTimeout (	function() {
	$("#WallInput").val(value);   
	}, 100);
}
function clearTimeoutBlurCheck () {
   clearTimeout(e);
}
function changeWriteStatus (target) {
	
	if(target.value.length > 0){
		writeState = 1;
	}
	else
		writeState = 0;
}

var searchStatus = false, searchGenericStatus = false; 

function changeSearchStatus (target) {
	if(target.value.length > 0){
		searchStatus = true;
	} else {
		searchStatus = false;
	}
}

function changeGenericSearchStatus (target) {
	if(target.value.length > 0){
		searchGenericStatus = true;
	} else {
		searchGenericStatus = false;
	}
}

function futboleaGenericSearch() {
	if ( $('#generic_search').val() != "" && searchGenericStatus == true) {
		$.doPost('find.php', {sq: encodeURIComponent($('#generic_search').val())})
	}
}
function futboleaGenericSearchPage() {
	if ( $('#generic_search_page').val() != "" && searchStatus == true) {
		$.doPost('find.php', {sq: encodeURIComponent($('#generic_search_page').val())})
	}
}

$(function(){
    $("#pageNext").click(function(){
		futboleaChangePage('next');
	});
});
		
$(document).ready(function(){
	
	//$(document).pngFix();
	
    $.extend({
        doGet: function(url, params) {
            document.location = url + '?' + $.param(params);
        },
        doPost: function(url, params) {
        	var $form = $("<form>")
                .attr("method", "post")
                .attr("action", url);
            $.each(params, function(name, value) {
                $("<input type='hidden'>")
                    .attr("name", name)
                    .attr("value", value)
                    .appendTo($form);
            });
            $form.appendTo("body");
            $form.submit();
        }
    });
 
	$("#pageNext").attr("style", "cursor: pointer;");
	
	// If an event gets to the body
	$("body").click(function(){
		$("#slNotificationContent").hide();
		$("#slNotificationTab").removeClass('open');
		$("#slNotificationTab").addClass('close');
		notification.status = 'close';
		
		$("#MailNotificationContent").hide();
		$("#MailNotificationTab").removeClass('open');
		$("#MailNotificationTab").addClass('close');
		pm.status = 'close';
	});

	// Prevent events from getting pass .popup
	$("#slNotificationContent").click(function(e){
		e.stopPropagation();
	});
	
	// Prevent events from getting pass .popup
	$("#slNotificationTab").click(function(e){
		e.stopPropagation();
	});

	// Prevent events from getting pass .popup
	$("#MailNotificationContent").click(function(e){
		e.stopPropagation();
	});
	
	// Prevent events from getting pass .popup
	$("#MailNotificationTab").click(function(e){
		e.stopPropagation();
	});
});
