jQuery.noConflict();

jQuery(document).ready(function($) {
	
	// menu drop downs
	$("#nav-header li").hover(
		function () {
			$(this).find('ul:first').stop(true,true).slideDown(200);
			$(this).addClass('hover');
		}, 
		function () {
			$(this).find('ul:first').stop(true,true).slideUp(100);
			$(this).removeClass('hover');
		}
	);
	
	$('ul.sub-menu li:first-child').addClass('firstitem');
	
	$("ul li:first-child").addClass("first");
	$("ul li:last-child").addClass("last");	
	
	$(".message_box_content :last-child").addClass("last");
	
	$('ul.pagelist li:nth-child(2n+1)').addClass("firstcol");
	
	// submit form with a.submit
	$('a.submit').click ( function(e) {
		e.preventDefault();
		$('form').submit();
	});
	
	$("a.button").wrapInner("<span />");
	$("li.section").wrapInner("<div />");
	$("div.box, div.info, div.notice, div.success, div.error, div.details").wrapInner("<div class='message_box_content' />");
	
	// home page slide show
	$('.slideshow').cycle({
		fx: 'fade',
    speed:    1000, 
    timeout:  6000, 
    pause:  0,
    next:   '#slide-next', 
    prev:   '#slide-prev', 
    pager:  '#slide-nav', 
    sync:   1
	});
	
	
	// accordian
	// Set default open/close settings
	$('.acc_container').hide(); // Hide/close all containers
	$('.acc_trigger:first').addClass('active').next().slideDown('fast');
	
	// On Click
	$('.acc_trigger').click(function(){
		if( $(this).next().is(':hidden') ) { //If immediate next container is closed...
			$('.acc_trigger').removeClass('active').next().slideUp('fast'); //Remove all .acc_trigger classes and slide up the immediate next container
			$(this).toggleClass('active').next().slideDown('fast'); //Add .acc_trigger class to clicked trigger and slide down the immediate next container
		}
		return false;
	});
	
	$('.acc_container p:lastchild').addClass('last');
	
	
	
	// click to slide up
	$('.click-slide').click ( function(e) {
		e.preventDefault();
		$(this).slideUp('fast', function () {
			$(this).remove();
		});
	});
	$('.click-fade').click ( function(e) {
		e.preventDefault();
		$(this).fadeOut('fast', function () {
			$(this).remove();
		});
	});
	$('.click-hide').click ( function(e) {
		e.preventDefault();
		$(this).hide(function () {
			$(this).remove();
		});
	});
	$('.auto-slide').delay(5000).slideUp('normal', function () {
			$(this).remove();
		});
	
	//add a class to each link
	// based on what file it is linking to
	$('a[href]').each(function() {
		if((C = $(this).attr('href').match(/[.](doc|xls|pdf|ai|psd)$/))) {
			 $(this).addClass(C[1]);
		}
	});
	
	//external links - add class
	//$('a[href^="http://"]').addClass('external').attr("target", "_blank");
	//$('a:is([href*="theprintmachine.com.au"]):is([href^="#"]):is([href^="/"])').removeClass('external').attr("target", "_blank");
	

	// smooth scrolling
	function filterPath(string) {
		return string
			.replace(/^\//,'')
			.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
			.replace(/\/$/,'');
		}
  var locationPath = filterPath(location.pathname);
  var scrollElem = scrollableElement('html', 'body');

  $('a[href*=#]').each(function() {
    var thisPath = filterPath(this.pathname) || locationPath;
    if (  locationPath == thisPath
    && (location.hostname == this.hostname || !this.hostname)
    && this.hash.replace(/#/,'') ) {
      var $target = $(this.hash), target = this.hash;
      if (target) {
        var targetOffset = $target.offset().top;
        $(this).click(function(event) {
          event.preventDefault();
          $(scrollElem).animate({scrollTop: targetOffset}, 400, function() {
            location.hash = target;
          });
        });
      }
    }
  });

  // use the first element that is "scrollable"
  function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
  }
	
	$("input.zerofill").blur(function() {
		var val =  $(this).val()
		if (val == '') {
			$(this).val(0)
		}
	});
	
	// jump menu
	$(".jumpmenu").change(function() {
		$('.pricedisplay').fadeTo(10, 0.5);
		var val =  $(this).children("select option:selected").val()
		if (val != '') {
				location.href = val;
				$('form.fadeform select').attr("disabled","disabled");
				$('form.fadeform').fadeTo(10, 0.5);
		}
	});
	$(".resetpricecalc").click(function(e) {
		e.preventDefault();
		$('.pricedisplay').fadeTo(10, 0.5);
		var val =  $(this).attr("href");
		if (val != '') {
				location.href = val;
				$('form.fadeform select').attr("disabled","disabled");
				$('form.fadeform').fadeTo(10, 0.5);
		};
	});
	
});

// shadowbox
Shadowbox.init({
    overlayOpacity: 0.8
});
