function write_mt(x,y,z){ document.write('<a href="mailto:', y, '@', x, '">', z, '</a>'); }
function write_ml(x,y){ document.write('<a href="mailto:',y,'@',x,'">',y,'@',x,'</a>'); }

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}



$(document).ready(


    function() {
	
	    $("body").addClass("js");

        jQuery.fn.betterToggle = function(accordion, header, body, speed, openClass) {
            var parent = this;

            $(parent).find(header).removeClass(openClass);
            $(parent).find(header).hover(
                function() {
                    $(this).addClass("hover");
                },
                function() {
                    $(this).removeClass("hover");
                }
            );

            $(parent).find(body).hide();
            $(parent).find(header).click(
                function() {
                    if(accordion && !$(this).hasClass(openClass)) {
                        $(parent).find(header).next(body + ":visible").slideToggle(speed);
                        $(parent).find(header).removeClass(openClass);

                        $(this).next(body).slideToggle(speed);
                        $(this).hasClass(openClass) ? $(this).removeClass(openClass) : $(this).addClass(openClass);
                    } else if(!accordion) {
                        $(this).next(body).slideToggle(speed);
                        $(this).hasClass(openClass) ? $(this).removeClass(openClass) : $(this).addClass(openClass);
                    }
                }
            );

            $(parent).find(header + ":first").addClass(openClass);
            $(parent).find(header + ":first").next(body).show();

        };

if($('.b-filter-cats').length > 0){

			if ($.browser.msie && $('#viewcats').length > 0) $('body').addClass('fixIEbold');
			
			if($('.as-table').length > 0){$('.first-header').addClass('open'); $('.last-header').addClass('open'); }else{ $('.first-header').removeClass('open'); $('.last-header').removeClass('open');}

			var anim = false;
			var filtercontainer = $('#viewcats');
			var containerWidth = filtercontainer.width();
			var blockWidth = $('li',filtercontainer).eq(0).outerWidth();
			var blockHeight = $('li',filtercontainer).eq(0).height();
			
			$('.filter-nav').each(function() {
				var $t = $(this);
				$backnav = $('<li class="click-li"></li>').appendTo($t);
				setCurrpos();

				$('a',$t).click(function() {
					if(!$(this).parent().hasClass('current') && anim === false) {
					var $clickel = $(this).parent()[0];
						clickrel = this.getAttribute('rel');
						moveCurr($clickel);
						lengthrel = $('.'+clickrel,'#viewcats').size()
						if(clickrel != null) {
							if(clickrel != 'all'){
								if(lengthrel != 0) {
										$('li','#viewcats').removeClass('markerItemShow');
										$('#viewcats li').not('.'+clickrel).stop().animate({opacity:'0'}, {queue:false, duration:300, complete:function(){
											$(this).hide().css('filter','');
										}});
								
										curIndexes = $('.'+clickrel,'#viewcats').show();
										showfilter(curIndexes);
										
										$('.'+clickrel,'#viewcats').addClass('markerItemShow');
								}
								else {
									curIndexes = $('li','#viewcats').hide();
									$('li.markerItemShow','#viewcats').removeClass('markerItemShow');
									alert('1');
								}
							}
							else {
								curIndexes = $('li','#viewcats').show();
								showfilter(curIndexes);
								$('li','#viewcats').addClass('markerItemShow');
							};
						}
						else {
							$('li','#viewcats').hide();
							$('li.markerItemShow','#viewcats').removeClass('markerItemShow');
						}
					};
					return false;
				});
				
				function setCurrpos() {
					var $el = $t.find('.current')[0] || $t.find('li:first-child').addClass("current")[0];
					$backnav.css({"top": $el.offsetTop +"px"});
				};
				function moveCurr(el) {
					$t.find('.current').removeClass('current');
					$t.find('.click-li').stop().animate({top: el.offsetTop}, 500, function(){
						$(el).addClass('current');
					});
				};
			});
			function showfilter(indexes) {
			
					anim = true;
					var curIndexes = indexes;
					var prodInRow=1;
					var totalRow = 0;
					
					if(!$('.as-table').length){

					var curIndexes = indexes;
					prodInRow= Math.floor(containerWidth/blockWidth);
					totalRow = Math.ceil(curIndexes.length/prodInRow);
					$('.first-header').removeClass('open');
					$('.last-header').removeClass('open');
					
					}else{
					prodInRow= 1;
					totalRow = Math.ceil(curIndexes.length)+1;
					if(!$('.first-header').hasClass('open')) $('.first-header').addClass('open');
					if(!$('.last-header').hasClass('open')) $('.last-header').addClass('open');
					
					}
					
					filtercontainer.css('height',totalRow*blockHeight+'px');
					filtercontainer.parent().css('height',totalRow*blockHeight+'px');
					curIndexes.eq(curIndexes.length-1).data('position','last');

					for(var i=0;i<curIndexes.length;i++) {
						var fromPos=Math.floor(Math.random()*curIndexes.length);
						var fromLeft=(fromPos%prodInRow)*blockWidth;
						var fromTop=Math.floor(fromPos/prodInRow)*blockHeight;
						var toLeft=(i%prodInRow)*blockWidth;
						var toTop=Math.floor(i/prodInRow)*blockHeight;
						$(curIndexes[i]).css({position:'absolute',left:fromLeft+'px',top:fromTop+'px',opacity:'0.1'}).animate({opacity:'1',left:toLeft+'px',top:toTop+'px'}, { queue:false, duration:800, complete:function(){
							$(this).attr('style','');
							if($(this).data('position')==='last') {
								filtercontainer.css('height','');
								$(this).data('position','');
								anim = false;
							};
						}});
					}
					anim = false;
			}
			$('.b-filter-cats a').click(function() {
				var $t = $(this).parent();
				var catrel = $(this).attr('rel');
				$('.b-filter-cats li').removeClass('current')
				$t.addClass('current')
				$('ul.filter-nav').each(function(){
					var fnav = $(this)
					fnav.hide()
					if(!$('li:first-child',fnav).hasClass('current')) {
						fnav.find('li').removeClass('current');
						fnav.find('li:first-child').addClass('current')
						fnav.find('li.click-li').css('top',0);

						if($('#viewcats li.markerItemShow').length > 0) $('#viewcats li.markerItemShow').removeClass('markerItemShow');
						$('#viewcats li').addClass('markerItemShow');
						curIndexes = $('li','#viewcats').show();
						showfilter(curIndexes);
					}
				});
				$('.'+catrel).show();
				return false;
			});
}

if($('.top-tabs').length > 0){

			$('.top-tabs a').click(function() {
			    
				var $t = $(this).parent();
				
				if($t.hasClass('current')) return false;
				
				var catrel = $(this).attr('rel');
				$('.top-tabs li').removeClass('current');
				$t.addClass('current');

			        if($('.tab-pages').length > 0) {
		                    $('.tab-pages').slideUp(350, function() {
		                            $('.tab-pages div').removeClass('current');
		                    });
		                    $('.tab-pages').slideDown(250, function() {
		                            $('.'+catrel).addClass('current');
		                    });
					}
			return false;
			});
}

if($('.view-cats').length > 0){

			if(!getCookie('biona-production-view')){
				setCookie('biona-production-view', 'as-picture', 30);
				}
			
			if(getCookie('biona-production-view')){

					        if(getCookie('biona-production-view') == 'as-picture') {
									$('#viewcats').addClass('b-viewcats').removeClass('as-table');
									$('.first-header').removeClass('open');
							}else if(getCookie('biona-production-view') == 'as-table')
							{
								$('#viewcats').addClass('as-table').removeClass('b-viewcats');
								$('.first-header').addClass('open');
							}
							
								filtercontainer = $('#viewcats');
								containerWidth = filtercontainer.width();
								blockWidth = $('li',filtercontainer).eq(0).outerWidth();
								blockHeight = $('li',filtercontainer).eq(0).height();
								
						if($('#viewcats li.markerItemShow').length > 0) showfilter($('li.markerItemShow','#viewcats').show());
			}

			$('.view-cats a').click(function() {
			
				var $t = $(this).parent();
				
				if($(this).attr('rel') != 'as-file'){
				
				setCookie('biona-production-view', $(this).attr('rel'), 30);
				
						if($t.hasClass('current')) return false;
						$('.view-cats li').removeClass('current');
						$t.addClass('current');
						
						
					        if($(this).attr('rel') == 'as-picture') {
									$('#viewcats').addClass('b-viewcats');
									$('#viewcats').removeClass('as-table');
									$('.first-header').removeClass('open');
							}else if($(this).attr('rel') == 'as-table')
							{
								$('#viewcats').addClass('as-table');
								$('#viewcats').removeClass('b-viewcats');
								$('.first-header').addClass('open');
							}
							
								filtercontainer = $('#viewcats');
								containerWidth = filtercontainer.width();
								blockWidth = $('li',filtercontainer).eq(0).outerWidth();
								blockHeight = $('li',filtercontainer).eq(0).height();
								
						if($('#viewcats li.markerItemShow').length > 0) showfilter($('li.markerItemShow','#viewcats').show());
						
						
				}
			return false;	
			});
}





//table tools
if($('table.highlightTable').length > 0){
        $('table.highlightTable tr').hover(function() {
        	$(this).addClass('hover-tr')
        }, function() {
        	$(this).removeClass('hover-tr')
        });
}

if($('#viewcats li').length > 0){
        $('#viewcats li').hover(function() {
        	$(this).addClass('hover-tr');
        }, function() {
        	$(this).removeClass('hover-tr')
        });
}


if($('.CommentForm').length > 0){

       $('.CommentForm a.OpenButton').click(function() {
		   
		   $('.CommentForm .prevCommentForm').slideUp(50, function(){
				$('.CommentForm .prevCommentForm').addClass('hide');
			});
			
		   $('.CommentForm .mainCommentForm').slideDown(350, function(){
				$('.CommentForm .mainCommentForm').addClass('open');
			});
			
		return false;
	   });
	   
       $('.CommentForm a.CancelButton').click(function() {
		   
		   $('.CommentForm .mainCommentForm').slideUp(350, function(){
				$('.CommentForm .mainCommentForm').removeClass('open');
			});
			
		   $('.CommentForm .prevCommentForm').slideDown(50, function(){
				$('.CommentForm .prevCommentForm').removeClass('hide');
			});
			
		return false;
	   });
	   
 
}


    if($('body ul').length > 0){ $("body ul").betterToggle(true, "h3.toggler", "div.element", "fast", "open");}
	if($('a.zoom').length > 0){ $("a.zoom").fancybox(); }
	if($('ul.faq-page').length > 0){ $("ul.faq-page").betterToggle(false, "h3.toggler", "div.element", "fast", "open");}
	if($('ul.faq-in-page').length > 0){ $("ul.faq-in-page").betterToggle(true, "h4.toggler", "div.element", "fast", "open");}
	if($('.order-form').length > 0) { validator('.order-form'); }
	if($('.mail-form').length > 0) { validatorFeedback('.mail-form'); }



    }
);

