var imagepath = "fileadmin/templates/schloss_velden/";
var runningFader;
var bgActive = 0;
var emptyLinks = 0;
var tableFlag = false;
var oTable;
var gallery_scroll = false;
var gallery_scroll_element;

jQuery(document).ready(function(){
     jQuery("ul.menu_top").supersubs({ 
            minWidth:    12,   // minimum width of sub-menus in em units 
            maxWidth:    27,   // maximum width of sub-menus in em units 
            extraWidth:  1     // extra width can ensure lines don't sometimes turn over 
                               // due to slight rounding differences and font-family 
        }).superfish({
            animation: {height:'show'}
        });
        
    /*verical menu*/
    jQuery('.accordion_vertical .head_vertical').click().toggle(function() {
        jQuery(this).parent().animate({
            left: '-=146'
        }, 500);
    }, function() {
        jQuery(this).parent().animate({
            left: '+=146'
        }, 500);
    });
    /*verical menu END */

    /* submenu */
    jQuery('.submenu_link').click(function() {
        jQuery(this).next().slideToggle('slow',function() {
            changeArrow(this);
        });
        return false;
    });    
    /* submenu END */

    jQuery(window).resize(updateFooter);
    changeBg(1);

    runningFader = window.setInterval("fader()", 4000);
    checkContentVisible();
    
    $('#fader_arrow_back').click(function() {
    	stopfader();
    	fader( true );
    	return false;
    });
    $('#fader_arrow_forward').click(function() {
    	stopfader();
    	fader();
    	return false;
    });
    
	// Karte hover //
	$(".punkt").hover(function(){
	   	$(this).children().stop(true, true).show();
	   },
	   function() {
  			$(this).children().stop(true, true).hide();
	});
	$("#schalter").click(function(){
		$('#strassen').toggle();
	});
	
	// wenn karte_content dann padding von content_inner auf 0 setzen
	$('.karte_content').closest('.content_inner').css('padding', '0')
	
	$('.punkt').hover(function(){
		$(this).addClass('punkt_aktive');
		$(this).removeClass('punkt_aktive');
	});

	// check for mobile device for gallery scroll
	if ( !DetectMobileLong() && !DetectTierTablet() ) { // gallery with hover
		// gallery buttons
    $('.gallery_arrow_back, .gallery_arrow_forward').click(function() {
    	return false;
    });
    $('.gallery_arrow_forward').hover(
    	function () {
    		gallery_scroll_element = $(this);
    		gallery_scroll = window.setInterval("gallery_scroll_forward()", 10);
    	},
    	function () {
    		window.clearInterval(gallery_scroll);
    	}
    );
    $('.gallery_arrow_back').hover(
    	function () {
    		gallery_scroll_element = $(this);
    		gallery_scroll = window.setInterval("gallery_scroll_back()", 10);
    	},
    	function () {
    		window.clearInterval(gallery_scroll);
    	}
    );
	}
	else { // gallery with click
		$('.gallery_arrow_forward').click(function () {
			gallery_scroll_element = $(this);
			gallery_scroll_forward(200);
			return false;
    });
    $('.gallery_arrow_back').click(function () {
    	gallery_scroll_element = $(this);
    	gallery_scroll_back(200);
			return false;
    });
    // and footer relative
    $('#footer').css('position', 'relative');
	}

	
});

