// LOG FUNCTION

// usage: log('inside coolFunc', this, arguments);

window.log = function() {
	log.history = log.history || [];	// store logs to an array for reference
	log.history.push(arguments);
	if (this.console) {
		arguments.callee = arguments.callee.caller;
		console.log(Array.prototype.slice.call(arguments));
	}
};
(function(b) {
	function c() {
	}

	for (var d = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a; a = d.pop();) {b[a] = b[a] || c; }
})(window.console = window.console || {});

// LIGHTBOX FUNCTION



function colorboxLink(htmlElement) {
    var thisElement = $(htmlElement);
    var thisA = thisElement.find(".livecam_link:first");
    console.log(thisA);
    if(thisA.length > 0) {
        thisA.colorbox({
            iframe: true,
            open: true,
            width: 1000,
            height: 700,
            transition: "elastic",
			speed: 250,
			opacity: 0.4
		});
        thisA.removeClass("cboxElement");
    }

	return false;
}

function openLightboxLink(htmlElement) {
    var thisElement = $(htmlElement);
    var thisA = thisElement.find(".livecam_link:first");
    if(thisA.length > 0) {
        thisA.colorbox({
            open: true,
			opacity: 0.4
		});
        thisA.removeClass("cboxElement");
    }
	return false;
}

//submit function

function submitForm (htmlElement) {
    $('#' + htmlElement).submit();
    return false;
}

// 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").length > 0 ) {
				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() {

	// FADE IN SLIDER

	if ( $("#slider").length ) {
		$("#slider").fadeIn(2000);
	}

	// FADE IN SLIDER

	if ( $("#locations").length ) {
		$("#locations").fadeIn(2000);
	}

	// GOOGLE MAPS

	if (  $("#map_canvas").length ) {
		
		// GM  POSITIONS

		//var wa_pos = new google.maps.LatLng(47.635784, 13.590088);
        if(locationLat != undefined && locationLng != undefined) {
            var hs_pos = new google.maps.LatLng(locationLat, locationLng);
        }

		initialize(hs_pos, true);

		$("a.button",".fline.calcroute").click(function() {
			calcRoute();
            return false;
		});

		// for the destination picking see the fakeselect function below

	}

});

