jQuery.noConflict();

jQuery(document).ready(function(){
	
	// activates the lightbox page, if you are using a dark color scheme use another theme parameter
	my_lightbox("a[rel^='prettyPhoto'], a[rel^='lightbox']");
	k_menu(); // controls the dropdown menu
//	jQuery($elements).contents("img").
	//if ( !(jQuery.browser.msie && parseInt(jQuery.browser.version) < 9)) {
		jQuery('#stranka').kriesi_image_preloader({delay:200});	// activates preloader for non-slideshow images
	//}else {
	//	jQuery('#stranka img[src*='.png']').kriesi_image_preloader({delay:200});	// activates preloader for non-slideshow images
	//		img[src*='.png']
	//}
});

function my_lightbox($elements)
{	
	jQuery('.gallery-item a').attr('rel','lightbox[grouped]');
	
	//volba svetly nebo tmavy theme
	var usedCSS = 1;
	/*jQuery('link').each(function()
	{	
		styleURL = jQuery(this).attr('href'); 
		CSSnumber = styleURL.match(/style(\d).css/);
		if(CSSnumber && CSSnumber.length > 0)
		{
			usedCSS = CSSnumber[1];
		}
	});*/
		
	
	var theme_selected = 'light_rounded';
	if (usedCSS == 2 || usedCSS == 4)
	{
		theme_selected = 'dark_rounded';
	}
	
	jQuery($elements).prettyPhoto({
			"theme": theme_selected, social_tools: false /* light_rounded / dark_rounded / light_square / dark_square */																});
	
	jQuery($elements).each(function()
	{	
		var $image = jQuery(this).contents("img");
		$newclass = 'lightbox_video';
		
		if(jQuery(this).attr('href').match(/(jpg|gif|jpeg|png|tif)/)) $newclass = 'lightbox_image';
			
		if ($image.length > 0)
		{	
			if(jQuery.browser.msie &&  jQuery.browser.version < 7) jQuery(this).addClass('ie6_lightbox');
			
			var $bg = jQuery("<span class='"+$newclass+" ie6fix'></span>").appendTo(jQuery(this)),
				$padding_x = parseInt($image.css('padding-left')) + parseInt($image.css('padding-right')),
				$padding_y = parseInt($image.css('padding-top')) + parseInt($image.css('padding-bottom')),
				$border_x = 0//parseInt($image.css('border-left-width')) + parseInt($image.css('border-right-width')),
				$border_y = 0//parseInt($image.css('border-top-width')) + parseInt($image.css('border-bottom-width'));
			
			jQuery(this).bind('mouseenter', function()
			{
				$height = parseInt($image.height()) + $padding_x + $border_x;
				$width = parseInt($image.width()) + $padding_y + $border_y;
				$pos =  $image.position();
				$bg.css({height:$height, width:$width, top:$pos.top, left:$pos.left});
			});
		}
	});	
	
	jQuery($elements).contents("img").hover(function()
	{
		if (jQuery(this).is("img[src*='.png']") && (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) ){
		}else {
			jQuery(this).stop().animate({opacity:0.5},400);
		}
	},
	function()
	{
		if (jQuery(this).is("img[src*='.png']") && (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) ){
		}else {
			jQuery(this).stop().animate({opacity:1},400);
		}
	});
}

function k_menu()
{
	// k_menu controlls the dropdown menus and improves them with javascript
	
	jQuery(".nav a, .catnav a").removeAttr('title');
	jQuery(" .nav ul, .catnav ul ").css({display: "none"}); // Opera Fix
	
	
	// remove the last border from category menu item if there are 7 items, that border is not needed
	if(jQuery(".catnav>li").length >= 7)
	{
		jQuery(".catnav>li:last").addClass('noborder');
	}
	
	
	//set equal height for all category main items, in case a description is too long
	var mainitem = jQuery(".catnav>li>a");
	mainitem.each(function()
	{
		if(jQuery(this).height() < 34)
		{
			jQuery(this).css({height:"34px"});
		}
	});
	mainitem.equalHeights();
	
	
	
	//smooth drop downs
	jQuery(".nav li, .catnav li").each(function()
	{	
		
		var $sublist = jQuery(this).find('ul:first');
		
		jQuery(this).hover(function()
		{	
			$sublist.stop().css({overflow:"hidden", height:"auto", display:"none"}).slideDown(400, function()
			{
				jQuery(this).css({overflow:"visible", height:"auto"});
			});	
		},
		function()
		{	
			$sublist.stop().slideUp(400, function()
			{	
				jQuery(this).css({overflow:"hidden", display:"none"});
			});
		});	
	});
}

//equalHeights by james padolsey
jQuery.fn.equalHeights = function() {
    return this.height(Math.max.apply(null,
        this.map(function() {
           return jQuery(this).height()
        }).get()
    ));
};
	
// -------------------------------------------------------------------------------------------
// The Image preloader
// -------------------------------------------------------------------------------------------


(function($)
{
	$.fn.kriesi_image_preloader = function(variables, callback) 
	{
		var defaults = 
		{
			fadeInSpeed: 600,
			maxLoops: 10,
			callback: '',
			delay:500
		};
		
		var options = $.extend(defaults, variables);
			
		return this.each(function()
		{
			var container 	= $(this);
			if ( (jQuery.browser.msie && parseInt(jQuery.browser.version) < 9)) {
				var	images		= $("img:not([src*='.png'])", this).css({opacity:0, visibility:'visible', display:'block'});
			}else {
				var	images		= $("img", this).css({opacity:0, visibility:'visible', display:'block'});
			}
			var	parent = images.parent(),
				imageCount = images.length,
				interval = '',
				allImages = images ;
				
			
			var methods = 
			{
				checkImage: function()
				{
					images.each(function(i)
					{
						if(this.complete == true) images = images.not(this);
					});
					
					if(images.length && options.maxLoops >= 0)
					{
						options.maxLoops--;
						setTimeout(methods.checkImage, 500);
					}
					else
					{
						methods.showImages();
					}
				},
				
				showImages: function()
				{
					allImages.each(function(i)
					{
						var currentImage = $(this);
						setTimeout(function()
						{	
							currentImage.animate({opacity:1}, options.fadeInSpeed, function()
							{
								if(allImages.length == i+1) methods.callback(i);
							});
						},options.delay*(i+1));
						
					});
				},
				
				callback: function()
				{				
					if (variables instanceof Function) { callback = variables; }
					if (callback  instanceof Function) { callback.call(this);  }
					if(options.callback != '') (options.callback)();

				}
			};
			
			methods.checkImage();

		});
	};
})(jQuery);
