	jQuery(document).ready(function() {
		// run jCarousel
		if (!$('body#hp').length)
		{
			if ($('.carousel li').length > 1)
			{
				$('.carousel').jcarousel({
					scroll: 1,
					wrap: 'circular'
				});
			}
		}

		// wrap a last word in the container into span  
		// use for adding the arrows attached to the last word in container		
		var lastWordArrow = function () {
			var letters = $(this).text().split(' ');
			var new_letters = "";
			for (i in letters) {
				new_letters += (([letters.length-1] == i)?("<span>"):"") + letters[i] + (([letters.length-1] == i)?("</span>"):" ");
			}
			$(this).html(new_letters);			
		}
		
		if ($("section.lead article p a").length)				{ $("section.lead article p a").each(lastWordArrow); }
		if ($("section.lead-list article header h4 a").length)	{ $("section.lead-list article header h4 a").each(lastWordArrow); }		
		
		$('ul.productMin li a').click(function() {
			var $rel = $(this).attr('rel');
			$('ul.product li').hide();
			$('ul.product li').eq($rel).fadeIn('fast');
			return false;
		});

		//wrap
		$('ul.wrap > li > a').click(function(){
			var $a = $(this);
			var $div = $a.next('div')
			if ($a.hasClass('actv')){
				if ($a.parent().hasClass('level1')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1'))
						{
							return false;
						} else if ($(this).hasClass('level2') || $(this).hasClass('level3') || $(this).hasClass('level4'))
						{
							$(this).hide();
						}
					});
				}
				if ($a.parent().hasClass('level2')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1') || $(this).hasClass('level2'))
						{
							return false;
						}
						$(this).hide();
					});
				}
				if ($a.parent().hasClass('level3')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1') || $(this).hasClass('level2') || $(this).hasClass('level3'))
						{
							return false;
						}
						$(this).hide();
					});
				}
				$div.slideUp('fast');
				$a.removeClass('actv');
			} else {
				if ($a.parent().hasClass('level1')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1'))
						{
							return false;
						} else if ($(this).hasClass('level2'))
						{
							$(this).show();
						}
					});
				}
				if ($a.parent().hasClass('level2')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1') || $(this).hasClass('level2'))
						{
							return false;
						} else if ($(this).hasClass('level3'))
						{
							$(this).show();
						}
					});
				}
				if ($a.parent().hasClass('level3')) {
					$a.parent().nextAll().each(function() {
						if ($(this).hasClass('level1') || $(this).hasClass('level2') || $(this).hasClass('level3'))
						{
							return false;
						}
						$(this).show();
					});
				}
				$div.slideDown('fast');
				$a.addClass('actv');
			}
			return false;
		});

		//line1
		var line1 = function(element, h) {
			var $a = $(element);
			var height = $a.height();
			if (height<h) { $a.addClass('line1'); }			
		}
		$('#main > aside nav#local-menu > ul > li > a').each(function(){ line1(this, 16); });
		$('ul.prod-group li div a').each(function(){ line1(this, 19); });
		//$('ul#product-group-list li')
		// main menu
		$("#main-menu > ul > li").hover(
				function() {
					var $li = $(this);
					var $a = $(this).find('a:first');
					$a.addClass('actv');
					//$li.parent().find('li ul').css('display','none');
					$li.parent().find('li ul').fadeOut('fast');
					//$a.next('ul').css('display','block');
					$a.next('ul').fadeIn(400);
					$li.find('ul li a').each(function(){ line1(this, 15); });
				}, 
				function() {
					var $li = $(this);
					var $a = $(this).find('a:first');
					$a.removeClass('actv');
					//$a.next('ul').css('display','none');
					$a.next('ul').fadeOut('fast');
				});

		// wrap an img in the article into div.img positioned relatively
		// take the string from img title and put it into span positined absolutely in div.img 
		// if an img is wrapped into a, add a small tag positioned absolutely in div.img
		// fix all of the tinyMCE problems
		$('section:not(.lead-list) article img').load(function(){
			var $img = $(this);
			var title = $img.attr('title');
			var imgW = $img.width();
			var attr = '';
			if ($img.attr('align') != undefined) { attr = 'float-' + $img.attr('align').charAt(0); }
			if ($img.css('float') != undefined) { attr = 'float-' + $img.css('float').charAt(0); }
			if ($img.parent().is('p') || ($img.parent().is('a') && $img.parent().parent().is('p'))) {
				if ($img.parent().is('a') && $img.parent().parent().is('p')) {
					$p = $img.parent().parent();
					$img.parent().insertBefore($p);
				} else {
					$p = $img.parent();
					$img.insertBefore($p);
				}
				if (!$p.html().length>0){$p.remove();};
			}
			if ($img.parent().is('a')) {
				$img.parent().wrap('<div class="img">');
				$img.before('<small>&nbsp;</small>');
				if (title) {
					$img.parent().after('<div><span>'+title+'</span></div>');
				}
			} else {
				$img.wrap('<div class="img">');
				if (title) {
					$img.after('<div><span>' + title + '</span></div>');
				}
			}
			$img.parents('div:first').addClass(attr).css('width', imgW + 'px');
		});
		
		//galleries
		var galleries = $('.ad-gallery').adGallery();
		$('#switch-effect').change(
		  function() {
		    galleries[0].settings.effect = $(this).val();
		    return false;
		  }
		);
		$('#toggle-slideshow').click(
		  function() {
		    galleries[0].slideshow.toggle();
		    return false;
		  }
		);
		$('#toggle-description').click(
		  function() {
		    if(!galleries[0].settings.description_wrapper) {
		      galleries[0].settings.description_wrapper = $('#descriptions');
		    } else {
		      galleries[0].settings.description_wrapper = false;
		    }
		    return false;
		  }
		);
		
		//fancybox
		$('section.gallery:not(.implement) > ul > li > a, section.gallery.fBox > ul > li > a, div#main >section > article a[href$=.jpg]').fancybox();
		$('a.zoom').fancybox();
		
		
		
		
		$("#topSearch").focus( function() {
			$(this).val("");
		});
		$("#topASearchSubmit").click( function() {
			$("#topSearchForm").submit();
		});
		
		$("a.back").click( function() {
			history.back();
			return false;
		});
		
		$("body#hp ul.prod-group li > a").hover(
			function() {
				$(this).next().find('a').addClass('active');
			},
			function() {
				$(this).next().find('a').removeClass('active');
			}
		);

		$("section.search-results > section > header > h4 > a, section.product > section > header > h3 > a, section.product > article > header > h3 > a, section.acrobat > header > h3 > a").click(function() {
			if ($(this).parent().hasClass('actv')) {
				$(this).parent().parent().next().slideUp('fast');
				$(this).parent().removeClass('actv');
			} else {
				$(this).parent().parent().next().slideDown('fast');
				$(this).parent().addClass('actv');
			}
			return false;
		});
	});
