/**
 * @author Ecliptik.net
 */
alert = jAlert;//replace default alert function by a jquery plugin
var addthis_config =
{
   ui_language: "fr",
   username: "thecheat",
   ui_click: true
}
function login(F)
{
	$.ajax({
		type: "POST",
		url: "/login.html",
		data: $(F).serialize(),
		success: function(html){
			$('#profile').html(html);
		}
	});
}
function logout()
{
	$.ajax({
		type: "POST",
		url: "/logout.html",
		success: function(html){
			$('#profile').html(html);
			if(location.href.indexOf('myProfile')>-1)
				location.href = "/index.html";
		}
	});
}

function setUserSelect(el, elementId)
{
	$('#user_info, #ad_stats, #billing').hide();
	$('#'+elementId).show();
	$(el).siblings().css('text-decoration','none');
	$(el).css('text-decoration','underline');
	if(elementId=="ad_stats")
		showAdStats();
}

function showAdStats(year, month)
{
	var user_id = $("#user_id").attr("value");
	if(!year)
		var year = "";
	if(!month)
		var month = "";
	
	var query = "user_id="+user_id+"&year="+year+"&month="+month;
	$.ajax({
		type: "POST",
		url: "/ctrl_profile/showAdStats.html",
		data: query,
		beforeSend: function(){
			$("img.loading").show();
		},
		success: function(response){
			$("#ad_stats").html(response);
		},
		complete: function(){
			$("img.loading").hide();
		}
	});
}
function add_comment(F)
{
	var post_id = F.post_id.value;
	$.ajax({
		type: "POST",
		url: "/comment/"+post_id+".html",
		data: $(F).serialize(),
		success: function(xml){
			if($(xml).find(".comment").length==0)
			{
				$('#nyroModalContent').html(xml);
			}
			else
			{
				$.nyroModalRemove();
				$('#comments').append(xml);
			}
		}
	});
	return false;
}

function likeComment(intCommentId, counter)
{
	_scoreComment(intCommentId, counter, "likeComment");
}

function dislikeComment(intCommentId, counter)
{
	_scoreComment(intCommentId, counter, "dislikeComment");
}

function _scoreComment(intCommentId, counter, elem_class)
{
	$.ajax({
		type: "POST",
		url: "/"+elem_class+".html",
		data: "comment_id="+intCommentId+"&counter="+counter,
		success: function(response){
			$('#comment_'+intCommentId+' .'+elem_class).html(response);
		},
		error: function(XMLHttpRequest, textStatus, errorThrown){
			if(XMLHttpRequest.status==406) alert(XMLHttpRequest.responseText)
		}
	});
}

function addAlias()
{
	$.ajax({
			type: "POST",
			url: "/ctrl_profile/addAlias/",
			success: function(html){
				$('#alias_fieldset div').append(html);
				setAliasId();
			}
		});
}

function setAliasId()
{
	$('#alias_fieldset label').each(function(i){
		$(this).attr('for','email_alias_'+(i+1))
		$(this).html('alias #'+(i+1))
	});
	$('#alias_fieldset input').each(function(i){
		$(this).attr('id','email_alias_'+(i+1))
		$(this).html('alias #'+(i+1))
	})	
}

function removeAlias(el)
{
	$(el).prev('input').remove();
	$(el).prev('label').remove();
	$(el).prev('br').remove();
	$(el).remove();
	setAliasId();
}

function resizeText()
{
	var currentFontSize = $('body').css('font-size');
	var currentFontSizeNum = parseFloat(currentFontSize);
	var className = $('#font_size').attr('class');
	if(className == "big")
	{
		var newFontSize = currentFontSizeNum-2;
		$('#font_size').attr('class','normal')
		$.ajax({type: "POST",url: "/fontSize/normal"});
	}
	else
	{
		var newFontSize = currentFontSizeNum+2;
		$('#font_size').attr('class','big')
		$.ajax({type: "POST",url: "/fontSize/big"});
	}
	$('body').css('font-size', newFontSize);
	
	/* set the cookie font size */
    $.cookie('fontSize', newFontSize);
}

$(document).ready(function()
{
	if ($("#birthday").length > 0) {
		$("#birthday").datepicker({
			changeYear: true,
			dateFormat: 'yy-mm-dd',
			yearRange: '1910:2010'
		});
	}
	if ($("#description").length > 0)
	{
		CKEDITOR.replace('description',{ toolbar : 'Basic', width : 450, height : 150 });
		/*var oFCKeditor = new FCKeditor('description') ;
		oFCKeditor.Width = "570" ;
		oFCKeditor.Height = "200" ;
		oFCKeditor.BasePath = "/js/fckeditor/" ;
		oFCKeditor.Config["CustomConfigurationsPath"] = "/js/fckeditor/myconfig.js"  ;
		oFCKeditor.ToolbarSet = 'MyToolbar' ;
		oFCKeditor.ReplaceTextarea() ;*/
	}
	
	if($("#imageInput").length > 0)
	{
		$('#imageInput').uploadify({
			'uploader'  : '/uploadify/uploadify.swf',
			'script'    : '/ctrl_upload/upload',
			'cancelImg' : '/img/cancel.png',
			'scriptData': {'type': 'image', 'maxwidth': 500},
			'auto'      : true,
			'fileDesc'	: 'Only image files are allowed',
			'fileExt'	: '*.jpg;*.jpeg;*.png;*.gif;*.bmp',
			'sizeLimit'	: $("#image_upload").attr("class"),
			'buttonText': 'Parcourir',
			'folder'    : '/upload/tmp',
			'onComplete': function(event, queueID, fileObj, response, data){
				$('#image_upload').html(response);
				jQuery('#cropbox').load(function(){
					try {
						if(jQueryCropBox) 
							jQueryCropBox.destroy();
					}
					catch(e){}
					loadCropBox();
				});
				return true;
			}
		});
	}
	
	/* read the cookie to set the font size */
	var startSize = $.cookie('fontSize');
	var startSize = parseFloat(startSize);
	$('body').css('font-size', startSize);
	
	
});

function refreshPage(elts, settings)
{
	window.location.reload();
}

$(function() {
	$.nyroModalSettings({
		//minWidth: 500, // Minimum width
		//minHeight: 300 // Minimum height
		//endRemove: refreshPage
	});
});
