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

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

};

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'); }
	},
	
	faux_submit: function() {
		$('.faux_submit').click(function(){
			$(this).closest('form').submit();
			return false;
		});
	}
};

var slideshow = {
	initialise: function(){
		num_of_slides = $('.slideshow .slides li').length;
		$('.slideshow .slides').width(num_of_slides * slideshow_slide_width);
		slideshow.set_up_controls();
		slideshow_timeout = setTimeout("slideshow.next_slide();", 4000);
		$('.slideshow .controls a').each(function(index){
			$(this).click(function(){
				clearTimeout(slideshow_timeout);
				current_slide = index;
				if(index == 0){
					//to fake jumping forwards
					current_slide = ($('.slideshow .controls li').length - 1);
				}else{
					current_slide = index;
				}
				slideshow.go_to_slide(current_slide);
				return false;
			});
		});
	},
	
	next_slide: function(){
		current_slide = current_slide + 1;
		if(current_slide == num_of_slides){
			current_slide = 0;
		}
		slideshow.go_to_slide(current_slide);
	},
	
	
	go_to_slide: function(slide_num){
		slideshow.switch_off();
		
		//if it's the last one...
		var timeout_length = 3000;
		var animation_speed = 888;
		
		$('.slides').stop().animate({
			left: '-'+ (slideshow_slide_width * slide_num) +'px'
		}, animation_speed, function() {
			if(current_slide == ($('.slideshow .controls li').length - 1) ){
				//to fake jumping forwards
				$('.slides').css('left', '0px');
				current_slide = 0;
			}
			slideshow_timeout = setTimeout("slideshow.next_slide();", timeout_length);
			slideshow.switch_on();
		});
	},
	
	switch_off: function(){
		$('.slideshow .controls li').removeClass('on');
	},
	
	switch_on: function(){
		slideshow.switch_off();
		$('.slideshow .controls li:eq('+current_slide+')').addClass('on');
		if(current_slide == ($('.slideshow .controls li').length - 1)){
			slideshow.switch_off();
			$('.slideshow .controls li:eq(0)').addClass('on');
		}
		$('.slideshow .controls li:eq('+($('.slideshow .controls li').length - 1)+')').hide();
	},
	
	set_up_controls: function() {
		var button = '<li><a href="#"><span></span></a></li>';
		var i = 0;
		for (i=0;i<=(num_of_slides - 1);i++){
			$('.slideshow .controls').append(button);
		}
		slideshow.switch_on();
	}
};

var homepage_boxes = {
	initialise: function(){
		//just in case the content spills over, not actually used for layout
		var max_h = 0;
		var blue_boxes = $('#homepage_footer .green_box');
		blue_boxes.each(function(){
			if( $(this).height() > max_h){
				max_h = $(this).height();
			}
		});
		
		blue_boxes.each(function(){
			$(this).height(max_h);
		});
		
		var max_h = 0;
		var plumb_boxes = $('#homepage_footer .plumb_box');
		plumb_boxes.each(function(){
			if( $(this).height() > max_h){
				max_h = $(this).height();
			}
		});
		
		plumb_boxes.each(function(){
			$(this).height(max_h);
		});
	}
};

var expander = {
	initialise: function() {
		$('.expander .expander_content').slideUp(0);
		$('.expander .open_link').click(function(){
			var parent_expander = $(this).parent().parent();
			//parent_expander.find('.expander_content').slideToggle(toggle_speed);
			//parent_expander.toggleClass('open');
			// $('.expander .expander_content').each(function(){
			// 	$(this).removeClass('open').slideUp();
			// });
			
			$('.expander_content').removeClass('on');
			parent_expander.find('.expander_content').addClass('on');
			$('.expander_content').each(function(){
				if(!($(this).hasClass('on'))){
					$(this).slideUp();
					$(this).parent().removeClass('open');
				}
			});
			parent_expander.find('.expander_content').slideToggle( function(){
				content.set_height();
			});
			
			parent_expander.toggleClass('open');
			
			return false;
		});
	}
};


var slideshow_slide_width = 610;
var current_slide = 0;
var num_of_slides = null;
var slideshow_timeout = null;

var toggle_speed = 333;

$(document).ready(function(){
	homepage_boxes.initialise();
	app.initialise();
	slideshow.initialise();
	expander.initialise();
	
	if(is_browser.mozilla) { $('body').addClass('firefox'); }
	
	$("a.jqbookmark").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
		var url = this.href;
		var title = this.title;

		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else if(window.opera) { // Opera 7+
			return false; // do nothing - the rel="sidebar" should do the trick
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Unfortunately, this browser does not support the requested action,'
			 + ' please bookmark this page manually.');
		}

	});
	
	
	$('.open_link').each(function(index){
		$(this).attr('name', $(this).text().toLowerCase().replace(" ", "-"));
	});

	var anchor = window.location.hash.replace("#", "");
	
	
	$('.open_link').each(function(index){		
		if($(this).attr('name') == anchor){
			toggle_speed = 0;
			$(this).click();
			toggle_speed = 333;
		}
	});
	
});

var content = {
	set_height: function() {
		$('.content').css('min-height', $('.content').height() + 'px')
	}
};


$(window).load(function(){
	
	$('.map').wrap('<div id="map_holder" style="height:'+$('.map').height()+'px" >');
	
	$('.map_link').each(function(){
		$('#map_holder').append('<a target="_blank" href="'+$(this).attr('href')+'" id="'+$(this).attr('id')+'_link" class="map_overlay_link" ></a>');
	});
	
	$('.map_overlay_link').animate({opacity:0}, 0);
	
	content.set_height();
	
});












