$(document).ready(function(){
	
	
	$('#left_nav.clients li').click(function(e){
		e.preventDefault();
		$('#left_nav.clients li').removeClass('active');
		var nameofclass = $(this).attr('class');
		var liClass = 'li.' + nameofclass;
		$('#main_content li').removeClass('active');
		$('#main_content ul').addClass('grey');
		$(this).addClass('active');
		$(liClass).addClass('active');
	});
	
	
	$('#kwicks_wrapper .kwicks').kwicks({  
		min : 72,
		sticky: true
	});
	
	$("#kwicks_wrapper .color:gt(0)").fadeTo(0,0);
	$("#kwicks_wrapper .bw:eq(0)").fadeTo(0,0);
	
	var t=setTimeout("$('#flash_intro img').show()",'1000');
	
	initPortfolio();
	initPortfolioGallery();
	initFadeOut();
	initSetFlashHeight();
	initCookieCheck();
	
	$('.right_content.clients ul').columnize({width:200});
});

function initCookieCheck() {
	if($.cookie('showFlash') != "no"){
		$('#id_1 #main_content').hide();
	}
}

function initSetFlashHeight(){
	if ($.browser.msie){
		var flashHeight = document.documentElement.clientHeight;
	}else{
		var flashHeight = window.innerHeight;
	}
	var flashAdjusted = flashHeight - 83;
	$('#flashintro_object').height(flashAdjusted);
	
}

function initFadeOut(){

	$('body').fadeIn("slow",0.0);
	$('body').fadeIn("slow",1.0);
	
	$('a').click(function(){
		if(!$(this).hasClass("nofade")){
			$('body').fadeOut("slow");
			setTimeout("nav('"+this.href+"')",1000);
			return false;
		}
	});
	
}

function nav(href){
		location.href=href;
	}


$(window).load(function(){
	$("#entrance").animate({
		opacity: 0
	}, 'fast', '', function(){
		$("#entrance").css('display', 'none');
	});
});

function initEntrance(){

	var width = $(window).width();
	var height = $(window).height();
	if($('body').height() > height){
		height = $('body').height();
	}
	
	if($('body').width() > width){
		width = $('body').width();
	}

	$("#entrance").css('opacity', 0);
	$("#entrance").animate({
		opacity: 0.9
	}, 'fast');
	$("#entrance").css('width', width);
	$("#entrance").css('height', height);
	
	var valignHeight =  ($(window).height() - $("#entrance h1").height()) / 2;
	
	
	$("#entrance h1").css('margin-top', valignHeight);
}

function initPortfolioGallery(){

	$("#hoverTarget").hover(function(){
		$("#gallery_holder").addClass('hoveredGallery');
	}, function(){
		$("#gallery_holder").removeClass('hoveredGallery');
	});

	var totalImages = $(".photo").length ;
	var loadedImages = 0;

	$("#hoverTarget").css('opacity', 0);
	$("#gallery_holder").prepend("<img id='loading' src='web_assets/css/images/preloader.gif' />");

	$(".photo").each(function(i){
		var number = i + 1;
		if(number < 15){
			$("#gallery_paging").append("<li><a href='javascript:hitImage(" + i + ")'>" + number + "</a></li>");
		}
		
		var width = 728;
		var height = 411;
		//grab the image and preload it.
		var src = "image.php?&src=" + $(this).find('a').attr('href') + "&h=" + height + "&w=" + width + "&zc=1";
		$('<img />').attr('src', src).load(function(){
        	loadedImages++;
        	if(loadedImages == totalImages){
        		$("#loading").animate({
        			opacity: 0
        		}, 500, function(){
        			$("#loading").remove();
        		});
        		$("#hoverTarget").animate({
        			opacity: 1
        		}, 1000, 'swing', function(){
        			
        		});
        	}
    	});
		
	});
	
	$("#gallery_left .bg, #gallery_right .bg").css('opacity', 0.65);
	
	$("#gallery_left").click(function(e){
		e.preventDefault();
		if(galleryIndex > 0){
			var nextIndex = galleryIndex - 1;
			hitImage(nextIndex);
		}
	});
	
	
	$("#gallery_right").click(function(e){
		e.preventDefault();
		if((galleryIndex + 1) < $(".photo").length){
			var nextIndex = galleryIndex + 1;
			hitImage(nextIndex);
		}
	});
	
	$(window).keyup(function(e){
		
		if(e.keyCode==37) {
			$("iframe").focus();
			$("#gallery_left").trigger('click');
		} else if (e.keyCode == 39) {
			$("iframe").focus();
			$("#gallery_right").trigger('click');
		}
	});
	
	if($(".photo").length > 0){
		hitImage(0);
	}
	
	
	
}

var galleryIndex = 0;

function hitImage(index){
	galleryIndex = index;
	
	if(galleryIndex > 0){
		$("#gallery_left").addClass('available');
	} else {
		$("#gallery_left").removeClass('available');
	}
	
	if(galleryIndex < ($(".photo").length - 1)){
		$("#gallery_right").addClass('available');
	} else {
		$("#gallery_right").removeClass('available');
	}
	
	var width = 728;
	var height = 411;
	var photo = $(".photo:eq(" + index + ")");
	var src = $("a", photo).attr('href');
	var caption = $("p", photo).html();

	/*
$("#imageWrap").animate({
		opacity: 0
	}, 500, 'swing', function(){
		$("#imageWrap").html("<img src='image.php?&src=" + src + "&h=" + height + "&w=" + width + "&zc=1' />");
		$("#imageWrap").animate({
			opacity: 1
		}, 500, 'swing');
	});
*/

	$("#imageWrap").html("<img src='image.php?&src=" + src + "&h=" + height + "&w=" + width + "&zc=1' />");
	$("#imageCaption").html(caption);
	$('#gallery_paging li a').addClass('nofade');
	$("#gallery_paging li a").removeClass('active');
	$("#gallery_paging li a:eq(" + index + ")").addClass('active');
}



function initPortfolio(){
	$(".crossed_images .alt").css('opacity', 0);
	$(".portfolio_thumb").each(function(i){
	
	//use opacity as well.
	
		$(this).hover(
			function(){
				$(this).addClass('hovered');
				$(".portfolio_thumb:not(.portfolio_thumb:eq(" + i + ")) .crossed_images").hide();
				$(".portfolio_thumb:not(.portfolio_thumb:eq(" + i + ")) .bw_port").show();
				
				$(".portfolio_thumb:eq(" + i + ") .crossed_images .alt").animate({
					opacity: 1
				}, 100);
				$(".portfolio_thumb:eq(" + i + ") .crossed_images .color_port").animate({
					opacity: 0
				}, 200);
				
			}, function(){
				$(this).removeClass('hovered');
				$(".portfolio_thumb:not(.portfolio_thumb:eq(" + i + ")) .crossed_images").show();
				$(".portfolio_thumb:not(.portfolio_thumb:eq(" + i + ")) .bw_port").hide();
				
				$(".portfolio_thumb:eq(" + i + ") .crossed_images .alt").animate({
					opacity: 0
				}, 200);
				$(".portfolio_thumb:eq(" + i + ") .crossed_images .color_port").animate({
					opacity: 1
				}, 100);
				
			}
		);
		
		$(this).click(function(e){
			e.preventDefault();
			var target = $(this).attr('id') + "_link";
			$("#" + target).trigger('click');
		});
	
	});
}


	
