function setCookie(name, value, days) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+days);
	document.cookie=name+ "=" +escape(value)+((days==null) ? "" : ";expires="+exdate.toGMTString())+";path=/";
}

function getCookie(name) {
	if( document.cookie.length>0 ) {
		c_start=document.cookie.indexOf(name + "=");
		if( c_start != -1 ) {
			c_start=c_start + name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if( c_end == -1 ) 
				c_end = document.cookie.length;
	    	return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return null;
}

function clearCookie(name) {
	var pdate = new Date(); 
	pdate.setTime(pdate.getTime() - 1); 
	document.cookie = name+"=; expires=" + pdate.toGMTString(); 
}

function updateRatingImg(score, imgclass) {
	$(imgclass).each(function(i){
		vote = $(this).attr('alt');
		if (vote <= score){
			$(this).attr('src','/images/r-gift-box-tiny.png?'+ Math.random());
		}
	});
}
function addHoverHide(linkId, layerId) {
  var t;
  $("#" + linkId).hover(function() {
    clearTimeout(t)
    $("#" + layerId).fadeIn('slow');
	t = setTimeout(function() {$("#" + layerId).fadeOut('slow')}, 2000);
  }, function() {
    $("#" + layerId).fadeOut('slow');
  });
} 

$(document).ready(function() {
	addHoverHide('prevbox', 'prevcontents');
	addHoverHide('nextbox', 'nextcontents');
	$('#sourcebox').hover(
		 function() {
			$('#sourcecontents').fadeIn("fast");
		 },
		 function() {
			$('#sourcecontents').fadeOut("fast");
	});
	$('#agehighenough').click(function() {
		setCookie('ageisok', 1, 30);
		window.location = 'http://boxofbooty.com/';
	});
	
	$('#ageistoolow').click(function() {
		window.location = 'http://google.com/';
	});
	
	$('#sortby-n').click(function() {
		setCookie('sortby', 'n', 30);
		window.location = 'http://boxofbooty.com/n';
	});
	
	$('#sortby-r').click(function() {
		setCookie('sortby', 'r', 30);
		window.location = 'http://boxofbooty.com/r';
	});
   
	$('.popup-switch').click(function() {
	
		$('.popup-hidden').hide("slow");
		if( $(this).next().is(':hidden') ) {
			$(this).next().show("slow");
		}
	
	});
	
	$('#Upload').click(function() {
		// temporarily switching off upload
		alert('Box of Booty is currently suffering heavy server load, so uploads are disabled for a day or two until we have that under control. Sorry about that!');
		return false;
		/* 
		if( !$('#acceptterms').attr('checked') ) {
			alert('You may not upload without agreeing to our terms and conditions.');
			return false;
		}
		return true;
		*/
	});

	$('.rateradio').click(function() {
		var rating = $(this).attr('alt');
		var post = $('#post_id').attr('value');

		updateRatingImg(rating, '.rateradio');
		
		$.post(	"http://boxofbooty.com/async/rate/", 
				{ rating: rating, postid: post },
				function(data) {
					updateRatingImg(rating, '.rateradio');
					$('#rating-form').append("<br>Thank you for voting!");
					if( $('#nextlink').length ) {
						$('#rate-this .popup-hidden').fadeOut(2000, function(){
							window.location = $('#nextlink').attr('href');
						});
					}
					else {
						updateRatingImg(data.rating, '.ratebox');
						$('#rate-this .popup-hidden').fadeOut(2000);
					}
				},
				"json"				
		);
	
	});
	
	$('.ratebox').click(function() {
		var rating = $(this).attr('alt');
		var post = $('#post_id').attr('value');
		
		$('.ratebox').attr('src', '/images/g-gift-box-tiny.png?' + Math.random());
		updateRatingImg(rating, '.ratebox');
		
		$.post( "http://boxofbooty.com/async/rate/", 
				{ rating: rating, postid: post },
				function(data) {
					updateRatingImg(rating, '.ratebox');
					var appendspan = $('<span>Thank you for voting</span>');
					$('#rating').append(appendspan);
					var newrating = data.rating;
					setTimeout(function() {
						updateRatingImg(newrating, '.ratebox');
						$('#rating span').remove();
						if( $('#nextlink').length ) {
							window.location = $('#nextlink').attr('href');
						}
					},
					3000
					);
				},
				"json"
		);
	
	});
	
	$('#report-submit').click(function() {
		
		var r_email = $('#report-email').attr('value');
		var r_text  = $('#report-text').attr('value');
		var r_post  = $('#post_id').attr('value');
		if( r_email.length == 0 || r_text.length == 0 ) {
			alert('No no, don\'t be shy. Tell us who you are and what you don\'t like. Please?');
		}
		else {
			$.post( "http://boxofbooty.com/async/report/",
					{ email: r_email, message: r_text, postid: r_post },
					function(data) {
						$('#reportresponse').show("slow").fadeOut(2000);
						$('#report-email').attr('value', '');
						$('#report-text').attr('value', '');
						$('.popup-hidden').hide("slow");
					}
			);
		}
	});
	
	$('#send-submit').click(function() {
		
		var t_email = $('#send-email').attr('value');
		var m_text  = $('#send-text').attr('value');
		var r_post  = $('#post_id').attr('value');
		var f_email = $('#from-email').attr('value');
		var f_name  = $('#from-name').attr('value');
		
		if( t_email.length == 0 || f_name.length == 0 ) {
			alert('We need to know where to send it, and they need to know who it\'s from!');
		}
		else {
			$.post( "http://boxofbooty.com/async/email/",
					{ email: t_email, from: f_name, fromemail: f_email, message: m_text, postid: r_post },
					function(data) {
						$('#emailresponse').show("slow").fadeOut(2000);
						$('#send-email').attr('value', '');
						$('#send-text').attr('value', '');
						$('#from-email').attr('value', '');
						$('#from-name').attr('value', '');
						$('.popup-hidden').hide("slow");
					}
			);
		}
	});
	

});

