var isPhotoLoaded = false;
var isipad = /iPad|iPhone|Android/.test( navigator.userAgent );
function photoLoaded()
{
  isPhotoLoaded = true;
}

// GOTOLINK FUNCTION

function gotoLink (htmlElement) {

	var destinationURL = "";
	var target = "";

	if ( htmlElement.href ) {
		destinationURL = htmlElement.href;
		target = htmlElement.getAttribute("target");
	}
	else {
		if ( htmlElement.childNodes ) {
			if( htmlElement.getElementsByTagName("a") ) {
				destinationURL = htmlElement.getElementsByTagName("a")[0].href;
				target = htmlElement.getElementsByTagName("a")[0].getAttribute("target");
			}
		}
	}

	if ( destinationURL.length > 0 ) {
		if( target == "_blank" ) {
			window.open(destinationURL);
		}
		else {
			location.href = destinationURL;
		}
	}
	return false;
}



$(window).load(function() {
    //table alternate from old site
    if(document.getElementsByTagName)
	{
		tables = document.getElementsByTagName("table");
		for(d=0;d<tables.length;d++)
		{
			rows = tables[d].getElementsByTagName("tr");
			for(i = 0; i < rows.length; i++)
			{
				if(i % 2 == 0)
				{
					rows[i].className = "even";
				}
				else
				{
					rows[i].className = "odd";
				}
			}
		}
	}
	// FADE IN SLIDER

	if ( $("#slider").length ) {
		$("#slider").fadeIn(2000);
	}

	// FADE IN SLIDER

	if ( $("#locations").length ) {
		$("#locations").fadeIn(2000);
	}

	// GOOGLE MAPS

	if (  $("#map_canvas").length ) {
		initialize();
	}

});

