$(document).ready(function() {

	// need to cover-up some horrible artifacts left when chrome does
	// css transforms
	if($.browser.chrome) {
		// style this appended element to cover the white artifacts in chrome
		$('#polaroids').prepend('<div class="c"></div>');
	}

	setInterval("cyclePolaroids()", 8000);
	
	// do do do the quotation rotation
	active_quote = $('#quotes li:first');
	active_quote.siblings().hide();
	setInterval("showNext(active_quote)", 15000);
});

function cyclePolaroids() {
	var imgArr = ['jump.jpg', 'luskentyre.jpg', 'pup.jpg', 'boreray.jpg', 'beach.jpg', 'inside.jpg', 'main_high.jpg', 'boat2.jpg'];

	$('.polaroid').each(function(i, el) {
		var $$ = $(this);
		var cp = el.src.split('/');
		var cs = cp[cp.length-1];

		var n = imgArr.indexOf(cs) + 1;
		if(n > imgArr.length - 1) {
			n = 0;
		}

		var imgClass = $$.attr('class');

		($$.hasClass('rotated')) ? z = '100' : z = '95';

		if(!$.browser.msie) {
			var im = $('<img src=/img/' + imgArr[n] + ' width="379" height="253" class="' + imgClass + '" />');
			$$.parent().prepend(im);

			im.css({
				'position' 	: 'absolute',
				'top'			: $$.css('top'),
				'left'		: $$.css('left'),
				'z-index'	: z
			});

			$$.animate({opacity: 0}, 1100, function() {
				$$.remove();
			});
		} else {
			var cFilters = $$.css('filter');
			this.style.filter = cFilters + "blendTrans(duration=1)";
			this.filters.blendTrans.apply();
			this.filters.blendTrans.play();
			this.src = '/img/' + imgArr[n];
		}
	});

	return true;
}

function ieOpacity(el, opacity, current) {
	el.style.filter = current + 'Alpha(opacity=' + opacity + ')';

	return true;
}

$.jq_random = 0;
$.extend($.expr[":"], {
	random: function(a, i, m, r) {
		if(i == 0) {
			$.jq_random = Math.floor(Math.random() * r.length);
		};
		
		return i == $.jq_random;
	}
});

function showNext(t) {
	$(t).fadeOut('slow');
	var next_item = $(t).siblings('li:random');
	if(!next_item.attr('class')) {
		next_item = $('#quotes li:first');
	}
	
	next_item.fadeIn('slow');
	active_quote = next_item;
}

// implementation of indexOf for arrays - for IE
if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for(var i=0;i<this.length;i++) {
			if(this[i]==obj) {
				return i;
			}
		}
		return -1;
	}
}

// fairly nasty but effective browser sniffing
(function($) {
	var userAgent = navigator.userAgent.toLowerCase();
	$.browser = {
		version: (userAgent.match( /.+(?:rv|it|ra|ie)[\/: ]([\d.]+)/ ) || [0,'0'])[1],
		safari: /webkit/.test( userAgent ),
		opera: /opera/.test( userAgent ),
		chrome: /chrome/.test( userAgent ),
		msie: /msie/.test( userAgent ) && !/opera/.test( userAgent ),
		mozilla: /mozilla/.test( userAgent ) && !/(compatible|webkit)/.test( userAgent )
	};
})(jQuery);
