$(document).ready(function(){
	
	var productNav = $('#product-nav');
	var supernav = $('#supernav');
	
	// Main navigation listener
	$('#product-nav, #supernav').live('mouseover mouseout', function(event) {
		
		if (event.type == 'mouseover') {
			supernav.stop().animate({
				"height" : "270px"
			}, 300);
			productNav.addClass('active');
		} else {
			supernav.stop().animate({
				"height" : "0"
			}, 300);
			productNav.removeClass('active');
		}
		
	});
	
	// Request a quote drop down menu listener
	$('a#request-quote').live('click', function() {
		$('#form-container').stop().animate({
			height: '470px'
		}, 300, function(){
			$('#form-container #handle').show();
			$(this).css('overflow', 'visible');
		});
	});

		// Request a quote form close button listener
	$('#form-container a.close').live('click', function() {
		$('#form-container').stop().animate({
			height: '0'
		}, 300, function(){
			$('#form-container #handle').hide();
			$(this).css('overflow', 'hidden');
		});
	});
	
	// Request a quote form submit listener
	$('#quoteForm').validate({
		rules: {
			'data[Quote][firstName]': "required",
			'data[Quote][lastName]': "required",
			'data[Quote][company]': "required",
			'data[Quote][jobTitle]': "required",
			'data[Quote][address]': "required",
			'data[Quote][city]': "required",
			'data[Quote][province]': "required",
			'data[Quote][postal]': "required",
			'data[Quote][phone]': "required",
			'data[Quote][email]': {	required: true,
									email: true
								},
			'data[Quote][productName]': "required",
			'data[Quote][productQuantity]': "required"
		},
		errorClass: "invalid"
	});

	
	
	// Enable sub menu accordian if exists
	if ($('.sub-navigation').length > 0) {

		// Close all slides
		$('.sub-navigation ul').each(function() {
			if ($(this).attr('class') != 'active') {
				$(this).hide();
			}
		});
		
		$('.sub-navigation h3 a').click(function() {

			var listContents = $(this).parent().next();
			
			// Remove "active" class from all links
			listContents.removeClass('active');
			  
			// Close all open slides
			$('.sub-navigation ul').slideUp('normal');
	   
			// Open next slide if closed
			if(listContents.is(':hidden') == true) {
				
				// Add "active" class to link
				listContents.addClass('active');
				  
				// Open slide
				listContents.slideDown('normal');
			 } 
			  
		 });

	}
	
	// Project list listener
	if ($('.project').length > 0) {

		// Project thumbnail click listener
		$('.project #project-thumbnails a').live('click', function() {
			
			var link = $(this).attr('href');
			var fullImg = link.replace('medium','full');
			if (link == '#') {
				return false;
			}
			$('.project #project-medium a').attr('href', fullImg);
			$('.project #project-medium a img').attr('src', link);
			
		    Shadowbox.setup();

			return false;
		});
			
	};
	
	// Contact list region hover listener
	$('#contactList ul li a').live('mouseover mouseout', function(event) {
		
		var regionCode = $(this).attr('id');
		var region = $('#regions #'+regionCode);
				
		if (event.type == 'mouseover') {
			
			region.stop().animate({
				opacity: 1.0
			}, 300);
			
		} else {
			
			if ($(this).attr('class') != 'current') {
				region.stop().animate({
					opacity: 0
				}, 300);
			}
		}
		
	});
	
	// Contact list region click listener
	$('#contactList ul li a').live('click', function() {

		var link = $(this).attr('href')+'/a';
		var regionCode = $(this).attr('id');
		var region = $('#regions #'+regionCode);
		var newWidth = 200 + 'px';
		
		// Animate open reps block on first load
		if ($('#reps').attr('class') != 'open') {
			$('#reps').addClass('open');
			$('#reps').stop().animate({
				opacity: 1.0
			}, 300);
			$('#contact a.close').show();
		}
		
		// Populate title
		$('#contact h1 span').html($(this).html());
		
		// Get contacts data
		$.ajax({
			url: link,
			success: function(data) {
				$('#reps').html(data);
			}
		});

		// Fade out current class
		$('#regions .current').stop().animate({
			opacity: 0
			}, 300, function() {
				$('#regions .region').removeClass('current');
			});
		
		// Reset "current" class
		$('#contactList ul li a').removeClass('current');
		$(this).addClass('current');
		
		// Remove expanded region
		$('#regions .expanded').stop().animate({
			opacity: 0
		}, 300, function(){
			$(this).remove();
		});
		
		region.clone().attr('id',regionCode+'Copy').addClass('expanded').appendTo('#regions').attr('src','/img/contact/large/'+regionCode+'.png');
		$('#regions #'+regionCode+'Copy').stop().animate({
				left: '-230px',
				top: '30px',
				width: newWidth
			}, 300, function(){
				// $(this).attr('src', '/img/contact/large/'+regionCode+'.png');
				region.addClass('current');
			});
		
		return false;
		
	});
	
	
	// Contact list close button listener
	$('#contact a.close').live('click', function() {
		
		// Animate open reps block on first load
		if ($('#reps').attr('class') == 'open') {
			$('#reps').removeClass('open');
			$('#reps').stop().animate({
				opacity: 0
			}, 300);
			$(this).hide();
		}
		
		$('#contact h1 span').html('');

		// Fade out current class
		$('#regions .current').stop().animate({
			opacity: 0
			}, 300, function() {
				$('#regions .region').removeClass('current');
			});
		
		// Reset "current" class
		$('#contactList ul li a').removeClass('current');
		
		// Remove expanded region
		$('#regions .expanded').stop().animate({
			opacity: 0
		}, 300, function(){
			$(this).remove();
		});
		
		return false;
		
	});
	
	// Disable click through for all placeholder links with # and empty href
	$('a').live('click', function() {
		var linkTarget = $(this).attr('href');
		if (linkTarget == '' || linkTarget == '#') {
			return false;
		}
	});

	// Find slidwshow and enable cycle plugin
	if ($('.carousel').length > 0) {
	    $('.carousel').cycle({
	        fx:     'fade',
	        speed:	1000,
	        timeout: 5000,
	        pager:  '.pager',
	        pagerAnchorBuilder: function(idx, slide) {
	            // Return sel string for existing anchor
	            return '<li><a href="#">' + slide.title + '</a></li>'; 
	        }
	    });
	};
	
});
