$(document).ready(function(){

	// Preloading images
	var img2preload = new Array("slideshow-button-hover.png", "slideshow-pause-hover.png", "slideshow-play-hover.png");
	var imgPath = "css/images/";
	for(var i = 0; i<img2preload; i++)
	{
		var $img = $("<img />");
		$img.attr("src", imgPath + img2preload[i]);
	}
	
	var slideshowNavOffsetL;
	$("#slideshow .holder ul").jcarousel({
	    scroll: 1,
	    wrap:"both",
	    auto: 6,
	    start: 1,
	    itemFirstInCallback:  {
		    onAfterAnimation: function(carousel, item, idx, state) {
				$('#slideshow .navigation ul a').removeClass('active');
				$('#slideshow .navigation ul a').eq(idx-1).addClass('active');
				
			},
			onBeforeAnimation: function(carousel, item, idx, state) {
				var $arr = $("#slideshow .arr");
				$arr.stop().animate({ 
					left: $("#slideshow .navigation ul a").eq(idx-1).offset().left - slideshowNavOffsetL - 4
				 }, "fast");
				 
				!$.browser.msie && $("#slideshow .data .slide:visible").fadeOut("fast", function(){
					$("#slideshow .data .slide").eq(idx-1).fadeIn();
				});
				
				if($.browser.msie)
				{
					$("#slideshow .data .slide:visible").hide();
					$("#slideshow .data .slide").eq(idx-1).show();
				}
			}
		},
	    initCallback: function(carousel) {
		    $('#slideshow .navigation ul a').bind('click', function() {
		        carousel.scroll($.jcarousel.intval($(this).text()));
		        return false;
		    });
		    
		    $("#slideshow .navigation .link-play").click(function() {
		    	carousel.startAuto();
		    	return false;
		    });	       
		    
		    $("#slideshow .navigation .link-pause").click(function() {
		    	carousel.stopAuto();
		    	return false;
		    });	       
		    
		    $("#slideshow .navigation ul").each(function() {
		    	$(this).css({
		    		float: "none",
		    		width: $(this).width()
		    	});
		    });
		    
		    slideshowNavOffsetL = $("#slideshow .navigation").offset().left;
		    $arr = $("#slideshow .arr");
		    $arr.css({
		    	left: $("#slideshow .navigation ul a:eq(0)").offset().left - slideshowNavOffsetL - 4
		    });
		    
		    $("#slideshow .data .slide").hide();
		    $("#slideshow .data .slide").eq(0).show();
		},
	    // This tells jCarousel NOT to autobuild prev/next buttons
	    buttonNextHTML: null,
	    buttonPrevHTML: null
	});
	
	$("#navigation ul li").each(function() {
		$(this).hover(function() {
			$(".dd, .dd-dialog", this).fadeIn();
			$("a:eq(0)", this).addClass("hover");
		}, function() {
			$(".dd, .dd-dialog", this).fadeOut();
			$("a:eq(0)", this).removeClass("hover");		
		});
	});
	
	$("a[rel=facebox]").facebox();
	
	$(".to-top").click(function() {
		$("html,body").animate({
			scrollTop: $(this).parents(".box").offset().top - 10
		}, "fast")
		return false;
	});
	
	$("#fader .holder li").simpleFade({
		init: function() {
			var fader = this;
			
			$("#fader .navigation a").click(function() {
				fader.fadeTo( $(this).parent().index() );
				return false;
			});
		},
		onFade:function(idx) {
			$("#fader .navigation a").removeClass("active").eq(idx).addClass("active");
		}
	});
	
	$(".link2top").click(function() {
		$("html,body").animate({
			scrollTop:0
		});
		return false;
	});
	
	$(".page-jump a").click(function() {
		var $href = $( $(this).attr('href') );
		if( $href.size() )
		{
			$("html,body").animate({
				scrollTop: $href.offset().top - 30
			});
		}
		return false;
	});
	
	$("#fnt-destination .link2top").each(function() {
		var ie6 = $.browser.msie && $.browser.version.substr(0,1) == 6;
		if(!ie6)
		{
			var $link = $(this);
			var $footer = $("#fn-ft");
			var initLeft = $link.offset().left - $link.parents(".shell").offset().left;
			var bottomSpacing = $footer.outerHeight();
			var defaultBottom = $("#fnt-destination").outerHeight() - $link.outerHeight() - $link.position().top;
			
			$link.css({
				position: "fixed"
			});
			
			$(window).resize(function() {
				$link.css({
					left: $link.parents(".shell").offset().left + initLeft
				});
			}).resize();
			
			$(window).scroll(function() {
				if( $(window).scrollTop() + $(window).height() > $footer.offset().top )
				{
					var tooMuch = $(window).scrollTop() + $(window).height() - $footer.offset().top;
					$link.css({
						bottom: defaultBottom + tooMuch
					});
				}
				else
				{
					$link.css({
						bottom: defaultBottom
					});
				}
			});
		}
	});

});

function htmlLoaded()
{
	if($.browser.msie && $.browser.version.substr(0,1) == 6 ) { 
		DD_belatedPNG.fix('#logo a, #navigation .dd-t, #navigation .dd-b, #slideshow .navigation .link-play, #slideshow .navigation .link-pause, #slideshow .navigation .arr, #slideshow .navigation');
	}
}
