	Array.prototype.in_array = function(q) { 
								   for (var i = 0; i < this.length; i++) {
									   if (this[i] === q) return true ;
								   }
								   return false;
							   }		
	
	function rand(n) { return (Math.floor(Math.random()*n+1)) ; }
	
	function getDisplayPics(sourcePics) {
		var displayPics = [] ;
		var showPicID = 0 ;
		if (sourcePics.length < 3) return false ;
		while (displayPics.length < 3) {
			var showPic = sourcePics[rand(sourcePics.length)-1] ;
			if (!(displayPics.in_array(showPic))) {
				displayPics.push(showPic) ;
			}
		}
		return displayPics ;
	}
	
	function crossfadeBoxTo(boxContainerID,sourcePics) {
			// Fade out the current box
		$("#box"+currBox).fadeOut("slow",function() {
				// Empty out all of the images (we do this because the images are variable width, if we just changed the src it wouldnt update the width)
			$(this).find("img").each(function() { 
				$(this).remove() ; 
			}) ;
				// Get some new images for next time around
			var editing = $(this).attr("id").replace(/box/,"") ;
			pics[editing] = getDisplayPics(sourcePics) ;
			$("#box"+editing).append('<img src="'+pics[editing][0]+'" /><img src="'+pics[editing][1]+'" /><img src="'+pics[editing][2]+'" />')
		}) ;
		
			// Fade in the next box
		$("#"+boxContainerID).fadeIn("slow") ;
		currBox = parseInt(boxContainerID.replace(/box/,"")) ;
	}
	
	var pics = [getDisplayPics(sourcePics),getDisplayPics(sourcePics)] ;
	var currBox = 0 ;
	
	$(function() {
		if ($.browser.mozilla) {
			if (navigator.userAgent.toLowerCase().indexOf("mac") == -1) {
				$("body").css("opacity","0.999") ;
			}
		}
		
		if (pics[0].length > 0) {
				// Start the crossfader
			setInterval('crossfadeBoxTo("box"+(currBox === 1 ? 0 : 1),sourcePics)',10000) ;
		}

			// JS Maxlengths for textareas
		$("textarea[name=advertisement]").keyup(function() {
			if ($(this).val().length > 250) {
				$(this).val($(this).val().substr(0, 250)) ;
				return false ;
			}
		}) ;
		$("textarea[name=description]").keyup(function() {
			if ($(this).val().length > 250) {
				$(this).val($(this).val().substr(0, 250)) ;
				return false ;
			}
		}) ;
	});