function init(){
	$('div#overlay').fadeOut('2000');
	itemScroll();
	lightbox();
	contactForm();

}
function contactForm(){

	$(function() {
	  $('.error').hide();
		$('input#name').click(function(e){
			e.preventDefault();
			$('#name_error').fadeOut();
		});
	
		$('input#email').click(function(e){
			e.preventDefault();
			$('#email_error').fadeOut();
		});
	
		$('textarea#phone').click(function(e){
			e.preventDefault();
			$('#phone_error').fadeOut();
		});
	
	  $(".button").click(function() {

	    $('.error').hide();
			
		  var name = $("input#name").val();
			if (name == "") {
	      $("label#name_error").show();
	      $("input#name").focus();
	      return false;
	    }
			var email = $("input#email").val();
			if (email == "") {
	      $("label#email_error").show();
	      $("input#email").focus();
	      return false;
	    }
			var phone = $("textarea#phone").val();
			if (phone == "") {
	      $("label#phone_error").show();
	      $("textarea#phone").focus();
	      return false;
	    }
			
			var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone;
			
			$.ajax({
	      type: "POST",
	      url: "bin/process.php",
	      data: dataString,
	      success: function() {
	        $('#contact_form').html("<div id='message'><h2>Success</h2><p>Thank you for your interest, I'll be in touch shortly.</p></div>").fadeIn('slow');
	      }
	     });
	    return false;
		});
	});
	
	runOnLoad(function(){
	  $("input#name").select().focus();
	});

}

function lightbox(){
	$('.lightbox').each(function(){
		
		$(this).bind('click',function(e){

			e.preventDefault();
			var $link = $(this).attr('href');
			$('body').css('overflow-y','hidden');
			$('<div id="lightbox"><p>Click to Close</p><div class="loader"></div></div>').appendTo('body').hide();
			$('#lightbox').fadeIn('slow',function(){
				$('<img src="#"/>').appendTo('#lightbox').hide();
				$('#lightbox img').attr('src','../_images/images/'+$link+'"').bind('load',function(){
					$('.loader').remove();
					$(this).fadeIn();
				});
			});
						
			$('#lightbox').bind('click',function(e){
				e.preventDefault();
				$(this).remove();
			});
		});
	});
}

// JavaScript Document
$.fn.preloader = function(options){
	var defaults = {
           delay:200,
					 //preload_parent:"a",
					 check_timer:300,
					 ondone:function(){ },
					 oneachload:function(image){  },
					 fadein:500 
					};
	
	// variables declaration and precaching images and parent container
	 var options = $.extend(defaults, options),
	 root = $(this) , images = root.find("img").css({"visibility":"hidden",opacity:0}) ,  timer ,  counter = 0, i=0 , checkFlag = [] , delaySum = options.delay ,
	 
	 init = function(){
		
		timer = setInterval(function(){
			
			if(counter>=checkFlag.length)
			{
			clearInterval(timer);
			options.ondone();
			return;
			}
		
			for(i=0;i<images.length;i++)
			{
				if(images[i].complete==true)
				{
					if(checkFlag[i]==false)
					{
						checkFlag[i] = true;
						options.oneachload(images[i]);
						counter++;
						
						delaySum = delaySum + options.delay;
					}
					
					$(images[i]).css("visibility","visible").delay(delaySum).animate({opacity:1},options.fadein,
					function(){ $(this).parent().removeClass("preloader");   });
					
					
					
				 
				}
			}
		
			},options.check_timer) 
		 
		 
		 } ;
	
	images.each(function(){
		
		if($(this).parent(options.preload_parent).length==0)
		$(this).wrap("<a class='preloader' />");
		else
		$(this).parent().addClass("preloader");
		
		checkFlag[i++] = false;
		
		
		}); 
	images = $.makeArray(images); 
	
	
	var icon = jQuery("<img />",{
		
		id : 'loadingicon' ,
		src : '_images/navigation/loading.gif'
		
		}).hide().appendTo("body");
	
	
	
	timer = setInterval(function(){
		
		if(icon[0].complete==true)
		{
			clearInterval(timer);
			init();
			 icon.remove();
			return;
		}
		
		},100);
	
	}
	
function experienceTabs(){
	$('#history > div').hide();
	$('#history div:first').show();
	$('#history_nav li:first').toggleClass('active');
	$('#history_nav li').bind('click', function(){
		if($(this).hasClass('active')){
			return false;
		}else{
			$('#history_nav li').removeClass('active');
			$(this).toggleClass('active');
			$('#history > div').hide();
			var activeTab = $(this).find('a').attr('href');
			$(activeTab).fadeIn();
			return false;
		}
	});
}

