function loadStyle(url, media){
    if(!$("link[rel=stylesheet][href*='"+url+"']").size()){

   		var head = document.getElementsByTagName('head')[0];
		$(document.createElement('link'))
			.attr({type: 'text/css', href: url, rel: 'stylesheet', media: media})
			.appendTo(head);
	}
}
        function formatMonetary(datas){
            var result = 0.00;
            if ($.isArray(datas)){
                $.each(datas,function(i,n){
                    result += parseFloat(n);
                });
            }else result = datas;
            result = (Math.round(result*100)/100).toFixed(2);

            return result;
        }
var externalLink = function (){
    $('#container a:not(:has(img))').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).addClass('externalLink').attr('target','_blank');
    $('#container a').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target','_blank');
}

$(function(){


$("#loading")
	.ajaxStart(function(){
   		$(this).show();
	})
	.ajaxStop(function(){
   		$(this).hide();
    });

    var timeOut = null;
    $(".checknickname").live("keyup",function(e){
        var input = $(this);
        // when press the following key we need not to make any ajax request, you can customize it with your own way
			switch(e.keyCode)
			{
				//case 8:   //backspace
				case 9:		//tab
				case 13:	//enter
				case 16:	//shift
				case 17:	//ctrl
				case 18:	//alt
				case 19:	//pause/break
				case 20:	//caps lock
				case 27:	//escape
				case 33:	//page up
				case 34:	//page down
				case 35:	//end
				case 36:	//home
				case 37:	//left arrow
				case 38:	//up arrow
				case 39:	//right arrow
				case 40:	//down arrow
				case 45:	//insert
				//case 46: 	//delete
					return;
			}
        if (timeOut != null) clearTimeout(timeOut);
		timeOut = setTimeout(function(){

            $.ajax({
                url: "/ajax/member.ajax.php",
                data: "checkNickname="+$(input).val(),
                type: "POST",
                dataType: "json",
                success: function(o){
                   if (o.status=="ok"){
                        $(input).removeClass("taken");
                        $(input).parents("p").next(".warning").remove();
                        $(input).parents("form").find("input[type=submit]").removeAttr("disabled");

                   }else {

                        $(input).addClass("taken");
                        $(input).parents("form").find("input[type=submit]").attr("disabled","disabled");
                        $(input).parents("p").after("<p class=\"warning\">Ce pseudonyme est déjà pris !</p>");
                        if(o.msg)  $.gritter.add({title: "Envie cadeaux", text: o.msg});
                   }
                }
            });

        }, 1000);  // delay delay ajax request for 1000 milliseconds



    });


$('#login_form input[name=login], #login_form input[name=pass]').focus(function(){
    $(this).val('');
    return true;
});



$("input[type=submit].del").live("click",function(){return confirm("Êtes-vous sûr de vouloir continuer ?"); return false;});

$("table tr")
    .live("mouseover", function(){$(this).addClass("current");})
    .live("mouseout", function(){$(this).removeClass("current");});

externalLink();
/*
$('.listselect').each(function(i){
    var list = $(this);
    var current = $(this).find("a.current");
    console.debug(current.parent('li').html());
    var selectDiv = '<div class="listselect" id="'+$(this).attr('id')+'">'+$(this).find("a.current").parent('li').html()+"</div>";
    $(this).before(selectDiv).hide();
    console.debug($(selectDiv).find("a.current"));
    $(selectDiv).find("a.current").hover(function(){ $(list).show(); return false;}, function(){$(list).slideUp()});
});
*/

/*
$("#login").click(function(){
    var url = $(this).attr("href").replace(".html", "-fromAjax-1-contentOnly-1.html");
    $.colorbox({href:url});
    return false;
});
*/
$('.listselect').each(function(i){

    var list = $(this);
    var current = $(this).find("a.current").parent('li');
    $(list).after('<br class="clear"/>');
    $(current).clone().attr("class","selector").attr("href","#").prependTo($(this));
    $(this).find("li:not(.selector)").hide();
    //console.debug($(this).children("li a.selector"));
    $(this).find("li.selector").toggle(function(){
        $(list).children("li").show();
        return false;
    },
    function(){
        $(list).children("li:not(.selector)").hide();
        return false;
    });
    //$(this).find('li a.current').mouseover(function(){ $(this).parent('li').siblings('li').show(); });
    //$(this).mouseout(function(){ $(this).parent('li').siblings('li').hide(); });
});

$("img.vthumb").each(function(i,o){
$(o).css("background","url("+$(o).attr("src")+") no-repeat center");
$(o).css("width",$(o).width());
$(o).css("height",$(o).height());
$(o).attr("src","images/play_small.png");

});
$("h2[class*=title], h1[class*=title]").css("line-height","12px").wrapInner("<span>");

});
