jQuery(function( $ ){

	closeCB = function() {
		$.fn.colorbox.close();
	}

	gotoNav = function(newhash) {
		$.scrollTo( $(newhash), { duration:1000, axis:'xy'});
		//setSubNav(hash);
	}
	
	$('.newswrap img').wrap('<div class="imgwrap" />');
	$('#news .column img').wrap('<div class="imgwrap" />');
	
	$('.imgwrap').css('position','relative');
	$('.imgwrap img').css('position','absolute');
	$('.imgwrap img').each(function() {
		var imgh = $(this).height();
		var imgw = $(this).width();
		if(imgh>138) {
			var hdiff = (108-imgh)/2;
			$(this).css('top',hdiff);
		}
		if(imgw>235) {
			var wdiff = (235-imgw)/2;
			$(this).css('left',wdiff);
		}
	});
	
	var d = 0;
	$('.client-thumbs div').each(function() {
		d += 75;							  
		$(this).delay(d).fadeIn(1000);								  
	});
	//alert($('.client-img')[0]);
	$('.client-img').eq(0).delay(1000).fadeIn(1000);
	$('.client-thumbs div').click(function() {
		$('.client-img').fadeOut(200);
		$('#'+$(this).attr('class')).delay(200).fadeIn(200);
	});
	
	$(".iframe").colorbox({width:"960", height:"560", scrollbars:false, iframe:true, opacity:.8});
	$('.iframe').click(function() {
		if($(this).attr('href').indexOf('section=work')>0) {
			$('.backwrap .backbtn').css('background','url(_img/back-portfolio.png) no-repeat');
		} else {
			$('.backwrap .backbtn').css('background','url(_img/back-capabilities.png) no-repeat');
		}
	});
	$('#cboxOverlay').append('<div class="backwrap"><div class="backbtn"></div></div>');
	$('.backwrap').css({'top':($(window).height()/2)-280, 'left':($(window).width()/2)-480});
	
	$(window).resize(function() {
		$('.backwrap').css({'top':($(window).height()/2)-280, 'left':($(window).width()/2)-480});
	});

	
	setSubNav();
	/**
	 * 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.
	*/
	
	// The default axis is 'y', but in this demo, I want to scroll both
	// You can modify any default like this
	$.localScroll.defaults.axis = 'xy';
	$.localScroll.defaults.queue = true;
	$.localScroll.defaults.duration = 1000;
	$.localScroll.defaults.margin = 'auto';
	jQuery.easing.def = "easeInOutQuad"; 
	
	/* Scroll initially if there's a hash (#something) in the url 
	
	if(window.location.hash == '') {
		$('#wrap').scrollTo( '#home', { duration:1, axis:'xy'});
	} else if(window.location.hash == '#home') {
		window.location.hash == '';
		$('#wrap').scrollTo( '#home', { duration:1, axis:'xy'});
	}  else {
		$.localScroll.hash({
			queue:true,
			duration:1
		});
	}
	
	$('#nav a').add('a.scroll').click(function () {
        var target = $(this.hash);
        var hash = this.hash;
        // $target = $target.length && $target
        // || $('[name=' + this.hash.slice(1) +']');
		setSubNav(hash);
        if (target.length) {
			window.location.hash = hash;
            var targetOffset = target.offset().top;
            $('#wrap').scrollTo( targetOffset, { duration:1000, axis:'xy'});
		}
	});
	*/
	
		// Scroll initially if there's a hash (#something) in the url 
	if(window.location.hash!='') {
		window.location.hash = '';
		setSubNav();
	}
		
	/**
	 * 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({
		hash:true,
		onAfter:function( anchor, settings ){
			setSubNav();
		}
	});


		
	/**
	 * 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({
		hash:true,
		onBefore:function( e, anchor, $target ){
			//alert(window.location.hash);
		},
		onAfter:function( anchor, settings ){			
			setSubNav();
		}
	});*/
	 
	$('.team-tammy').hide();
	$('.team-alison').hide();
	
	$('.newsitem2').hide();
	$('.newsitem3').hide();
	
	var thisbio = 'zahra';
	//var prevbio = 'zahra';
	var nextbio = 'alison';
	var oldbio = '';
	
	$('#team .left-link').css('display','none');
	$('#team .left-link').click(function() {
		$('.team-'+thisbio).fadeOut(500);
		$('.team-'+prevbio).fadeIn(500);
		oldbio = thisbio; 
		thisbio = prevbio;
		prevbio = nextbio;
		nextbio = oldbio;
		$('#link-'+thisbio).addClass("active");
		$('#link-'+prevbio).removeClass("active");
		$('#link-'+nextbio).removeClass("active");
	});
	
	$('#team .right-link').click(function() {
		/*
		oldbio = thisbio; 
		thisbio = nextbio;
		nextbio = prevbio;
		prevbio = oldbio;
		*/
		if(thisbio=='zahra') {
			thisbio = 'alison';
			nextbio = 'zahra';	
		} else {
			thisbio = 'zahra';
			nextbio = 'alison';	
		}
		$('.team-'+nextbio).fadeOut(500);
		$('.team-'+thisbio).fadeIn(500);
		$('#link-'+thisbio).addClass("active");
		$('#link-'+prevbio).removeClass("active");
		$('#link-'+nextbio).removeClass("active");
	});
	
	var thisnews = '1';
	var prevnews = '2';
	var nextnews = '3';
	var oldnews = '';
	
	$('.newswrap .left-link').click(function() {
		$('.newsitem'+thisnews).fadeOut(500);
		$('.newsitem'+prevnews).fadeIn(500);
		oldnews = thisnews; 
		thisnews = prevnews;
		prevnews = nextnews;
		nextnews = oldnews;
	});
	
	$('.newswrap .right-link').click(function() {
		$('.newsitem'+thisnews).fadeOut(500);
		$('.newsitem'+nextnews).fadeIn(500);
		oldnews = thisnews; 
		thisnews = nextnews;
		nextnews = prevnews;
		prevnews = oldnews;
	});
	
	$('body').addClass("bodybg");
});
