// JavaScript Document
var ANIM_TIME = 800;
var SECTION_WIDTH = "725px";
var HEADING_WIDTH = "33px";

var about;
var services;
var portfolio;
var contact;
var aboutSub;
var servicesSub;
var portfolioSub;
var contactSub;
var current;
var currentSub;

var slides;
var curSlide;

$(document).ready(function(){
	//Portfolio list toggle
	slides = $("ul.portfolio_items");
	curSlide = slides[0];
	
	$("ul.portfolio_items").css("display", "none");
	$("ul.pager li.prev").css("visibility", "hidden");
	$(curSlide).css("display", "block");
	$("ul.pager a").click(function () {
		togglePortfolio($(this).attr('rel'));
	});
	
	//Mail link
	/*var mail_link = document.getElementById("mail_link");
	mail_link.onclick = function() {
		decode('ocknvq<qhhkegBxkxkfvgej0di',2);
	}*/
	
	about = $("div#about");
	services = $("div#services");
	portfolio = $("div#portfolio");
	contact = $("div#contact");
	
	aboutSub = $("div#about div.sub-section");
	servicesSub = $("div#services div.sub-section");
	portfolioSub = $("div#portfolio div.sub-section");
	contactSub = $("div#contact div.sub-section");
	
	current = about;
	currentSub = aboutSub;
	
	// Height
   	setHeight();
	
	$("div.section").css("width", HEADING_WIDTH);
	$(about).css("width", SECTION_WIDTH);
	
	//Hide content
	$("div.sub-section").css("display", "none");
	$(aboutSub).css("display", "block");
   
   //Click events
	$("div#about h2 a").click(function () {
										
	  if (about != current) {
		  $(this).addClass("current");
		  $("div#services h2 a").removeClass("current");
		  $("div#portfolio h2 a").removeClass("current");
		  $("div#contact h2 a").removeClass("current");
		  
		  $(currentSub).fadeOut();
		  $(about).animate({ 
			width: SECTION_WIDTH
		  }, ANIM_TIME, function() {
			  myCallBack(about, aboutSub);
		  } );
		  if (current == services) {
			  $(services).animate({
				width: HEADING_WIDTH
			  }, ANIM_TIME );
		  }
		  else if (current == contact) {
			  //$(contact).css("float", "right");
			  $(contact).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
		  else if (current == portfolio) {
			  $(portfolio).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
	  }
	  return false;
    });
	
	$("div#services h2 a").click(function () {								   
	  if (services != current) {
		  
		  $(this).addClass("current");
		  $("div#about h2 a").removeClass("current");
		  $("div#portfolio h2 a").removeClass("current");
		  $("div#contact h2 a").removeClass("current");
		  
		  $(currentSub).fadeOut();
		  $(services).animate({ 
			width: SECTION_WIDTH
		  }, ANIM_TIME, function() {
			  myCallBack(services, servicesSub);
		  } );
		  if (current == contact) {
			  //$(contact).css("float", "right");
			  $(contact).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME );
		  }
		  else if (current == about) {
			  $(about).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
		  else if (current == portfolio) {
			  $(portfolio).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
	  }
	  current = services;
	  return false;
    });
	
	$("div#portfolio h2 a").click(function () {								   
	  if (portfolio != current) {
		  
		  $(this).addClass("current");
		  $("div#about h2 a").removeClass("current");
		  $("div#contact h2 a").removeClass("current");
		  $("div#services h2 a").removeClass("current");
		  
		  $(currentSub).fadeOut();
		  $(portfolio).animate({ 
			width: SECTION_WIDTH
		  }, ANIM_TIME, function() {
			  myCallBack(portfolio, portfolioSub);
		  } );
		  if (current == contact) {
			  //$(contact).css("float", "right");
			  $(contact).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME );
		  }
		  else if (current == about) {
			  $(about).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
		  else if (current == services) {
			  $(services).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
	  }
	  current = portfolio;
	  return false;
    });
	
	$("div#contact h2 a").click(function () {									   
	  if (contact != current) {
		  //$(contact).css("float", "left");
		  
		  $(this).addClass("current");
		  $("div#about h2 a").removeClass("current");
		  $("div#services h2 a").removeClass("current");
		  $("div#portfolio h2 a").removeClass("current");
		  
		  $(currentSub).fadeOut();
		  $(contact).animate({ 
			width: SECTION_WIDTH
		  }, ANIM_TIME, function() {
			  myCallBack(contact, contactSub);
		  } );
		  if (current == services) {
			  $(services).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME );
		  }
		  else if (current == about) {
			  $(about).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
		  else if (current == portfolio) {
			  $(portfolio).animate({ 
				width: HEADING_WIDTH
			  }, ANIM_TIME);
		  }
	  }
	  return false;
	  current = contact;
    });
		
 });
function setHeight() {
	//$("div.sub-section").css("height", "auto");
	var maxH = 0;
	var sections = $("div.sub-section");
	//if(contHeight > 300) {
	for(i=0; i<sections.length; i++) {
		if($(sections[i]).height() > maxH) {
			maxH = $(sections[i]).height();
		}
		//alert(maxH);
	}
	if(maxH > 300) {
		$("div.section h2 a").css("height", maxH-40+"px");
	}
	else {
		$("div.section h2 a").css("height", "300px");
	}
	//}
	//else {
		//$("div.section h2 a").css("height", "300px");
	//}
}
function myCallBack (o, oSub) {
	current = o;
	currentSub = oSub;
	$(currentSub).fadeIn();
	//setHeight();
}
//Spam mails protection
function decode(l,shift)
{
	g = "";c = "";
	for ( i=0;i < l.length;i++)
	{c = l.charAt(i);g += String.fromCharCode( c.charCodeAt(0) - shift );}
	window.location.href = g;
}

function encode(l,shift)
{
	g = "";
	c = "";
	for ( i=0;i < l.length;i++)
	{
		c = l.charAt(i);
		g += String.fromCharCode( c.charCodeAt(0) + shift ) ;
	}

	prompt("Copy and paste the code below and put it in your a href=","javascript:decode('" +g+"'," +shift+");");

}
function togglePortfolio(o) {
	if(o == "0") {
		$("ul.pager li.prev").css("visibility", "hidden");
		$("ul.pager li.next").css("visibility", "visible");
	}
	else{
		$("ul.pager li.prev").css("visibility", "visible");
		$("ul.pager li.next").css("visibility", "hidden");
	}
	$(curSlide).fadeOut();
	curSlide = slides[o];
	$("span#curSlideNum").text(parseInt(o)+1);
	$(curSlide).fadeIn();
	
}
