var R,RS;
$(document).ready(function(){
	setTimeout("banner();",5000);
	R = $('#R').val();
	RS = $('#RS').val();
	
	$.ajaxSetup({
		url: R+'~do',
		type: 'POST'
	});
});

function banner () {
	/* Fading Banner */
	var p = $('#banner div:visible').prev();
	if(!p.size()) p = $('#banner div:last');
	$('#banner div:visible').fadeOut(750);
	p.fadeIn(750);
	setTimeout("banner();",5000);
}

// Show/Hide and position the animating ajax loader
function setLoader(b,x,y){
	b = b ? (typeof b=="object" ? b : $(b)) : false;
	var l = $('#spinner');
	if(!b)
		l.stop().fadeOut('fast');
	else
		l.css({
			'left':Math.ceil(b.offset().left - (b.outerWidth()/2 - l.outerWidth()/2) + 100 + (x?x:0))+'px',
			'top': Math.ceil(b.offset().top + (b.outerHeight()/2 - l.outerHeight()/2)+(y?y:0))+'px'
		}).stop().fadeIn(500);
}

function setOrder(o) {
	setLoader("#sort select",-16,37);
	$('#gemPage').load(R+"~do",{cmd:'gemPage',order:o,page:$('#P').val(),type:$('#T').val()},function(){
		setLoader();
	});
}

function update(t){
	if($(t).attr('checked')) $('#comEmail').show();
	else $('#comEmail').hide();
}

// Save a Comment
function com_save(i){
	var a = $('#com_'+i+' a.button');
	var d = $('#com_'+i+' :input').serialize();
	var m = $('#com_'+i+' p.msg');
	if($('#com_'+i+' .updates').attr('checked') && $('#com_'+i+' .email').val()=='') {
		alert('Please enter your email address if you wish to receive new comment updates.');
		$('#com_'+i+' .email').focus();
		return false;
	} else {
		m.html('Saving... please wait').show();
		setLoader(a,-10,-30);
		a.hide();
		$.ajax({
			data: d+'&cmd=comment_gem',
			success: function(h){
				setLoader();
				if(h.substr(0,1)!='#') {
					$('#comments').replaceWith(h);
					m.html('Thanks! Your comment has been posted.');
					$('#com_'+i+' textarea').val('');
				}
				else {
					h = h.substr(1);
					m.html(h);
					a.fadeIn(300);
				}
			}
		});
	}
}

function search() {
	var kw = $('#barSearch').val();
	if(!kw) {
		alert('Please enter some keywords!');
		$('#barSearch').focus();
	} else window.location.href=R+"Search.html?kw="+kw;
}
