var interval;

var ie = (navigator.appName == "Microsoft Internet Explorer");

function slide(el, turn, handler, f, f_b, time) {

  if(f_b!=undefined) f_b();

  _obj = el.find('div.slide');

  count = _obj.children().length;

  margin = parseInt(_obj.css('marginLeft'));

  act = $(_obj.children().get(0));

  _size = parseInt(act.width())+parseInt(act.css('paddingLeft'))+parseInt(act.css('paddingRight'));

  _show = parseInt(el.find('div.slide').width()/_size);

  if(isNaN(margin)) margin=0;

  if(turn=='left') {

    if(margin==0) return;

    el.find('.sq_l').unbind('click');

		_obj.stop().animate({'marginLeft':(margin+_size)+'px'}, time, function() {

			if(f!=undefined) f();

		});

  } else if(turn=='right') {

    if((margin/_size) < (0+_show-count)) margin=_size;

    el.find('.sq_r').unbind('click');

		_obj.stop().animate({'marginLeft':(margin-_size)+'px'}, time, function() {

			if(f!=undefined) f();

		});

  }

}



function to_left(time, f) {

	slide($("#products .list"), 'left', undefined, function() {

		_item = $($("#products .slide div").get(0));

		_w = parseInt(_item.css('width'))+parseInt(_item.css('paddingLeft'))+parseInt(_item.css('paddingRight'));

		marginLeft = parseInt($("#products .slide").css('marginLeft'));

		_news = $("#products .slide div:last");

		$("#products .slide").prepend(_news);

		$("#products .slide").css({'marginLeft': (marginLeft-_w)+'px'});

		if(f!=undefined) f();

	}, function() { }, time);

}



function to_right(time, f) {

	slide($("#products .list"), 'right', undefined, function() {

		_item = $($("#products .slide div").get(0));

		_w = parseInt(_item.css('width'))+parseInt(_item.css('paddingLeft'))+parseInt(_item.css('paddingRight'));

		marginLeft = parseInt($("#products .slide").css('marginLeft'));

		_news = $("#products .slide div:first");

		$("#products .slide").append(_news);

		$("#products .slide").css({'marginLeft': (marginLeft+_w)+'px'});

		if(f!=undefined) f();

	}, function() { }, time);

}



