if (!Array.prototype.shuffle) {
Array.prototype.shuffle = function() {
// Clone this array.
var result = this.concat();
var arr=this;
// Swap each element with another randomly selected one.
for (var i = 0; i < result.length; i++) {
var j = i;
while (j == i) {
j = Math.floor(Math.random() * result.length);
}
var contents = result[i];
result[i] = arr[j];
result[j] = contents;
}

return result;
};
}


var is_browser = {
	webkit: jQuery.browser.webkit,
	ie6: (jQuery.browser.msie && jQuery.browser.version.substr(0,1)<7),
	ie: (jQuery.browser.msie)
};

var app = {
	initialise: function() {
		app.log('initialised');
		//alert('initialised');
	},
	
	log: function(message) {
		if(typeof console != 'undefined') console.log(message);
	}

};

var gallery_switcher = {
	initialise: function() {
		$('.gallery ul li a').click(function(){
			var link = $(this);
			var gallery = link.parent().parent().parent();
			gallery_switcher.load_in_image(gallery, link.attr('href'), link.find('.hidden_caption').text(), link.find('.original_url').text());
			gallery.find('li').each(function(){
				$(this).removeClass('on');
			});
			link.parent().addClass('on');
			return false;
		});
		
	},
	
	load_in_image: function(gallery, link, caption, original_link) {
		gallery.find('.current_image img').attr('src', link)
		gallery.find('.current_image a').attr('href', original_link)
		gallery.find('.current_image p').html(caption);
	}
};

var backend = {
	classyfy_inputs: function() {
		$('input[type=text]').addClass('input_text');
		$('input[type=password]').addClass('input_text');
		$('input[type=button]').addClass('input_button');
		$('input[type=submit]').addClass('input_button submit_button');
		$('input[type=checkbox]').addClass('input_checkbox');		
		if (is_browser.webkit) { $('input[type=button], input[type=submit]').addClass('webkit_input_button'); }
	},
	
	label_heights: function() {
		$('.input').each(function(index) {
			var input_div = $(this);
			var label_height = input_div.find('label').height();
			if(input_div.find('label').height() > 1 && label_height >  input_div.height() ){
				input_div.css('min-height',  (label_height + 'px') )
			}
		});		
	}
};

var lightbox = {
	initialise: function() {
		$('.full_size').click(function(){
			$('#image_holder').show();
			lightbox.load_big_image($(this).attr('href'));
			overlay.show();
			return false;
		});
		
		$('#image_holder .close').live('click', function(){
			lightbox.close();
			return false;
		});
	},
	
	load_big_image: function(href){
				
		var img = new Image();
		
		img.onload = function() {			
			img.style.display = 'none';
			var target = $('#image_holder_contents');
			var image_holder = $('#image_holder_contents');
			image_holder.empty();
			image_holder.append(img);
			target.prepend('<a class="close" style="display:none;" href="">Close</a>')
			target.animate({
				width: (img.width + 12),
				height: (img.height + 12)
			}, 500, function (){
				$( img ).fadeIn( 'normal', function() {
					image_holder.find('.close').show();
				});
			});
			
			if(is_browser.ie6){
				$('#image_holder_contents, #overlay').live('click', function() {
					lightbox.close();
				});
			}
			
		};
		
		img.src = href;
	},
	
	close: function() {
		$('#image_holder').fadeOut(333, function(){
			$('#image_holder_contents').empty().append('<span class="loading" >Loading...</span>');
		});
		overlay.hide();
	}
};


var overlay = {
	initialise: function() {
		overlay.hide();	
	},
	
	show: function() {
		$('#overlay').height($(document).height()).show().animate({opacity: 0.8}, 333);
	},
	
	hide: function() {
		$('#overlay').animate({opacity: 0}, 0, function(){
			$(this).hide();
		});
	}
};

var slideshow = {
	initialise: function() {
		slideshow_div = $('#slideshow');		
		num_slides = slideshow_slides.length;
		slideshow.next_slide();
	},
	
	next_slide: function() {
		
		var slideshow_height = $('#slideshow').height();
		
		slideshow.iterate();
		var img = new Image();		
		img.onload = function() {
			img.style.display = 'none';
			slideshow_div.empty().append(img);
			var j_image = $(img);
			
			var j_image_height = j_image.height();
			
			if(is_browser.ie){
				var animation_speed = 0;
				var top_to = parseInt(slideshow_slides[current_slide].top);
				j_image.css('top', top_to + 'px');
			}else{
				var animation_speed = (j_image_height * 4);
				var top_to = 0;
				
				j_image.css('top', '-'+(j_image_height - slideshow_height)+'px');
			}
				
				
			
			j_image.fadeIn(1000, function() {
				$(this).animate({
				    top: top_to
				  }, animation_speed, function() {
					setTimeout("slideshow.next_slide();", 5000);
				  });
			});
			
		};
		img.src = slideshow_slides[current_slide].image;
	},
	
	iterate: function() {
		current_slide = current_slide + 1;
		if(current_slide == num_slides){
			current_slide = 0;
		}
	}
};






var num_slides = 0;
var current_slide = 0;
var slideshow_div = undefined;

$(document).ready(function(){
	app.initialise();
	gallery_switcher.initialise();
	backend.classyfy_inputs();
	backend.label_heights();
	overlay.initialise();
	lightbox.initialise();
		
	$('table').css('width','');	
	
	$('.error-message').prepend('<span class="arrow" >&uarr;</span> ');
	$('.nice_form .error:first input').focus();
	$('.required label').append('<span class="required_label">(required)</span>');
	
});

$(window).load(function(){
	if($('#slideshow').length > 0){
		slideshow.initialise();		
	}
});



