function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function changeTheme()
{
   $('body').toggleClass('lightTheme');
   
   if($('body').hasClass('lightTheme'))
   {
      $('#themeBar li:nth-child(2)').html('Current theme: day.');
      createCookie('style', 'day');      
   }
   else
   {
      $('#themeBar li:nth-child(2)').html('Current theme: night.');
      createCookie('style', 'night');      
   }   
   return false;
}

function showNextEvent()
{
   var myLi=$('#eventsList li.showme');
   var nextLi=myLi.next('li');   
   if(nextLi.is('li'))
   {
      myLi.fadeOut('slow');      
      nextLi.hide();  
      nextLi.addClass('showme'); 
      nextLi.fadeIn('slow'); 
      myLi.removeClass('showme');      
   }
   else
   {
      myLi.fadeOut('slow');      
      $('#eventsList li:first').hide();      
      $('#eventsList li:first').addClass('showme');     
      $('#eventsList li:first').fadeIn('slow'); 
      myLi.removeClass('showme');   
   }
}

function showPreviousEvent()
{
   var myLi=$('#eventsList li.showme');
   var prevLi=myLi.prev('li');   
   if(prevLi.is('li'))
   {       
      myLi.fadeOut('slow');      
      prevLi.hide();  
      prevLi.addClass('showme'); 
      prevLi.fadeIn('slow'); 
      myLi.removeClass('showme');
   }
   else
   {     
      myLi.fadeOut('slow');      
      $('#eventsList li:last').hide();      
      $('#eventsList li:last').addClass('showme');     
      $('#eventsList li:last').fadeIn('slow'); 
      myLi.removeClass('showme');   
       
   }
}


function setClock() 
{
   var clock_time = new Date();
   var clock_hours = clock_time.getHours();
   var clock_minutes = clock_time.getMinutes();
   var clock_seconds = clock_time.getSeconds();
   var clock_suffix = "AM";
   if (clock_hours > 11)
   {
      clock_suffix = "PM";
      clock_hours = clock_hours - 12;
   }

   if (clock_hours == 0)
   {clock_hours = 12;}
   if (clock_hours < 10)
   {clock_hours = "0" + clock_hours;}
   if (clock_minutes < 10)
   {clock_minutes = "0" + clock_minutes;}
   if (clock_seconds < 10)
   {clock_seconds = "0" + clock_seconds;}

   $('#themeBar li:first').html(clock_hours + ":" + clock_minutes + " " + clock_suffix + ' - ');
   setTimeout("setClock()", 60000);
}

function openModal()
{
   $("body").wrapInner("<div class='modalAllContainer'></div>");

var insert='<div class="modalBcg"></div><div class="modalContainer"><div class="modal"><h3>Newsletter<em><a href="#">close window</a></em> </h3> <form method="post" action="http://email.nublue.co.uk/form.php?form=16" id="frmSS16"> <div class="formItems"> <p class="mailContainer"> <label for="email">Your e-mail</label> <input type="text" id="email" name="email" value=""/> <input type="hidden" id="format" name="format" value="h" /><p class="sendButton"> <input type="image" alt="Send" src="images/send-btn.gif"/><p></p></div> </form> </div> </div>'; 

   $("#container").before(insert);
   
   if(!$.browser.msie)
   {
      $("div.modalBcg").fadeIn('normal');      
   }
   else
   {
      $("div.modalBcg").show();      
   }
   $("div.modalContainer").fadeIn('normal');
   
   $('.modal h3 a').click(function () { 
      closeModal();
      return false;   
   });
}


function closeModal()
{
   $("body div:first").replaceWith($("body div:first").html());
   
   if(!$.browser.msie)
   {
      $("div.modalBcg").fadeOut('normal');      
   }
   else
   {
      $("div.modalBcg").hide();      
   }
   
   $("div.modalBcg").queue(function () {
        $(this).remove();
        $(this).dequeue();
      });   
      
   $("div.modalContainer").fadeOut('normal');
   
   $("div.modalContainer").queue(function () {        
        $(this).remove();
        $(this).dequeue();        
      }); 
      
   bindEvents();
}

function bindEvents()
{
   $('#themeBar a').click(function () { 
      changeTheme();
      $(this).blur();
      return false;   
    });
    
  $('#eventsNavig li.next a').click(function () { 
      showNextEvent();
      $(this).blur();
      return false;   
    });
    
  $('#eventsNavig li.prev a').click(function () { 
      showPreviousEvent();
      $(this).blur();
      return false;   
    });
    
  setClock();
  
  $('#newsletterContainer a').click(function () { 
      openModal();
      return false;   
  });
}



function setTheme()
{
   var clock_time = new Date();
   var clock_hours = clock_time.getHours();
   var clock_minutes = clock_time.getMinutes();
   var clock_seconds = clock_time.getSeconds();

   if(readCookie('style') != null) 
   {
      if(readCookie('style')=='day')
      {
         $('body').addClass('lightTheme');
         $('#themeBar li:nth-child(2)').html('Current theme: day.');
      }
      else
      {
         $('body').removeClass('lightTheme');
         $('#themeBar li:nth-child(2)').html('Current theme: night.');
      }
      
   } 
   else 
   {
      if(clock_hours > 5 && clock_hours < 18)
      {
         $('body').addClass('lightTheme');
         $('#themeBar li:nth-child(2)').html('Current theme: day.');
      } 
      else 
      {
         $('body').removeClass('lightTheme');
         $('#themeBar li:nth-child(2)').html('Current theme: night.');
      }
   }
   setClock();
}


$(document).ready(function(){
   bindEvents();   
   setTheme();
});
