/* 
 * The main JS file for whole user interface.
 * Put here things that are needed in every page, other page special ones can
 * be included in each page seperately.
 */

var timestamp = new Date();

function setInputTitles(){
	$('input').each(function(){
		var title = $(this).attr('title');
		
		if(typeof(title) != 'undefined'){
			$(this).val(title);
			var form = $(this).parents('form:first');
			form.submit(removeInputTitles);
		}
	});
}

function removeInputTitles(){
	$(this).find('input').each(function(){
		var title = $(this).attr('title');
		
		if(typeof(title) != 'undefined' && $(this).val() == title){
			$(this).val('');
		}
	});
	return true;
}

function initFlatrates(){
  $('ul.flatrate-prices li div.price-title a').click(function(){
    var element = $(this).parents('li').find('.room-infos-wrapper');
    if(element.css('display') == 'none'){
      element.slideDown();
    }else{
      element.slideUp();
    }
    return false;
  });
}

function initWellnesses(){
  $('ul.wellnesses li a.category, ul.wellnesses li ul li a.treatment').click(function(){
    if($(this).next().css('display') == 'none'){
      $(this).next().slideDown();
    }else{
      $(this).next().slideUp();
    }
    $(this).toggleClass('open');
  });
}

var teasergalleries = {};

function initTeaserGalleries(){
  $('div.teasergallery-wrapper').each(function(){
    teasergalleries[$(this).attr('teasergallery')] = false;
    $(this).find('.teasergallery-prev-teaser a, .teasergallery-next-teaser a').click(function(){
      var gnr = $(this).parents('div.teasergallery-wrapper').attr('teasergallery');
      if(gnr && teasergalleries[gnr] === false){
        teasergalleries[gnr] = true;
        
        var element = $(this).parents('div.teasergallery-wrapper').find('ul.teasergallery-teasers');
        var tcw  = 588; // teaser content width
        var not = $(element).children().length;
        var act = parseInt($(element).css('left'));
        var max = ((not - 1) * tcw) * -1;
        
        if($(this).attr('class') == 'prev'){
          var pos = act + tcw;
          if(pos > 0){ // max left
            pos = max;
          }
        }else{
          var pos = act - tcw;
          if(pos < max){ // max right
            pos = 0;
          }
        }
        
        $(element).animate({ left: pos }, 500, function() { teasergalleries[gnr] = false });
      }
    });
  });
}

var multidimensionalteasergalleries = {};

function initMultidimensionalTeaserGalleries(){
  $('div.multidimensionalteasergallery-wrapper').each(function(){
    
    $(this).find('div.multidimensionalteasergallery-navigation ul li a').click(function(){

      var mnr = $(this).parents('div.multidimensionalteasergallery-wrapper').attr('multidimensionalteasergallery');
      if(mnr){ // && multidimensionalteasergalleries[mnr] === false){
        multidimensionalteasergalleries[mnr] = true;

        var gallery = $(this).attr('gallery');
        var element = $(this).parents('div.multidimensionalteasergallery-wrapper').find('ul.multidimensionalteasergallery-galleries li[gallery="' + gallery + '"]');
        var act = parseInt($(element).css('top'));
        var pos = 0;
        var stop = false;
        
        // make active
        $(this).parents('div.multidimensionalteasergallery-navigation').find('li.tab').removeClass('active');
        $(this).parent('li.tab').addClass('active');
        
        $(element).parent('ul.multidimensionalteasergallery-galleries').find('li.multidimensionalteasergallery-gallery').each(function(){
          if($(this).attr('gallery') == gallery) stop = true;
          if(stop === false) pos++; 
        });
        
        pos = (pos * 188) * -1; // teaser gallery height
        
//        console.debug(element);
//        console.debug(gallery);
//        console.debug(act);
//        console.debug(pos);
        
        if(pos != act){
          $(element).parent('ul.multidimensionalteasergallery-galleries').find('li.multidimensionalteasergallery-gallery').animate({ top: pos }, 500, function() { multidimensionalteasergalleries[mnr] = false });
        }
        
      }
      
    });
  });
}

function showRequireMarks(){
  $('.content .text-wrapper .form .required').each(function(){
    if($(this).hasClass('checkbox') || $(this).hasClass('radio')){
      
    }else{
        $(this).find('label:first').append('<span class="required">*</span>');
    }
  });
}

function metaDataHandler(width, height, id){
  var obj = swfobject.getObjectById(id);
  if(width > 590){
    height = Math.floor((590/width) * height);
  }
  obj.height = height + 25;
}

function datepickerMinDate(text,inst,e){
  $(e).parents('form').find('input.datepicker.departure').datepicker('option', 'minDate', new Date(parseInt(inst.selectedYear), parseInt(inst.selectedMonth), parseInt(inst.selectedDay) + 1))
}

function openSeekdaPopUp(lang){
  $.prettyPhoto.open('https://ibe.seekda.com/ibe/AT_NEU_JAGDHOF?languageCode=' + lang + '&iframe=true&width=720&height=510');
}

function openSlideshowPopUp(lang,album){
  var slideshow = ['/slideshow/slideshow.swf?width=900&height=525&flashvars=xmlfile=/' + lang + '/slideshow.xml&textlang=' + lang.substr(0,2) + '&album=' + album];
  var slideshow_titles = [''];
  var slideshow_descriptions = [''];
  $.prettyPhoto.open(slideshow,slideshow_titles,slideshow_descriptions);
}

