jQuery(function( $ ){
	/**
	 * Most jQuery.localScroll's settings, actually belong to jQuery.ScrollTo, check it's demo for an example of each option.
	 * @see http://flesler.demos.com/jquery/scrollTo/
	 * You can use EVERY single setting of jQuery.ScrollTo, in the settings hash you send to jQuery.LocalScroll.
	 */
	 
	jQuery.easing.def = 'easeInExpo';
	
	/**
	 * Restart the scroll position to ( 0, 0 ) (Firefox doesn't reset it)
	 * could use $(target).scrollTo( 0, {axis:'xy'));
	 * but this needs to be quick(synchronous), to reset before $.localScroll.hash() begins
	 */
	$('#content').attr({scrollTop:0,scrollLeft:0});
	
	// Scroll initially if there's a hash (#something) in the url 
	$.localScroll.hash({
		target: '#content', //could be a selector or a jQuery object too.
		axis:'xy',//the default is 'y'
		queue:true,
		duration:1500
	});
	
	var $last = $([]);//save the last link
	
	/**
	 * NOTE: I use $.localScroll instead of $('#navigation').localScroll() so I
	 * also affect the >> and << links. I want every link in the page to scroll.
	 */
	$.localScroll({
		target: '#contentArea', //could be a selector or a jQuery object too.
		axis:'xy', //the default is 'y'
		queue:true,
		duration:1000,
		hash:true,
		onBefore:function( e, anchor, $target ){//'this' is the clicked link
			$last.removeClass('scrolling');
			$last = $(this).addClass('scrolling');
			if( this.blur )
				this.blur();//remove the awful outline
		},
		onAfter:function( anchor ){
			$last.removeClass('scrolling');
		}
	});
	
	/**
	 * Communication Center
	 * Please ensure that any content sent to boxy is wrapped in a single div.  Failure to properly wrap content may cause display / functionality errors.
	 */
	loadContacts();
	
	$('.toTop').click(function(e){
	    $.scrollTo(0,400);
    	return false;
    });


	lLamp = new LavaLamp('#lavaNav', {fx: 'backout', speed: 600 });
	
	var actuators = $('#lavaNav a');

	actuators.click(function() {
		lLamp.setCurr(actuators.index(this));
		return false;
	});
	
    $('.ajaxify').unbind('click').click(function(){
		$.blockUI({ message: null });

    	var rel		= $(this).attr('rel').split(' ');
    	var url		= $(this).attr('href');
    	
    	if ($.browser.msie){
    		$(rel[0]).hide();
    	}else{
			$(rel[0]).fadeOut();
    	}
    		
    	$(rel[0]).load(url, function(){

			if ($.browser.msie){
				$(rel[0]).show();
			}else{
	   			$(rel[0]).fadeIn();
    		}
    		
    		buildLinks();

			buildExternal();
	
			buildPSH();
			
			blogPosts();

			lightBox();
	
			reel();
			
			loadContacts();

    	});

		$.scrollTo(0,400);
		
		$('#contentArea').scrollTo(rel[1], 400, {onAfter:function(){window.location.hash = rel[1];}});
		
		lLamp.setCurr(actuators[rel[2]]);
		
		$.unblockUI();
    	
  		return false;
    });
    
    
    $('.interactive-portfolio').click(function(){
    	$.scrollTo(0,400);

			$("#flashNav a").click(function(){
				$("#flashOverlay").css("display", "none");
			});

			var flashvars = {
			
			bkgndTransparent:		"true",
			showFullscreenOption:	"false",
			useFlickr:				"false",
			xmlURL:					"/index.php/portfolio/gallery/",
			maxJPGSize:				"1200",
			useReloadButton:		"true",
			columns:				"4",
			showFlipButton:			"true"
		};
		
		var params = {
			allowFullScreen:		"true",
			wmode:					"transparent",
			bgcolor:				"#000000"
		};
		var attributes = {};
		
		swfobject.embedSWF("/flash/TiltViewer.swf", "flashcontent", "100%", "100%", "9.0.28", false, flashvars, params);
		
		var d = jQuery(document);

		$("#flashOverlay").css("display", "block").css({ height: d.height()});

		return false;


    });
    
	buildLinks();	
	
	buildExternal();
	
	buildPSH();

	blogPosts();
	
	lightBox();
	
	reel();
	
	loadContacts();
});

function loadContacts(){

	$('.communicate,#contactUs,.contactUs a').unbind('click').click(function(){

		$.get($(this).attr('href'), function(data){
			new Boxy(data, {
				'title': 'Contact Us',
				'modal': true,
				'unloadOnHide': true,
				afterShow: function() {
					this.getContent().css({height: (Boxy._viewport().height / 2) - 50});
					this.center();
					$.scrollTo(0,400);
				}
			});
		});
	
		return false;
	});
}

function buildPSH(){
	$('.portfolioShowHide').click(function(){
		$('.portfolioExpand').hide("slide", {direction: 'right'}, 600, function(){
			$('.portfolioShow').show();		
		});
		return false;
	});

	$('.portfolioShow').click(function(){
		$('.portfolioShow').hide("slide",{direction: 'left'}, 80);
		$('.portfolioExpand').show("slide", {direction: 'right'}, 600);
		return false;
	});
}

