var headOpened = null;
var pageloadini = false;

$(function() {
 $("#content").hide();
 if(uriPath != '/')
  document.location = '/#'+uriPath;
 $.historyInit(pageload);
 if(!pageloadini) {
  $("#content").show();
  pageloadini = true;
 }

 $("#contactsClose").click(contactsCloseClick);
 $("#contactsTrigger").click(contactsTriggerClick);
 $("#profileClose").click(profileCloseClick);
 $("#profileTrigger").click(profileTriggerClick);
 $("a.ll").live('click', llClick);
});

function pageload(hash) {
 if(hash) {
  if(pageloadini) {
   var h = $("#ch").height();
   $("#ch").height(h);
   $(document).scrollTop(0);
   $("#co").animate({opacity:'toggle'}, llDuration.fadeOut, function() {
    loadC(hash);
   });
  }
  else {
   $("#co").hide();
   $("#ch").height(0);
   $("#content").show();
   loadC(hash);
  }
 }
 pageloadini = true;
}

function loadC(hash) {
 $("#ch").addClass('hi');
 $("#content").load(hash, function() {
  var h = $("#co").height();
  $("#ch").removeClass('hi');
  $("#ch").animate({height:h}, llDuration.hMod, function() {
   $("#co").animate({opacity:'toggle'}, llDuration.fadeIn);
  });
 });
}

function llClick() {
 contactsCloseClick();
 profileCloseClick();
 var hash = $(this).attr('href');
 hash = hash.replace(/^.*#/, '');
 $.historyLoad(hash);
 return false;
}

function ajModifyLink(index) {
 var url = $(this).attr('href');
 $(this).removeClass("aj").addClass("ll").attr('href', '/#'+url);
}

function ajModify() {
 $("a.aj").each(ajModifyLink);
}

function contactsCloseClick() {
 if(headOpened == 'contacts') {
  $("#contacts").animate({height:'toggle',opacity:'toggle'}, headDuration.contacts);
  headOpened = null;
 }
}

function profileCloseClick() {
 if(headOpened == 'profile') {
  $("#profile").animate({height:'toggle',opacity:'toggle'}, headDuration.profile);
  headOpened = null;
 }
}

function contactsTriggerClick() {
 if(headOpened == 'profile')
  $("#profile").animate({height:'toggle',opacity:'toggle'}, headDuration.profile, function() {$("#contacts").animate({height:'toggle',opacity:'toggle'}, headDuration.contacts)});
 else
  $("#contacts").animate({height:'toggle',opacity:'toggle'}, headDuration.contacts);
 headOpened = (headOpened == 'contacts')? null: 'contacts';
}

function profileTriggerClick() {
 if(headOpened == 'contacts')
  $("#contacts").animate({height:'toggle',opacity:'toggle'}, headDuration.contacts, function() {$("#profile").animate({height:'toggle',opacity:'toggle'}, headDuration.profile)});
 else
  $("#profile").animate({height:'toggle',opacity:'toggle'}, headDuration.profile);
 headOpened = (headOpened == 'profile')? null: 'profile';
}

function alignTab(tab) {
 var maxH = 0;
	$("div.item", tab).each(function() {maxH = Math.max(maxH, $(this).height())});
	$("div.item", tab).height(maxH);
}
