/*jslint browser: true, undef: true, sloppy: true, white: true, maxerr: 50, indent: 4 */
$(document).ready(function () {

    removePX = function(s){
        if (s) {
            return parseFloat(s.toString().replace('px', ''));
        }else{
            return 0;
        }
    }

    var delay = (function(){
        var timer = 0;
        return function(callback, ms){
            clearTimeout (timer);
            timer = setTimeout(callback, ms);
        };
    })();

    $('.lightbox').prettyPhoto({
        animationSpeed: 'slow',
        default_width: 800,
        default_height: 600,
        counter_separator_label: ' von ',
        theme: 'light_rounded',
        hideflash: false,
        wmode: 'opaque',
        autoplay: true
    });

    animateLeft = function(){
        $('#featured-item-arrow-left, #featured-item-arrow-right').unbind('click');
        $('#featured-inner-wrapper').stop(true,true).animate({
            left: '+=241'
        },1000,'easeInOutExpo',function(){
            checkScrolling();
        });
    }

    animateRight = function(){
        $('#featured-item-arrow-left, #featured-item-arrow-right').unbind('click');
        $('#featured-inner-wrapper').stop(true,true).animate({
            left: '-=241'
        },1000,'easeInOutExpo',function(){
            checkScrolling();
        });
    }

    checkScrolling = function(){
        var totalWidth = $('.featured-item').length * 241 * -1;
        var curPos = removePX($('#featured-inner-wrapper').css('left'));

        if(curPos < 0){
            $('#featured-item-arrow-left').fadeTo(500,1.0).bind('click', animateLeft);
        }else{
            $('#featured-item-arrow-left').fadeTo(500,0.4);
        }

        if(curPos-241 > totalWidth){
            $('#featured-item-arrow-right').fadeTo(500,1.0).bind('click', animateRight);
        }else{
            $('#featured-item-arrow-right').fadeTo(500,0.4);
            $('#featured-item-arrow-right').stopTime();
        }
    }

    $('#featured-item-arrow-right').everyTime(8000,function() {
        $(this).click();
    });

    checkScrolling();

    Cufon.replace('#header, h1, h2, h3, h4');

    $('.day-box-text').each(function (){
        var h = removePX($(this).height());
        if(h > 125 || $(this).parent().parent().parent().find('.day-box-images').length){
            $(this).parent().find('.day-box-readmore a').toggle(function (event){
                event.preventDefault();
                h = $(this).parent().parent().height();
                h += 70;
                if($(this).parent().parent().parent().find('.day-box-images').length){
                    h += 150;
                }
                $(this).parent().parent().parent().animate({
                    height:h
                });
                $(this).text('[zuklappen]');
            },function(event){
                event.preventDefault();
                $(this).parent().parent().parent().animate({
                    height:150
                });
                $(this).text('[weiterlesen]');
            });
        } else {
            $(this).parent().find('.day-box-readmore').remove();
        }
    });

    $("#finder").keyup(function(){
        delay(function(){

            var input = $.trim($('#finder').val());
            var rgxp = new RegExp(input, 'gi');
            if(input.length>1){
                $('#finder-result-wrapper').fadeIn('slow');
                $('#finder-result').html('<div class="infoText">Ihre Suche wird bearbeitet...<br /><br /><img src="fileadmin/images/lightbox-ico-loading.gif" alt="" title="Loading..." /></div>');
                $.ajax({
                    url: 'index.php?id=49',
                    dataType: 'json',
                    data: {
                        mode: 'search',
                        term: input
                    },
                    success: function(data){
                        $('#finder-result').text('');
                        if(data.length){
                            var i=0;
                            $.each(data, function(key, value){
                                i++;
                                var title = value.title.replace(rgxp, '<span class="highlight">$&</span>');
                                var short_description = value.short_description.replace(rgxp, '<span class="highlight">$&</span>');
                                $('#finder-result').append('<div class="result-item'+(i%2?' odd':' even')+'"><h3>'+title+'</h3>'+short_description+'<div class="result-link"><a href="'+value.link+'">weitere Informationen &gt;&gt; </a></div></div>');
                            });
                        }else{
                            $('#finder-result').html('<div class="infoText">Ihre Suche ergab leider keine Treffer.</div>');
                        }
                    }
                });
            }

        }, 250 );
    });

    $("#finder-form").submit(function(event){
        event.preventDefault();
        $("#finder").trigger('keyup');
    });

    $.sekom = (function(my){
        my.getStartpageVoyage = function(id){
            $('#voyage-info-startpage').html('<div class="infoText">Ihre Suche wird bearbeitet...<br /><br /><img src="fileadmin/images/lightbox-ico-loading-red.gif" alt="" title="Loading..." /></div>');

            $.ajax({
                url: 'index.php?id=49',
                data: {
                    mode: 'startpage',
                    term: id
                },
                success: function(data){
                    if(data.length){
                        $('#voyage-info-startpage').html(data);
                    }else{
                        $('#voyage-info-startpage').html('<div class="infoText">Fehler bei der Abfrage!</div>');
                    }
                    Cufon.replace('h3');
                }
            });
        };
        return my;
    })({});

    $.finder = (function(my){
        my.setCountry = function(id){
            var oldVal = $('#finder').val();
            $.ajax({
                url: 'index.php?id=49',
                data: {
                    mode: 'country',
                    term: id
                },
                success: function(data){
                    if(data.length){
                        $('#finder').val(data+' '+oldVal);
                        $("#finder").focus().trigger('keyup');
                    }
                }
            });
        };
        return my;
    })({});

});

function getVoyageDetailInfo(id){
    $.sekom.getStartpageVoyage(id);
}

function setCountry(id){
    $.finder.setCountry(id);
}

function googleTranslateElementInit() {
  new google.translate.TranslateElement({
    pageLanguage: 'de',
    includedLanguages: 'ar,en,fr,it,pt,es',
    autoDisplay: false,
    gaTrack: true,
    gaId: 'UA-24751572-1',
    layout: google.translate.TranslateElement.InlineLayout.SIMPLE
  }, 'google_translate_element');
}
