$(document).ready(function() {
$(".thumbs").show();
$(".thumbs a:first").addClass("active");
var imageWidth = $(".images_wrap").width();
var imageSum = $(".images_reel img").size();
var imageReelWidth = imageWidth * imageSum;
$(".images_reel").css({'width' : imageReelWidth});
rotate = function(){    
    var triggerID = $active.attr("rel") - 1;
    var image_reelPosition = triggerID * imageWidth;
    hideComment();
    $(".thumbs a").removeClass('active');
    $active.addClass('active');
    $(".images_reel").animate({
        left: -image_reelPosition
    }, 500 );
    showComment();
};
rotateSwitch = function(){        
    play = setInterval(function(){
        $active = $('.thumbs a.active').next();
        if ( $active.length == 0) {
            $active = $('.thumbs a:first');
        }
        rotate();
    }, 7000);
};
rotateSwitch();
$(".images_reel a").hover(function() {
    clearInterval(play);
    play = null;
}, function() {
    rotateSwitch();
});    
showComment = function(){
    $(".link").eq( $('.thumbs a.active').attr("rel") - 1 ).slideDown("slow");
    return true;
};
showComment();
hideComment = function(){
    $(".link").stop(true,true).slideUp('normal');
};
$(".link").hover(function(){
   clearInterval(play);
    play = null;
}, function() {
   rotateSwitch();
});    
$(".thumbs a").click(function() {    
    $active = $(this);
    clearInterval(play);
    rotate();
    rotateSwitch();
    return false;
}); 

var tip;
$(".tip_trigger").hover(function(){
	tip = $(this).find('.tip').remove();
	$('body').append(tip);
	tip.fadeIn('fast');
}, function() {
	tip.hide().remove();
	$(this).append(tip);
}).mousemove(function(e) {
			var mousex = e.pageX + 20;
			var mousey = e.pageY + 20;
			var tipWidth = tip.width();
			var tipHeight = tip.height();
			var tipVisX = $(window).width() - (mousex + tipWidth);
			var tipVisY = $(window).height() - (mousey + tipHeight);
	if ( tipVisX < 20 ) {
		mousex = e.pageX - tipWidth - 20;
		$(this).find('.tip').css({  top: mousey, left: mousex });
	} if ( tipVisY < 20 ) {
		mousey = e.pageY - tipHeight - 20;
		tip.css({  top: mousey, left: mousex });
	} else {
		tip.css({  top: mousey, left: mousex });
	}
});

$(function() {
	var offset = $("#menu-left").offset();
    var topPadding = 20;
    $(window).scroll(function() {
        if ($(window).scrollTop() > offset.top) {
            $("#menu-left").stop().animate({
                marginTop: $(window).scrollTop() - offset.top + topPadding
            });
        } else {
            $("#menu-left").stop().animate({
                marginTop: 0
            });
        };
       });
    });	
});

