(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.shuffle = function(arr) {
    for(
      var j, x, i = arr.length; i;
      j = parseInt(Math.random() * i),
      x = arr[--i], arr[i] = arr[j], arr[j] = x
    );
    return arr;
  }
})(jQuery);






$.fn.fader = function() {
    
    return this.each(function() {
    
        var $container = jQuery(this);
        var $images = $container.find("div.innerfaderbox");
		$images = $.shuffle($images);


        var $first_image = $images.filter(":first");
        
        $images.filter(":not(:first)").hide();
        
        $container.height($first_image.height()+30);
        $container.width($first_image.width());
        
        $images.css("position", "absolute");
		$images.css("visibility", "visible");

        
        $images.filter(":not(:last)").each(function(index) {
            $(this).data("next", index + 1);
        });
        $images.filter(":last").data("next", 0);
        
        function fade_image() {
        
            var $current_image;
            var next_id;
            var $next_image;
            
            $current_image = $images.filter(":visible");
            next_id = $current_image.data("next");
            $next_image = $images.filter(":eq(" + next_id + ")");
            
            $current_image.fadeOut(1000);
            $next_image.fadeIn(1000);
        }
        
        if ($images.length>1) {
			window.setInterval(fade_image, 10000);
		}
    });

}





jQuery(document).ready(function() {

    var jq = jQuery;
    var topCookie = jq.cookie('top');
	var roomCookie = jq.cookie('room');
	var weatherCookie = jq.cookie('weather');
	var $suche_position='oben';
	var $wetter_position='unten';
	
		var currentHeight = jq('#roomBox').height();

	
    //slideUp
    //var toggleHeightMain = jq("top").css('height')-285;
	
	var topHeight = jq("#top").css('height');
	topHeight = topHeight.replace('px','');
	
	
	
	
	
	var toggleHeightMain = topHeight-260;
	
	if (topHeight==560){
		vidHeight=610;
	}else{
		vidHeight=580;
	}
	
	
	
	
	
	
	
	var language_code = jq("html").attr("lang");
	
	
	jq('#flickr_badge_uber_wrapper a').attr('target','_blank');
	
	
	
	
	jq('.tx_iccbetriebe_pi1_daten_header').click(function() {
		if (jq(this).find('.tx_iccbetriebe_pi1_daten_morelink').css('background-position')=='0px -30px') {
			jq(this).find('.tx_iccbetriebe_pi1_daten_morelink').css('background-position','0px 0px');
		}else{
			jq(this).find('.tx_iccbetriebe_pi1_daten_morelink').css('background-position','0px -30px');
		}
		jq(this).parent().find('.tx_iccbetriebe_pi1_daten_infos').slideToggle();
	});
	
	$.cookie('layout','norm',{path: '/'});
  
    if(topCookie == 'up'){
        jq('#top').css('height', toggleHeightMain);
        jq('#playVid').css('display', 'none');
        jq('#slideButton').css('background-position', '-342px 0px');
        jq('#slideButton').toggle(
            function () {
                jq('#top').animate({
                    height: '+='+260
                }, 800 );
                jq('#playVid').fadeIn();
                jq('#slideButton').css('background-position', '-376px 0px');

                jq.cookie('top', 'down', {path: '/'});
                topCookie = 'down';
            },
            function () {
                jq('#top').animate({
                    height: toggleHeightMain
                }, 800 );
                jq('#playVid').fadeOut();
                jq('#slideButton').css('background-position', '-342px 0px');

                jq.cookie('top', 'up', {path: '/'});
                topCookie = 'up';
            }
        );
    }else{
        jq('#slideButton').toggle(
            function () {
                jq('#top').animate({
                    height: toggleHeightMain
                }, 800 );
                jq('#playVid').fadeOut();
                jq('#slideButton').css('background-position', '-342px 0px');

                jq.cookie('top', 'up', {path: '/'});
                topCookie = 'up';
            },
            function () {
                jq('#top').animate({
                    height: '+='+260
                }, 800 );
                jq('#playVid').fadeIn();
                jq('#slideButton').css('background-position', '-376px 0px');


                jq.cookie('top', 'down', {path: '/'});
                topCookie = 'down';
            }
        );
    }

    //Topvid
    var config =  {
        playlist: {
            title: 'Hochpustertal',
            videos: [ { id: 'dLTkBt92vL4', title: 'Hochpustertal' } ]
        },
        width: 980,                     // player width (integer or string)
        height: vidHeight,                    // player height (integer or string)
		repeat: 0,                      // repeat videos (boolean)
        playlistHeight: 0,
		videoParams: {                  // video <object> params (object literal)
            allowfullscreen: 'true',
            allowScriptAccess: 'always',
            wmode:  'opaque'
        },
        showToolbar: 0,                 // show or hide the custom toolbar (boolean)
        toolbarButtons: {},             // custom toolbar buttons
        toolbar: 'play',                // comma separated list of toolbar buttons
        toolbarAnimation: {
            opacity: 1
        }

    };

    var player = jq(".player").player(config);
    //var destroyed = false;
	
	
	var currentHeight = jq('#roomBox').height();
    var toggleHeightrooms = currentHeight-54;	

    jq('#playVid').click(function(){
        player.player('playVideo');       
        jq('#topMovie').css({"z-index": "3", "margin-top": "-50px"});
        jq('#slideButton').fadeOut();
		jq('#roomBox').css('height','54px');
		jq('#roomBox h1').css('background-position', 'top right');
		jq('#weatherBox').css('height','230px');
		jq('#weatherBox .tx_iccsuedtirolwetter_pi1_box_header').css('background-position', 'top right');
		$suche_position='unten';
		$wetter_position='unten';
    });

    jq('#playPause').toggle(
        function(){
            player.player('pauseVideo');
			jq('#playPause').css('background-position', '-278px 0px');
        },
        function(){
            player.player('playVideo');
			jq('#playPause').css('background-position', '-310px 0px');
        }
    );

    jq('#closeMovie').click(function(){
        jq('#topMovie').css({"z-index": "0", "margin-top": "-1000px"});
        //player.player('destroy');
        player.player('pauseVideo');
        //player = jq(".player").player(config);
        jq('#slideButton').fadeIn();
    });

    //weatherBox
    var toggleHeightWeather = 260-54;
   
   jq('#weatherBox').mouseenter(
        function() {
            jq('#weatherBox').stop().animate({
				height: '436px'
			}, 800 );
			jq('.tx_iccsuedtirolwetter_pi1_box_header').css('background-position', 'bottom right');
        }
    );

    jq('#weatherBox').mouseleave(
        function() {
            jq('#weatherBox').stop().animate({
				height: '230px'
			}, 800 );
			jq('.tx_iccsuedtirolwetter_pi1_box_header').css('background-position', 'top right');
        }
    );

    //langmenu	
    jq('#langmenu').mouseenter(
        function() {
            jq('#langmenu').css("height", "auto");
			if ((roomCookie == 'up' || roomCookie == null) && topCookie == 'up') {
				jq('#roomBox').css('height','54px');
				jq('#roomBox h1').css('background-position', 'top right');
			}
        }
    );

    jq('#langmenu').mouseleave(
        function() {
            jq('#langmenu').css("height", "37px");
			if ((roomCookie == 'up' || roomCookie == null) && topCookie == 'up') {
				jq('#roomBox').css('height',currentHeight);
				jq('#roomBox h1').css('background-position', 'bottom right');
			}
        }
    );

    //accordion
    jq('.flexboxClosed h1').nextAll().hide();
    jq('.flexboxOpen h1').addClass('triggerActive');
    jq('.flexboxOpen h1').nextAll().addClass('boxActive');
	jq('.flexboxOpen .flexboxOpenClose').addClass('flexboxOpenCloseActive');
    jq('.flexbox h1').click(function(){
        var trigger = jq(this);
		
		trigger.prev().toggleClass('flexboxOpenCloseActive');
		
        trigger.toggleClass('triggerActive').nextAll().slideToggle();
        trigger.nextAll().addClass('boxActive');
        trigger.parent().siblings().children().nextAll('.boxActive').slideUp();
        trigger.parent().siblings().children().removeClass('triggerActive');
    });
	
	
	
	
	// SocialMedia
	
	jq('.hasBox').click(function () {
		var icon = jq(this).attr('id');
		switch (icon){
			case 'fb':
				if (jq.cookie('layout')=='norm') {
					var act = jq(this).hasClass('act');
					jq('.hasBox').removeClass('act');
					jq('.socialmedia').hide();
					jq('#mainmenu').css('width',$.cookie('spaltenbreite'));
					$.cookie('sm_active','nix',{path: '/'});
					if (act == false) {
						jq(this).addClass('act');
						jq('div.fb').show();
						$.cookie('sm_active','fb',{path: '/'});
						jq('#mainmenu').css('width',menu3col);
					}
				} else {
					if (language_code=='de' || language_code=='en'){
						window.open('http://www.facebook.com/dolomiten');
					}else{
						window.open('http://www.facebook.com/ConsorzioAltaPusteria');
					}
				}
				break;
			case 'tw':
				if (jq.cookie('layout')=='norm') {
					var act = jq(this).hasClass('act');
					jq('.hasBox').removeClass('act');
					jq('.socialmedia').hide();
					jq('#mainmenu').css('width',$.cookie('spaltenbreite'));
					$.cookie('sm_active','nix',{path: '/'});
					if (act == false) {
						jq(this).addClass('act');
						jq('div.tw').show();
						$.cookie('sm_active','tw',{path: '/'});
						jq('#mainmenu').css('width',menu3col);
					}
				} else {
					if (language_code=='de' || language_code=='en'){
						window.open('http://twitter.com/#!/Hochpustertal');
					}else{
						window.open('http://twitter.com/#!/AltaPusteria');
					}
				}
				break;
			case 'yt':
				if (jq.cookie('layout')=='norm') {
					var act = jq(this).hasClass('act');
					jq('.hasBox').removeClass('act');
					jq('.socialmedia').hide();
					jq('#mainmenu').css('width',$.cookie('spaltenbreite'));
					$.cookie('sm_active','nix',{path: '/'});
					if (act == false) {
						jq(this).addClass('act');
						jq('div.yt').show();
						$.cookie('sm_active','yt',{path: '/'});
						jq('#mainmenu').css('width',menu3col);
					}
				} else {
					if (language_code=='de' || language_code=='en'){
						window.open('http://www.youtube.com/altapusteria');
					}else{
						window.open('http://www.youtube.com/altapusteria');
					}
				}
				break;
			case 'fl':
				if (jq.cookie('layout')=='norm') {
					var act = jq(this).hasClass('act');
					jq('.hasBox').removeClass('act');
					jq('.socialmedia').hide();
					jq('#mainmenu').css('width',$.cookie('spaltenbreite'));
					$.cookie('sm_active','nix',{path: '/'});
					if (act == false) {
						jq(this).addClass('act');
						jq('div.fl').show();
						$.cookie('sm_active','fl',{path: '/'});
						jq('#mainmenu').css('width',menu3col);
					}
				} else {
					if (language_code=='de' || language_code=='en'){
						window.open('http://www.flickr.com/photos/altapusteria/');
					}else{
						window.open('http://www.flickr.com/photos/altapusteria/');
					}
				}
				break;
		}
	});
	
	var menu3col = '500px';
	var menu2col = '730px';
	
	$.cookie('spaltenbreite',menu3col,{path: '/'});
	
	
	if (!$.cookie('sm_active')) {
		$.cookie('sm_active','fb',{path: '/'});
		jq('#fb').addClass('act');
	} else {
		jq('.socialmedia').hide();
		jq('.hasBox').removeClass('act');
		switch ($.cookie('sm_active')) {
			case 'fb':
				jq('#fb').addClass('act');
				jq('div.fb').show();
				$.cookie('sm_active','fb',{path: '/'});
				break;
			case 'tw':
				jq('#tw').addClass('act');
				jq('div.tw').show();
				$.cookie('sm_active','tw',{path: '/'});
				break;
			case 'yt':
				jq('#yt').addClass('act');
				jq('div.yt').show();
				$.cookie('sm_active','yt',{path: '/'});
				break;
			case 'fl':
				jq('#fl').addClass('act');
				jq('div.fl').show();
				$.cookie('sm_active','fl',{path: '/'});
				break;
		}
	}
	

    if(roomCookie == 'up' || roomCookie == null){
		jq('#roomBox h1').css('background-position', 'bottom right');
	}else{
		jq('#roomBox h1').css('background-position', 'top right');
		jq('#roomBox').css('height', '54px');
	}




	jq('#roomBox h1').click(function(){
		if (jq('#roomBox').css('height')=='54px'){
			jq('#roomBox').animate({
				height: currentHeight
			}, 800 );
                jq(this).css('background-position', 'bottom right');
				jq.cookie('room', 'up', {path: '/'});
				roomCookie = 'up';
		}else{
			jq('#roomBox').animate({
				height: '54px'
			}, 800 );
                jq(this).css('background-position', 'top right');
				jq.cookie('room', 'down', {path: '/'});
				roomCookie = 'down';
		}
		
		
		
		
		
		
	});












	
	
	jq('#langmenu a.active').removeAttr('href');
	
	
	jq('.faderbox').fader();
	
	$(function() {
		$("#tx_icciframe_pi1_form_anreise").datepicker({ minDate: 0 });
	});
	


});