function openPanoramaPopUp(){
  // loadPano=zimmer/doppelzimmer_komperdellblick/b5444e/b5444e
  var panorama = ['/panorama/embedPano.swf?custom=panorama&width=800&height=550&flashVars=language=de&defaultpanel=1&mainmenu=true&topmenu=true&controlbar=false&localPath=http://www.hotel-jagdhof.at/panorama/'];
  var panorama_titles = [''];
  var panorama_descriptions = [''];
  $.prettyPhoto.open(panorama,panorama_titles,panorama_descriptions);
}

function openWebcamsPopUp(){
  var webcams = ['http://webtv.feratel.com/webtv/?bgcolor=000000&cam=5671&color1=ffffff&color2=FFFFFF&color3=ffffff&color4=ffffff&color5=000000&design=600v2&lg=de&getLatest=1&c2=0&custom=iframe&width=600&height=640',
                 'http://webtv.feratel.com/webtv/?bgcolor=000000&cam=5710&color1=ffffff&color2=FFFFFF&color3=ffffff&color4=ffffff&color5=000000&design=600v2&lg=de&getLatest=1&c2=0&custom=iframe&width=600&height=680',
                 'http://webtv.feratel.com/webtv/?bgcolor=000000&cam=5714&color1=ffffff&color2=FFFFFF&color3=ffffff&color4=ffffff&color5=000000&design=600v2&lg=de&getLatest=1&c2=0&custom=iframe&width=600&height=530'];
  var webcams_titles = ['Fulpmes','Neustift – Stubaier Gletscher','Neustift – Hochstubai/Elferlifte'];
  var webcams_descriptions = ['Fulpmes','Neustift – Stubaier Gletscher','Neustift – Hochstubai/Elferlifte'];
  $.prettyPhoto.open(webcams,webcams_titles,webcams_descriptions);
}

function openMoviesPopUp(){
  var movies = ['/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f055012-3f04-41a1-8e59-486d0a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // zen
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f05500d-4668-4409-96a9-47fc0a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // spa
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f055005-dfb4-43c4-93aa-47e80a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // schneekönige
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f054fff-0d20-4ba2-9921-47d90a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // naturgarten
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f054ffa-e1e8-4c87-b5eb-47c90a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // kuschelnester
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f054ff2-1950-4d93-bed1-47b40a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // gourmet
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4f054feb-c6e4-4c4c-8fa8-479f0a000287&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // bergsommer
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=559&height=498&flashVars=videoURL=/vid/dyn/4efb2c85-a320-4cc0-8b8e-0bfe0a00028a&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1',  // bergfrühling
                '/js/jcplayer/DefaultSkin/jcplayer.swf?custom=flash&width=544&height=361&flashVars=videoURL=/vid/dyn/4ef18a2b-78b8-4426-805b-79000a000286&amp;startPhotoSource=/img/jagdhof/movie.png&amp;autoPlay=true&amp;scaleMode=maintainAspectRatio&amp;loop=false&amp;highlightColor=0xA88947&amp;bufferTime=1']; // stubai tirol
  var movies_titles = ['Zen', 'SPA', 'Schneekönige', 'Naturgarten', 'Kuschelnester', 'Gourmet', 'Bergsommer', 'Bergfrühling', 'Stubai Tirol'];
  var movies_descriptions = ['Zen', 'SPA', 'Schneekönige', 'Naturgarten', 'Kuschelnester', 'Gourmet', 'Bergsommer', 'Bergfrühling', 'Stubai Tirol'];
  $.prettyPhoto.open(movies,movies_titles,movies_descriptions);
}

function initFontSize(){
  if($.cookie("fontsize") != null && $.cookie("fontsize") != 'fontSizeNormal'){
    $('body').removeClass('fontSizeNormal');
    $('body').addClass($.cookie("fontsize"));
  }
}

function increaseFontSize(){
  if($('body').hasClass('fontSizeSmall')){
    $('body').removeClass('fontSizeSmall');
    $('body').addClass('fontSizeNormal');
    $.cookie("fontsize", "fontSizeNormal", { path: '/' });
  }else if($('body').hasClass('fontSizeNormal')){
    $('body').removeClass('fontSizeNormal');
    $('body').addClass('fontSizeLarge');
    $.cookie("fontsize", "fontSizeLarge", { path: '/' });
  }else if($('body').hasClass('fontSizeLarge')){
    //don't make font too large!
    $.cookie("fontsize", "fontSizeLarge", { path: '/' });
  }
}

function decreaseFontSize(){
  if($('body').hasClass('fontSizeSmall')){
    //don't make font too small!
    $.cookie("fontsize", "fontSizeSmall", { path: '/' });
  }else if($('body').hasClass('fontSizeNormal')){
    $('body').removeClass('fontSizeNormal').addClass('fontSizeSmall');
    $.cookie("fontsize", "fontSizeSmall", { path: '/' });
  }else if($('body').hasClass('fontSizeLarge')){
    $('body').removeClass('fontSizeLarge').addClass('fontSizeNormal');
    $.cookie("fontsize", "fontSizeNormal", { path: '/' });
  }
}
