$(document).ready(function(){

    $('a[href^="http://"]:not([href*='+document.domain+'])').attr("target", "_blank");

    // slideshow
    setTimeout(function() {
	$("#gallery").cycle({
	    cleartype:  1,
	    timeout: 0,
	    speed: 500,
	    //before: onBefore,
	    //after: onAfter,
	    //next: "#right-arrow",
	    //prev: "#left-arrow",
	    pager:  '#pagination',
	    pagerEvent: 'mouseover',
	    pagerClick: function(zeroBasedSlideIndex, slideElement) {
		//window.location = $(slideElement).find("a").attr("href");
	    },
	    pagerAnchorBuilder: function(idx, slide) { 
		// return selector string for existing anchor 
		return '#pagination li:eq(' + idx + ') a'; 
	    }
	});
    }, 1000);
    function onAfter(curr, next, opts) {
	$('.caption').fadeIn(1000);
    }
    function onBefore(curr, next, opts) {
	$('.caption').fadeOut(1000);
    }
    
    // current
    function filterPath(string) {
	return string
	    .replace(/^\//,'')
	    .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
	    .replace(/\/$/,'');
    } 
    $("#nav a").filter(function() {
	return filterPath(this.pathname) == filterPath(location.pathname);
    }).parent().addClass('current');
	
    // swap value
    swapValues = [];
    $(".swap").each(function(i){
	swapValues[i] = $(this).val();
	$(this).focus(function(){
	    if ($(this).val() == swapValues[i]) {
		$(this).val("");
	    }
	}).blur(function(){
	    if ($.trim($(this).val()) == "") {
		$(this).val(swapValues[i]);
	    }
	});
    });
    
    $("#side-menu li:last").addClass("last");
    
    // hovers
    $("#nav a .spanbg").hide();
    $("#nav a").hover(function(){
	$(this).find(".spanbg").stop(true,true).fadeIn(50);
    },function(){
	$(this).find(".spanbg").stop(true,true).fadeOut(200);
    });
    $("#nav li.current a .spanbg").fadeIn(50);
    
//    $("#gallery a").hover(function(){
//	$(this).find(".arrow").fadeIn(50);
//    },function(){
//	$(this).find(".arrow").stop().fadeOut(200);
//    });
    
    $("#pagination a").hover(function(){
	$(this).find("img").stop().animate({
	    opacity: "1"
	}, 100);
    },function(){
	$(this).find("img").stop().animate({
	    opacity: ".6"
	}, 500);
    });

    $("#side-menu a").click(function(e){
	e.preventDefault();
	$("#side-menu li").removeClass("current");
	$(this).parent().addClass("current");
	var toload = $(this).attr('href');
	$("#main li:visible").fadeOut(200,function(){
	    $(toload).fadeIn(500);
	});
    });
    
    $("#search-results a").each(function(){
	var link = parseUri($(this).attr('href')).path;
	$(this).attr('href',link.split("/")[1]);
    });
    
    // swish
    $("#main a").swish();
    $("#nav a").swish({hoverclass:"navhover"});
    
    // viz hacks
    $("#what-our-customers-are-saying h3").each(function(){
	$this = $(this);
	$(this).parent().append($this);
    });
    
    $("#our-team .film").each(function(){
	var href = $(this).find("h3").next().attr('href');
	$(this).find("h3").wrapInner($("<a href='"+href+"'></a>"));
    });
    
    $("#our-facility #films").masonry();

});