(function($) { $(document).ready(function() {
	
	$('.GalleryComponent').each(function() {
		var $component = $(this);
		$component.find("ul.galleryul li img").click(function(){
			var path = $(this).attr("src");
			var fullimage = $(this).parent().attr("href");
			$component.find(".placeholderimage img").fadeOut(1200, function() {
				$(this).attr({ src: fullimage }).fadeIn(1200);
				$(this).load(function() {
					$component.find(".placeholderimage").height($(this).height());
				});
			});
			return false;
		});
	});
	
	// Apply colours to OCV model links
	jQuery('#OCVMenu .primaryLink a').each(function() {    
		jQuery(this).html("<span class='basejumper'>" + jQuery(this).html().replace(/\s+/g, "</span> <span class='basejumpermodel'>") + '</span>'); 
		Cufon.replace('.basejumpermodel',{ color: '#272B2D' });
		Cufon.replace('.basejumper',{ color: '#272B2D' });
		Cufon.replace('.basejumpermodel:odd',{ color: '#FFFFFF' });
	});
	
	// Set accordion heights for more consistent animation
	//$("#OCVLowerMenu .AccordionComponent > div").each(function() {
	//	$(this).height($(this).height() + 20);
	//});
	// Add accordion to OCV
	$("#OCVLowerMenu").accordion({ header: 'h3', collapsible: true, active: false, autoHeight: false });
	
	var nav = $('<ul class="tabs"></ul>');
	var licount = 0;
	$('.horizontal-tabs, #ocvLeftMenu .vertical-tabs').each(function(i) {
		licount++;
	});
	$('.horizontal-tabs, #ocvLeftMenu .vertical-tabs').each(function(i) {
		var title = $(this).find('h5:first').text();
		var id 		= title.replace(/[^a-zA-Z0-9]+/g, '-');
		
		// Append a number if id exists
		var append = '';
		if ($('#tab-' + id).size()) {
			append = 0;
			while ($('#tab-' + id + append).size()) {
				append++;
			}
		}
		$(this).attr('id', 'tab-' + id + append);
		nav.append($("<li style='width: "+($(this).width()-licount)/licount+"px;'><a href='#tab-" + id + append + "'>" + title + "</a></li>"));	
	});
	
	// Vertical tabs (modify dom, initialize tabs and perform text replacement)
	$('#ocvLeftMenu').prepend(nav);
	$('#ocvLeftMenu').tabs({ event: 'mouseover' });
	Cufon.replace('#ocvLeftMenu a', { color: '#000000' });		
	// Horizontal tabs (modify dom, initialize tabs and perform text replacement)
	$('.horizontal-tabs:first').before(nav);
	$('#middleColumn').tabs({ select: function(event, ui) { Cufon.replace('.ui-tabs-nav a', { color: '#7e865c' }); }
	});
	Cufon.replace('.ui-tabs-nav a', { hover: true });

	// Drop down nav
	$("ul.nav li").hover(function() {
		$(this).find("ul").stop().slideDown('fast').show();
	}); 
	
	$("ul.nav li").mouseleave(function() {
		$(this).find("ul").stop().hide().css('height', 'auto');
	});
	
	jQuery("ul.localNav .primaryLink a").wrap("<span></span>");
  	jQuery("ul.localNav .primaryLink a").each(function(i) {
		jQuery(this).text().replace(" ", "</span><span>");
	});
	
	// FormComponent ajax submit
	jQuery('.FormComponent form').submit(function() {
		var form = jQuery(this);
		var formValues = form.serialize();
		var formHeight = form.height();
		var fail = false;
		
		// return if there's an outstanding message - this doesn't stop a user from clicking on submit before creating a message. TODO. Need to validate first
		form.find('.message').each(function() {
			if ($(this).css('display') == 'block') fail = true;
		});
		if (fail) return;
		
		jQuery.ajax({  
			type: "POST",  
			url: form.attr('action'),
			data: formValues,
			success: function(msg) {  
				form.fadeOut(1500, function() {  
					form.after("<div class='response'>" + msg + "</div>");
					form.parents('.FormComponent').find('.response').height(formHeight);
				});
			}
		});  
		return false;  
	});


	
});})(jQuery);
