var selImg=0;
var EventNext=4;
var totEvents=0;

$(document).ready( function()
{
   PEPS.rollover.init();
   
	//$('#newsletter a').lightBox();
		
	$(window).bind("load",function(){
		$('#offers img').eq(0).fadeIn(300,function(){
			$('#offers img').show().css('left',259).eq(0).css('left',0);
			if($('#offers img').length>1){CycleImages();}										   
		});
		
		$('#th_container a').stop().lightBox();
		
		$('#bp').cycle({fx: "fade", timeout: 3900, speed: 1800});
		$('#tlp').cycle({fx: "fade", timeout: 4100, speed: 1800});
		
		$('#gs1').cycle({fx: "fade", timeout: 4000, speed: 1800});
		$('#gs2').cycle({fx: "fade", timeout: 3900, speed: 1800});
		$('#nye1').cycle({fx: "fade", timeout: 4100, speed: 1800});
		
		$('#op').cycle({fx: "fade", timeout: 4000, speed: 1800});
		$('#fnf').cycle({fx: "fade", timeout: 3900, speed: 1800});
		$('#kp').cycle({fx: "fade", timeout: 4100, speed: 1800});	
		
	   $('#disp_img').stop().cycle({
			speed:    1000, 
			timeout:  6000					
		});
	   
	   $.post("scripts/events.php",{func:'eventcount'},function(data){
			totEvents=data;
			if(data>4){$('#moreevents').customFadeIn(300);}
		});
	   
	  	$.post("scripts/events.php",{func:'eventlist',sfrom:0},function(data){
			$('.eventslist').hide().html(data).customFadeIn(300);
			
			$('.eventslist li a').click(function(){
				eventIndex=$(this).attr('rel');
				
				$('#eventsbox').fadeOut(300,function(){
					$.post("scripts/events.php",{func:'eventlist',sfrom:eventIndex},function(data){
						$('#eventsbox').html(data).customFadeIn(300);																		
					});								 
				});	
				return false;
			});
			
			firstIndex=$('.eventslist li a').eq(0).attr('rel');
			$.post("scripts/events.php",{func:'eventlist',sfrom:firstIndex},function(data){
				$('#eventsbox').hide().html(data).customFadeIn(300);																	
			});
		});
		
		$('#moreevents').click(function(){
			$('.eventslist').fadeOut(300,function(){
				
				if(EventNext>=totEvents)
				{
					EventNext=0;	
				}
				$.post("scripts/events.php",{func:'eventlist2',nextList:EventNext},function(data){
					$('.eventslist').html(data).customFadeIn(300);
					EventNext=EventNext+4;
					$('.eventslist li a').click(function(){
						eventIndex=$(this).attr('rel');
						
						$('#eventsbox').fadeOut(300,function(){
							$.post("scripts/events.php",{func:'eventlist',sfrom:eventIndex},function(data){
								$('#eventsbox').html(data).customFadeIn(300);																		
							});								 
						});	
						return false;
					});
					
				});	
				
			});
			return false;
		});
	   
	});

});


(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);


function CycleIndex(cycleid)
{
	$('#disp_img').stop().cycle(cycleid);
}

PEPS = {};

PEPS.rollover =
{
   init: function()
   {
      this.preload();
     
      $(".ro").hover(
         function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
         function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      $(window).bind('load', function() {
         $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '_o' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/_o\./, '.');
   } 
};

function CheckEmail(inputemail)
{
	var emailaddress = inputemail;
	var AtPos = emailaddress.indexOf("@")
	var StopPos = emailaddress.lastIndexOf(".")
	var reval=true;
	
	if (emailaddress == "")
	{
		reval=false;
	}
	
	if (AtPos == -1 || StopPos == -1)
	{
		reval=false;
	}
	
	if (StopPos < AtPos)
	{
		reval=false;
	}
	
	if (StopPos - AtPos == 1)
	{
		reval=false;
	}
	
	if(!reval)
	{
		return false;
	}
	else
	{
		return true;	
	}
}

function CheckEmailSignUpForm()
{
	if(CheckEmail(document.emailsignup.email_address.value))
	{
		if(document.emailsignup.name.value=="")
		{
			alert("Please enter you'r name");
			return false;
		}
		else
		{
			var i=0;
			if(document.emailsignup.live_entertainment.checked){i++;}
			if(document.emailsignup.promotional_offers.checked){i++;}
			if(document.emailsignup.special_events.checked){i++;}
			if(i>0)
			{
				return true;
			}
			else
			{
				alert("Please select an interest");
				return false;	
			}
		}
	}
	else
	{
		alert("Please enter a valid email address");	
		return false;
	}
}

function CycleImages()
{
	$('#offers').animate({opacity:1},3000,function(){
		elmnName=$('#offers img');
		imgCount=elmnName.size()-1;
		if(selImg==imgCount)
		{
			aniThis=selImg;
			selImg=0;
		}
		else
		{
			aniThis=selImg;
			selImg++;
		}
		elmnName.eq(aniThis).animate({left:'-259px'},{duration:600, queue:false});
		
		elmnName.eq(selImg).animate({left:0},600,function(){
			elmnName.eq(aniThis).css('left',259);
			CycleImages();
		});
	});
}
