
jQuery(function($){

	jQuery.fn.center = function(loaded) {
	    var obj = this;
	    body_width = parseInt($(window).width());
		body_height = parseInt($(window).height());
		block_width = parseInt(obj.width());
		block_height = parseInt(obj.height());
		
		left_position = parseInt((body_width/2) - (block_width/2)  + $(window).scrollLeft());
		if (body_width<block_width) { left_position = 0 + $(window).scrollLeft(); };
		top_position = parseInt((body_height/2) - (block_height/2) + $(window).scrollTop());
		if (body_height<block_height) { top_position = 0 + $(window).scrollTop(); };
		
		
	    if(!loaded) {
	    	
	    	obj.css({'position': 'absolute'});
			obj.css({'left': left_position, 'top': top_position});
	        $(window).bind('resize', function() { 
	        	obj.center(!loaded);
	    	});
			$(window).bind('scroll', function() { 
	        	obj.center(!loaded);
	    	});
	    	
	    } else {
	        obj.stop();
	        obj.css({'position': 'absolute'});
	        obj.animate({'left': left_position, 'top': top_position}, 200, 'linear');
	    }
	}	
	

	function show_overlay() {
		hide_overlay();
		$('body').prepend('<div id="overlay">&nbsp;</div>');
		setup_overlay_height();
		$('#overlay').fadeIn('fast');
	}
	function hide_overlay() {
		$('#overlay').remove();
	}
	function setup_overlay_height() {
		body_height = $(document).height();
		$('#overlay').css('height', body_height);
	}
	function hide_popups() {
		if ($('.popup').length) { $('.popup').fadeOut('fast') };
		$('#ajax-holder').remove();
		hide_overlay();
	}
	
	$('.popup').center();
	$('a[rel=popup]').live('click', function() {
		show_overlay();
		$('.popup').fadeIn();
		return false;
	});
	$('.popup .close').live('click', function() {
		hide_popups();
		return false;
	});
	
	$('#overlay').live('click', function() {
		hide_popups();
	});
	
	$(document).keypress(function(e){  
		if(e.keyCode==27){
			hide_popups();
		}
	});
	
	
	$('#footer .mini-col ul li, .arrowed ul li, .arrowed2 li').prepend('> ');
	
    $('.blink').
        focus(function() {
            if(this.title==this.value) {
                this.value = '';
            }
        }).
        blur(function(){
            if(this.value=='') {
                this.value = this.title;
            }
        });
        
    $('.arrowed h4 a').click(function(){
    	$(this).toggleClass('active');
    	$(this).parent().parent().find('ul').slideToggle();
    	return false;
    });
    
    
    $('.gallery ul').jcarousel({
    	scroll: 1,
    	visible: 1,
    	wrap: "both"
    });   
    
	$('#navigation .dd').each(function() {
		var w = $(this).find('.dd-expander').width() + 6;
		$(this).width(w);
	});
	
	$('#navigation ul li').hover(
		function() {
			$(this).addClass('hover');
			if($(this).find('.dd').length > 0) {
				$(this).find('.dd').css('visibility', 'visible');
				$(this).find('.filler').show();
			}
		},
		function() {
			$(this).removeClass('hover');
			if($(this).find('.dd').length > 0) {
				$(this).find('.dd').css('visibility', 'hidden');
				$(this).find('.filler').hide();
			}			
		}
	);
	
	$('.home-content p:last, .list-services li:last').addClass('last');
	
	function change_slide() {
		var current = $('.slideshow img').index($('.slideshow img:visible'));
		var total = $('.slideshow img').length;
		var next;
		if (current < total - 1) {
			next = current + 1;
		} else {
			next = 0;
		}
		$('.slideshow img:eq(' + current + ')').fadeOut(1000);
		$('.slideshow img:eq(' + next + ')').fadeIn(1000);
	}
	
	var interval = setInterval(change_slide, 7000);
	
	$('.inspiration-gallery a').fancybox({
		'transitionIn' : 'elastic',
		'transitionOut' : 'elastic',
		'speedIn' : 400, 
		'speedOut' : 200,
		'titlePosition': 'over',
		'overlayOpacity': 0.75,
		'overlayColor': '#20638E',
		'showNavArrows': true
	});
	
	$('#navigation ul li').each(function() {
		var filler = $(this).find('.filler');
		var w = $(this).width();
		filler.width(w+1);
	});
})

