// JavaScript Document

/********** getElementById - IE fix ***********/
if (/msie/i.test (navigator.userAgent)) //only override IE
{
	document.nativeGetElementById = document.getElementById;
	document.getElementById = function(id)
	{
		var elem = document.nativeGetElementById(id);
		if(elem)
		{
			//make sure that it is a valid match on id
			if(elem.id == id)
			{
				return elem;
			}
			else
			{
				//otherwise find the correct element
				for(var i=1;i<document.all[id].length;i++)
				{
					if(document.all[id][i].id == id)
					{
						return document.all[id][i];
					}
				}
			}
		}
		return null;
	};
}

/************* External Links *****************/
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
  var anchor = anchors[i];
  if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
    anchor.target = "_blank";
 }
} 

/********** moving background ***************/
/**
 * @author Alexander Farkas
 * v. 1.02
 */
(function(jQuery) {
 jQuery.extend(jQuery.fx.step,{
   backgroundPosition: function(fx) {
     if (fx.state === 0 && typeof fx.end == 'string') {
       var start = jQuery.curCSS(fx.elem,'backgroundPosition');
       start = toArray(start);
       fx.start = [start[0],start[2]];
       var end = toArray(fx.end);
       fx.end = [end[0],end[2]];
       fx.unit = [end[1],end[3]];
     }
     var nowPosX = [];
     nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
     nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
     fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];
    
     function toArray(strg){
       strg = strg.replace(/left|top/g,'0px');
       strg = strg.replace(/right|bottom/g,'100%');
       strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
       var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
       return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
     }
   }
 });
})(jQuery);


// no confict
jQuery.noConflict();


//functions
function heightResize(){  
  var minHeight = 477;
  var bgHeight  = 80;
  var actHeight = jQuery('.contentText').height();
  //alert(actHeight); 
  var toResizeHeight = actHeight-minHeight; 
  
    
  if( toResizeHeight>0 && (toResizeHeight % bgHeight != 0)){
    var toRepeat = Math.round(toResizeHeight/bgHeight + 0.5);
    //jQuery('.contentText').height(toRepeat*bgHeight+minHeight);
    var h =toRepeat*bgHeight+minHeight;
    jQuery('.contentText').css({'min-height':h+'px'});
    jQuery('.contentText').css({'_height':h+'px'});
  }  
}

function init(){
  externalLinks();  
  //jQuery('.titleFont').fontReplace({fontFile: 'listfceb'});  
  jQuery('h1').fontReplace({fontFile: 'Bedrockn'},{fontColor: '5a3d1c'});
  //heightResize();    
}

