//Update banner clicks count.
function update(id){
	$.getJSON("/js/banner_record/banner_record.php", { banner_id: id});
}

$(document).ready(function(){
	
	//Display alert.
	$("#browser_alert").animate({ opacity: 1 }, 2000, function(){
		$("#browser_alert").slideDown("slow");
	});
	
	$("#close_browser_notice").click(function(){
		//Set cookie.
		var today = new Date();
		var expire = new Date();
		expire.setTime(today.getTime() + 3600000*24*7);
		document.cookie = "browser_alert=1; expires="+expire.toGMTString();
		
		//Hide alert.
		$("#browser_alert").slideUp("normal");
	});
	
	$("#weather").tooltip({
		delay: 250,
		direction: "down",
		offset: [-10, -118],
		position: "bottom center",
		tip: "#weather_tooltip"
	});
	
	// Select all links with lightbox class
	$('a.lightbox').lightBox();
		
	/* Newsflash
	 ******************************/
		var speed = 8000;
	
		//Get Newsflash items.
		$.getJSON("/js/newsflash/newsflash.php", function(data){
			if(data.status == 1){
				var newsflashCount = 0;				
				jQuery.each(data.newsflash_item, function(i){
					i++;
					$("#newsflash span").append("<div class='newsflash_item' id='newsflash_"+i+"' style='display:none;'>"+this.Body+"</div>");
					newsflashCount++;
				});
				
				//Animate between the various messages.
				function animateNewsflash(a){
					//Fade out displayed newsflash item.
					$(".newsflash_item").each(function(){
						if($(this).css("display") === "block"){
							$(this).animate({ opacity: "0" }, 750, function(){ $(this).css({ "display": "none" }); });
						}
					});
					
					if(a == newsflashCount){
						$("#newsflash_1").css({ "display": "block" }).animate({ opacity: "1"}, 750);
						a = 1;
					} else {
						$("#newsflash_"+(a+1)).css({ "display": "block" }).animate({ opacity: "1"}, 750);
						a++;
					}
					setTimeout(function(){ animateNewsflash(a) }, speed);
				}
				animateNewsflash(1);
			} else {
				$("#newsflash span").html("No news items at this time.");
			}
		});
		
		
	/* Sponsors
	 ******************************/
		$.getJSON("/js/sponsors/sponsors.php", function(data){
			if(data.status == 1){
				var b = 0;
				function sponsor_rotation(){
					if(b > ((data.count)-1)){ b=0; }
					$("#sponsor_crop img").animate({ opacity: 0 }, 750, function(){
						$("#sponsor").attr({ "href": data.Sponsor[b].Link, "onclick": "javascript: udpate(data.Sponsor[b].ID);" });
						$("#sponsor_crop img").attr({ src: "/CMS/Media/"+data.Sponsor[b].Media });
						$("#sponsor_crop img").animate({ opacity: 1 }, 750, function(){
							b++;
							setTimeout(function(){ sponsor_rotation(); }, 6000);
						});
					});
				}
				sponsor_rotation();
			}
		});
});
