$(document).ready(function() {
  $('#toggleButton').click(function() {
    if ($('#kontaktni_formular').is(":hidden"))
      {
        $('#kontaktni_formular').slideDown("slow");
      }
    else
      {
        $('#kontaktni_formular').slideUp("slow");
      }
  });
});

// cenik
function ukazCenu(typ,produktID) {
  if (typ == 1){
    var produktVelikostBaleni = document.getElementById('vyberBaleni'+produktID+'').value;
    }
  else {
    var produktVelikostBaleni = document.getElementById('vyberPocetKusu'+produktID+'').value;  
  }

  $('ul.cenadphskladem>li.produktID'+produktID+'').load('/jadro/s_eshop_cena.php?produktID='+produktID+'&produktVelikostBaleni='+produktVelikostBaleni+'&produktTyp='+typ+'');
}

// slideshow
var slideshow = {	
	
	currentItem : 0,
	nextItem : 1,
	currentOpac : 100,
	timeout: 0,
	
	init:function(){		
		var self = slideshow;
		var imgs = id('galerie').getElementsByTagName('img');
		slideshow.max  = imgs.length-1;
		slideshow.imgs = imgs;
		slideshow.prevItem = slideshow.max;
		
		// SET UP IMAGES, OPACITIES, ETC
		slideshow.setUpImages();
		
		// SET UP FIRST IMAGE		
		slideshow.imgs[0].xOpacity = 100;
		slideshow.setOpacity(slideshow.imgs[0]);
		slideshow.imgs[0].style.display = 'block';
		slideshow.imgs[0].style.className = 'current';	
		//id('more-info').innerHTML = slideshow.imgs[0].alt;	
		
		slideshow.interval = 6500; // SET INTERVAL
		slideshow.timer = slideshow.fade.delay(slideshow.interval);
		slideshow.slideControls();	// INITIATE SLIDESHOW CONTROLS		
	},	
	
	setUpImages:function(){
		for(var b=0; b<slideshow.imgs.length; b++){
			//slideshow.imgs[b].style.display = 'block';
			slideshow.imgs[b].xOpacity = 0;
			slideshow.imgs[b].id = 'image-'+b;
		}
	},	
	

	slideControls:function(){
		id('prev').onclick = function(){
			clearTimeout(slideshow.timer);
			slideshow.previousSlide();
			return false;
		}	
		
		id('next').onclick = function(){			
			clearTimeout(slideshow.timer);
			slideshow.fade();
			return false;
		}
		
	},		
	
	previousSlide:function(){
		
		// give previous slide opacity
		slideshow.imgs[slideshow.prevItem].xOpacity = 0;		
		slideshow.imgs[slideshow.prevItem].style.display = 'block';
		slideshow.setOpacity(slideshow.imgs[slideshow.prevItem]);
		
		// fade out current slide		
		var outtie = 0;
		for(var i=100; i>=0; i--){
			setTimeout("changeOpac("+i+", '"+id(slideshow.imgs[slideshow.currentItem]).id+"')", (outtie*(slideshow.interval/1000)));
			outtie++;
		}
		
		// fade in previous slide
		var innie = 0;
		for(var a=0; a<=100; a++){
			setTimeout("changeOpac("+a+", '"+id(slideshow.imgs[slideshow.prevItem]).id+"')", (innie*(slideshow.interval/1000)));
			innie++;
		}		
		//id('more-info').innerHTML = id(slideshow.imgs[slideshow.prevItem].id).alt
		
		// when there finished fading, update the current/past items
		var cur = slideshow.currentItem;
		var prev = slideshow.prevItem;
		var next = slideshow.nextItem;
		
		slideshow.nextItem = slideshow.currentItem;
		slideshow.currentItem = slideshow.prevItem;
		slideshow.prevItem = (prev == 0) ? slideshow.max : prev-1;
		slideshow.timer = slideshow.fade.delay(slideshow.interval);
	},	
	
	fade:function(slides){
		slideshow.nextItem = (slideshow.currentItem == slideshow.max) ? 0 : slideshow.currentItem+1;		
		
		slideshow.imgs[slideshow.nextItem].xOpacity = 0;
		slideshow.setOpacity(slideshow.imgs[slideshow.nextItem]);
		slideshow.imgs[slideshow.nextItem].style.display = 'block';
		
				
		// FADE OUT
		var timer = 0;
	    for(i = 100; i >= 0; i--) {
	        setTimeout("changeOpac("+i+", '"+id(slideshow.imgs[slideshow.currentItem]).id+"')", (timer*(slideshow.interval/1000)));
	        timer++;
	    }	    
	    
	    // FADE NEXT ITEM IN
	    var innie = 0;
	    for(a = 0; a<=100; a++){
	    	setTimeout("changeOpac("+a+", '"+id(slideshow.imgs[slideshow.nextItem]).id+"')", (innie*(slideshow.interval/1000)));
	    	innie++;
	    }
	   	
	    slideshow.prepNextSlide();
	    slideshow.timer = slideshow.fade.delay(slideshow.interval);	    	
	},	
	
	prepNextSlide:function(){
		//id('more-info').innerHTML = id(slideshow.imgs[slideshow.currentItem]).alt;
		slideshow.currentItem = slideshow.nextItem;
		slideshow.nextItem = (slideshow.imgs[slideshow.currentItem+1]) ? slideshow.currentItem+1 : 0;
		slideshow.prevItem = (slideshow.currentItem == 0) ? slideshow.max : slideshow.currentItem-1;		
	},
		
	setOpacity:function(obj){
		obj.style.opacity = (obj.xOpacity/100);
		obj.style.MozOpacity = (obj.xOpacity/100)
		obj.style.filter = 'alpha(opacity=' + obj.xOpacity + ')';		
	}
	
}