$(document).ready(function() {

    if($(".contentblock.wysiwyg table").length > 0) {
        $(".contentblock.wysiwyg table tr:odd td").addClass("dark");
    }

    // CONTACT SUBMIT

    if ( $(".contact_submit").length > 0) {
        $(".contact_submit").click(function () {
            $("#contact_form").submit();
            return false;
        });
    }

    $("a").focus(function () {
        $(this).blur();
    });

	// FAKESELECT

	if ( $(".fakeselect").length ) {

        $(".fakeselect").css("z-index","100");
		$(".fakeselect .dropdown").hide();

		$(".fakeselect").live("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",".fakeselect").live("click", function() {

			var thisfakeselect = $(this).parent().parent();
			var thisfakec = thisfakeselect.attr("class");
			var thisfakeclass = thisfakec.slice(11);
			var thisval = $(this).html();
			var thisid = $(this).attr("dir");

			if ( thisfakeselect.hasClass("destination") ) {

				if ( $(this).hasClass("interpret") ) {

					var wa_pos = new google.maps.LatLng(47.635784, 13.590088);
					var hs_pos = new google.maps.LatLng(47.69998, 14.15633);

					var destination = $(this).attr("id");
					currentdestination = destination;
					//initialize(eval(currentdestination), true);
					$("#directionsPanel").empty();
					
				}
			}
			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 = $("input#"+thisfakeclass);
			var thisdropdown = $(this).parent();
			thisdropdown.hide();
			$(thisinput).val(thisid);

		});
	}

	// ELSTRETCH

	$(window).elStretch({
		imagewidth: 1280,
		imageheight: 800,
		alttext: ""
	});

	// SWITCH LANG

	if ( $("#switch_lang").length ) {

		var sl_current = $(".currentlang","#switch_lang");
		var sl_a = $("ul li a", "#switch_lang");
		var sl_ul = $("ul", "#switch_lang");

		sl_current.toggle(function() {
			sl_ul.fadeIn();
			$("a", sl_current).blur();
		}, function() {
			sl_ul.fadeOut();
			$("a", sl_current).blur();
		});
	}

	// INPUT VALUE SWAP

	if ( $("form").length ) {
        $('form input').each(function(){
            if( $(this).val() == '' ){
                $(this).val( $(this).attr('title') );
            }
        });

		var swap_val = [];

		$("input.search, input.swapme, textarea").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]);
			   }
		   });
		});
		
	}

	// TOOLTIPS AUSTRIA MAP

	if ( $(".tooltip","#austria_map") ) {
		var map_poi = $(".poi","#austria_map");
		map_poi.hover(function() {
			$(this).css("z-index","20");
			if ( $(this).hasClass("patscherkofel") || $(this).hasClass("hochkoessen") ) {
				$(".tirol","#austria_map").show();
			}
            if ( $(this).hasClass("grossglockner") ) {
                $(".kaernten","#austria_map").show();
            }

			if ( $(this).hasClass("kasberg") || $(this).hasClass("hochficht") || $(this).hasClass("hinterstoder") || $(this).hasClass("wurzeralm") ) {
				$(".oberoesterreich","#austria_map").show();
			}
			if ( $(this).hasClass("oetscher")) {
				$(".niederoesterreich","#austria_map").show();
			}
			$(".tooltip",$(this)).stop().show();
			$(".infobox",$(this)).stop().show();
		}, function() {
			$(this).css("z-index","9");
			$(".tooltip",$(this)).stop().hide();
			$(".infobox",$(this)).stop().hide();
			$(".tirol","#austria_map").hide();
            $(".kaernten","#austria_map").hide();
			$(".oberoesterreich","#austria_map").hide();
			$(".niederoesterreich","#austria_map").hide();
		});

		$(".close",map_poi).bind({
			click:	function() {
				$(".tooltip",map_poi).stop().hide();
				$(".infobox").stop().hide();
				$(".tirol","#austria_map").hide();
                $(".kaernten","#austria_map").hide();
				$(".oberoesterreich","#austria_map").hide();
				$(".niederoesterreich","#austria_map").hide();
				
			}
		});
	}

	// EL SLIDER VARIATIONEN

	if ( $(".skisport_slider .elslider").length ) {
		$(".skisport_slider .elslider").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider","#regionalteaser_short").length ) {
		$(".elslider","#regionalteaser_short").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: doSlide,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider","#regionalteaser_long").length ) {
		$(".elslider","#regionalteaser_long").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider","#rightcol").length ) {
		$(".elslider","#rightcol").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider",".wurzeralm .bwebcams").length ) {
		$(".elslider",".wurzeralm .bwebcams").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: true,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider",".wurzeralm .bnews").length ) {
		$(".elslider",".wurzeralm .bnews").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: true,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 1500
		});
	}

	if ( $(".elslider",".hinterstoder .bwebcams").length ) {
		$(".elslider",".hinterstoder .bwebcams").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: true,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	if ( $(".elslider",".hinterstoder .bnews").length ) {
		$(".elslider",".hinterstoder .bnews").elSlider({
			vertical: false,
			continuous: true,
			numbers: false,
			thumbs: false,
			fade: false,
			autoplay: true,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 1500
		});
	}

	// VORUEBERGEHENDER INIT VON ELSLIDER IM CONTENTBLOCK --> WIRD DANN MIT AREA.JS GEMACHT

	if ( $(".elslider",".contentblock.slider").length ) {
		$(".elslider",".contentblock.slider").elSlider({
			vertical: false,
			continuous: true,
			numbers: true,
			thumbs: false,
			fade: false,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200
		});
	}

	// SUB BUTTON

	if ( $("#sub_button").length ) {
		$("#sub_button").click(function() {
			$(this).attr("style","display: none !important");
			$("#sub").fadeIn();
			return false;
		});
	}

	// ELSLIDER CONTENT ADAPTIONS (FOR CORRECTT POSITIONING OF NRS)

	if ( $(".contentblock.slider").length ) {
		var thisnrs = $(".numbers",".contentblock.slider");
		var children = thisnrs.children().length;
		var childrenwidth = children * 28;
		thisnrs.width(childrenwidth);
	}

	// MAINNAV

	if ( $("ul#mainnav").length ) {

		$("ul#mainnav li").hover(function() {

			var children = $(this).children().length;
			if ( children > 1 ) {
				$(".dropdown",$(this)).show();
				$("span.shadowhider",$(this)).show();
			}


		}, function() {
			var children = $(this).children().length;
			$(".dropdown",$(this)).hide();
			$("span.shadowhider",$(this)).hide();
		});
		
	}

	// MAINNAV QUICKFINDER

	if ( $("#quickfinder").length ) {

		$("#logocontainer").hover(function() {
            if(activateLogoHover) {
                $("#quickfinder").show();
                $("#overlay").show();
            }
		}, function() {
            if(activateLogoHover) {
                $("#quickfinder").hide();
                $("#overlay").hide();
            }
		});

		var qf_li = $("li","#quickfinder");
		var infoareas = $(".infoarea","#quickfinder");
		qf_li.bind({
			mouseenter: function() {
				qf_li.removeClass("current");
				$(this).addClass("current");
				var infoarea = $(this).find(".infoarea");
				infoareas.css("display","none");
				infoarea.css("display","block");
			}
		})
	}



	// FAKECHECK

	if ( $("a.fakecheck").length ) {

		$("a.fakecheck").bind({
			click: function() {
				$(this).toggleClass("clicked");
				var parent = $(this).parent();
				if ($(this).hasClass("clicked")) {
					//$("input#type", parent).val(value);
				}
				return false;
			}
		});
		
	}

	// TABLES

	if ( $("table.anlagen_pisten").length ) {
		var table = $("table.anlagen_pisten");

		$("tr th:nth-child(1)").addClass("first");
		$("tr th:nth-child(2)").addClass("second");
		$("tr th:nth-child(3)").addClass("third");

		$("tr td:nth-child(1)").addClass("first");
		$("tr td:nth-child(2)").addClass("second");
		$("tr td:nth-child(3)").addClass("third");
	}

	if ( $("table.2cols").length ) {
		$("table.2cols tr td:first-child").width(73);
		$("table.2cols tr td:nth-child(2) div").width(380).css("margin-left","20px");
	}

	if ( $("table.prices").length ) {
		$("table.prices tr.line:even td").addClass("dark");
	}

	// DATEPICKER

	if ( $("input.date" ).length ) {
		$("input.date").datepicker({
			numberOfMonths: 1, regional: "de", dateFormat: 'dd.mm.yy', firstDay: 1,
			monthNames: ['Januar','Februar','M&auml;rz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'],
			dayNamesMin: ['So','Mo','Di','Mi','Do','Fr','Sa'],
			prevText: 'zur&uuml;ck', nextText: 'vor',
			minDate: 0
		});
	}

	// LIGHTBOXEN

    if ( $("a#openlightbox_link").length ) {
        $("a#openlightbox_link").colorbox({
            iframe: true,
            open: true,
            width: 875,
            height: 725,
            transition: "elastic",
            speed: 250,
            opacity: 0.4
        });
    }

	if ( $("a.lightbox").length ) {
		$("a.lightbox").click(function() {
			$(this).colorbox({
                opacity: 0.4
            });
		});
	}

    /*
    if ( $("a.livecam_link").length ) {
		$("a.livecam_link").colorbox({
            iframe: true,
            width: 1500,
            height: 700,
            transition: "elastic",
			speed: 250,
			opacity: 0.8
		});
	}
	*/

	// PROSPEKTBESTELLUNG

	if ( $("#hiddentemplate").length ) {

		var prospectline = $("#hiddentemplate").find(".prospectline").clone();
		var closebutton = $("a.close",".prospectline");
		var p_fakecheck = $(".fakecheck",".teaser.infomaterial");
		var lines = $("#lines");
		var thisid;

		p_fakecheck.bind({
			click: function() {
				var pthis = $(this);
				var pname = pthis.parent().find(".title").text();
				if ( pthis.hasClass("clicked") ) {
					thisid = pthis.parent().parent().parent().attr("id");
					var clone = $("#hiddentemplate").find(".prospectline").clone();
					clone.find(".desc").html(pname);
					clone.find("input").attr("name",thisid);
					clone.appendTo(lines);
					if ( lines.children().length > 1  ) {
						$("span",lines).addClass("isnone");
					}
				}
				else {
					thisid = pthis.parent().parent().parent().attr("id");
					var selectedlineidcont = $(".prospectline","#lines").find("input[name="+thisid+"]");
					selectedlineidcont.parent().parent().slideUp("200", function() {
						$(this).remove(); 
						if ( lines.children().length == 1  ) {
							$("span",lines).removeClass("isnone");
						}
					});
				}
				return false;
			}
		});

		closebutton.live("click", function() {
			var thisid = $(this).parent().find("input").attr("name");
			$("#"+thisid).find(".fakecheck").removeClass("clicked");
			$(this).parent().slideUp("200", function() {
				$(this).remove();
				if ( lines.children().length == 1  ) {
					$("span",lines).removeClass("isnone");
				}
			});
			return false;
		});

	}


});

