$(document).ready(function(){		
	var offset = 0;
	var limit = 6;
	var old_offset = 0;
	var new_offset = 0;
	$("#loading").ajaxStart(function () {
		$(this).show();
	}).ajaxStop(function () {
		if($('#slide_backward').hasClass("hide_div")){
			if(old_offset != 0){
				$("#slide_backward").toggleClass("hide_div");
			}
		}
		if($('#slide_forward').hasClass("hide_div")){
			if(total_spotlights > new_offset){
				$("#slide_forward").toggleClass("hide_div");
			}
		}
		$(this).hide();
	});
	function get_spotlight(offset, limit, type){
		$('#content_slider').empty();
		if(type == "f"){
			offset = offset + limit;
		} else if(type == "b") {
			offset = offset - limit;
		}
		if(offset < 0) {
			offset = 0;
		}
		old_offset = offset;
		new_offset = old_offset;
		$.ajax({
			type: "GET",
			url: "/xml/index_spotlight.php",
			dataType: "xml",
			data: "offset="+offset+"&limit="+limit+"",
			success: function(xml) {
				$(xml).find('item').each(function(){
					var id = $(this).attr('id');
					var img = $(this).attr('img');
					var media_id = $(this).attr('media_id');
					var title = $(this).find('title').text();
					var height = $(this).find('height').text();
					$('<div class="content_item" style="margin-top:' + height + 'px"></div>').html('<p><a href="/record.php?id='+id+'&media_id='+media_id+'" title="'+title+'" onclick="replace_spotlight('+id+', '+media_id+'); return false;"><img src="'+img+'" width="70" height="53" alt="'+title+'" /></a></p>').appendTo('#content_slider');
				});
			}
		});
	};
	get_spotlight(offset, limit);
	$('#slide_forward').click(function () {
		$("#slide_forward").toggleClass("hide_div");
		$('#content_slider').slideUp("fast", function () {
			get_spotlight(old_offset, limit, "f");
			$('#content_slider').show("fast");
			new_offset = old_offset+limit;
		});
	});
	$('#slide_backward').click(function () {
		$("#slide_backward").toggleClass("hide_div");
		$('#content_slider').slideUp("fast", function () {
			get_spotlight(old_offset, limit, "b");
			$('#content_slider').show("fast");
		});
	});
});
function replace_spotlight(id, media_id) {
	$('#feature_content_inner').fadeOut("fast", function () {
		$('#feature_content_inner').empty();
		$.ajax({
			type: "GET",
			url: "/xml/index_spotlight_flv.php",
			cache: false,
			dataType: "html",
			data: "id="+id+"&media_id="+media_id+"",
			success: function(html) {
				$("#feature_content_inner").append(html);
			}
		});
		$('#feature_content_inner').fadeIn("fast");
	});
	return true;
}
