$(document).ready(function() {                
	
	// initialize scrollable together with the navigator plugin
	//$("#browsable").scrollable().navigator();
	
  // match active page with a nav link
	activePage = $('body').attr('id').match(/[^p-].*/); // find body tag's id attribute then match anything after the initial 'p-'
	activeLink = $('ul#navigation').find('a#navigation-' + activePage); // find the matching link item
	$(activeLink).addClass('active'); // add '.active' class to link	
	
	/*email protect*/
	$("span.safemail").each(function(){
		exp = $(this).text().search(/\((.*?)\)/) != -1 ? new RegExp(/(.*?) \((.*?)\)/) : new RegExp(/.*/);
		match = exp.exec($(this).text());
		addr = match[1] ? match[1].replace(/ at /,"@").replace(/ dot /g,".") : match[0].replace(/ at /,"@").replace(/ dot /g,".");
		link = match[2] ? match[2] : addr;
		subject = $(this).attr('title') ? "?subject="+$(this).attr('title').replace(/ /g,"%20") : "";
		$(this).after('<a href="mailto:'+addr+subject+'">'+ link + '</a>');
		$(this).remove();
	});

	// Open links in new window when rel="external" is applyed to a tag
	$('a[rel="external"]').click(function(){
		this.target = "_blank";
	});	

  // Open colorbox links
	$('a.colorbox, input.colorbox').colorbox();	
	
  //hide email and add the hidden email_To on submit
  $("#enquiryform").submit(function() {
    var s1 = 'simon.mateljan';
    var s2 = '@';
    var s3 = 'webfirm.com';
    $(this).append('<input type="hidden" name="Email_To" value="' + s1 + s2 + s3 + '" />');
  });

	$("#product-nav li").hover(function(){
		$(this).find("ul:first").show();
	},function(){
		$(this).find("ul").hide();
	});
	
	$("#nsw").hover(function(){
		$(this).css("z-index", "10");
	}, function(){
		$(this).css("z-index", "5");		
	});
	
	$('#banner').cycle();
	
	$('ul.gallery').galleria({
				history   : true, // activates the history object for bookmarking, back-button etc.
				clickNext : true, // helper for making the image clickable
				insert    : '#main_image', // the containing selector for our main image
				onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

					// fade in the image & caption
					if(! ($.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
						image.css('display','none').fadeIn(1000);
					}
					caption.css('display','none').fadeIn(1000);

					// fetch the thumbnail container
					var _li = thumb.parents('li');

					// fade out inactive thumbnail
					_li.siblings().children('img.selected').fadeTo(500,0.3);

					// fade in active thumbnail
					thumb.fadeTo('fast',1).addClass('selected');

					// add a title for the clickable image
					image.attr('title','Next image >>');
				},
				onThumb : function(thumb) { // thumbnail effects goes here

					// fetch the thumbnail container
					var _li = thumb.parents('li');

					// if thumbnail is active, fade all the way.
					var _fadeTo = _li.is('.active') ? '1' : '0.3';

					// fade in the thumbnail when finnished loading
					thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);

					// hover effects
					thumb.hover(
						function() { thumb.fadeTo('fast',1); },
						function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
					)
				}
			});
	
	$('#map a').click(function(){
		 var area = $(this).attr("href").replace(/#/g,'');
		
		$('#boxes div').each(function(){
			var box = $(this);
			if(box.show())
			{
				$(this).hide();
			}
		});

		$('#' + area).show();
	});

  $.fn.clear_default = function(){
  return this.focus(function() {if( this.value == this.defaultValue ) {this.value = "";}})
  .blur(function() {if( !this.value.length ) {this.value = this.defaultValue;}});};

  //search box clear, replace
  $("#search .field").clear_default();

  $("form#order_form").validate();

	//Superfish
	$(".sf-menu").superfish();

});




