var targetElement = 'countdown';
var targetTimeElement = 'cdownnum';
var targetTextElement = 'cdowntxt';
var countdownText = 'Valget avsluttes om';



$(document).ready( function () {
  countdown_clock(2012,1,6,12,00,1);
  $('#'+targetElement).html('<div id="cdowntxt"></div><div id="cdownnum"></div>');
  $('#'+targetElement).bind('click', function() { document.location.href="http://valg.kabin.no"});
  $('#'+targetElement).css('cursor', 'pointer');
})


function countdown_clock(year, month, day, hour, minute, format)
         {
         //I chose a div as the container for the timer, but
         //it can be an input tag inside a form, or anything
         //who's displayed content can be changed through
         //client-side scripting.
         

         Today = new Date();
         Todays_Year = Today.getFullYear();
         Todays_Month = Today.getMonth();

         //Computes the time difference between the client computer and the server.
         
         Server_Date_full = (new Date(2012, 1, 23,
                                 2, 21, 13));
        Server_Date = Server_Date_full.getTime();
         Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                                 Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
                                 
			// alert( Todays_Year+ ' ' +Todays_Month+ ' ' +Today.getDate() + ' ' + Today.getHours()+ ' ' +Today.getMinutes()+ ' ' +Today.getSeconds());

         countdown(year, month, day, hour, minute, (Todays_Date - Server_Date), format);
         }

function countdown(year, month, day, hour, minute, time_difference, format)
 {
 Today = new Date();
 Todays_Year = Today.getFullYear();
 Todays_Month = Today.getMonth();

 //Convert today's date and the target date into miliseconds.

 Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                         Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
 Target_Date = (new Date(year, month, day, hour, minute, 00)).getTime();

 //Find their difference, and convert that into seconds.
 //Taking into account the time differential between the client computer and the server.
 Time_Left = Math.round((Target_Date - Todays_Date + time_difference) / 1000);

 if(Time_Left < 0)
    Time_Left = 0;
  
  if(Time_Left == 0)
  {
    $('#'+ targetTimeElement).text('');
    $('#'+ targetTextElement).text( 'Valget er avsluttet');
  }
  else
  {
    switch(format)
    {
       case 0:
            //The simplest way to display the time left.
            $('#'+targetTimeElement).text( Time_Left + ' sekunder');
            break;
       case 1:
            //More datailed.
            days = Math.floor(Time_Left / (60 * 60 * 24));
            Time_Left %= (60 * 60 * 24);
            hours = Math.floor(Time_Left / (60 * 60));
            Time_Left %= (60 * 60);
            minutes = Math.floor(Time_Left / 60);
            Time_Left %= 60;
            seconds = Time_Left;

            dps = 'er'; hps = 'r'; mps = 'er'; sps = 'er';
            //ps is short for plural suffix.
            if(days == 1) dps ='';
            if(hours == 1) hps ='';
            if(minutes == 1) mps ='';
            if(seconds == 1) sps ='';
            
            if( days >= 1 )
            {
              $('#'+targetTimeElement).html( days + '<span class="xt"> dag' + dps + ' og </span>'+ hours + '<span class="xt"> time' + hps + '</span>');
            }
            else if( ( days < 1) && ( hours > 0) )
            {
              $('#'+targetTimeElement).html(  hours + '<span class="xt"> time' + hps + '</span> ' + minutes + '<span class="xt"> minutt' + mps + '</span><span class="xt"> og </span>' + seconds + '<span class="xt"> sekund' + sps + '</span>' );
            }
            else
            {
              $('#'+targetTimeElement).html( minutes + '<span class="xt"> minutt' + mps + ' og </span>' + seconds + '<span class="xt"> sekund' + sps + '</span>' );
            }
            
            break;
       default:
            $('#'+targetTimeElement).html( Time_Left + '<span class="xt"> sekunder</span>');
      }
      $('#'+ targetTextElement).text( countdownText );

      //Recursive call, keeps the clock ticking.
      setTimeout('countdown(' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ',' +
              time_difference + ', ' + format + ');', 1000);
  }
}