$(document).ready(function() {

  if($.browser.msie && $.browser.version.substr(0,1)>=8) {

    $("#products .slide img").each(function(){

      $(this).css({'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+$(this).attr("src")+'\', sizingMethod=\'scale\')'}).attr('src', '/fancybox/blank.gif');

    });

  }

  if($.browser.msie && $.browser.version.substr(0,1)==7) {

//    $("img.logo").css({'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'\i\logo.png\', sizingMethod=\'scale\')'}).attr('src', '/fancybox/blank.gif');

  	$("#products .slide img").each(function() {

  		$(this).parent().css({'background': 'url(\''+$(this).attr("src")+'\') no-repeat center center'});

  		$(this).attr("src", "/fancy/blank.gif");

  	});



	$("#category > .a ul").css({'marginLeft':'-20px', 'marginTop':'3px'});

  }



	if ($('.overview').height() > 280) {

		$('.scroolbarfx > .scrollbar').css('display', 'block');

		$('.scroolbarfx').tinyscrollbar();	

	}

	if ($('.spec .overview').length) {

		img = new Image();

		img.src = $(".spec .overview a img").attr("src");

		img.onload = function() {

			$(".spec .overview a img").css({'height':img.height+'px'});

			if(img.height > 280) {

				$('.spec .scroolbarfx > .scrollbar').css('display', 'block');

				$('.spec .scroolbarfx').tinyscrollbar();

			}

		};

	}



	if ( window.location.pathname == '/index.php' || window.location.pathname == '/' ) {



		//$('.logo').attr('src', 'i/logo_white.png');

		$('.logo').attr('src', 'i/logo-stroke.png');

	}



	$("#faq > li > b").css({'cursor':'pointer'}).click(function() {

		if ( $(this).parent().find('div').is(':visible') ) {

			$("#faq > li > div").stop().hide();

			$(this).parent().find('div').css({'opacity':0}).hide();

		} else {

			$("#faq > li > div").stop().hide();

			$(this).parent().find('div').stop().css({'opacity':0}).show().animate({'opacity':1});

		}

	});



	$("#product_r > ul > li").css({'cursor':'pointer'}).click(function() {

		_idx = $("#product_r ul li").index($(this));

		$("#product_r > ul > li").removeClass("a");

		$(this).addClass("a");

		//$("#product_r div").animate({'opacity': 0}, 300, function() {

		$("#product_r > div").hide();

		$($("#product_r > div").get(_idx)).stop().css({'opacity':0}).show().animate({'opacity':1}, 300, function() {

				if ($('.desc .overview').height() > 280) {

					$('.desc .scroolbarfx > .scrollbar').css('display', 'block');

					$('.desc .scroolbarfx').tinyscrollbar();	

				}

				if ($('.akces .overview').height() > 280) {

					$('.akces .scroolbarfx > .scrollbar').css('display', 'block');

					$('.akces .scroolbarfx').tinyscrollbar();	

				}

				if ($('.spec .overview').length) {

		//alert("A");

					img = new Image();

					img.src = $(".spec .overview a img").attr("src");

					img.onload = function() {

						$(".spec .overview a img").css({'height':img.height+'px'});

			//alert("B");

			//alert(img.height);

						if(img.height > 280) {

							$('.spec .scroolbarfx > .scrollbar').css('display', 'block');

							$('.spec .scroolbarfx').tinyscrollbar();

						}

					};

					if($('.spec .overview').height() > 280) {

					$('.spec .scroolbarfx > .scrollbar').css('display', 'block');

					$('.spec .scroolbarfx').tinyscrollbar();

					}

			}

	});

		//});

	});

	

	$("#top > ul > li").hover(function() {

		ul = $(this).find('ul');

		_height = 31;

		if($.browser.safari)

			_height = 29;

		else if($.browser.msie && $.browser.version.substr(0,1)==7)

			_height = 30;

		$(this).find('ul').show().stop().css({'opacity':0}).height(0).animate({'opacity':1, 'height': (ul.find('li').length*_height)+'px'}, 300);

	}, function() {

		ul = $(this).find('ul');

		ul.css({'opacity':0}).height(0).hide();

	});



	$("#product_l img.mini").click(function() {

		_src = $(this).attr("src");

		$("#product_l img.mini").each(function(_i, _item) {

			__src = $(this).attr("src").split("-");

			if(_src == $(this).attr("src")) {

				$("#product_l img.main").attr("src", __src[0]+"-3.png");

				$("#product_l a.main").attr("href", __src[0].replace("crop", "org")+".png");

			} else {

				$($("#product_l a[rel=fancybox][class!='main']").get(_i)).attr("href", __src[0].replace("crop", "org")+".png");

			}

		});

//		_src = $(this).attr("src").split("-");

//		$("#product_l img.main").attr("src", _src[0]+"-3.png");

//		$("#product_l a.main").attr("href", _src[0].replace("crop", "org")+".png");

	});



	$("#product_l a[rel=fancybox]").fancybox();

	$("a[rel=lightbox]").fancybox();



    $("#products .slide img").mouseover(function() {

			$(this).stop().css({'opacity': 1}).fadeOut(200).fadeIn(50);

    }).css({'cursor':'pointer'}).click(function() {

		$("#products .slide img").removeClass("a").stop().css({'opacity':1}).animate({'left':'40px', 'top':'30px', 'height':'70px'});

		$(this).stop().addClass("a").animate({'left':'20px', 'top':'15px', 'height':'100px'});

		_index = $(this).attr('alt');



		_top = parseInt(($("#content").height()-products[_index].height)/2);

		if ( !ie ) {

			$("#content .img")

			.css({'opacity':0, 'width':'600px', 'top':'-100px', 'left': '0px'})

			.attr('src', products[_index].img)
			//'width':'350px', 
			.animate({'opacity':1, 'width':'350px', 'top':_top+'px', 'left':'80px'});

		} else if($.browser.version.substr(0,1)>=8) {

		  	_top-=20;

			$("#content .img")
			//'width':'350px', 
			.css({'opacity':0, 'height':products[_index].height+'px', 'top':'-100px', 'left': '0px'})

			.attr('src', '/fancybox/blank.gif')

      		.animate({'opacity':1, 'width':'350px', 'top':_top+'px', 'left':'80px'}, function() {

        		//$("#content .img").css({'width':'auto'});

      		})

			.css({'filter':'progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\''+products[_index].img+'\', sizingMethod=\'scale\')'});

		} else {

			if($("#content .img").length) {

				$("#content .img").remove();

				_div = $('<div class="img"></div>');
				//'width':'350px', 

				_div.css({'background':'url(\''+products[_index].img+'\') no-repeat', 'height':products[_index].height+'px', 'top':'-100px', 'left': '0px'});

				$("#content").append(_div);

				$("#content .img").attr("rel", _index);

				$("#content .img").animate({'top':_top+'px', 'width':'350px', 'left':'80px'});

				$("#content .img").click(function() {

					_index = $(this).attr("rel");

					window.location = 'product,'+_index+'.html';

				});

			}

		}



		if ( window.location.pathname == '/index.php' || window.location.pathname == '/' ) {

			$('#content').css({'background-image': 'url('+products[_index].bg+')'});

		}



		$("#content .img").attr('rel', $(this).attr('rel'));



		setInt();

                

                $("#content .title>.title_in").html(products[_index].name);

		$("#content .title").css({'opacity':0, 'left': '1000px'}).animate({'opacity':1, 'left':'453px'});



    	$("#content .txt2").css({'left': '1000px'}).animate({'left':'453px'}).html(products[_index].desc);

		try {

		$("#content .txt").width($("#content .txt2").width() + 30).height($("#content .txt2").height() + 30).css('background-color', '#fff');

		} catch(e){}

		$("#content .txt").css({'opacity':0, 'left': '1000px'}).animate({'opacity':0.8, 'left':'453px'});

    }).click(function() {

		_item = $($("#products .slide div").get(0));



		_w = parseInt(_item.css('width'))+parseInt(_item.css('paddingLeft'))+parseInt(_item.css('paddingRight'));

		marginLeft = parseInt($("#products .slide").css('marginLeft'));

		all = $("#products .slide").width()/_w;

		_index = $("#products .slide img").index($(this));

		_i = all-(0-marginLeft)/_w-_index;

		switch(_i) {

			case 0: to_right(200, function() { to_right(200); });

				break;

			case 1: to_right(400);

				break;

			case 3: to_left(400);

				break;

			case 4: to_left(200, function() { to_left(200); });

				break;

		}

    }).mousedown(function(){

		if ( window.location.pathname != '/index.php' && window.location.pathname != '/' ) {

			_index = $(this).attr('rel');

			window.location = 'product,'+_index+'.html';

		}

	});



	$('#content .img').click(function(){

		_index = $(this).attr('rel');

		window.location = 'product,'+_index+'.html';

	});



	var timeout = false


/*
    $(".prev").css({'cursor':'pointer'}).mouseover(function() {

		if (timeout)



			return false;



		timeout = true;



		setInt();

    	_idx = $("#products .slide div img").index($("#products .slide div img.a"));

    	$($("#products .slide div img").get(_idx-1)).trigger('click');

		setTimeout(function(){

			timeout = false

		}, 1000);

    });
	*/
	 $(".prev").css({'cursor':'pointer'}).click(function() {

		if (timeout)



			return false;



		timeout = true;



		setInt();

    	_idx = $("#products .slide div img").index($("#products .slide div img.a"));

    	$($("#products .slide div img").get(_idx-1)).trigger('click');

		setTimeout(function(){

			timeout = false

		}, 1000);

    });


/*
	$(".next").css({'cursor':'pointer'}).mouseover(function() {

                

		if (timeout)

			return false;



		timeout = true;



		setInt();



    	_idx = $("#products .slide div img").index($("#products .slide div img.a"));

    	$($("#products .slide div img").get(_idx+1)).trigger('click');



		setTimeout(function(){

			timeout = false

		}, 1000);

	});
*/
		$(".next").css({'cursor':'pointer'}).click(function() {

                

		if (timeout)

			return false;



		timeout = true;



		setInt();



    	_idx = $("#products .slide div img").index($("#products .slide div img.a"));

    	$($("#products .slide div img").get(_idx+1)).trigger('click');



		setTimeout(function(){

			timeout = false

		}, 1000);

	});
	


	$($("#products .slide img").get(4)).trigger('click');

	

});



function setInt () {

	clearInterval(interval);

	interval = setInterval(function(){

		$(".next").trigger('click');

	}, 9000);

}
