picto_count=3; // poÄŤet pictogramĹŻ 


function isdefined( variable){
	type=(typeof(window[variable]) == "undefined")?  false: true;
	if (type) type=(window[variable]!=-1);
	return type;
}

var active_category=-1;
function gallery_categories_fx_init() {
	/* switch category effects */
	var list = $$('div.category_icon');
	var i=-1;
	list.each(function(element) {
		i++; var myself=i;
		
		element.addEvent('click', function( event ){
			if (event) {
				event.preventDefault();
				if (active_category==myself) return;
			}
			if (active_category==myself) return;
			var old = active_category; active_category=myself; 

			/* galerie */
			var old_thumbnail = active_thumbnail; active_thumbnail = -1;
			
			if ( old_thumbnail != -1 ) {
				$$('a.gallery_thumbnail')[ old_thumbnail ].fireEvent( 'mouseleave' );
			}
			
			// hide old gallery and show this gallery
			if (old != -1) {
				$('galleries_tabs').getChildren()[old].removeClass('active');
				$('gallery_'+old).removeClass('active');
			}
			$('c'+myself).addClass('active');
			$('gallery_'+myself).addClass('active');

			/* multiboxes */
			if (typeof MultiBoxes != "undefined") {
				for(i=0;i<MultiBoxes.length;i++) {
					MultiBoxes[i].reset();
				}
			}
			clearTimeout( slideshow_timer );
			if (slideshow_activated) slideshow_timer = setTimeout("slideshow_next();", slideshow_duration);
			if ($('setup_dialog_thumbnail_slideshow_activated')) {
				$('setup_dialog_thumbnail_slideshow_activated').disabled = 0; // enable thumbnail slideshow checkbox
                $('setup_dialog_thumbnail_slideshow_activated').erase( 'title' );
                $('label_setup_dialog_thumbnail_slideshow_activated').erase( 'title' );
			} 

		});
	});			
}

function click_first_thumbnail( thumb_list ) {
	for (i=0; i<thumb_list.length; i++) {
		if( thumb_list[ i ].parentNode.hasClass ('active') ) {
			thumb_list[ i ].fireEvent( 'mouseenter' );
			thumb_list[ i ].fireEvent( 'click', [{ preventDefault: function(){}, target: thumb_list[ i ] }]);
			return;
		}
	}
}

function click_next_category () {
	var category_list = $$('div.category_icon');
	var next_category = active_category + 1;
	if (next_category >= category_list.length) next_category = 0;
	$$('div.category_icon')[ next_category ].fireEvent( 'click' );
}

function run_slideshow_timer () {
	clearTimeout( slideshow_timer );
	slideshow_timer = setTimeout("slideshow_next();", slideshow_duration);
}

function slideshow_next() {
	/* thumbnails slideshow */
	if ( ! slideshow_activated ) return;
	
	if (thumbnail_slideshow_activated) {

		if( active_category != -1 ) {
			/* je zvolená některá řada */
			
			var thumb_list = $$('a.gallery_thumbnail');

			/* pokud není aktivní žádný náhled, klikneme na první */
			if( active_thumbnail == -1 ) {
				click_first_thumbnail (thumb_list);
				run_slideshow_timer();
				return;
			}
			
			/* najdeme následující náhled */ 
			next_thumb = active_thumbnail + 1;
			
			/* pokud je následující náhled za koncem kategorie, klikneme na další kategorii */
			if (next_thumb >= thumb_list.length || thumb_list[ next_thumb ].parentNode.hasClass ('active') == false ) {

				/* pokud není aktivováno přecházení mezi kategoriemi, klikneme na první náhled */
				if (category_slideshow_activated == false) {
					click_first_thumbnail (thumb_list);
					run_slideshow_timer();
					return;
				}

				/* pokud je aktivováno přecházení mezi kategoriemi, klikneme na další kategorii */
				click_next_category();
				run_slideshow_timer();
				return;
				
			}

			/* nejsme na konci kategorie, tedy klikneme na další náhled */			
			thumb_list[ next_thumb ].fireEvent( 'mouseenter' );
			thumb_list[ next_thumb ].fireEvent( 'click', [{ preventDefault: function(){}, target: thumb_list[ next_thumb ] }]);
			run_slideshow_timer();
			return;
		}
	}
	
	/* pokud jsme zatím nic nezvolili, klikneme na další kategorii */
	click_next_category();
	run_slideshow_timer();
}


var active_thumbnail=-1;
function thumbnails_fx_init() {
	/* gallery thumbnails effects  */
	var list = $$('a.gallery_thumbnail');
	var i=-1;
	list.each(function(element) {
		i++; var myself=i;
		var img=element.getChildren('img'); // thumbnail image
		var fx_img = new Fx.Tween(img, {duration:500});
		element.addEvent('mouseenter', function(){
			if (active_thumbnail==myself) return;
			img.fade(0.5)
		});
		element.addEvent('mouseleave', function(){
			if (active_thumbnail==myself) return;
			img.fade(1)
		});
		element.addEvent('click', function(){
			clearTimeout( slideshow_timer );
			if (slideshow_activated) slideshow_timer = setTimeout("slideshow_next();", slideshow_duration);
			if (active_thumbnail==myself) return;
			var old_active=active_thumbnail; active_thumbnail=myself; 
			// restore previously actibe thumbnail
			if (old_active!=-1) $$('a.gallery_thumbnail')[old_active].fireEvent('mouseleave'); 
		});
	});			
}

