/* -------- jQuery -------- */

jQuery.fn.slideToggle = function(speed, easing, callback) {
  var h = this.height() + parseInt(this.css('paddingTop')) + parseInt(this.css('paddingBottom'));
  return this.animate({marginTop: parseInt(this.css('marginTop')) < 0 ? 315 : -h}, speed, easing, callback);  
};

jQuery.fn.slideUp = function(speed, easing, callback) {
  return this.animate({marginTop: 25}, 1500 , 'swing', callback);
};

jQuery.fn.slideDown = function(speed, easing, callback) {
  var h = this.height() + parseInt(this.css('paddingTop')) + parseInt(this.css('paddingBottom'));
  return this.animate({marginTop: 315}, 1000 , 'linear', callback);  
};

jQuery.fn.unwrap = function(expr) {
  return this.each(function(){
     $(this).parents(expr).eq(0).after($('#header, #nav, #left'),this).remove();
  });
};

function crossSlide () {
  // Slideshow header
  $('#slides').crossSlide({
	  shuffle: true,
	  speed: 12.5, //in px/sec
	  fade: 1.25 //in sec
  }, [
	  { src: 'img/header/1.jpg' },
//	  { src: 'img/header/2.jpg' },
	  { src: 'img/header/3.jpg' },
//	  { src: 'img/header/4.jpg', dir: 'up' },
	  { src: 'img/header/5.jpg', dir: 'down'   },
	  { src: 'img/header/6.jpg', dir: 'up'   },
	  { src: 'img/header/7.jpg' },
	  { src: 'img/header/8.jpg' },
	  { src: 'img/header/9.jpg' },
//	  { src: 'img/header/10.jpg' },
	  { src: 'img/header/11.jpg' },
	  { src: 'img/header/12.jpg' },
	  { src: 'img/header/13.jpg' },
	  { src: 'img/header/14.jpg' },
	  { src: 'img/header/15.jpg', dir: 'down'   },
	  { src: 'img/header/16.jpg', dir: 'up'   }
  ]);
}

$(document).ready(function() {
  // Intro
  var intro = document.getElementById("box-inner");
  if (intro){
	  $('#content, #footer, #bottom, #header, #nav, #top, #loader').hide();
	  $('#content H1').css({ "visibility":"hidden" }); /* Typeface */
	  $('#outer').css({ "height":"100%", "padding-top":"0px", "background":"url(img/intro_top.jpg) no-repeat" }).show();
	  $("#nav").fadeTo("slow", 0.1);

	  var $box = $('#box-inner').wrap('<div id="box-outer"></div>');
	  $('#intro_title, #intro_btn').click(function() {
		$box.slideUp('slow', false, function(){ 
			$('#intro_btn, #intro_title, #intro_bottom, #left').hide(); 
			$('#content, #footer').show();
			$('#outer').css({ "height":"auto", "background":"url(img/bg_top.jpg) no-repeat left top" });
			$('#box-inner').css({ "background-image":"url(img/bg_box.jpg)" });
		}).slideDown('slow', false, function(){
			$('#header, #nav, #top, #left, #bottom').show(); 
			$('#content H1').css({ "visibility":"visible" }); /* Typeface */
			$("#content").unwrap("#box-outer");
			$('#outer').css({ "background":"url(img/bg_box.jpg) repeat-y 6px" });
			crossSlide();
			$("#nav").fadeTo("slow", 0.8);
			$('#content').after('<br class="clear"/>');
		});
	  });
	  $('#intro_title, #intro_btn').bind('mouseover', function() { /*$('#intro_btn').addClass('over').removeClass('out');*/ $('#intro_btn').css({ "background-position":"left -150px" }); });
	  $('#intro_title, #intro_btn').bind('mouseout', function() { /*$('#intro_btn').addClass('out').removeClass('over');*/ $('#intro_btn').css({ "background-position":"left top" }); });
  } else {
	  crossSlide();
	  //window.setTimeout('crossSlide()', document.all ? 900 : 0); /* IE seems to need some extra time to think */
  }
  $('#nav').hover( function(e) { $('#nav').fadeTo("normal", 0.9) }, function(e) { $('#nav').fadeTo("slow", 0.8) });

  // Fancy fonts
  _typeface_js.renderDocument();

  // Place button clones on top Typeface 
  $('#nav a').each(function() {
	var $link = $(this).clone();
	$link.text("").addClass("clone").css({ "width":$(this).width() }).insertAfter(this);
  });
});
