var imgOld = '';
var imgNew = '';
var toId = 0;

jQuery(document).ready(function(){

    buildHeaderChanger();

    //news delete last border
    jQuery('.news-latest-item:last').css('borderBottom', 0);

    jQuery('ul.dynamics250').roundabout({
        reflect: true,
        minOpacity: 1,
        shape: 'lazySusan',
        minScale: 0.1,
        maxScale: 1.0
    });

    $( "ul.dynamics250 img" ).each(
        function( intIndex ){
            $(this).bind ("mouseover",function(){
                $(this).click();
            });
        });
    jQuery('.contentFlow .container').cycle({
        timeout: 100000,
        speed: 2000,
        prev:	'.prevcf',
        next:	'.nextcf',
        fx: 'scrollHorz'
    });


    //PNG Fix
    jQuery.fn.pngFix ? jQuery(document).pngFix() : '';


    convertListStars();
    if(!isBrowser()){
        anchorCheck();
    }

    var otherOption = jQuery('.tx-thmailformplus-pi1 #travelType').find('option[value="sonstiges"]', '.tx-thmailformplus-pi1 #travelType').clone();
    jQuery('option[value="Pauschalreise"], option[value="Ferienhaus"], option[value="sonstiges"]', '.tx-thmailformplus-pi1 #travelType').remove();
    jQuery('option[value="weiss ich nicht"]', '.tx-thmailformplus-pi1 #travelType').val('a la carte').html('&agrave; la carte');
    jQuery('.tx-thmailformplus-pi1 #travelType option[value="Mietwagen"]').after(jQuery('<option>', {
        value:'Cruises',
        text:'Cruises'
    }));
    jQuery('.tx-thmailformplus-pi1 #travelType').append(otherOption);

    /**
	*	timePicker
	*
	*	the timePicker function executes first to set the default options,
	*	then the extension starts when the input gets the focus.
	*
	*/
    if(jQuery('#page_8950').length){
        jQuery('#callbackTimeBq-2').timePicker({
            endTime: new Date(0, 0, 0, 20, 00, 0)
            });

        jQuery('#callbackTimeBq-2').click(function(){
            jQuery('#callbackTimeBq-2').timePicker({
                startTime: new Date(0, 0, 0, 8, 00, 0),
                endTime: new Date(0, 0, 0, 20, 00, 0), // Using Date object here.  (0, 0, 0, hours, minutes, 0)
                show24Hours: true,
                separator: ':',
                step: 15
            });
        });
    }

    if(jQuery('#callbackDateBq').length && jQuery('#callbackDateBq').val() == ''){
        jQuery('#callbackDateBq').val('TT.MM.JJ');
    }

    //AOWLB Rater
    jQuery('.AOWLB-Search .raterStars .raterRate ul').before('<ul style="background-position: 0pt -39px;"><li>&nbsp</li></ul>');

    if(jQuery('.tx-thmailformplus-pi1 .formsTop h1').length && get('message')) {
        jQuery('.tx-thmailformplus-pi1 .formsTop h1').append(' ' + decodeURI(get('message')));
    }


    // Navigation flyout
    jQuery('#mainMenu ul li').hover(function() {
        if(jQuery(this).children('ul.subsubmenu').length && !isBrowser()) {
            var maxWidth = 0;
            jQuery(this).children('ul').show().children('li').each(function() {
                maxWidth = Math.max(maxWidth, jQuery(this).outerWidth());
            });
            jQuery(this).children('ul').width(maxWidth);
        } else {
            jQuery(this).children('ul').fadeIn(400);
        }
    }, function() {
        jQuery(this).children('ul').fadeOut(400);
    });

    isWorst() ? jQuery('.roundAbout', '#contentLeft').css({
        width:219,
        height:200,
        backgroundImage:'url(fileadmin/ati/pics/img/roundabout_dummy.jpg)',
        cursor:'pointer'
    }).empty().click(function() {
        location.href=jQuery(this).next('.roundAboutText').find('a').attr('href')
        }) : '';

    if(jQuery('.tx-thmailformplus-pi1 #formsTop h1').length && get('message')) {
        jQuery('.tx-thmailformplus-pi1 #formsTop h1').append(': ' + decodeURIComponent(get('message')));
    }

    //tt booking
    if(jQuery('#ibenav1 a').length > 0) {
        jQuery('#ibenav1 a').click(function() {
            if(jQuery('.tt_quiTab_suche').length == 1) {
                jQuery('.tt_quiTab_suche').trigger('click');
            } else {
                jQuery('#searchArea1').hide();
                jQuery('#searchArea2').show();
            }

            return false;
        });
        jQuery('#ibenav1 a').attr('href', 'javascript:void(0);');
    }
});