function checkSubmenu() {
    var submenu = jQuery('.submenu_div').children();
    if (submenu.length < 1) {
        jQuery('.submenu_link img').hide();
    }
}
function fader( _reverse ) {
    pictures = jQuery('#bg_image_container').children();
    if ( _reverse ) {
    		if  ( bgActive < (2)) {
        		changeBg(pictures.length - 1 - emptyLinks);
		    }
		    else {
		        changeBg(bgActive-1);
		    }
  	}
  	else {
		    if  ( bgActive > (pictures.length - 2 - emptyLinks)) {
		        changeBg(1);
		    }
		    else {
		        changeBg(bgActive+1);
		    }
		}
}
function stopfader() {
    window.clearInterval(runningFader);
}
function checkStart() {
    var url = window.location.href;
    var urlArray = url.split("/");
    var urlString = urlArray[(urlArray.length-2)];
    var submenu = jQuery('.submenu_div');
    jQuery(submenu).show();
    var content = jQuery('.content_inner');
    jQuery(content).show();
}
function checkContentVisible() {
		var content = false;
		
    if ( jQuery.trim(jQuery('.content_image').html()) == "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->" ) {
        jQuery('.content_image').hide();
    }
    else {
    	content = true;
    }
    if ( jQuery.trim(jQuery('.content_map').html()) == "<!--TYPO3SEARCH_begin--><!--TYPO3SEARCH_end-->" ) {
        jQuery('.content_map').hide();
    }
    else {
    	content = true;
    }
    if ( jQuery('.content_inner').text() == "" ) {
        jQuery('.content_inner').hide();
    }
    else {
    	content = true;
    }
    
    if ( content ) {
    		if ( jQuery.trim(jQuery('#submenu_div').text()) != "" ) {
    			jQuery('.leftbar .accordion').show();
    		}
    		stopfader();
    }
    else {
    	jQuery('.arrow_wrapper').fadeIn(2000);
    }
}
function updateFooter( _type, _id, bgOld ) {
    /*var height;
    height = jQuery(window).height() - 747;
    if ( height < 10 ) {
        height = 10;
    }
    if ( _type == "noAnimation" ) {
        jQuery('#page_container').css('margin-bottom',height);
    }
    jQuery('#page_container').animate({
        'margin-bottom': height
        }, 100, function() {*/
            var width = jQuery(window).width();
            if ( width < 900 ) {
                width = 900;
            }
            if ( _type == "noAnimation" ) {
                jQuery('.bg_image').css('width', width );
            }
            else {
                jQuery('.bg_active').animate({
                    'width': width}, 200);
            }
            var top = ((width*-1)/2.3)+492;
            if ( top > 70 ) {
                top = 70;
            }
            jQuery('#bg_image_container').css('top', top);
            if ( _id ) {
                jQuery(_id).addClass('bg_active');
                // disable arrows during fade to prevent doublefade
                $('.arrow_wrapper').hide();
                jQuery(_id).fadeIn(500,function() {
                    if (typeof(bgOld[0]) != "undefined" ) {
                    		jQuery(bgOld).hide();
                    		jQuery(getCopyright(bgOld[0].id)).hide();
                		}
                    // reactivate arrows
                    $('.arrow_wrapper').show();
                });
                jQuery(getCopyright(_id)).show();
            }
        //});
}
function getCopyright ( _id ) {
	var id_array = _id.split('_'); //#bg_image_1
  var id_only = id_array[(id_array.length - 1)];
  return '#bg_image_copyright_'+id_only;
}

function changeBg( _el ) {
    var id = '#bg_image_';    
    if ( _el != bgActive ) {        
        if ( _el < bgActive ) {
        		$('.arrow_wrapper').hide();
            jQuery(id+bgActive).fadeOut(200);
            jQuery(getCopyright(id+bgActive)).hide();
        }
        jQuery(id+bgActive).removeClass('bg_active');
        bgOld = jQuery(id+bgActive);
        bgActive = _el;
        
        updateFooter( 'noAnimation', id+bgActive, bgOld );
    }
}
function gallery_scroll_forward( distance ) {
	if ( !distance ) {
		distance = 4;
	}
	else {
		var animate = true;
	}
  var left = $(gallery_scroll_element).prev().scrollLeft();
  var thumbs = $(gallery_scroll_element).prev().find('li');
  if ( (thumbs.length*91)-492 > left) {
  	$(gallery_scroll_element).prev().scrollLeft(left+distance);
  }
}
function gallery_scroll_back( distance ) {
	if ( !distance ) {
		distance = 4;
	}
	var left = $(gallery_scroll_element).next().scrollLeft();
  $(gallery_scroll_element).next().scrollLeft(left-distance);
}

