$(document).ready(function(){
    var vote_url = '';

    if (!('placeholder' in document.createElement('input'))) {
      $('input[placeholder]').each(function(i, el) {
        el = $(el);
        el.attr('value', el.attr('placeholder'));
        el.focusin(function() {
          if (el.attr('value') === el.attr('placeholder')) {
            el.attr('value', '');
          }
        });
        el.focusout(function() {
          if (el.attr('value') === '') {
            el.attr('value', el.attr('placeholder'));
          }
        });
      });
    }

    $('a.vote_glid').click(function(e){
        vote_url =  $(this).attr("href");
        $.get( vote_url +'?' + Math.round(new Date().getTime()), {},
            function(data){
                $('#lightAlert').html(data.message);
                $('#fadeAlert').show();
                $('#lightAlert').show();
                if (data.success || data.message == 'Vous avez déjà votez') {
                    $(this).remove();
                }
                if (data.message == 'Vous n\'êtes pas connecté.') 
                {
                    var csrftoken = getCookie('csrftoken');
                    $('#lightAlert').css({'width': '300px', 'height': 'auto', 'top': '30%'});
                    $('#lightAlert').html('<h3>Se connecter</h3><div id="vote_glid_error"></div><form method="post" id="login_form" action="/glid/signin/"><input type="hidden" name="csrfmiddlewaretoken" value="'+ csrftoken +'" /><input type="text" value="" placeholder="E-mail ou Nom d\'utilisateur" name="identification" id="email_2" class="validate[required] placeholder"><input type="password" value="" placeholder="Mot de passe" name="password" id="passe_2" class="validate[required] placeholder"><label for="id_remember_me"><input id="id_remember_me" type="checkbox" class="required column" name="remember_me" /> Se souvenir de moi pendant un mois </label><input type="submit" name="go" class="submit" value="Connexion" /></form><div style="text-align:left; color: #fff;"><a style="color: #fff;" href="/glid/signup/">DEVENIR GLIDER</a>');
                    $('#email_2').focus();
                }
                
            }, 'json');
        e.preventDefault();
    });

    $(document).keyup(function(e) {
        if (e.keyCode == 27) { 
            $('#fadeAlert').hide();
            $('#lightAlert').hide();
        }
    })
    
    $('#fadeAlert').click(function(e){
        $('#fadeAlert').hide();
        $('#lightAlert').hide();
        e.preventDefault();
    });

    $('#social img').hover(function() { 
        $(this).animate({ marginTop: '-5px' }, 200);
    }, function() { 
        $(this).animate({ marginTop: 0 }, 200);
    });

    $("#nav li > a").click(function(e)
    {
        var v = $(this).attr('class');
        
        if( $(".box-"+v).is(":visible") ) 
         {
             $(".box-"+v).slideUp("slow");

           }
           else
           {
               $('.dropdown').hide(); 
             $(".box-"+v).slideDown("slow");

           }
        //$(".box-"+v).toggle("slow");
        e.preventDefault();
    });

    $(".submit").live("click", function(){
        //$('.error').hide();
        var email = $("#email_2").val();
        if (email == "") {
            $("#email_2").focus();
            return false;
        }
        var passe = $("input#passe_2").val();
        if (passe == "") {
            $("input#passe_2").focus();
            return false;
        }
        var csrftoken = getCookie('csrftoken');
        $.post("/glid/signin/", { identification: email, password: passe, remember_me: $("#remember_me_2").val(), csrfmiddlewaretoken: csrftoken},  
        function(data) {
            if (data.success || data.message == 'Loggedin') {
                    $.get(vote_url +'?' + Math.round(new Date().getTime()), {},
                        function(data){
                            $('#lightAlert').css({'width': '300px', 'height': 'auto', 'top': '30%', 'font-size':'15px'});
                            $('#lightAlert').html(data.message);
                            $('#fadeAlert').show();
                            $('#lightAlert').show();
                            if (data.succes || data.message == 'Vous avez déjà votez') {
                                $(this).remove();
                            }
                        }, 'json');
            }
        }, 'json');
        return false;
    });

    $('a.agenda_nav').live("click", function(e){
        vote_url =  $(this).attr("href");
        $.get( vote_url +'?' + Math.round(new Date().getTime()), {},
            function(data){
                $('#calendar_content').html(data);
            });
        e.preventDefault();
    });
    
    $('.tab').click(function(e){
        var element = $(this);
        $('.tab').removeClass('select_agenda_nav')
        $('#agenda_content_ajax').html('<div style="height: 200px; width: 200px; padding-top: 160px;padding-left: 300px;"><img src="/media/img/loading.gif" /></div>');
        $.get(element.attr("rel"),function(msg){
            $('#agenda_content_ajax').html(msg);
            element.addClass('select_agenda_nav');
        });
        e.preventDefault();
    })
    
    $('.ajax_link').live("click", function(e){
        var element = $(this);
        $('#agenda_content_ajax').html('<div style="height: 200px; width: 200px; padding-top: 160px;padding-left: 300px;"><img src="/media/img/loading.gif" /></div>');
        $.get(element.attr("rel"),function(msg){
            $('#agenda_content_ajax').html(msg);
            element.addClass('select_agenda_nav');
        });
        e.preventDefault();
    })

});

function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

/* PLAYER */
function listen_player(url) {
    newwindow=window.open(url,'HIT RADIO PLAYER','height=523,width=900');
    if (window.focus) {newwindow.focus()}
    return false;
}

nowplaying();
var nowplaying_song = -1;
function nowplaying() {
    $.getJSON('http://player.hitradio.ma/media/logs/hit_nowplaying.json?' + Math.round(new Date().getTime()), function(data) {
        if(data.titre != nowplaying_song)
        {
            nowplaying_song = data.titre;
            runtime = data.runtime;
            $("#titre").fadeOut(500,function()
            {
                $("#titre").empty();
                $("#titre").html(data.titre);
                $("#titre").fadeIn(500);
            });
            $("#artiste").fadeOut(500,function()
            {
                $("#artiste").empty();
                $("#artiste").html(data.artiste);
                $("#artiste").fadeIn(500);
            });
            $("#pochette").fadeOut(500,function()
            {
                $("#pochette").empty();
                $("#pochette").html('<img src="'+ data.pochette +'" alt="En ce moment '+data.guid+'-'+ data.titre +'" width="60" height="60"/>');
                $("#pochette").fadeIn(500);
            });
        }
    
    });
    setTimeout(function(){nowplaying();},75*1000);
}


