$(document).ready(function() {
    $('#maskBg img').hide().fadeIn(3000);

    $("ul.thumb li").hover(function() {
        $(this).css({'z-index' : '100'});
        /*Add a higher z-index value so this image stays on top*/
        $(this).find('img').addClass("hover").stop()/* Add class of "hover", then stop animation queue buildup*/
            .animate({
                marginTop: '-20px', /* The next 4 lines will vertically align this image */
                marginLeft: '-50px',
                top: '50%',
                left: '50%',
                width: '110px', /* Set new width */
                //			height: '110px', /* Set new height */
                padding: '0px'
            }, 200);
        /* this value of "200" is the speed of how fast/slow this hover animates */

    }, function() {
        $(this).css({'z-index' : '0'});
        /* Set z-index back to 0 */
        $(this).find('img').removeClass("hover").stop()/* Remove the "hover" class , then stop animation queue buildup*/
            .animate({
                marginTop: '0px', /* Set alignment back to default */
                marginLeft: '15px',
                top: '0',
                left: '0',
                width: '50px', /* Set width back to default */
                //			height: '50px', /* Set height back to default */
                padding: '0px'
            }, 400);
    });

    $.fn.markValid = function() {
        this.each(function(init) {
            var jqNode = $(this);
            jqNode.removeClass('error').parent().prev('dt').find('label').removeClass('error');
        });
        return this;
    }

    $.fn.markInvalid = function() {
        this.each(function(init) {
            var jqNode = $(this);
            jqNode.css({
                position : 'relative'
            }).addClass('error').parent().prev('dt').find('label').addClass('error');
            for (var x = 1; x <= 2; x++) {
                jqNode.animate({
                    left : -25
                }, 10).animate({
                        left : 0
                    }, 40).animate({
                        left : 25
                    }, 10).animate({
                        left : 0
                    }, 40);
            }
        });
        return this;
    }

    function isValidEmail($email) {
        var regex = new RegExp(
            /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
        return regex.test($email);
    }

    //Formular passwordForgottenForm Verhalten / Validierung
    $('#contactForm #absenden').click(function() {
        var $error = false;

        $firstnameInput = $('input#firstname');
        if ($firstnameInput.val() != '') {
            $firstnameInput.markValid();
        } else {
            $firstnameInput.markInvalid();
            $error = true;
        }

        $lastnameInput = $('input#lastname');
        if ($lastnameInput.val() != '') {
            $lastnameInput.markValid();
        } else {
            $lastnameInput.markInvalid();
            $error = true;
        }

        $emailInput = $('input#email');
        if (isValidEmail($emailInput.val())) {
            $emailInput.markValid();
        } else {
            $emailInput.markInvalid();
            $error = true;
        }

        $messageInput = $('textarea#message');
        if ($messageInput.val() != '') {
            $messageInput.markValid();
        } else {
            $messageInput.markInvalid();
            $error = true;
        }

        if ($error == true) {
            return false;
        }

        $(this).prev('form').submit();
    });


    //Formular passwordForgottenForm Verhalten / Validierung
    $('#commentForm #absenden').click(function() {
        var $error = false;

        $headlineInput = $('input#headline');
        if ($headlineInput.val() != '') {
            $headlineInput.markValid();
        } else {
            $headlineInput.markInvalid();
            $error = true;
        }

        $firstnameInput = $('input#name');
        if ($firstnameInput.val() != '') {
            $firstnameInput.markValid();
        } else {
            $firstnameInput.markInvalid();
            $error = true;
        }

        $emailInput = $('input#email');
        if (isValidEmail($emailInput.val())) {
            $emailInput.markValid();
        } else {
            $emailInput.markInvalid();
            $error = true;
        }

        $messageInput = $('textarea#comment');
        if ($messageInput.val() != '') {
            $messageInput.markValid();
        } else {
            $messageInput.markInvalid();
            $error = true;
        }

        if ($error == true) {
            return false;
        }

        $(this).prev('form').submit();
    });


    var galleries = $('.ad-gallery').adGallery({
        loader_image: baseUrl + '../scripts/ad-gallery/loader.gif',
//		  description_wrapper: $('.ad-description'), // Either false or a jQuery object, if you want the image descriptions
        // to be placed somewhere else than on top of the image
        slideshow: {
            enable: true,
            autostart: false,
            speed: 5000,
            start_label: 'Start',
            stop_label: 'Stop',
            stop_on_scroll: true, // Should the slideshow stop if the user scrolls the thumb list?
            countdown_prefix: '(', // Wrap around the countdown
            countdown_sufix: ')',
            onStart: function() {
                // Do something wild when the slideshow starts
            },
            onStop: function() {
                // Do something wild when the slideshow stops
            }
        },
        effect: 'fade', // or 'slide-vert', 'resize', 'fade', 'none' or false
        thumb_opacity: 0.7, // Opacity that the thumbs fades to/from, (1 removes fade effect)
        // Note that this effect combined with other effects might be resource intensive
        // and make animations lag
        start_at_index: 0, // Which image should be displayed at first? 0 is the first image
        cycle: true // If set to false, you can't go from the last image to the first, and vice versa
    });


    var tooltip = function() {
        xtOffset = 5;
        ytOffset = 5;
        $("area.clip").mouseover(function(e) {
                var getTooltipHtml = '';
                string = $('.tooltipContent').html();
                if ('string' === typeof string) {
                    getTooltipHtml = string;
                }

                var spotTmp = $(this).attr('href').split('#');
                var spot = spotTmp[1];

                var id_wtd_divesites = $(this).attr('rel');

                if ('' != getTooltipHtml) {
                    if ($('.cliptip')) {
                        $(".cliptip").remove();
                    }
                    $("body").append("<div class='cliptip'>" + getTooltipHtml + "</div>");
                    $('.cliptip').find('img').attr('src', baseUrl + 'images/clip_' + spot + '.jpg');
                    $('.cliptip').find('a').attr('href', baseUrl + 'divesites/' + spot + '/' + id_wtd_divesites);
                    $(".cliptip").css("top", (e.pageY - xtOffset) + "px").css("left", (e.pageX + ytOffset) + "px").fadeIn("fast");
                    $('#player').css('display', 'block');
                }
            }
        );
    };

    var tooltipRemove = function() {
        $('html').click(function() {
            $(".cliptip").remove();
        });
    }

    tooltip();
    tooltipRemove();


    // gallery fix - correct height for pictures
    startupPictureHeightString = $('.ad-image-wrapper .ad-image img').css('height');
    if ('undefined' != typeof startupPictureHeightString) {
        startupPictureHeight = startupPictureHeightString.split('px');
        $('.ad-image-wrapper').css('height', parseInt(startupPictureHeight[0]) + 5 + 'px');

        $('ul.ad-thumb-list a').click(function() {
            heightString = $('.ad-image-wrapper').find('img').css('height');
            height = heightString.split('px');
            wrapperHeight = $('.ad-image-wrapper').css('height', parseInt(height[0]) + 5 + 'px');
        });
    }

    // IE CSS3 nth-child workaround
    $('.prices tbody tr:nth-child(2n+1) td, .prices tbody tr:nth-child(2n+1) th').addClass('second');
    $('.prices tbody tr:nth-child(2n) td, .prices tbody tr:nth-child(2n) th').addClass('first');

    // IE opacity workaround
    $(".prices table td, .prices table th").css({ opacity: 0.5 });
});
