// Run the scripts
$(document).ready(function () {
    initLightbox();

    if ($('.help_messages').length != 0) {
        if ($('.help_messages').html().trim() == '') {
            $('.help_messages').hide();
        }
    }

    $("#video_preview .video").fancybox({
        'overlayShow': true,
        'frameWidth': 600,
        'frameHeight': 400
    });

    $(document).pngFix();

    $('.required label,.optional label').mgToolTipIcons();
    mgToolTipsHover();

    $("#message_board #messages").animate({ scrollTop: $("#message_board #messages").attr("scrollHeight") }, 2000);

    var h = $("#tabs_content .inner").height();
    /*if ($.browser.webkit) {
        h += 50;
    }*/
    $("#autoselect_overlay").height(h);

    $("a.anchorLink").anchorAnimate();
});

// JQuery Lightbox
// http://leandrovieira.com/projects/jquery/lightbox/

function initLightbox(){
	$('a[rel=lightbox]').lightBox();
};

// Mongoose JQuery Tooltips

// First create Tool Tip Icons based off the Label Titles
$.fn.mgToolTipIcons = function(){
	return this.each(function(){
		var label = $(this);
		var titleText = label.attr('title');
		if (titleText) {
			label.prepend(' <span class="mgToolTipIcon">' + titleText + '</span>');
			label.removeAttr('title');
		}
	});
};

// Then attach hover events to the Tool Tip Icons
function mgToolTipsHover() {
	$('.mgToolTipIcon').hover(
		function() {
			var tooltipText = $(this).text();
			$(this).append('<div class="mgToolTip">' + tooltipText + '</div>');
			$('.mgToolTip').fadeIn(300);
		},
		function() {
			$('.mgToolTip').fadeOut(300);
			$(this).children().remove();
			}
	);
};


/*******
***	Anchor Slider by Cedric Dugas   ***
*** Http://www.position-absolute.com ***
	
Never have an anchor jumping your content, slide it.
Don't forget to put an id to your anchor !
You can use and modify this script for any project you want, but please leave this comment as credit.
	
*****/
$.fn.anchorAnimate = function (settings) {
    //jQuery.fn.anchorAnimate = function (settings) {

    settings = jQuery.extend({
        speed: 1100
    }, settings);

    return this.each(function () {
        var caller = $(this);
        caller.click(function (event) {
            event.preventDefault();
            var locationHref = window.location.href;
            var elementClick = caller.attr("href");

            var destination = $(elementClick).offset().top;
            $("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination }, settings.speed, function () {
                window.location.hash = elementClick
            });
            return false;
        })
    })
};
