function bling(){
                var audioElement = document.createElement('audio');
audioElement.volume=0.1;
audioElement.setAttribute('src', "/sound/son.wav");
audioElement.play();
}
var tchat = false;
var first = true;
$(function() {
    
		$( "#vchat" ).resizable({ minHeight: 200,minWidth: 200,stop: function() { setCookie("vchatdim",$(this).css("width")+"X"+$(this).css("height"),365); }  }).draggable({ handle: '#h-m',containment: "#cadre",stop: function() { setCookie("vchatposition",$(this).css("top")+"X"+$(this).css("left"),365); } });
                $('#subvChat').click(function(){
                                send();
                });
                $('#vchatmsgvChat').keyup(function(e) {
                            if(e.keyCode == 13) {
                                 send();
                             }
                });
                $('#consoletoggle').click(function(){
                    toogglevchat();
                });
                $('.zeta').live('hover', function(){
                    $(this).children('img').fadeToggle();
                });
                $('.vsuppr').live('click', function() {
                    $.post("/shoots/supprimer/"+$(this).attr('id'));
                    $(this).parent().parent('.vchatmsg').fadeOut();
                    refreshvchat();
                });
                $('#vchatcross').click(function(){
                    toogglevchat();
                });
                initChat();
	});

function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
function refreshvchat(){
$.post("/shoots/get",{shoot:$('#vchatmsgvChat').val()},function(data){
 if($("#lobby2").text() != data){
 $("#lobby2").text(data);
 $("#lobby").html(data);
if(!tchat){$('#consoletoggle').addClass('newmsg')}
else{bling();}
if($('#subvChat').attr('class')=='subvChatb'){$('#subvChat').removeClass('subvChatb').addClass('subvChat');}
}
});
}

function send(){
    txt = $('#vchatmsgvChat').val();
if(txt==''){return false;}

if($('#subvChat').attr('class')=='subvChatb'){ return false;}
else{$('#subvChat').removeClass('subvChat').addClass('subvChatb');}
                    if(txt==''){return false;}
                    var muted = '^:mute [A-z0-9=?!@:.-]+';
                    if(txt.match(muted)){
                        pseudo = txt.split(' ');
                        $.post("/shoots/mute",{n:pseudo[1]},function(data){alert(data);$('#vchatmsgvChat').val(''); if($('#subvChat').attr('class')=='subvChatb'){$('#subvChat').removeClass('subvChatb').addClass('subvChat');}});
                    }
                    else{
                        var unmuted = '^:unmute [A-z0-9=?!@:.-]+';
                        if(txt.match(unmuted)){
                            pseudo = txt.split(' ');
                            $.post("/shoots/unmute",{n:pseudo[1]},function(data){alert(data);$('#vchatmsgvChat').val(''); if($('#subvChat').attr('class')=='subvChatb'){$('#subvChat').removeClass('subvChatb').addClass('subvChat');}});
                        }
                        else{
                    $('#vchatmsgvChat').val('');
                    $.post("/shoots/ajouter",{shoot:txt},function(data){
                        if(data == "muted"){alert("Vous êtes muté(e)."); if($('#subvChat').attr('class')=='subvChatb'){$('#subvChat').removeClass('subvChatb').addClass('subvChat');}}
                     refreshvchat();
                     
                             });
                        }
                    }
     }


function toogglevchat(){
    if(first){vtimer = setInterval('refreshvchat()', 750); first=false;}
    if(tchat){
    $("#vchat").fadeOut();
     tchat = false;
     setCookie("vchatenable",0,365);
     return false;
    }
    else{
    $("#vchat").fadeIn();
            refreshvchat();
    tchat = true;
    $('#consoletoggle').removeClass('newmsg')
     setCookie("vchatenable",1,365);
     return false;
    }
}

function initChat(){
 var position = getCookie("vchatposition");
 if (position!=null && position!=""){
                position = position.split("X");
                $("#vchat").css({"top":position[0],"left":position[1]});
 }
 
  var dims = getCookie("vchatdim");
 if (dims!=null && dims!=""){
                dims = dims.split("X");
                $("#vchat").css({"width":dims[0],"height":dims[1]});
 }
  var onoff = getCookie("vchatenable");
 if (onoff!=null && onoff!=""){
      if(onoff == 1){
                toogglevchat();
      }
 }
 
 
}
