submitBtnText = "";
$(document).ready(function () {
    initFormFields();

    // init_topImgRotation();
    getRandomTopImg();

    // navigation top
    $('.navigationMain li ul').each(function () {
        $(this).css('left', -99999);
        $(this).prepend('<li class="handle"><u></u><b></b></li>');
        halfWidth = Math.round($(this).parent().width() / 2);
        var em = $('<em></em>');
        $(em).css('width', halfWidth).css('marginLeft', -halfWidth);
        $(this).find('li').prepend($(em));
        $(this).parent().hover(function () {
            halfWidth = Math.round($(this).width() / 2);
            mLeft = halfWidth + 9;
            $(this).find('ul').css('left', mLeft);
        }, function () { $(this).find('ul').css('left', -99999); });

    });
    $('.navigationMain li li a').hover(function () { $(this).parent().parent().find('em').css('display', 'none'); }, function () { $(this).parent().parent().find('em').css('display', 'block'); });
    $('.navigationMain > li > a.selected').parent().prev().addClass('prevSelected');
    $('.navigationMain > li > a.selected').parent().next().addClass('nextSelected');

    //submitknapper
    $('.btnSubmit').click(function () { $(this).closest('form').submit() });

    $('.bodyBoxLeft .text p:last').addClass('last');

    if ($('.listSpots li').size() > 0)
        initiateFrontPageSpots();

    var regExpImgCaptionPattern = /.*imageCaption.*/;
    $('.contentLeft p').has('img').each(function () { if ($(this).next().attr('class').match(regExpImgCaptionPattern)) { $(this).css('margin', 0); } });
    $('.contentRight p').has('img').each(function () { if ($(this).next().attr('class').match(regExpImgCaptionPattern)) { $(this).css('margin', 0); } });
});
var rotationTimeInSeconds = 2;

function getRandomTopImg() {
    if ($('.imgRotator img').size() > 1) {
        var imgAmount =  $('.imgRotator img').size();
        var randomnumber = (Math.floor(Math.random() * (imgAmount)))+1;
        $('.imgRotator img').css('display','none');
        $('.imgRotator img:nth-child('+randomnumber+')').css('display','block');
    }
}
function initiateFrontPageSpots() {
    var animationDuration = 200;
    $('.listSpots li').css('position', 'relative');
    $('.listSpots li').css('zIndex', '79');
    $('.listSpots li').height($('.listSpots li:first').height()).width($('.listSpots li:first').width());
    $('.listSpots li > a').each(function () {
        var position = $(this).position();
        var leftPos = position.left ;
        $(this).css({ 'top': position.top + 'px', 'left': leftPos + 'px', 'zIndex': 80 });
        $(this).attr('data-pos-top', position.top);
        $(this).attr('data-pos-left', position.left - 1);
        $(this).attr('data-height', $(this).height());
    }).mouseenter(function () {
        if ($(this).attr('data-enter') != 'yes' && $(this).attr('data-leave') != 'yes') {
            $(this).attr('data-leave', 'no')
            $(this).attr('data-enter', 'yes')
            var largeVersionPositionTop = $('.imgRotator').outerHeight() * -1;
            var largeVersionHeight = $('.listSpots').height() + $('.imgRotator').height() - 1;
            $(this).stop(true, true).animate({
                top: largeVersionPositionTop,
                height: largeVersionHeight + 'px'
            }, animationDuration, function () {
                $(this).attr('data-enter', 'no')
            });
        }
    }).mouseleave(function () {
        if ($(this).attr('data-leave') != 'yes') {
            $(this).attr('data-enter', 'no')
            $(this).attr('data-leave', 'yes')
            var largeVersionPosition = $('.imgRotator').position();
            largeVersionPositionTop = largeVersionPosition.top;
            var largeVersionHeight = $(this).height() + $('.imgRotator').height() - 1;
            $(this).stop(true, true).animate({
                top: $(this).attr('data-pos-top'),
                height: $(this).attr('data-height') + 'px'
            }, animationDuration, function () {
                $(this).attr('data-leave', 'no')
            });
        }
    });
    $('.listSpots li > a').css('position', 'absolute');
}
function init_topImgRotation() {
    if ($('.imgRotator img').size() > 1) {
        var tempHolder = $('<div></div>');
        $('.imgRotator img').each(function (index, value) {
            $(tempHolder).prepend(value);
        });
        $('.imgRotator').html($(tempHolder).html());
        //playTopImgRotation();
        rotationTimer = setTimeout(function () { playTopImgRotation() }, rotationTimeInSeconds * 1000);
    }
}

function playTopImgRotation() {
    $(".imgRotator img:last-child").fadeOut("slow", function () {
        $('.imgRotator').prepend($('.imgRotator img:last-child'));
        $('.imgRotator img:first-child').show();
    });
    rotationTimer = setTimeout(function () { playTopImgRotation() }, rotationTimeInSeconds * 1000);
}
function initFormFields() {
    $('.contourMessageOnSubmit').after('<div class="bottomBox"></div>');
    $('.cntFormBox input[type=text]').each(function () { $(this).attr('title', $(this).closest('.contourField').find('label:first').text()); });
    $('.cntFormBox textarea').each(function () { $(this).attr('title', $(this).closest('.contourField').find('label:first').text()); });
    $('.cntFormBox').closest('form').submit(function () { formReadyForSubmit($(this)); });
    prepareFormFieldsLabelInside();
}
function formReadyForSubmit(obj) {
    $('textarea[title]').each(function () { if ($(this).val() == $(this).attr('title')) { $(this).val("") } });
    $('input[title]').each(function () { if ($(this).val() == $(this).attr('title')) { $(this).val("") } });
    //.focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });
    //$('input[type=text][title]').focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });

}

function prepareFormFieldsLabelInside() {
    $('textarea[title]').focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });
    $('input[type=text][title]').focus(function () { showHideInputValue($(this), 'focus') }).blur(function () { showHideInputValue($(this), 'blur') }).each(function () { showHideInputValue($(this), 'blur') });
}

function showHideInputValue(argObj, argStrType) {
    if (argStrType == 'focus' && $(argObj).val() == $(argObj).attr('title'))
        $(argObj).val('')
    else if ($(argObj).val() == "")
        $(argObj).val($(argObj).attr('title'))
}