function buildHeaderChanger(){
    if(jQuery('#slides a, #slides img').length > 1) {
        jQuery('#next,#prev').css('display','block');
    }
    jQuery('#slides').cycle({
        timeout: 10000,
        speed: 2000,
        prev:	'#prev',
        next:	'#next'
    });
}

/**
 *  Build the Stage Flyout Teaser

function buildHeaderChanger(){
	if(isWorst()) {
		var img = new Array();
		jQuery('a', '#stage').each(function(i){
			img[i] = new Image();
			img[i].src = jQuery(this).attr('onmouseover').toString().match(/changeStageImage\(\"?\'?(.*?)\"?\'?\)/)[1];
		});
	}
	imgNew = jQuery('body').css('backgroundImage').replace(/url\((.*?)\)/, '$1');
	jQuery('a', '#stage').hover(changeStageBg, function(){});
	jQuery('a:first', '#stage').trigger('mouseover');
    jQuery('.flexTeaser', '#stage').mouseover(function() {
        if(!jQuery(this).hasClass('active')) {
            jQuery('.flexTeaser').children('.flexContent').stop().animate({width:1}, 500);
            jQuery('.flexTeaser').removeClass('active');
            jQuery(this).children('.flexContent').stop().animate({width:193}, 500, function(){
                jQuery(this).parent().addClass('active');
            });
        }
    }).click(function() {
		location.href = jQuery(this).find('.flexContent a', '#stageNavi').attr('href');
	});
	jQuery('.flexTeaser:first', '#stageNavi').trigger('mouseover');
	!isBrowser() ? jQuery('.flexTeaser:first', '#stageNavi').each(changeStageBg) : '';
}
 */


/**
 * Builds and displays a loader image whenever an AJAX request is on it's way.
 *
 * @param {Object} The container DOM element to insert the loader
 */
function buildLoader(container, extraImageCSS, extraImageHTML){
    if(!jQuery(container).length) return false;
    jQuery(container).append('<div class="loadingOverlay"></div><div class="loadingImage"></div>');
    jQuery('.loadingOverlay', container).css({
        width: jQuery(container).outerWidth(),
        height: jQuery(container).outerHeight()
    });

    if(typeof extraImageCSS != 'undefined') {
        jQuery('.loadingImage', container).css(extraImageCSS);
    }
    if(typeof extraImageHTML != 'undefined') {
        jQuery('.loadingImage', container).html(extraImageHTML);
    }

    jQuery('.loadingImage', container).css({
        left: (jQuery('.loadingOverlay', container).width() / 2) - (jQuery('.loadingImage', container).width() / 2)
    });
    var yOffset = window.pageYOffset || document.documentElement && document.documentElement.scrollTop || document.body.scrollTop;
    var viewportHeight = window.innerHeight || document.documentElement && document.documentElement.clientHeight || document.getElementsByTagName('body')[0].clientHeight;
    var topPos = yOffset - jQuery('.loadingOverlay').offset().top;
    if(topPos > 0) { /* top is over viewport */
        if(jQuery('.loadingOverlay').height() - topPos > viewportHeight) { /* overlay bottom is under viewport */
            topPos = viewportHeight / 2 - jQuery('.loadingImage').height() / 2;
        } else if(topPos + jQuery('.loadingImage').height() > jQuery('.loadingOverlay').height()) { /* image does not fit into overlay */
            topPos = jQuery('.loadingOverlay').height() - jQuery('.loadingImage').height();
        } else { /* overlay bottom in viewport */
            topPos += (jQuery('.loadingOverlay').height() - topPos) / 2 - jQuery('.loadingImage').height() / 2;
        }
    } else if(viewportHeight + topPos > 0) { /* top is in viewport */
        if(viewportHeight + topPos < jQuery('.loadingOverlay').height()) { /* overlay bottom is under viewport  */
            topPos = (viewportHeight + topPos) / 2 - jQuery('.loadingImage').height() / 2;
        } else { /* whole overlay in viewport  */
            topPos = jQuery('.loadingOverlay').height() / 2 - jQuery('.loadingImage').height() / 2;
        }
    }
    jQuery('.loadingImage', container).css({
        top: topPos
    });
    jQuery('.loadingOverlay', container).bgiframe();
    return jQuery('.loadingOverlay, .loadingImage', container);
}

