$(document).ready(function() {

  swapImages = function() {
    $count = $("#header img").length;
    if($count > 1) $("#header img:eq(0)").remove(); 
    $img = '<img src="/img/header.php?'+Math.round(Math.random()*100000)+'" alt="" />';
    $("#header").append($img);
    $("#header img:eq(1)").fadeIn(1000);
  }
  setInterval('swapImages()', 5000);

  var repl = document.location.href.replace(/#+(.*)/g, '');
  var anchor = new String(window.location);
  if(anchor.indexOf('#')) {
   	var $target = $(window.location.hash), target = window.location.hash;
   	if (target && (repl != "http://"+location.hostname+"/restaurace/") && (repl != "http://"+location.hostname+"/")) {
   		var targetOffset = $target.offset().top;
   		$('html').animate({scrollTop: targetOffset}, 400, function() {
   			location.hash = target;
   		});
   	}
  }
	
  $('a[href*=#]').each(function() {
   	if ( location.hostname == this.hostname || !this.hostname && this.hash.replace(/#/,'') ) {
   	  if((repl != "http://"+location.hostname+"/restaurace/") && (repl != "http://"+location.hostname+"/")) {
     		var $target = $(this.hash), target = this.hash;
     		if (target) {
     			var targetOffset = $target.offset().top;
     			$(this).click(function(event) {
     				event.preventDefault();
     				$('html').animate({scrollTop: targetOffset}, 400, function() {
     					location.hash = target;
     				});
     			});
     		}
   		}
   	}
  });
	
	$("#menuIndex").tabs();


	/* dialog error */				 
	$(".dialog.error").dialog({
		bgiframe: true,
		minHeight: 20,
		width: 200,
		height: 80,
		modal: true,
		show: 'slide',
		resizable: false,
		hide: 'slide'
	});
	
	$("#msg").dialog({
		bigframe: true,
		minwidth: 100,
		minHeight: 100,
		show: 'drop',
		hide: 'drop',
		resizable: false,
		position: ['right', 'bottom'], 
		open: function() { window.setTimeout('$("#msg").dialog("close")', 5000) }
	});
	
	$(".gb").mouseover(function(){
		$(".options", this).addClass("showed");
			$(".options").mouseover(function(){
				$(this).addClass("auto");
			}).mouseout(function(){
				$(this).removeClass("auto");
			});
	}).mouseout(function(){
		$(".options", this).removeClass("showed");
	});
		
	$(".permalink").bind("click", function(){
      $(this).next().dialog({
			bigframe: true,
			minwidth: 100,
			minHeight: 100,
			modal: true,
			title: "Stálý odkaz na příspěvek",
			show: 'drop',
			hide: 'drop',
			resizable: false,
			position: ['center', 'center']
		});
  });
	
	//$("textarea").textlimit('span.counter',256);
	
	/* input tip messages */
	$(".help").oxtip({float: "left", width: 200, opacity: 0.9, a: "mouseover", d: "mouseout"});
	$(".tip").oxtip({float: "right", maxWidth: 270, a: "focus", d: "blur"});
	
	// gallery scroller 
	$(".slide .move-left").mouseover(function() {
    alert($(this).parent().html());
  });
		
});

/* oxtip */
jQuery.fn.oxtip = function(e) {

	this.bind(e.a, function(){
		
		var de = document.documentElement;
		var w = self.innerWidth || (de&&de.clientWidth) || document.body.clientWidth;
		var clickElementy = getAbsoluteTop(this.id); //set y position
		var clickElementx = getAbsoluteLeft(this.id); //set x position
		
		if(e.float == "left") clickElementx -= ((this.offsetWidth*1) + e.width); else clickElementx += ((this.offsetWidth*1) + 15);
		
		var div = "<div id='tip'><div class='arrow-"+e.float+"'></div>";
		if(this.title != false) div += "<div class='title'>"+this.title+"</div>";
		div += "<div id='tipText'><img src=\"/img/animations/tip-loader.gif\" alt=\"nahrávám...\" /></div></div>";
		
		$("body").append(div);//left side
		
		$('#tipText').load("/app/scripts/tip.php?tip="+this.name);
		$('#tip').css({left: clickElementx+"px", top: clickElementy+"px"});
		$('#tip').css({left: clickElementx+"px", top: clickElementy+"px", "width": e.width+"px", "max-width": e.maxWidth+"px", "-moz-opacity": e.opacity, "filter": "filter: alpha(opacity="+(e.opacity*100)+")"});
		$('#tip').show(500);
		
	});
	this.bind(e.d,function(){
		$('#tip').remove();
	})
}

function getAbsoluteLeft(objectId) {
	// Get an object left position from the upper left viewport corner
	o = document.getElementById(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent != null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	return oLeft
}

function getAbsoluteTop(objectId) {
	// Get an object top position from the upper left viewport corner
	o = document.getElementById(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	return oTop
}

jQuery.fn.stripTags = function() {
  return this.replaceWith( this.html().replace(/<\/?[^>]+>/gi, '') );
};


// slider js
var scrollStep = 2;
var timer = 0;

jQuery.fn.scrollGallery = function(side, stop) {
  if(stop > 0) {
    if(side == 1) elm.scrollLeft += scrollStep;
    else $(this).scrollLeft -= scrollStep;
    timer = setTimeout("sLeft('"+id+"')",10);
  } else
    clearTimeout(timer);
}

function sLeft(id) {
  clearTimeout(timerRight)
  document.getElementById(id).scrollLeft+=scrollStep
  timerRight=setTimeout("sLeft('"+id+"')",10)
}

function sRight(id) {
  clearTimeout(timerLeft)
  document.getElementById(id).scrollLeft-=scrollStep
  timerLeft=setTimeout("sRight('"+id+"')",10)
}

function stopMe() {
  clearTimeout(timerRight)
  clearTimeout(timerLeft)
}