$(document).ready(function() {

var bPort = "false";
var firstID = $('#firstID').attr('title');
var lastID = $('#lastID').attr('title');
var recentHash = "";

$('.active a').live('mouseover', function(){$(this).lightBox();});

	//Waits for thumbnails images to load before display them
	$('img.thumb').load(function(){
	  if($(this).height() > 159) {
		$('.thumb').fadeIn('slow');
	  }
	});

$('.workhover').fadeIn('slow');
//$('#logo').find("img").animate({opacity: 0.6}, 'fast');
$("#logo").hover(function(){
		$(this).find("img").animate({opacity: 0.6}, 'fast');
	}, function(){
		$(this).find("img").animate({opacity: 1}, 'fast');
	});
	
	$('.pagi_btn').find("img").animate({opacity: 0.2}, 'fast');
	$(".pagi_btn").hover(function(){
		$(this).find("img").animate({opacity: 1}, 'fast');
	}, function(){
		$(this).find("img").animate({opacity: 0.2}, 'fast');
	});
	
	//function reloadContact() { $("#contact_content").load("frontend/contact.php"); }

	$('.navlink2').live('click', function() {
		$("#contact_content").load("frontend/contact.php");
		return false;
	});
	
	// POST CONTACT MESSAGE FORM
	$('.btn_send_message').live('click', function() {
		$.post("frontend/contact.php", $("#contact_form").serialize(), function(da){ $("#contact_content").html(da).fadeIn('fast');
		});
		return false;
	});

	$('.btn_send_message').live('mouseover', function() {
			$(this).stop(false, true).animate({backgroundColor: '#295b97', color: '#f8f8f8', border: '1px solid #3e8dee'}, 'fast');
	}).live('mouseleave', function() {
			$(this).stop(false, true).animate({backgroundColor: '#fff', color: '#111', border: '1px solid #1e1e1e'}, 'slow');
		});
		
	$('#activework').live('mouseover', function() {
			$(".pagi_btn").stop(false, true).fadeIn('fast');
	}).live('mouseleave', function() {
			$(".pagi_btn").stop(false, true).fadeOut('fast');
		});
	
	//Thumbnail hover
	$(".work").hover(function(){
		$(this).find("a > .workhover").stop(false, true).animate({ top: '0px' }, 300 );
		$(this).find("a > .thumb").stop(false, true).animate({ 'top': '-180px' }, 300 );
	}, function(){
		$(this).find("a > .workhover").stop(false, true).animate({ top: '180px' }, 300 );
		$(this).find("a > .thumb").stop(false, true).animate({ 'top': '0px' }, 300 );
	});
	//END KON LOOK HERE
	function close(closethis) {
		$(closethis).animate({ height: 'hide', opacity: 'hide' });  
	}
	
	function open(openthis) {
		$(openthis).animate({ height: 'show', opacity: 'show' }, 'fast'); 
	}
	
	$('.a_contact').live('click', function() {
		if ( $('#contact_wrap').is(':visible')){
			close("#contact_wrap", function(){ $("#contact_content").load("frontend/contact.php") });
		} else {
			open("#contact_wrap");		
		}	
		return false;
	});
	
	$('.toggle_active').live('click', function() {
		if ( $('#activework').is(':visible')){
			close("#activework");
		} else {
			open("#activework");		
		}	
		return false;
	});
	
	
	//Close contact window when body is clicked.
	$('body').click(function(event) {
    if (!$(event.target).closest('#contact_wrap').length) {
        close("#contact_wrap", function(){ $("#contact_content").load("frontend/contact.php") });
		};
	});
	
	//To close the project window when body is clicked except for when lightbox script is active.
	$('body').click(function(event) {
    if (!$(event.target).closest('#activework').length && !$('#jquery-overlay').is(':visible')) {
        close("#activework"); window.location.hash = "";
		};
	});
	
	//Link hover effects.
	$("#social li").mouseover(function(){
		$(this).children(".tip").stop(false, true).fadeIn('fast');
		$(this).find("a > span").stop(false, true).animate({backgroundColor: '#295b97'}, 'fast');
	}).mouseleave (function(){ 
		$(this).children(".tip").stop(false, true).hide(); 
		$(this).find("a > span").stop(false, true).animate({backgroundColor: '#111'}, 'slow');
		
	});
	
	$(".weblink").mouseover(function(){
		$(this).stop(false, true).animate({backgroundColor: '#295b97', color: '#0c0c0c'}, 'slow');
	}).mouseleave (function(){ 
		$(this).stop(false, true).animate({backgroundColor: '#0c0c0c', color: '#f8f8f8'}, 'slow');
		
	});
	
	//ANIMATE CLOSE BUTTON
	$('.close').live('mouseover', function() {
		$(this).stop(false, true).animate({backgroundColor: '#295b97'}, 'fast');
	}).live('mouseleave', function() {
		$(this).stop(false, true).animate({backgroundColor: '#111'}, 'slow');
	});
	
	//ANIMATE CLOSE links
	$('a').live('mouseover', function() {
		$(this).stop(false, true).animate({color: '#3e8dee'}, 'slow');
	}).live('mouseleave', function() {
		$(this).stop(false, true).animate({color: '#295b97'}, 'slow');
	});
	
	$('.pagi_btn').live('mouseover', function() {
		$(this).children(".tip").fadeIn('fast');
	}).live('mouseleave', function() {
		$(this).children(".tip").hide();
	});
	//End of Hover effects.
	
	//Navigation between projects
	var speed = 500;
		var elementWidth = 870;
		var i = $(".work-wrapper").length;
		var difference = elementWidth * 6;
		var max = elementWidth * i;
		var maxshow = elementWidth * (i - 1);
		var current = 0;
		//$("#work_wrap").css("width", max + "px");
		
    	$("#prevProject").stop(false, true).click(function(event) {
    		if (current <= 0) { current = (6 - 1) * elementWidth;
			$("#work_wrap").animate({ left: "-" + current }, speed );
			window.location.hash = "#6";
    		} else { 
    		current -= elementWidth; 
				$("#work_wrap").animate({ left: "-" + current + "px"  }, speed );
				recentHash = window.location.hash;
				newID = recentHash.replace("#", "") - 1;
				window.location.hash = "#" + newID;
    		}	
			
			return false;
    	});
		
    	$("#nextProject").stop(false, true).click(function(event) {
    		if (current >= maxshow) { 
				$("#work_wrap").animate({ left: 0 }, speed ); current = 0;
				window.location.hash = "#1";
    		} else { 
    		current += elementWidth; 
				$("#work_wrap").animate({ left: "-" + current + "px" }, speed );
				recentHash = window.location.hash;
				newID = parseInt(recentHash.replace("#", "")) + 1;
				//newID =  newID ++;
				window.location.hash = "#" + newID;
    		}
			return false;
				//$("#work_wrap").animate({ left: "-" + current + "px" }, speed, function(){$(this).animate({ left: "-" + current - 3 + "px"}, 100, function(){$(this).animate({ left: "-" + current + 3 + "px"}, 100)})});
    	});
	
		//When the thumbnail is clicked navigate to the corresponding work project.
	$('.update').live('click', function() {
		close('#aboutMe');
		var id = $(this).attr('rel');
		window.location.hash = "#" +id;
		open("#activework");		
		current = (id - 1) * elementWidth;
		$("#work_wrap").animate({ left: "-" + current + "px" }, speed );
		return false;
	});
	
     if (window.location.hash==recentHash) {
     }
	 else {
     recentHash = window.location.hash;
	 recentHash = recentHash.replace("#", "");
		open("#activework");		
		current = (recentHash - 1) * elementWidth;
		$("#work_wrap").animate({ left: "-" + current + "px" }, speed );
		$("#footer").append(recentHash + " " + current);
	}
});