function reel(){
	$('.reel').unbind('click').click(function(){

		$.get($(this).attr('href'), function(data){
			new Boxy(data, {
				'title': 'Our Reel',
				'modal': true,
				'unloadOnHide': true,
				afterShow: function() {
					this.getContent().css({height: 345});
					this.center();
					
					player = $f("reel-player", "/reel/flowplayer.swf");
					player.load();

					$('.boxy-wrapper').attr('id','our-reel-wrapper');
				},
				beforeUnload: function() {
					$("reel-player").remove();
					player.unload();
				}
			});
		});
	
		return false;
	});
}

function communicationCenterPopup(){
	$.get('/index.php/communication/popup/', function(data){
		new Boxy(data, {
			'title': 'Contact Us',
			'modal': true,
			'unloadOnHide': true,
			afterShow: function() {
				this.getContent().css({height: (Boxy._viewport().height / 2) - 50});
				this.center();
				$.scrollTo(0,400);
			}
		});
	});
}

function flashReel(){
		$.get('/index.php/portfolio/reel/', function(data){
			new Boxy(data, {
				'title': 'Our Reel',
				'modal': true,
				'unloadOnHide': true,
				afterShow: function() {
					this.getContent().css({height: 345});
					this.center();
					
					player = $f("reel-player", "/reel/flowplayer.swf");
					player.load();

					$('.boxy-wrapper').attr('id','our-reel-wrapper');
				},
				beforeUnload: function() {
					$("reel-player").remove();
					player.unload();
				}
			});
		});
}

function flashInteractive(){
    	$.scrollTo(0,400);

			$("#flashNav a").click(function(){
				$("#flashOverlay").css("display", "none");
			});

			var flashvars = {
			
			bkgndTransparent:		"true",
			showFullscreenOption:	"false",
			useFlickr:				"false",
			xmlURL:					"/index.php/portfolio/gallery/",
			maxJPGSize:				"1200",
			useReloadButton:		"true",
			columns:				"4",
			showFlipButton:			"true"
		};
		
		var params = {
			allowFullScreen:		"true",
			wmode:					"transparent",
			bgcolor:				"#000000"
		};
		var attributes = {};
		
		swfobject.embedSWF("/flash/TiltViewer.swf", "flashcontent", "100%", "100%", "9.0.28", false, flashvars, params);
		
		var d = jQuery(document);

		$("#flashOverlay").css("display", "block").css({ height: d.height()});
}

function buildLinks(){
    $('.interior').unbind('click').click(function(){
    
	    $.blockUI({ message: null });
    
		if ($(this).parents('#sublevelNavigation').length > 0){

			$('#levelTwoSelected').attr('id',null);
			$('#levelTwoSelected').removeAttr('id');

			$(this).attr('id','levelTwoSelected');
		}

    	var content	= $(this).attr('rel');
    	var url		= $(this).attr('href');
    	
    	if ($.browser.msie)
    	{
    		$(content).hide();
    	}else{
 			$(content).fadeOut();
    	}
    	
    	if ($.browser.msie)
    	{
    		$('#interiorSubNavigation').hide().remove();
    	}else{
 			$('#interiorSubNavigation').fadeOut().remove();
    	}
    	
    	$(content).load(url, function(){
    	
    		try{
				$('#interiorSubNavigation').appendTo('#level3');

				if ($.browser.msie){
					$('#level3').show();
				}else{
					$('#level3').fadeIn();
    			}		
    		}catch(e){}
    		
    		if ($.browser.msie)
    		{
    			$(content).show();
    		}else{
 				$(content).fadeIn();
			}
					
			buildLinks();	
			
			buildExternal();
			
			buildPSH();
			
			blogPosts();

			lightBox();
			
			reel();

    	});
    	
    	$.unblockUI();
    	
    	return false;
    });
}

function buildExternal(){
	 $('a[rel="external"]').unbind('click').click(function() {
        window.open( $(this).attr('href') );
        return false;
	 });
}

function blogPosts(){
	
	$('.blogPost').unbind('click').click(function(){
	
		var url = $(this).attr('href');
		
		var title = ($(this).attr('title')) ? $(this).attr('title') : 'Blog Post';

		try{
			Boxy.get(this).hideAndUnload();
		}catch(e){}
		
		$.get(url, function(data){
			new Boxy(data, {
				'title': title,
				afterShow: function() {
					this.getContent().css({height: (Boxy._viewport().height * .85)});
					
					var d = jQuery(document);
					
					$('.boxy-wrapper').css({top:'100px'});
					
					jQuery('.boxy-modal-blackout').css({ height: d.height()});
					
					$.scrollTo(0,400);
							
					blogPosts();
			
					$("form").submit(function(){
						
						var inputs = [];
						
						$(':input', this).each(function() {
							if($(this).is(":checked") || $(this).attr("type") == "text" || $(this).attr("type") == "hidden"){
								inputs.push(this.name + '=' + escape(this.value));
							}
						});
						
						$('textarea', this).each(function() {
							inputs.push(this.name + '=' + escape(this.value));
						});
						
						jQuery.ajax({
							type: "POST",
							data: inputs.join('&'),
							url: this.action
						});

						
						Boxy.get(this).hide();

						return false;
					});
				}	
			});
		});
		
		return false;

	});
	
}

function lightBox(){
	$('.lightbox').click(function(){

		var image_link = $(this).attr('href');
		
		var data = '<div><img src="'+image_link+'" /></div>';
	
		new Boxy(data, {
			'title': $(this).attr('title'),
				afterShow: function() {

					$('.boxy-wrapper').css({top:'100px'});
				}	
		});
		
		return false;
	});
}