jQuery(document).ready(function() {

// ------------------------------------------------------------------- //
// ---------------- JQUERY TABS - /includes/jquery.js ---------------- //
// ------------------------------------------------------------------- //

	$('#product_tabs > ul').tabs();

// ---------------------------------------------------------------- //
// ---------------- JQUERY CLICKABLE BLOCKS ----------------------- //
// ---------------------------------------------------------------- //

	$('.block_clickable li').click(function(){
		window.location = $(this).find('a').attr('href');
		return false;
	});

// ------------------------------------------------------------------------------ //
// ---------------- JQUERY NAV MENU - /includes/jquery.js ----------------------- //
// ------------------------------------------------------------------------------ //

	$(document).ready(function() {
		$('#nav li').hover(function() { // over
			$(this).addClass('over');
		}, function() { // out
			$(this).removeClass('over');
		});
	});

// ------------------------------------------------------------------------------- //
// ---------------- JQUERY ACCORDIAN - /includes/ ----------------------- //
// ------------------------------------------------------------------------------- //

	accordian('#id1');
	accordian('#id2');
	accordian('#id3');
	accordian('#id4');

	function accordian(id) {
		$(id + ':eq(0) > div').hide();
		$(id + ':eq(0) > h3').click(function() {
			$(this).toggleClass("on");

			var $nextDiv = $(this).next();
			var $visibleSiblings = $nextDiv.siblings('p:visible');
			if ($visibleSiblings.length ) {
				$visibleSiblings.slideUp('fast', function() {
					$nextDiv.slideToggle('fast');
				});
			} else {
				$nextDiv.slideToggle('fast');
			}
		});
	}

  $('div.demo-show2:eq(0) > div').hide();
  $('div.demo-show2:eq(0) > h3').click(function() {
    $(this).next('div').slideToggle('fast')
    .siblings('div:visible').slideUp('fast');
  });


// ------------------------------------------------------- //
// ---------------- JQUERY SITEMAP ----------------------- //
// ------------------------------------------------------- //

	$('#sitemap > li').each(function() {
		if ($(this).find('li').size()) {
			$(this).find('ul').hide();
			$(this).append('<span class="collapsed"></span>');
			$(this).find('span').click(function() {
				ul = $(this).siblings('ul');
				ul.removeClass().addClass((ul.attr('display') == 'none' ? 'collapsed' : 'expanded'));
				ul.toggle();
			});
		}
	});

// --------------------------------------------------------------- //
// ---------------- JQUERY CLICKABLE BLOCK ----------------------- //
// --------------------------------------------------------------- //

	$(".quote h3").click(function(){
		window.location=$(this).find("a").attr("href");return false;
	});

// ----------------------------------------------------------------------------------- //
// ---------------- JQUERY MODAL WINDOW - /includes/jqModal.js ----------------------- //
// ----------------------------------------------------------------------------------- //

	$('body').append('<div id="modalMask"></div>');
	$('a[name^="modal-"]').click(function() {
		var id = '#' + $(this).attr('name').replace('modal-', '');
		
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
		
		$('#modalMask').css({'width': maskWidth,'height': maskHeight});
		$('#modalMask').show().animate({'opacity': '0.8'});
		
		var winH = $(window).height();
		var winW = $(window).width();
		
		$(id).css({'top':  winH/2-$(id).height(), 'left': winW/2-$(id).width()/2, 'position': 'fixed', 'z-index': '9999'}).fadeIn();
		
		// close modal code
		$('.modalClose, #modalMask').unbind('click').click(function () {
			$('#modalMask').animate({'opacity': '0'}, 'fast', function() { $(this).hide() });
			$(id).fadeOut('fast');
		});
		
		return false;
	});

// ------------------------------------------------------------------------------------------------ //
// ---------------- JQUERY CURVY CORNERS - /includes/jquery.curvycorners.js ----------------------- //
// ------------------------------------------------------------------------------------------------ //

	$('#side_menu_rounded_corners').corner({tl: { radius: 6 }, tr: { radius: 6 }, bl: { radius: 6 }, br: { radius: 6 }});
	$('.tabs_rounded_corners').corner({tl: { radius: 10 }, tr: { radius: 10 }, bl: { radius: 10 }, br: { radius: 10 }});

// ------------------------------------------------------------ //
// ---------------- JQUERY CONTACT FORM ----------------------- //
// ------------------------------------------------------------ //

// corner the Modal box's
	$('.modalWindow').corner();
	$('.modalInner').corner({tl: { radius: 6 }, tr: { radius: 6 }, bl: { radius: 6 }, br: { radius: 6 }});

	// focus/blur styles
	$('#contactus-form input, #contactus-form select, #contactus-form textarea').focus(function() {
		$(this).addClass('active-field');
	}).blur(function() {
		$(this).removeClass('active-field');
	});
	
	// submit handling
	$('#contactus-form-submit').click(function() {
		var errorState = false;
		var missingFields = new Array();
		
		// check required fields
		var requiredFields = new Array('Name', 'Email', 'Message');
		for (var i in requiredFields) {
			var field = $('*[name="' + requiredFields[i] + '"]');
			
			field.removeClass()
			if (!field.val()) {
				field.addClass('required-field');
				missingFields[missingFields.length] = requiredFields[i];
				errorState = true;
			}
		}
		
		// no errors?
		if (!errorState) {
			// AJAX
			$.ajax({
				'url':	'/_ajax.php',
				'data':	'app=contact-us&' + $('#contactus-form input, #contactus-form textarea, #contactus-form select').serialize(),
				'success': function(data) {
					$('#contactus-form .modalContent').fadeOut('fast', function() {
						$(this).html(data).fadeIn('fast');
					});
				},
				'beforeSend': function() {
					$('#contactus-form-submit').data('text', $('#contactus-form-submit').val());
					$('#contactus-form-submit').val('Sending, please wait...');
					$('#contactus-form-submit').attr('disabled', 1);
				},
				'error': function() {
					//alert('error');
					$('#contactus-form-submit').val('Error sending, please try again...');
					setTimeout(function() {
						$('#contactus-form-submit').removeAttr('disabled');
						$('#contactus-form-submit').val($('#contactus-form-submit').data('text'));
					}, 2000);
				}
			});
		} else {
			$('#contactus-form-submit').attr('disabled', 1);
			$('#contactus-form-submit').data('text', $('#contactus-form-submit').val());
			$('#contactus-form-submit').val('Missing required fields: ' + missingFields.join(', ') + '...');
			setTimeout(function() {
				$('#contactus-form-submit').val($('#contactus-form-submit').data('text'));
				$('#contactus-form-submit').removeAttr('disabled');
			}, 2000);
		}
	});
	
	$('.map').jqm();
}); //close doc ready

// -------------------------------------------------------------- //
// ---------------- JQUERY TOOLS CAROUSEL ----------------------- //
// -------------------------------------------------------------- //

	// CAROUSEL TABS
	$("div.tabs").tabs(".images > div", { 
		// enable "cross-fading" effect 
		fadeOutSpeed: "slow",
		fadeInSpeed: 1000,
		effect: 'fade',

		// start from the beginning after the last tab 
		rotate: true 

	// use the slideshow plugin. It accepts its own configuration 
	}).slideshow({autoplay: true, interval: 5000});

	// CONTENT TABS
	$("ul.content_tabs").tabs("> .pane");

// ------------------------------------------------------------------- //
// ---------------- JQUERY TOOLS IMAGE OVERLAY ----------------------- //
// ------------------------------------------------------------------- //
	$("img[rel]").overlay();

// ------------------------------------------------------------------- //
// ---------------- JQUERY ADD CSS CLASS WDW ----------------------- //
// ------------------------------------------------------------------- //
$(document).ready(function(){

	$("a[@href$=pdf]").addClass("pdf");

	$("a:not([@href*=http://www.northerm.yk.ca])").not("[href^=#]")
		.addClass("external")
		.attr({ target: "_blank" });

});
