// JavaScript Document
window.nid = 0;
window.tip = 0;

setInterval(dateInterval,1000);
window.pagination=setInterval(paginate,5000);
setInterval(tips,5000);

$(document).ready(function(){
	$(".slide").click(function(){
			clearInterval(slider);
			$(".bullet-select").removeClass("bullet-select");
			$(this).addClass("bullet-select");
			$("#slide-"+window.slidecnt).fadeOut(500);
			window.slidecnt=parseInt($(this).text(),10);
			$("#slide-"+window.slidecnt).fadeIn(500);
			slider=setInterval(slideme,5000); 
	});
	$(".pager").click(function(){
		if(window.nid!=$(this).attr("id")){
			clearInterval(window.pagination);
			$(".page-selected").removeClass("page-selected");
			$(this).addClass("page-selected");
			$(".newsbot").hide();
			window.nid=parseInt($(this).attr("id"));
			$("#newsbottom"+(window.nid)).fadeIn(500);
			window.pagination=setInterval(paginate,5000);
			$(".more-link").attr("href","news.php?id="+$("#newsbottom"+(window.nid)).attr("nid"));
		}
	});
	$(".search-form").keyup(function(e){
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;
		if(parseInt(code,10)==13) $("#search").submit();
	});
	$(".search-form").attr("name","keyword");
	$('.facebook').click(function(){window.open('http://www.facebook.com/pages/Clinique-Clarens-Slimming-Sanctuary/272806597112','_blank')});
	$('.twitter').click(function(){window.open('http://www.twitter.com/cliniqueclarens','_blank')});
	$("#subscribe-button").click(function(){
			subscribe();
	});
	$('a.lightbox').lightBox();
});

var days = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
var months = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var comp=0;
var bg;

function subscribe()
{
	//if($("subscribe-form").val()!="Enter your Email Address here" || $("subscribe-form").val()!="")
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test($("#subscribe-form").val()))
	{
					$.ajax({
						type: "POST",
						dataType: "html",
						url: "actions/subscribe.php",
						timeout: 30000,
						cache: false,
						data: {email:$("#subscribe-form").val()},
						success: function(json){
							if(json=="ok"){
								alert("Thank you for subscribing.");
								$("#subscribe-form").val("Enter your Email Address here");
							}
							else{
								alert("ERROR: Email is already in the database.");
							}
						},
						error: function(){
							subscribe();
						}
					});
	}
	else{alert("Please enter a valid email.")}
}
function dateInterval()
{
	var currentTime = new Date();
	var month = currentTime.getMonth();
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	var d = currentTime.getDay();
	var h = currentTime.getHours();
	var m = currentTime.getMinutes();
	var s = currentTime.getSeconds();
	m = (m<10) ? "0" + m:m;
	var ampm = (h>11) ? "PM" : "PM";
	h = (h>12) ? h-12:h;
	s = (s<10) ? "0" + s:s;
	$(".page-date").html(months[month]+" "+day+","+year);
}

function paginate()
{
	if($(".pagination").children().length!=1)
	{
		$(".page-selected").removeClass("page-selected");
		$(".newsbot").hide();
		if(window.nid >=$(".pagination").children().length-1)
		{
			$("#0").addClass("page-selected");
			$("#newsbottom0").fadeIn(500);
			window.nid=0;
			$(".more-link").attr("href","news.php?id="+$("#newsbottom0").attr("nid"));
		}
		else
		{
			$("#"+(parseInt(window.nid)+1)).addClass("page-selected");	
			$("#newsbottom"+(parseInt(window.nid)+1)).fadeIn(500);
			window.nid++;
			$(".more-link").attr("href","news.php?id="+$("#newsbottom"+(window.nid)).attr("nid"));
		}
	}
}

function tips()
{	
	if($(".tips-cloud").length-1!=0)
	$("#tip"+window.tip).fadeOut(500,function(){if(parseInt(window.tip)>=parseInt($(".tips-cloud").length)-1) window.tip=0; else window.tip++;$("#tip"+window.tip).fadeIn(500)});	
}

//HOME SLIDE
window.slidecnt=1;
var slider=setInterval(slideme,5000);     
function slideme(){
		if($(".splash-box").children().length>1)
		$("#slide-"+window.slidecnt).fadeOut(500,
			function(){
				window.slidecnt++;
				if(window.slidecnt>$(".splash-box").children().length)
				{
					window.slidecnt=1;
				}
				$("#slide-"+window.slidecnt).fadeIn(500);
					$(".bullet-select").removeClass("bullet-select");
					$(".slide").each(function(){
						if(parseInt($(this).text(),10)==window.slidecnt)
							$(this).addClass("bullet-select");
					});
			});
}
