var imgOld = '';
var imgNew = '';
var toId = 0;
		
jQuery(document).ready(function(){

    buildSubMenu();
    buildHeaderChanger();
    buildTeaserGrower();
    
	//tt popup
	if(window.name == 'popup') {
		// ausblenden
		jQuery('body').css('background-image', 'none');
		jQuery('#header').css('display', 'none');
		jQuery('#breadcrumbControl').css('display', 'none');
		jQuery('#stage').css('display', 'none');
		jQuery('#contentContainer').css('background-image', 'none');
		jQuery('#contentCenter .indent .arrowRight').css('display', 'none');
		jQuery('#contentRight').css('display', 'none');
		jQuery('#sitemap').css('display', 'none');
		jQuery('#footer').css('display', 'none');
	}
    
    //news delete last border
    jQuery('.news-latest-item:last').css('borderBottom', 0);
    
    //content right margin-top 0
    jQuery('#contentRight *:first').css('margin-top', '0');
    
    //PNG Fix
    jQuery.fn.pngFix ? jQuery(document).pngFix() : '';
    
    //ClockPick
	jQuery("#callbackTimeBq").clockpick({
		event:'focus',
        starthour: 8,
        endhour: 22,
        showminutes: 4,
		military: true
    });
	
	if(jQuery('#page_5247').length) {
		jQuery('.tx-thmailformplus-pi1').prev('h2').append(' ' + decodeURI(get('message')));
	}
	if(jQuery('#headerMap').length) {
		jQuery('#headerMap').prepend('<div id="backtoworld"></div>').find('#backtoworld').click(function() {
			document[jQuery('#headerMap object').length ? jQuery('#headerMap object').attr('id') : jQuery('#headerMap embed').attr('name')].changeMap();
	    })
	}
	if(jQuery('#page_5239').length){
		jQuery('.extendedSearchHotel').css('display','none');	
		jQuery('.extendSearchTrigger').children('input').val('Zur erweiterten Suche');	
	}
	
	if(jQuery('#page_5637').length){
		jQuery('.extendedSearchHotel').css('display','block');	
		jQuery('.hotelSearch').css('display','block');	
	}
	

	jQuery('.tab1').attr('href','javascript:changeTab("Hotel");');
	jQuery('.tab2').attr('href','javascript:changeTab("Flug");');
	jQuery('.tab3').attr('href','javascript:changeTab("Mietwagen");');
	
	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').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_5207').length){
		jQuery('#callbackTimeBq-2').timePicker();
		
		jQuery('#callbackTimeBq-2').click(function(){
			jQuery('#callbackTimeBq-2').timePicker({
				startTime: new Date(0, 0, 0, 8, 00, 0), 
				endTime: new Date(0, 0, 0, 22, 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');
	}
	
});

/**
 *  Build Submenu
 */
function buildSubMenu(){
	jQuery('#mainMenu').children('ul').children('li').hover(function() {
		jQuery('ul', this).show();
    }, function(){
		jQuery('ul', this).hide();
    }).children('ul').children('li').hover(function() {
		jQuery('a', this).addClass('active');
		jQuery('.navimg', this).stop().animate({height:60}, 250);
    }, function(){
		jQuery('a', this).removeClass('active');
		jQuery('.navimg', this).stop().animate({height:1}, 250, function() {
			jQuery(this).hide();
		});
    });
}

/**
 *  Build the Stage Flyout Teaser
 */
function buildHeaderChanger(){
	if(isWorst()) {
		var img = new Array();
		jQuery('.flexTeaser', '#stageNavi').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('.flexTeaser', '#stageNavi').hover(changeStageBg, function(){});
	jQuery('.flexTeaser:first', '#stageNavi').trigger('mouseover');
    jQuery('.flexTeaser', '#stageNavi').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) : '';
}

/**
 *  Change Stage BG
 */
function changeStageImage() {}
function changeStageBg() {
	imgOld = imgNew;
	imgNew = jQuery(this).attr('onmouseover').toString().match(/changeStageImage\(\"?\'?(.*?)\"?\'?\)/)[1];
	if(!jQuery('#stageHeaderFade', '#stage').length) {
		jQuery('#stage').prepend('<div id="stageHeaderFade"><img src=""></div>')
	}
	jQuery('#stageHeaderFade img', '#stage').attr('src', imgOld);		
	clearTimeout(toId);
	toId = setTimeout(function() {
		jQuery('#stageHeaderFade', '#stage').stop().css({opacity:1}).animate({opacity:0}, 1000);
		jQuery('body').css({backgroundImage: 'url(' + imgNew + ')'});
	}, 300);
}

/**
 *  Build the Stage Grow/Shrink Teaser
 */
function buildTeaserGrower() {
	jQuery('#stageTeaser .csc-textpic-text').after(jQuery('#stageTeaser .csc-textpic-text .growTeaser'));
	jQuery('#stageTeaser .growTeaser').css({position:'absolute', right:10, bottom:3}).click(function() {
		var growBtn = jQuery(this);
		if(!growBtn.is('.growTeaserActive')) {
			var containerHeight = jQuery('#stage').height()-45;
			growBtn.addClass('growTeaserActive').parents('#stageTeaser').append('<div class="shrinkTeaser"></div>').animate({width:isWorst() ? 610 : 580, height:containerHeight, left: 384}, 1000, 'easeOutExpo');
			growBtn.parents('#stageTeaser').find('.csc-textpic-text').animate({width:420 + (isWorst()?-3:0), height:containerHeight-41}, 1000, 'easeOutExpo');
			jQuery('#stageTeaser .shrinkTeaser').click(function(){
				jQuery(this).parents('#stageTeaser').animate({width:284 + (isWorst()?30:0), height:243 + (isWorst()?15:0), left:680}, 1000, 'easeOutExpo');
				jQuery(this).parents('#stageTeaser').find('.csc-textpic-text').animate({width:120, height:175}, 1000, 'easeOutExpo');
				jQuery(this).remove();
				growBtn.removeClass('growTeaserActive');
			});
			return false;
		}
	});
}

/**
 * change styles of the search form for the Hotel Detail search.
 * Needed for expand the whole search
 *
 * Warning! jQuery.browser is deprecated! If current jQuery version
 * is >1.3, current solution is obsolet.
 */
function changeForm() {
	jQuery('.extendedSearchHotel .jcombox').css('width', '165px');
	jQuery('.tx-tuisearch-pi1').css({display:'block', width:615});
    if(!isBrowser()) {
       jQuery('.tx-tuisearch-pi1').css({width:645, marginRight:0});
    }
}

/**
 * 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){
    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('#headerMap').prepend('<iframe id="googlemap" frameborder="0" scrolling="no" src="/specials/hotelsuche/?width=995&height=523&tx_neustahotelsearch_pi1[sword]=' + section + '|country|ati&tx_neustahotelsearch_pi1[mapOnly]=1&type=123"></iframe><div id="googlemapcloseCell"><div id="googlemapclose">zurü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/ati2/pics/img/star_blank_link.png">').replace(/\+/g, '<img src="/fileadmin/ati2/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'});
}