/**
 * Opens gmap in header
 *
 * @param {String} section-param for iframe-src
 */
function openHeaderGmap(section){
    jQuery('#googlemap, #googlemapcloseCell').remove();
    jQuery('#slides .flashMap').prepend('<iframe id="googlemap" frameborder="0" scrolling="no" src="/specials/hotelcountrymap/?type=123&q=' + section + '"></iframe><div id="googlemapcloseCell"><div id="googlemapclose">zur&uuml;ck</div></div>');
    jQuery('#googlemapclose').click(function() {
        jQuery('#googlemap, #googlemapcloseCell').remove();
    }).mouseover(function(){
        jQuery(this).animate({
            marginLeft: '0px'
        }, 1000);
    }).mouseout(function(){
        jQuery('#googlemapclose').animate({
            marginLeft: '56px'
        }, 1000);
    });
    window.setTimeout(function(){
        jQuery('#googlemapclose').animate({
            marginLeft: '56px'
        }, 1000);
    }, 10000);
}


function changeTab(tab){
    switch(tab){
        case 'Hotel':
            jQuery('.tx-tuisearch-pi1').css('display','block');
            jQuery('#flight').css('display','none');
            jQuery('#car').css('display','none');
            jQuery('.noBorders img:eq(0)').attr('src','fileadmin/ati2/pics/img/menu_hotel_hi.jpg');
            jQuery('.noBorders img:eq(1)').attr('src','fileadmin/ati2/pics/img/menu_flug.jpg');
            jQuery('.noBorders img:eq(2)').attr('src','fileadmin/ati2/pics/img/menu_mietwagen.jpg');
            break;
        case 'Flug':
            jQuery('.tx-tuisearch-pi1').css('display','none');
            jQuery('#flight').css('display','block');
            jQuery('#car').css('display','none');
            jQuery('.noBorders img:eq(0)').attr('src','fileadmin/ati2/pics/img/menu_hotel.jpg');
            jQuery('.noBorders img:eq(1)').attr('src','fileadmin/ati2/pics/img/menu_flug_hi.jpg');
            jQuery('.noBorders img:eq(2)').attr('src','fileadmin/ati2/pics/img/menu_mietwagen.jpg');
            break;
        case 'Mietwagen':
            jQuery('.tx-tuisearch-pi1').css('display','none');
            jQuery('#flight').css('display','none');
            jQuery('#car').css('display','block');
            jQuery('.noBorders img:eq(0)').attr('src','fileadmin/ati2/pics/img/menu_hotel.jpg');
            jQuery('.noBorders img:eq(1)').attr('src','fileadmin/ati2/pics/img/menu_flug.jpg');
            jQuery('.noBorders img:eq(2)').attr('src','fileadmin/ati2/pics/img/menu_mietwagen_hi.jpg');
            break;
    }

}


/**
 * build Star images in der Menu
 * replaceing * with an image
 */
function convertListStars(){
    jQuery("#contentLeft .listing a:contains('*')").each(function(){
        jQuery(this).html(jQuery(this).text().replace(/\*/g, '<img src="/fileadmin/ati/pics/img/star_blank_link.png">').replace(/\+/g, '<img src="/fileadmin/ati/pics/img/star_blank_half_link.png">')).children().wrapAll('<span style="white-space: nowrap;"></span>')
    });
}


/**
 * repair anchor problem in the ie6 + 7 on the special offer page
 */
function anchorCheck(){
    jQuery("a[id^='c']", '#page_5607').css({
        'display': 'block',
        'clear': 'both',
        'width': '10px',
        'height': '10px'
    });
}