var CORR = {}; // Corrotherm namespace
var WSHD = {}; // Watershed namespace
var $path;


$(document).ready(function() {
	$('html').removeClass('no-js');
	$path = window.location.pathname;
	$path = $path.split('/');
	
	// Kludge to help IE7/8 with the home page carousel and media queries
	if ( $('.ie7 .home').length || $('.ie8 .home').length ) {
		WSHD.mq_help();
	}
	
  $.localScroll({
    event:'click', //on which event to react
    axis:'y',
    duration:500,
    offset:-50,
    hash:true,
		stop:true      //avoid queuing animations
  });

  // Hide ranges and details contents
	$( '#grades .range, .value .contents' ).hide();
	// Show the active range
	$( '#grades .active' ).show();

	// Create click handlers for details
	$('.details_list .summary').wrapInner('<a href="#" />');
	$('.details_list .summary a').attr('title','More details').click(single_details);

  // Setup tabs and handlers
  $('.tabs a').unbind('click').click(activate_tab);
	
	// Set height of tabbed ranges
	ranges_minht();
	
	// Contact form
	$('.foot .contact fieldset .panel').hide();

	var $contact_hrefs = 'a[href="/contact"]';
	// If 'contact' is NOT a URL segment
	if ( $.inArray('contact', $path) == -1 ) {
		$contact_hrefs = 'a[href="http://www.corrotherm.co.uk/contact"], ' + $contact_hrefs;
	}
	// Rewrite contact hrefs
	$($contact_hrefs)
		.attr('href','#contact')
		.unbind('click').click(go_contact);
	
	// Create linked image hover blocks
	$('a .figure img').after('<span class="hover" />');
	
	// Replace scrollbars on overflowed apps lists
	$('#apps .unit ul').each(function() {
		var _list = $(this);

		if ( ($(this).attr('scrollHeight')-10) > $(this).height() ) {
			$(this).wrap('<div class="overflow" />').jScrollPane();
		}
	});
	
	// Reposition balance elements
  $('#composition .line').append( $('.balance') );
  
	// Carousels
	WSHD.carousel();
});

WSHD.mq_help = function() {
	if ( $(window).width() > 1200 ) {
		$('body').addClass('wide');
	}
}

function ranges_minht() {
	var $range_ht = 0;

	// Get the tallest height of the alloy range sections
	$('#grades .range').each(function(i) {
		if ( $(this).height() > $range_ht ) {
			$range_ht = $(this).height();
		}
	});
	// Add a buffer
	$range_ht = $range_ht + 20;

	$('.ranges').css('min-height',$range_ht + 'px');
}

// Grades tabs
function activate_tab() {	
	_tab = $(this);
	
	// Change the active tab
	$('.tabs a').removeClass('active');
	_tab.addClass('active');

	// Show the corresponding section
	var $old_section = $('#grades div.active');
	var $new_section = '#grades div.' + _tab.attr('href');

	$old_section.removeClass('active').fadeOut(0,function() {
		$($new_section).addClass('active').show(0);
	});

	return false;
}

// Details sliders
function single_details() {	
	var $details = $(this).parents('.details_list');
	var $detail = $(this).parents('.details');
	if ( !$detail.hasClass('open') ) {
		$(this).attr('title','Hide details');
		$details
			.find('.details').removeClass('open')
			.find('.contents').slideUp();
		$detail
			.addClass('open')
			.find('.contents').slideDown();
	}
	else {
		$(this).attr('title','More details');
		$detail
			.removeClass('open')
			.find('.contents').slideUp();
	}
	return false;
}

// Go contact
function go_contact() {
	var _wrapper = $('#form_wrapper');
	
	$.scrollTo('#contact',1000, {
		onAfter:function(){
		
			// Let's only do this if the contact form doesn't exist
			if ( !$('#form_wrapper form').length ) {
				
				_wrapper.hide().load('/contact #enquiry',function() {
					var $form = _wrapper.find('.panel');
					$form.hide();    // Hide the form so we can reveal it
					
					// Add a close button
					var $close = $('<a class="close" href="#" title="Close form">Close form</a>');
					$form.append($close);
					
					_wrapper.show(); // Show the wrapper so the form can grow into it
	
					// Show the form
					$form.slideDown(600,function() {
						// Close button handler
						$close.hide().fadeIn('fast').click(function() {
							$form.slideUp(600, function() {
								$('#enquiry').remove();
							});
							return false;
						});
					});
					
				}); // end load
			}
		}     // end onAfter
	});     // end scrollTo
	return false;
}

// Hide form
function hide_form() {	
	$('.contact fieldset .panel').slideUp(400);
	return false;
}

// Create carousels based on jCarouselLite
WSHD.carousel = function() {
  $('.carousel').wrap('<div class="carousel_wrapper" />');
  
	$('.carousel_wrapper').each(function(i) {
		var _carousel = $(this).find('.carousel');
		var $nv; // number we're going to make visible at a time
		
		// Check to see if the carousel should auto run
		var $autorun = null;
		if ( _carousel.hasClass('autorun') ) {
			$autorun = 5000;
		}
		
		if ( _carousel.hasClass('show4') ) {
			$nv = 4;
		}
		else if ( _carousel.hasClass('show6') ) {
			$nv = 6;
		}
		else {
			$nv = null; // Don't run the carousel!
		}
		
		if ($nv != null) {
		
			// Create control classes
			var $btnNext = 'n' + i;
			var $btnPrev = 'p' + i;
			
			// Create the controls
			var $controls = $('<div class="controls" />');

			var $next = $('<button class="next" title="Next">Next</button>');
			$next.addClass($btnNext);
			
			var $prev = $('<button class="prev" title="Previous">Previous</button>');
			$prev.addClass($btnPrev);

			$controls.append($prev).append($next);

			// Insert the controls
			_carousel.before($controls);
			
			// Adjust the control class strings
			$btnNext = '.' + $btnNext;
			$btnPrev = '.' + $btnPrev;
			
			// Initiate the carousel
			$(this).jCarouselLite({
				btnNext: $btnNext,
				btnPrev: $btnPrev, 
				visible: $nv,
				scroll: 2,
				speed: 400,
				auto: $autorun
			});
			
		}
	});
}


