    function getCookie(c_name){
      if (document.cookie.length>0)
        {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1)
          {
          c_start=c_start + c_name.length+1;
          c_end=document.cookie.indexOf(";",c_start);
          if (c_end==-1) c_end=document.cookie.length;
          return unescape(document.cookie.substring(c_start,c_end));
          }
        }
      return "";
    }
    /*
     * Function to publish a message to the users stream
     */     
     function pubToStream(){
      if( getCookie('pub_stream') == null || getCookie('pub_stream') == '' ){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+5);
        document.cookie = "pub_stream=sent;expires="+exdate;
        FB.api('/me', function(response) {
          var firstname = response.first_name;
          FB.ui(
           {
             method: 'stream.publish',
             attachment: {
               name: firstname + " skickade ett klirr till en vän!",
               description: (
                 'Skicka ett klirr till dina vänner så att de ska kunna byta ut sin Carlsberg Hof i glasflaska mot en miljövänligare PET-flaska – utan att gå miste om klirret. '
               ),
               href: 'http://klirrklirr.se'
             },
             action_links: [
               { text: 'Skicka ett klirr', href: 'http://klirrklirr.se' }
             ]
           }
          );
        });     
      }
     }

    /*
     * Lets the the user publish as message when they have sent an sms
     */     
    if( location.search == "?send=true" ){
      pubToStream();
    }
    
    /*
     *  AJAX submits forms
     */     
     $("form").live("submit", function(event) {
        event.preventDefault();
        var id = $(this).parent("div").attr("id");
        console.log( id );
        $.ajax({
           url: $(this).attr("action")+"&ajax=true",
           type: "post",
           data: $(this).serialize(),
           success: function( data ){
              if( id == "send-sms" ){
                if( $("#send-resp").length == 0 ){
                  $("#"+id+" h4").after("<p id='send-resp'>"+data+"</p>");
                }else{
                  $("#send-resp").text( data );
                }
                pubToStream();
              }else{
                $("#"+id).load("/ #"+id);
              }
           } 
        });
     });

    /*
     * Enables playing audio
     */     
    var params = {};
    params.wmode = 'transparent';
    var flashvarsa = {};
    var flashvarsb = {};
    var flashvarsc = {};
    flashvarsa.filepath = '/mp3/kompisklirret.mp3';
    flashvarsb.filepath = '/mp3/kapsylklirret.mp3';
    flashvarsc.filepath = '/mp3/pasklirret.mp3';
    swfobject.embedSWF("flash/player-free.swf", "play-kompisklirr", "76", "34", "9.0.0", "", flashvarsa, params);
    swfobject.embedSWF("flash/player-free.swf", "play-oppnarklirr", "76", "34", "9.0.0", "", flashvarsb, params);
    swfobject.embedSWF("flash/player-free.swf", "play-pasklirr", "76", "34", "9.0.0", "", flashvarsc, params);

    google.setOnLoadCallback(function() {
      $("a.video").colorbox({iframe:true, innerWidth:495, innerHeight:370, scrolling: false});
    });
    
    var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
    document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
    
    try {
    var pageTracker = _gat._getTracker("UA-15441286-1");
    pageTracker._trackPageview();
    } catch(err) {}
