/* Author: Daniel Jones */

// remap jQuery to $
(function($){


$(function() {
    $("[name=s_choice]").click(function(){
            $('.toHide').hide();
            $("#blk-"+$(this).val()).show('slow');
    });
 });


$(function() {
    $("[name=toggler]").click(function(){
            $('.toHide').hide();
            $("#blk-"+$(this).val()).show('slow');
    });
 });

	Cufon.replace('ul.menu li a.off', {
		color: 'rgb(255,255,255)',
		hover: {
			color: '#5c8e13'
		}
	});
	
	Cufon.replace('ul.menu li a.on', {
		color: '#5c8e13',
		hover: {
			color: '#5c8e13'
		}
	});
	
	Cufon.replace('h3, h2, h4');
	
	Cufon.replace('.cufon');

	Cufon.CSS.ready(function() { 
	  onCufon();
	}); 
	
	var intID = null;
	
	function onCufon(){
		$("#siteloader").fadeOut('fast', function(){
			$("#container").fadeIn('fast');
			$("footer").fadeIn('fast');
			$(".breadcrumb").fadeIn('fast');
		});
		$('.slideloader').fadeOut("fast");
		
		 setInterval('movePanels', 500);
	}
	
	
	$("body").delegate("#aboutNav", "click", function(){
		$("#aboutSubnav li").toggle();
		return false
	});
	
	/*$("body").delegate("header", "mouseout", function(){
		$("#aboutSubnav li").fadeOut();
	});*/
	
	var slides = $('#homeslide').find('.slide');
	 
	$('#homeslide').before('<ul class="countDisplay">').cycle({ 
		fx:     'scrollLeft', 
		speed:  'slow',
		timeout: 10000, 
		pager:  '#nav',
		pager: '.countDisplay',
		pagerAnchorBuilder: pagination //,
		//before:	onBefore
	}).css({ display: "block" });

	function pagination(ind, dom) {
		return '<li><a href="#">&nbsp;</a></li>';
	}

	/* PROJECTS STUFF */
	
	var count = 1;
	var imageWidth = 0;
	var oldWidth = 0;
	var firstWidth = 100;
	
	
	
	var theFirstProject = $('#projectLinks li:eq(0)').attr('href');
	$('#theProject').load(theFirstProject);
	
	
	$('.projectLink').live("click", function(){

		var theClickedProject = $(this);
		
		$('.projectLink').each(function(){
			$(this).removeClass("on");
			$(theClickedProject).addClass("on");
			Cufon.refresh();
		});
		
		var theProject = $(this).attr("href");
		
		$("#loader").fadeIn();
		$("#theProject").fadeOut();
		
		$('#theProject').load(theProject, function() {
		  $("#loader").fadeOut();
		  $(this).fadeIn();
		  $(document).height($(body).height);
		  
		  $(".slidingFrameOuter .slidingFrame .wide img").each(function(ind){
				imageWidth = $(this).width();
				$(this).parent().css({ width: imageWidth });
				alert(imageWidth);
				
				if(ind == 0){
					firstWidth = imageWidth;
				}
				
				count = 1;
			});
			
			$(".columnMiddle").animate({
				width: firstWidth
			}, speed, 'easeInOutExpo');
		
			Cufon.refresh();
			
		});	

		return false;
	});
	
	
	
	$('.forwardButton').live("click", function(){

	if(count < $("#theProject .slidingFrameOuter .slidingFrame .wide").last().index()+1){	
	
		count++;
		
		oldCount = count - 1;
		var currentImage = "#image" + count;
		var previousImage = "#image" + oldCount;
		imageWidth = $(currentImage).parent().width();
		oldWidth = $(previousImage).parent().width();


			$("#theProject .slidingFrameOuter .slidingFrame").animate({
				left: "-=" + oldWidth + "px"
			}, 800, 'easeInOutExpo');
			
			$(".columnMiddle").animate({
				width: imageWidth,
			}, 800, 'easeInOutExpo');
		}
		
		var numOfImages = $("#theProject .slidingFrameOuter .slidingFrame .wide").last().index()+1;
		$('.imageCount').html(count + " / " + numOfImages);
		movePanels();
		
		return false;
		
	});
	
	$('.backButton').live("click",function(){
		
		if(count > 1){
			
		count--;
		
		oldCount = count;
		var currentImage = "#image" + count;
		var previousImage = "#image" + oldCount;
		imageWidth = $(currentImage).parent().width();
		oldWidth = $(previousImage).parent().width();
		
			$("#theProject .slidingFrameOuter .slidingFrame").animate({
				left: "+=" + oldWidth + "px"
			}, 800, 'easeInOutExpo');
			
			$(".columnMiddle").animate({
				width: imageWidth,
			}, 800, 'easeInOutExpo');
		}
		
		var numOfImages = $("#theProject .slidingFrameOuter .slidingFrame .wide").last().index()+1;
		$('.imageCount').html(count + " / " + numOfImages);
		movePanels();
		
		return false;
	});
	
	$("input, textarea").toggleVal();
	
	/*function movePanels(){
		
		var newH = $(document).height();
		var containerHeight = $("#container").height();
		var menuWrapHeight = $(".menuWrap").height();
		var leftColumnHeight = $(".leftColumn").height();
		var panelsHeight = $(".panels").height();
		
		var panelMargin = containerHeight - menuWrapHeight;

		$(".panels").animate({
			marginTop: "" + panelMargin + "px"
		}, 500, 'easeOutExpo');
		alert("asdaS");
		//clearInterval(intID);
		return false;
	}*/
	
	$(".inline").fancybox({
		'transitionIn'		: 'fade',
		'transitionOut'		: 'fade',
		'hideOnContentClick': false,
		'showCloseButton' : true
	}); 
	//movePanels();
	
	$(".jobLink").click(function(){
		var theJob = $(this).html();
		$("#jobPosition").html(theJob);
	});
	

	/*var footerHeight = $(".footerWrap").height();
	var containerHeight = $("#container").height();
	var diff = newH - containerHeight - footerHeight;
	
	$(".footerWrap").animate({
		marginTop: "" + diff + "px"
	}, 500, 'easeOutExpo');
	*/
	

})(this.jQuery);