// slideshow
function changeOpac(opacity, el) {
    var object = id(el).style;
    object.opacity = (opacity / 100);
    object.MozOpacity = (opacity / 100);
    object.KhtmlOpacity = (opacity / 100);
    object.filter = "alpha(opacity=" + opacity + ")";
}

addEvent(window, 'load', slideshow.init);

// ALL PURPOSE FUNCTIONS
var Class = function(properties){
	var klass = function(){
		if (this.initialize && arguments[0] != 'noinit') return this.initialize.apply(this, arguments);
		else return this;
	};
	for (var property in this) klass[property] = this[property];
	klass.prototype = properties;
	return klass;
};


Class.empty = function(){};

Class.prototype = {

	extend: function(properties){
		var pr0t0typ3 = new this('noinit');

		var parentize = function(previous, current){
			if (!previous.apply || !current.apply) return false;
			return function(){
				this.parent = previous;
				return current.apply(this, arguments);
			};
		};

		for (var property in properties){
			var previous = pr0t0typ3[property];
			var current = properties[property];
			if (previous && previous != current) current = parentize(previous, current) || current;
			pr0t0typ3[property] = current;
		}
		return new Class(pr0t0typ3);
	},

	implement: function(properties){
		for (var property in properties) this.prototype[property] = properties[property];
	}

};



Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
}

Function.prototype.bindAsEventListener = function(object) {
  var __method = this;
  return function(event) {
    return __method.call(object, event || window.event);
  }
}

Object.extend = function(){
	var args = arguments;
	args = (args[1]) ? [args[0], args[1]] : [this, args[0]];
	for (var property in args[1]) args[0][property] = args[1][property];
	return args[0];
};

Object.Native = function(){
	for (var i = 0; i < arguments.length; i++) arguments[i].extend = Class.prototype.implement;
};

new Object.Native(Function, Array, String, Number, Class);

Function.extend({

	create: function(options){
		var fn = this;
		options = Object.extend({
			'bind': fn,
			'event': false,
			'arguments': null,
			'delay': false,
			'periodical': false,
			'attempt': false
		}, options || {});
		if (options.arguments != null && typeof options.arguments != 'undefined' && !(options.arguments instanceof Array))
			options.arguments = [options.arguments];
		return function(event){
			var args = options.arguments || arguments;
			if (options.event){
				event = (options.event === true) ? event || window.event : new options.event(event);
				args = [event].concat(args);
			}
			var returns = function(){
				return fn.apply(options.bind, args);
			};
			if (options.delay) return setTimeout(returns, options.delay);
			if (options.periodical) return setInterval(returns, options.periodical);
			if (options.attempt){
				try {
					var result = returns();
				} catch(err){
					result = err;
				} finally {
					return result;
				}
			} else return returns();
		};
	},

	pass: function(args, bind){
		return this.create({'arguments': args, 'bind': bind});
	},
	
	attempt: function(args, bind){
		return this.create({'arguments': args, 'bind': bind, 'attempt': true})();
	},

	bind: function(bind, args){
		return this.create({'bind': bind, 'arguments': args});
	},

	bindAsEventListener: function(bind, args){
		return this.create({'bind': bind, 'event': true, 'arguments': args});
	},

	delay: function(ms, bind, args){
		return this.create({'delay': ms, 'bind': bind, 'arguments': args})();
	},

	periodical: function(ms, bind, args){
		return this.create({'periodical': ms, 'bind': bind, 'arguments': args})();
	}

});

function externo(){
	var exts = getElementsByClassName(document, 'a', 'external');
	for(var i=0; i<exts.length; i++) {
		exts[i].onclick = function(){
			window.open(this.href);
			return false;
		}
	}
}
addEvent(window, 'load', externo);

function id() {
  var elements = new Array();

  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string')
      element = document.getElementById(element);

    if (arguments.length == 1)
      return element;

    elements.push(element);
  }

  return elements;
}
/*
    Written by Jonathan Snook, http://www.snook.ca/jonathan
    Add-ons by Robert Nyman, http://www.robertnyman.com

*/

function getElementsByClassName(oElm, strTagName, strClassName){	
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);	
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");	
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }	
    return (arrReturnElements)
}



// ============ ADD EVENTS ================ //
function addEvent(obj, evType, fn){
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, true);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on"+evType, fn);
        return r;
    } else {
	    return false;
    }
}
