var vag_light = { src: '/flash/vag_light.swf' };
var vag_bold = { src: '/flash/vag_bold.swf' };
sIFR.activate(vag_light, vag_bold);
sIFR.replace(vag_light, {
	selector: 'h1',
	wmode: 'transparent',
	css: { '.sIFR-root': { 'color': '#333333' }}
});
sIFR.replace(vag_bold, {
	selector: '#quotes h2',
	wmode: 'transparent',
	css: { '.sIFR-root': { 'color': '#97bb02' }}
});
sIFR.replace(vag_bold, {
	selector: '#other_users h2',
	wmode: 'transparent',
	css: { '.sIFR-root': { 'color': '#00558c' }}
});
sIFR.replace(vag_bold, {
	selector: '#content h2, #waiting>h2, #find_out_more>h2, #other_users h3',
	wmode: 'transparent',
	css: { '.sIFR-root': { 'color': '#333333' }}
});

$.validator.setDefaults({
	errorPlacement: function(error,element){
		error.insertBefore(element);
	}
});

$(function(){
	
	$('#slideshow').slideshow();
	
	$('#content_main>#quotes').cycleQuotes();
	
	$('#contact_form').validate();
	
});

$.fn.slideshow = function(){
	return this.each(function(){
		
		var slides = $('li',this);
		
		var extension = $.browser.msie && $.browser.version <= 6 ? 'gif' : 'png'; // no alpha PNGs for IE6
		
		// initialise controls
		var show = $('<button id="slideshow_show"><img src="/images/button_slideshow_show.'+extension+'" alt="Show" /></button>').hide();
		var prev = $('<button id="slideshow_previous"><img src="/images/button_slideshow_previous.'+extension+'" alt="Previous" /></button>');
		var next = $('<button id="slideshow_next"><img src="/images/button_slideshow_next.'+extension+'" alt="Next" /></button>');
		$(this).append(show).append(prev).append(next).hover(function(){
			show.fadeIn();
		},function(){
			show.fadeOut();
		});
		
		// initialise slides
		slides.filter(':first').addClass('current');
		slides.not(':first').css({opacity:0});

		var slide = function(e){
			var current = slides.filter('.current');
			var next = e.data.direction>0 ?
				(current.is(':last-child') ? slides.eq(0) : current.next()) :
				(current.is(':first-child') ? slides.eq(slides.length-1) : current.prev());
			
			if (!next.length) { return false; }
			
			current.animate({
				opacity: 0
			},{queue:false,duration:300}).removeClass('current');
			next.css({
				opacity: 0
			}).animate({
				opacity: 1
			},{queue:false,duration:300}).addClass('current');
			
			show.colorbox({
				href: '/includes/gallery.php?image='+(slides.index(slides.filter('.current'))+1),
				width: 650
			});
			
			return false;
		};

		next.bind('click',{direction:1},slide);
		prev.bind('click',{direction:-1},slide);
		
		show.colorbox({
			href: '/includes/gallery.php',
			width: 650
		});
		
		$(document).bind('cbox_complete',function(){
			var links = $('#gallery a');
			var img = $('#gallery_main');
			var gallery_caption = $('#gallery_caption');
			var indicator = $('<img src="/images/icon_gallery_over.png" alt="" id="gallery_hover_indicator" />');
			links.click(function(){
				img.attr('src','/images/gallery_'+(links.index(this)+1)+'.png');
				gallery_caption.text($(this).find('img').attr('alt'));
				return false;
			}).hover(function(){
				indicator.appendTo($(this).parent());
			});

		})
		
	});
}

$.fn.cycleQuotes = function(){
	return this.each(function(){
		
		var blockquotes = $('>blockquote',this).css('top','0');
		blockquotes.not(':first').hide();
		
		var cycle = function(){
			var current = blockquotes.filter(':visible');
			var next = blockquotes.eq((blockquotes.index(current)+1) % blockquotes.length);
			
			current.css('position','absolute').fadeOut(function(){$(this).css('position','static')});
			next.fadeIn();
		};
		
		var cycleTimer = window.setInterval(cycle,5000);
		$(this).hover(function(){
			window.clearInterval(cycleTimer);
		},function(){
			cycleTimer = window.setInterval(cycle,5000);
		});
	});
}