$(document).ready(function() {

	// INPUT VALUE SWAP

	if ( $("form").length ) {
        $('form input').each(function(){
            if( $(this).val() == '' ){
                $(this).val( $(this).attr('title') );
            }
        });

		var swap_val = [];

		$("input.search").each(function(i) {
		   swap_val[i] = $(this).val();
		   $(this).focusin(function(){
			   if ($(this).val() == swap_val[i]) {
				   $(this).val("");
			   }
		   }).focusout(function(){
			   if ($.trim($(this).val()) == "") {
				   $(this).val(swap_val[i]);
			   }
		   });
		});
		
	}

	// SLIDERCONTAINER

	if ( $(".slidercontainer","#slider").length ) {
		$(".elslider","#slider .slidercontainer").bruendlSlider({
			visibleitems: 1,
			vertical: false,
			continuous: false,
			numbers: true,
			thumbs: false,
			fade: false,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
		
		$(".pimcore_area_elements_gallery_slide").hover(function() {
			$(this).css("position","relative").css("z-index","1000");
		}, function() {
			$(this).css("position","relative").css("z-index","0");
		});
		
	}

	// CHANGE LOCATION

	if ( $("#changelocation").length ) {

	/*	$("#changelocation").hover(function() {
			$(this).addClass("active");
			$("ul#location").css("display","block");
		}, function() {
			$(this).removeClass("active");
			$("ul#location").css("display","none");
		});*/

	}

	// MAINNAV

	if ( $("#mainnav.content ul").length ) {
        $('#mainnav.content ul').each(function () {
            $(this).children('li:first').children('a').addClass('first');
        });
        $('#mainnav.content ul li').children('ul').each(function () {
            $(this).parent().addClass('has3rdlevel');
        });

		$("#mainnav.content ul li").hover(function() {
			var childrennr = $(this).children().length;
			if ( childrennr > 1 ) {
				if ( $(this).parent().hasClass("content") ) {
					$("a:eq(0)",this).addClass("selected");
				}

				if ( $(this).hasClass("has3rdlevel") ) {
					$("a:eq(0)",this).addClass("selected");
				}
				$("ul:first",this).show();
			}
		}, function() {
			var childrennr = $(this).children().length;
			if ( childrennr > 1 ) {
				if ( $(this).parent().hasClass("content") ) {
					$("#mainnav.content ul li a").removeClass("selected");
				}
				if ( $(this).hasClass("has3rdlevel") ) {
					$("a:eq(0)",this).removeClass("selected");
				}
				$("ul:first",this).hide();
			}
		});
	}


	// FAKESELECT

	if ( $(".fakeselect").length ) {

        $(".fakeselect").css("z-index","100");
		$(".fakeselect .dropdown").hide();

		$(".fakeselect").click(function() {

			var thisfake = $(this);
			var thisfakec = thisfake.attr("class");
			var thisfakeclass = thisfakec.slice(11);

			if($(this).css("z-index") < 120) {
                $(this).css("z-index","120");
			    $(".dropdown",this).fadeIn(0);
            } else {
                $(this).css("z-index","100");
			    $(".dropdown",this).hide();
            }

			$(".value",this).click(function() {

				var thisfakeselect = $(this).parent().parent();
				var thisval = $(this).html();
				var thisid = $(this).attr("dir");

				if ( thisfakeselect.hasClass("newsletter") ) {

					var hiddenselect = $("select.newsletter");
					$("option",hiddenselect).removeAttr("selected");

					var optionlength = $("option",hiddenselect).length;

					for ( i=0; i<optionlength; i++ ) {
						var currenttext = $("option:eq("+i+")",hiddenselect).text();
						var currentoption = $("option:eq("+i+")");
						if ( currenttext == thisval ) {
							currentoption.attr("selected","");
						}
					}

				}
				else {
					var hiddeninput = $("input."+thisfakeclass+"");
					if (thisid != undefined) {
						hiddeninput.attr("value", thisval);
					}
					else {
						hiddeninput.attr("value","");
					}
				}

				var thishead = $(this).parent().prev();
				thishead.html(thisval);
				var thisinput = $(this).parent().parent().prev();
				var thisdropdown = $(this).parent();
				thisdropdown.hide();
				$(thisinput).val(thisval);

			});
		});
	}

	// LIGHTBOXEN

	if ( $("a.lightbox").length ) {
		$("a.lightbox").colorbox({
			transition: "elastic",
			speed: 250,
			opacity: 0.8
		});
	}

	// SHOW IMAGE (SHOPS)

	if ( $("#contentandfooter.shops").length ) {

		if ( lang == "de" ) {
			var content = "Inhalt anzeigen";
			var foto = "Foto zeigen";
		} else {
			var content = "Show Content";
			var foto = "Show Photo";
		}

		$("a.redarrow.showimage").click(function() {
			if ( !$("#contentandfooter").hasClass("faded") ) {
				$("#contentandfooter").fadeOut("fast").addClass("faded");
				$(this).html(content + "<span></span>").blur();
			} else {
				$("#contentandfooter").fadeIn("fast").removeClass("faded");
				$(this).html(foto + "<span></span>").blur();
			}
			return false;
		});
	}

	// SHOP ACCORDION

	if ( $("#accordion").length ) {
		$("#accordion").accordion({
			autoHeight: false,
			navigation: true
		});
	}

	// TEASER WORKAROUND

	if ( $(".sp_teaser").length ) {
		var spteasercont = $("#sp_teaser");
		var spteaserheight = spteasercont.height();
		if ( spteaserheight > 180 ) {

			$(".sp_teaser").css("margin-bottom","20px");
			var difference = (spteaserheight + 20) - 180;
			var contentandfooter = $("#contentandfooter.portal");
			var margintop = contentandfooter.css("margin-top");
			margintop = parseInt(margintop.substring(0,3));
			margintop = margintop + difference;
			margintop = margintop + "px";
			contentandfooter.css("marginTop",margintop);
			
		}
	}
	
	// TEL
	
	if ( !isipad ) {
		if ( $(".tel").length ) {
			$(".tel").each(function() {
				var currentlink = $("a",$(this));
				currentlink.addClass("linkindisguise");
			});
		}
	}
	

});