function topControl(){

	$('#portfolio_single').hide();
	$('#portfolio_grid li a').click(function(){
		
		if($('#portfolio_single').is(':visible')){
			}else{		
			$('#portfolio_single').slideToggle('slow');
			$('div#shadow').fadeIn('slow');
		} 
		return false;
	});
				
		
	$('#portfolio_close').click(function(){
	
		if($('#portfolio_single').is(':visible')){
			$('#portfolio_single').slideToggle('slow');
			$('div#shadow').fadeOut('fast');
			$('#portfolio_grid li.current').removeClass('current');
		}
		return false;
	});
}

function itemScroll(){
	
	$('#portfolio_grid li.active').first().addClass('current');
	
	var slideWidth = 960;
	var hasActive = [];
	var deckWidth = $('#portfolio_deck div.active').length*slideWidth+1920;
	var deckIndex = $('#portfolio_deck div.active').index();
  var gridIndex = $('.portfolio_grid li.active').index();   



	// active section control
	$('#navigation li a').bind('click',function(e){

		e.preventDefault();
	
		var currentPannel = $('#portfolio_grid li').hasClass('current');
	
		$('#navigation li').removeClass('active');
		$(this).parent('li').addClass('active');
		var tab = $('#navigation li.active').find('a').attr('href');

		
		if(tab == '#portfolio'){
			$('div.tab, div#section_bkgnd').fadeOut();
		}
	
		if(tab == '#resume'){
			$('div.tab').fadeOut();
			$('div#section_bkgnd,div#resume_tab').fadeIn();
		}
		
		if(tab == '#contact'){
			$('div.tab').fadeOut();
			$('div#section_bkgnd,div#contact_tab').fadeIn();
		}

		$('a#next,a#prev,ul#portfolio_grid li a').bind('click',function(){
			$('div.tab,div#section_bkgnd').fadeOut();
			$('#navigation li').removeClass('active');
			$('#navigation li:first-child').addClass('active');
		});
	});	

	sortScroll();
	repeatScroll();
	
	function repeatScroll(){
	
		$('#portfolio_deck').width(deckWidth);
		$('#portfolio_deck div.active').first().addClass('first');
		$('#portfolio_deck div.active').last().addClass('last');
		$('#portfolio_deck div.active.first').clone().appendTo('#portfolio_deck');
		$('#portfolio_deck div.active.last').clone().prependTo('#portfolio_deck');
		$('#portfolio_deck').css({'left':-1*slideWidth+'px'});
	};

	$('.portfolio_sort a').click(function(event){

		$('div.active').removeClass('first');
		$('div.active').removeClass('last');
		$('#portfolio_deck div.active').first().remove();
		$('#portfolio_deck div.active').last().remove();
		$('ul.portfolio_sort li.active').removeClass('active');
		$(this).parent().addClass('active');

		var filterVal = $(this).text().toLowerCase().replace(' ','-');

		if(filterVal == 'all') {
			$('ul#portfolio_grid li, #portfolio_deck div.single').fadeIn('slow').addClass('active').removeClass('hidden');
			$('ul#portfolio_sort li a#all').toggleClass('active');
		} else {

			$('ul#portfolio_grid li.print, ul#portfolio_grid li.logo, ul#portfolio_grid li.web, #portfolio_deck div.single').each(function(){
				if(!$(this).hasClass(filterVal)) {
					$(this).fadeOut('normal').addClass('hidden').removeClass('active');
				} else {
					$(this).fadeIn('slow').addClass('active').removeClass('hidden');
				}		
			});
		}
		
		deckWidth = $('#portfolio_deck div.active').length*slideWidth+1920;
		hasActive = $('#portfolio_deck div.active').length;
		$('#portfolio_deck').width(deckWidth);
		$('#portfolio_deck').animate({left: -slideWidth}, 'fast');
		$('#portfolio_grid li').removeClass('current');
		if ($('#portfolio_single').is(':visible')) {
			$('#portfolio_grid li.active').first().addClass('current');
		}
		
		repeatScroll();
		sortScroll();	
		return false;

	});
	
	function sortScroll(){
	
		var $index = $(this).parent().index()+1;
		var $slideIndex = $('div.single').length -2;
	
		$('ul#portfolio_grid li a').bind('click',function(){
			$('ul#portfolio_grid li').removeClass('current');
			$(this).parent().toggleClass('current');
		});
	
		$('ul#portfolio_grid li.active a').each(
		
			function(intIndex){
				$(this).unbind('click.test');
				$(this).bind("click.test",function(){
					
					$('#portfolio_deck').animate({
						left: -slideWidth*(intIndex+1)
					});

					return false;
				});
			}
		);
	}	

	// multi tab sorting

	$(document).ready(function(){
		tabs({
			block : '#item-5',
			block1 : '#item-6',
			block2 : '#item-7',
			block3 : '#item-8',
			block4 : '#item-9'
		});
	});
	var previous = [];
	var current = [];
	var tablist = [];
	
	function change(block){
	
		if(current[block].reference == previous[block].reference) return;
		
		if(jQuery.browser.msie && jQuery.browser.version.substr(0.3) == "6.0")
		
			$(block + ' .image#' + current[block].reference).show();
		
		else
		
			$(block + ' .image#' + current[block].reference).fadeIn();
			
		
		$(block + ' div.detail ul.item-nav li a[href=#' + previous[block].reference + ']').parent().removeClass('active');
		$(block + ' div.detail ul.item-nav li a[href=#' + current[block].reference + ']').parent().addClass('active');
		
		$('#' + previous[block].reference).hide();
		previous[block].reference = current[block].reference;
		
	}
	
	function Tab(blockid){

		var z = 0;
		this.block = blockid;
		
		this.next = function(){
		
			previous[this.block].reference = $(this.block + ' div.detail ul.item-nav li a').get()[z].href.split('#')[1];
			if(z >= $(this.block + ' div.detail ul.item-nav li a').get().length-1) z = 0; else z++;
			current[this.block].reference = $(this.block + ' div.detail ul.item-nav li a').get()[z].href.split('#')[1];
			change(this.block);
		
		};
	
	}
	
	function Reference(reference){
	
		this.reference = reference;
	
	}
	
	function tabs(tobj){

		for(key in tobj){
		
			var params = tobj[key].split('&');
			var block = params[0];
			
			$(block + " .image:not(:first)").hide();
			$(block + " .image:first").show();
			
			$(block + " div.detail ul.item-nav li a:first").addClass('select');
			
			previous[block] = new Reference(jQuery(block + " div.detail ul.item-nav li a:first").attr('href').split('#')[1]);
			current[block] = new Reference(jQuery(block + " div.detail ul.item-nav li a").get()[1].href.split('#')[1]);
			
			tablist[block] = new Tab(block);
			
			if(params[1] != undefined){
			
				interid = setInterval("tablist['" + block + "'].next()", params[1]);
				tablist[block].intervalid = interid;
			
			}
		
			$(block + " div.detail ul.item-nav li a").click(function(event){
			
				target = "#"+event.target.getAttribute("href").split('#')[1];
				tblock = '#'+$(target).parent().attr('id');
				
				current[tblock].reference = $(this).attr("href").split('#')[1];
				
				change(tblock);
				
				clearInterval(tablist[tblock].intervalid);
				
				return false;
			
			});
		
		}
	
	}

	// next click

	$('#next').click(function(){

		$('#portfolio_grid li.current').next('li.active').addClass('current');
		$('ul#portfolio_grid li.current').first().removeClass('current');

		hasActive = $('#portfolio_deck div.active').length;

		$('#portfolio_deck').stop('true','true'); // complete any animation running, incase anyones click happy
		var cloneLeft = $('#portfolio_deck').position().left-(1*slideWidth); // calculate new position which is the current poition minus the width of one image
		
		$('#portfolio_deck').animate({
			
			left : cloneLeft
			
			},function(){ // slide to new position
		
			if(Math.abs(cloneLeft) == ((hasActive)*slideWidth-slideWidth)){ // if slider is displaying the last image, which is the clone of the first image
				
				$('#portfolio_grid li.active').first().addClass('current');
				
				$('#portfolio_deck').css({
					
					left : -1*slideWidth
					
				}); // reset the slider back to the first image without animateing
				
			}
		
		});

		return false;
		
	});

	// prev click

	$('#prev').bind('click',function(){
		
		$('#portfolio_grid li.current').prev('li.active').addClass('current');
		$('ul#portfolio_grid li.current.active').last().removeClass('current');

		hasActive = $('#portfolio_deck div.active').length;

		$('#portfolio_deck').stop('true','true'); // complete any animation running, incase anyones click happy
		
		var cloneLeft = $('#portfolio_deck').position().left+(1*slideWidth); // calculate new position which is the current poition minus the width of one image

		$('#portfolio_deck').animate({left : cloneLeft+'px'},function(){ // slide to new position
			
			if(Math.abs(cloneLeft) == (0)){ // slider is displaying the first image, which is the clone of the last image

				$('#portfolio_grid li.active').last('li.active').addClass('current');

				$('#portfolio_deck').css({
					
					left : -(hasActive)*slideWidth+1920
					
					}); // reset the slider back to the first image without animateing
			}
		
		});
		return false;
	});

 
};