var FadeIn = function(el){
	var div = $(el).setStyles({
		display:'',
		opacity: 0
	});
	new Fx.Style(div, 'opacity', {duration: 400} ).start(0.85);
};


function fill_gallery(container, g_index,g_images) {
	var base = g_images[0][0]; var j;
	for(j=1;j<g_images.length;j++){
		if (g_images[j][0]=='-') {
			new Element('br').inject(container, 'bottom'); 
			new Element('br').inject(container, 'bottom'); 
		}
		timerId='myTimer_'+g_index+"_"+j;
		title='vzor '+g_images[j][picto_count+1];
		var link = new Element('a', {
			id: 'mb_images_'+g_index+'_'+j,
			rel: '[mb_images_'+g_index+'_]',
			'class': 'mb_images_'+g_index+'_ gallery_thumbnail',
			href: 'data/galerie/'+base+'/'+image_dir+'/'+g_images[j][0],
			styles: { margin: 0, padding: '2px' }
		});
		var img = new Element('img', { 
			src: 'data/galerie/'+base+'/obrazky/t/'+g_images[j][0],
			styles: { margin: 0, padding: 0 },
			title: title
		});
		img.inject(link, 'bottom');

		link.inject(container, 'bottom');		
	}
}

function write_galleries() {
	var galleries = new Element('div', { id: 'gallery' });
	for (ind=0;ind<5;ind++) {
		var gallery = new Element('div', { id: 'gallery_'+ind, 'class' : serie_materials[ind].toLowerCase() }).inject(galleries, 'bottom');
		fill_gallery( gallery, ind, gallery_images[ind] );
	}
	galleries.inject($('galleries_content').empty(), 'bottom');

	if (typeof MultiBoxes=="undefined") MultiBoxes = new Array();
	window.addEvent('domready', function(){
		MultiBoxes[MultiBoxes.length] = new multiBox({
			mbClass: ".mb_images_0_", useOverlay: false, openFromLink: true,
			container: $(document.body), contentColor: '#F8EEE2'  
		});
		MultiBoxes[MultiBoxes.length] = new multiBox({
			mbClass: ".mb_images_1_", useOverlay: false, openFromLink: true, 
			contentColor: '#F8EEE2',container: $(document.body)  
		});
		MultiBoxes[MultiBoxes.length] = new multiBox({
			mbClass: ".mb_images_2_", useOverlay: false, openFromLink: true, 
			contentColor: '#F8EEE2',container: $(document.body)  
		});
		MultiBoxes[MultiBoxes.length] = new multiBox({
			mbClass: ".mb_images_3_", useOverlay: false, openFromLink: true, 
			contentColor: '#F8EEE2',container: $(document.body)  
		});
		MultiBoxes[MultiBoxes.length] = new multiBox({
			mbClass: ".mb_images_4_", useOverlay: false, openFromLink: true, 
			contentColor: '#F8EEE2',container: $(document.body)  
		});
		//MultiBoxes[l].open($('mb_images_1_1'));
	});
}

function clientSize(what) { // 'width' or 'height'
	  // http://www.howtocreate.co.uk/tutorials/javascript/browserwindow
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
		myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	  if (what=='widht') return myWidth;
	  if (what=='height') return myHeight;
	}
		
//http://davidwalsh.name/progress-bar-animated-mootools
//class is in
var dwProgressBar = new Class({
	
	//implements
	Implements: [Options],

	//options
	options: {
		container: $$('body')[0],
		boxID:'',
		percentageID:'',
		displayID:'',
		displayText: false,
		speed:10,
		initialCounter: 0,
		initialTotal: 0
	},
	
	//initialization
	initialize: function(options) {
		//set options
		this.setOptions(options);
		//create elements
		this.createElements();
	},
	
	//creates the box and percentage elements
	createElements: function() {
		var box = new Element('div', { id:this.options.boxID });
		var perc = new Element('div', { id:this.options.percentageID, 'style':'width:0px;' });
		perc.inject(box);
		box.inject(this.options.container);
		if(this.options.displayText) { 
			var text = new Element('div', { id:this.options.displayID });
			text.inject(this.options.container);
		}
		this.set(this.options.initialCounter,this.options.initialTotal);
	},
	
	//calculates width in pixels from percentage
	calculate: function(percentage) {
		return ($(this.options.boxID).getStyle('width').replace('px','') * (percentage / 100)).toInt();
	},
	
	//animates the change in percentage
	animate: function(counter, total){
		var to=(counter + 1) * (100 / total);
		$(this.options.percentageID).set('morph', { duration: this.options.speed, link:'cancel' }).morph({width:this.calculate(to.toInt())});
		//$(this.options.percentageID).set('tween', { duration: this.options.speed, link:'cancel' }).tween('width',this.calculate(to.toInt()));
		if(this.options.displayText) { 
			$(this.options.displayID).set('text', to.toInt() + '%  ('+(counter+1).toInt()+' ze '+total+')'); 
		}
	},
	
	//sets the percentage from its current state to desired percentage
	set: function(counter, total) {
		this.counter=counter;
		this.animate(counter, total);
	},
	
	get_counter: function() {
		return (this.counter);
	}
	
});