jQuery(document).ready(function() {
//scrollTo
if(window.location.pathname.length > 4){
  jQuery('body').scrollTo( '#scrollTo',2000 );
}

// events

  //logo button
  /*jQuery('.headerMidd a span')
    .css( {backgroundPosition: '-214 0'} )  
    .mouseover(function(){
      jQuery(this).stop().animate(
    	 {backgroundPosition:"(214px 0)"}, 
    	 {duration: 1000});
    })
    .mouseout(function(){
  		jQuery(this).animate(
  			{backgroundPosition:"(-214 0)"}, 
  			{duration:1});  		
    });*/
    jQuery('.headerMidd a span').fadeTo(1,0);
    jQuery('.headerMidd a span').hover(function() {    
    // on hovering over, find the element we want to fade *up*
    var fade = jQuery(this);
    
    // if the element is currently being animated (to a fadeOut)...
    if (fade.is(':animated')) {
      // ...take it's current opacity back up to 1
      fade.stop().fadeTo(250, 1);
    } else {
      // fade in quickly
      fade.fadeTo(250, 1);
    }
  }, function () {
    // on hovering out, fade the element out
    var fade = jQuery(this);
    if (fade.is(':animated')) {
      fade.stop().fadeTo(500, 0);
    } else {
      // fade away slowly
      fade.fadeTo(500, 0);
    }
  });
  
  //food buttons
  jQuery('.drinkBg div')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/drinkBgHover.png) no-repeat 0 0'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent url(/site/img/drinkBg.png) no-repeat 0 0'});
    });
  jQuery('.foodBg div')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/foodBgHover.png) no-repeat 0 0'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent url(/site/img/foodBg.png) no-repeat 0 0'});
    });
  jQuery('.tipOfDayBg div')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/tipOfDayBgHover.png) no-repeat 0 0'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent url(/site/img/tipOfDayBg.png) no-repeat 0 0'});
    });
  jQuery('.foodImg img')
    .mouseover(function(){
      jQuery(this).attr('src','/site/img/photoHover.png');      
    })
    .mouseout(function(){
      jQuery(this).attr('src','/site/img/photo.png');
    });
    
  //gallery buttons  
  jQuery('.galleryNav .leftNav')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/galleryPrevButton.png) no-repeat 0 0'});            
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent'});            
    });
  jQuery('.galleryNav .rightNav')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/galleryNextButton.png) no-repeat 0 0'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent'});      
    });  
  
  //submit buttons
  jQuery('.kontaktSubmit')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/kontaktButtonBgHover.png) no-repeat 0 0'});
      jQuery(this).children('span').children('input').css({color:'#372516'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent url(/site/img/kontaktButtonBg.png) no-repeat 0 0'});
      jQuery(this).children('span').children('input').css({color:'#F5F0AB'});      
    });
  jQuery('.bookingSubmit')
    .mouseover(function(){
      jQuery(this).css({background:'transparent url(/site/img/bookButtonBgHover.png) no-repeat 0 0'});
      jQuery(this).children('span').children('input').css({color:'#372516'});      
    })
    .mouseout(function(){
      jQuery(this).css({background:'transparent url(/site/img/bookButtonBg.png) no-repeat 0 0'});
      jQuery(this).children('span').children('input').css({color:'#F5F0AB'});
    });  
  
  //gallery img click IE fix
  jQuery('span.eventImage img').click(function(){
    window.location = jQuery(this).parent().parent().attr('href');
  });
  
  
  // galleria
  if (jQuery('ul.gallery').length > 0){
    jQuery('ul.gallery').galleria({
    	history   : false, // activates the history object for bookmarking, back-button etc.
    	clickNext : true, // helper for making the image clickable
    	insert    : '#actualImageWrapper', // the containing selector for our main image	
    	onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
    		
    		// fade in the image & caption
    		if(! (jQuery.browser.mozilla && navigator.appVersion.indexOf("Win")!=-1) ) { // FF/Win fades large images terribly slow
    			image.css('display','none').fadeIn(1000);
    		}
    		caption.css('display','none').fadeIn(1000);
    		
    		// fetch the thumbnail container
    		var _li = thumb.parents('li');
    		
    		// fade out inactive thumbnail
    		_li.siblings().children('img.selected').fadeTo(500,0.3);
    		
    		// fade in active thumbnail
    		thumb.fadeTo('fast',1).addClass('selected');
    		
    		// add a title for the clickable image
    		image.attr('title','Ďalší');
    	},
    	onThumb : function(thumb) { // thumbnail effects goes here
    		
    		// fetch the thumbnail container
    		var _li = thumb.parents('li');
    		
    		// if thumbnail is active, fade all the way.
    		var _fadeTo = _li.is('.active') ? '1' : '0.3';
    		
    		// fade in the thumbnail when finnished loading
    		thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
    		
    		// hover effects
    		thumb.hover(
    			function() { thumb.fadeTo('normal',1); },
    			function() { _li.not('.active').children('img').fadeTo('slow',0.3); } // don't fade out if the parent is active
    		)
    		thumb.click(
          function(){
            jQuery('.galleria_wrapper img').attr('href',jQuery(this).attr('rel'))
          }
        )
    	}	
    });
  }
  
  //init js
  init();  
});



