var productNaviSpeed = 2;
var productNaviClickSpeed = 10; 
var productNaviDirection = productNaviSpeed;
var productNaviDirectionResumeDirection = 0;
var productNaviTimeout = 30;
var productNaviPosition = 0;
var productNaviCount = 0;

jQuery(document).ready(function(){
	jQuery(".gallery").each(function(){
		jQuery(this).find(".lightbox").slimbox();
	});
	
	/*
	$(".lightbox").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});*/

	
	jQuery(".gallery-image-wrapper").each(function(){
		alignPicture(this);
	});
	
      if(jQuery.browser.mozilla) {
    	  jQuery("body").css("overflow-y","scroll");
      }		   
	
	$(".confirm").click(function(){
		if(confirm("Opravdu smazat?")){
			return true;
		} else {
			return false;
		}
	});
	
	//jQuery("#teaser")
	//runTeaser();
	
	initProductNavi();
	initNewsNavi();
	initPages();
});

var newsPage = 1;
var pagesLoaded = new Array();
var pages = 0;
var scroll = true;
function initPages(){
	pages = $("#pages").text();
	for(i=1;i<=pages;i++){
		pagesLoaded[i] = false;
	}
	pagesLoaded[1] = true;
}
function initNewsNavi(){
	
	// start height
	//var height = new Array();
	//height[newsPage] = $("#news").height();
	
	var he = $("#news ul:nth-child("+newsPage+")").height()+10;
	$("#news").animate({
		'height': he 
	}, 1000, function(){
		scroll = true;
		
	})
//	
//	alert("init - "+scroll);
	
	$('a[href$="#up"]').click(function(){
			if(scroll){
				scroll = false;
				//var hh = '+=610px';
				var len = $("#news ul:first-child").css("margin-top").length-2;
				var h = $("#news ul:first-child").css("margin-top").substr(0,len);
				//var hh = (parseInt(h)+710)+'px';
				
				/*
				var he = $("#news ul:nth-child("+newsPage+")").height()+10;
				$("#news").animate({
					'height': he 
				}, 500);*/
				var hh = (parseInt(h)+($("#news ul:nth-child("+(newsPage-1)+")").height()+10))+'px';
				
				$("#news ul:first-child").animate({
					'margin-top': hh
				}, 2000, function(){
					newsPage--;
					//alert(newsPage);
					if(newsPage==1){
						$(".uparrow:parent").css("padding-bottom","10px");
						$(".uparrow").fadeOut();
					}
					if(newsPage<pages){
						$(".downarrow").fadeIn();
					}
					// loading
					
					$(".downarrow").empty();
					$(".downarrow").append('<a href="#down"><img src="images/sk/arrow_down.png" alt="down" title="'+(newsPage+1)+'/'+pages+'" /></a>');
					$(".uparrow").empty();
					$(".uparrow").append('<a href="#up"><img src="images/sk/arrow_up.png" alt="up" title="'+(newsPage-1)+'/'+pages+'" /></a>');
					initNewsNavi();
					//scroll = true;
				});
			}
	});
	
	$('a[href$="#down"]').click(function(){
		if(scroll){
			scroll = false;
			var len = $("#news ul:first-child").css("margin-top").length-2;
			var h = $("#news ul:first-child").css("margin-top").substr(0,len);
			//var hh = (parseInt(h)-710)+'px';
			//alert($("#news ul:nth-child("+newsPage+")").height());
			var hh = (parseInt(h)-($("#news ul:nth-child("+newsPage+")").height()+10))+'px';
			
			//alert((newsPage+1)+" - "+pagesLoaded[(newsPage+1)]);
			if(pagesLoaded[(newsPage+1)]){
				//var hh = '-=610px';
				$("#news ul:first-child").animate({
					'margin-top': hh
				}, 2000, function(){
					//newsPage=newsPage+2;
					newsPage++;
					//alert("a - "+newsPage);
					
					// loading
					if(newsPage==pages){
						$(".downarrow").fadeOut();
					} else {
						if(newsPage>1){
							$(".uparrow:parent").css("padding","0px");
							$(".uparrow").fadeIn();
						} else {
							$(".uparrow:parent").css("padding-bottom","10px");
							$(".uparrow").fadeOut();
						}
					}
					
					$(".downarrow").empty();
					$(".downarrow").append('<a href="#down"><img src="images/sk/arrow_down.png" alt="down" title="'+(newsPage+1)+'/'+pages+'" /></a>');
					$(".uparrow").empty();
					$(".uparrow").append('<a href="#up"><img src="images/sk/arrow_up.png" alt="up" title="'+(newsPage-1)+'/'+pages+'" /></a>');
					initNewsNavi();
				});
			} else {
				// loading
				$(".downarrow").empty();
				$(".downarrow").append('<img src="images/sk/loading.gif" alt="Loading..." title="Loading..." />');
				
				$.get("/article/news?page="+(newsPage+1),function(data){
					$("#news").append(data); //  > ul
					//var hh = '-=610px';
					$("#news ul:first-child").animate({
						'margin-top': hh
					}, 2000, function(){
						newsPage++;
						pagesLoaded[newsPage] = true;
						//alert("a - "+newsPage);
						if(newsPage==pages){
							$(".downarrow").fadeOut();
						} else {
							// loading
							if(newsPage>1){
								$(".uparrow:parent").css("padding","0px");
								$(".uparrow").fadeIn();
							} else {
								$(".uparrow:parent").css("padding-bottom","10px");
								$(".uparrow").fadeOut();
							}
						}
						//
						$(".downarrow").empty();
						$(".downarrow").append('<a href="#down"><img src="images/sk/arrow_down.png" alt="down" title="'+(newsPage+1)+'/'+pages+'" /></a>');
						$(".uparrow").empty();
						$(".uparrow").append('<a href="#up"><img src="images/sk/arrow_up.png" alt="up" title="'+(newsPage-1)+'/'+pages+'" /></a>');
						initNewsNavi();
					});
				});
			}
		}
	});
}

var teaser_i = -1;
function runTeaser() {
	if(jQuery.browser.msie) {
		jQuery("#teaser .teaser").remove();
		if(teasers != undefined) {
			teaser_i++;
			teaser_i = teaser_i % teasers.length;
			
			jQuery("#teaser").append('<div class="teaser" style="visibility: hidden;">'+teasers[teaser_i]+'</div>');
			jQuery("#teaser .teaser").removeAttr("style").fadeIn(900);
			
			
			
			setTimeout(function(){runTeaser();},8000);
		}
	} else {
		jQuery("#teaser .teaser").fadeOut(900,function(){jQuery(this).remove()});
		if(teasers != undefined) {
			teaser_i++;
			teaser_i = teaser_i % teasers.length;
			
			jQuery("#teaser").append('<div class="teaser" style="visibility: hidden;">'+teasers[teaser_i]+'</div>');
			setTimeout(function(){
				jQuery("#teaser .teaser").removeAttr("style").hide().fadeIn(900);
			},1000);
			
			
			setTimeout(function(){runTeaser();},8000);
		}
	}
}
function alignPicture(pic) {
	var t = jQuery(pic);
	var i = jQuery(pic).find("img");
	//kvuli zaokrouhlovani
	if(i.width() > t.width() - 2
	|| i.height() > t.height() -2) {
		i.css("margin-left",(Math.round(t.width()-i.width())/2)+"px");
		i.css("margin-top",(Math.round(t.height()-i.height())/2)+"px");
	} else {
		setTimeout(function(){alignPicture(pic);},300);
	}
}
function galleryToggle(cls) {
	var o = jQuery("#toggle_"+cls);
	if(o.hasClass("ajaxLoaded")) {
		//hide or show
		if(jQuery("#toggle_"+cls).hasClass("collapsed")) {
			jQuery("."+cls).slideUp();
			jQuery("#toggle_"+cls).removeClass("collapsed").html("&raquo; zobrazit další fotky");
		} else {
			jQuery("."+cls).slideDown();
			jQuery("#toggle_"+cls).addClass("collapsed").html("&raquo; skrýt");
		}
	} else {
		//ajax load images
		var id = cls.substr(9);
		var p = o.parent();
		o.addClass("ajaxLoaded").html("načítám ...");
		jQuery.getJSON(URL+"/gallery/ajaxLoad?id="+id,function(data){
			jQuery.each(data.images, function(i,item) {
				var truncatedDescription = item.description;
				if(truncatedDescription.length > 20) {
					truncatedDescription = truncatedDescription.substr(0,20)+'...';
				}
				var code = '';
				code += '<li class="'+cls;
				if(i%3 == 0) code += ' middle';
				code += '">';
				code += '<a href="'+item.path+'?width=800&height=600&noenlarge=true" class="lightbox" title="'+item.description+'">';
				code += '<img src="'+item.path+'?height=120&width=150" alt="foto'+i+'" />';
			    code += '</a>';
				code += '<span class="label" title="'+item.description+'">'+truncatedDescription+'</span>';
				code += '</li>';
				p.before(code);
			});
			p.parent().find(".lightbox").unbind().slimbox();
			o.addClass("collapsed").html("&raquo; skrýt");
		});
	}
}

function initProductNavi()
{
	if(jQuery("#allBrandsBox>div").length < 4) return;
	
	jQuery("#allBrandsBox")
	.mouseover(productNaviStop)
	.mouseout(productNaviResume);
	
	jQuery("#pravaSipka")
	.mouseover(function(){
		productNaviDirection = productNaviSpeed;
	}).mousedown(function(){
		productNaviDirection = productNaviClickSpeed;		
	}).mouseup(function(){
		productNaviDirection = productNaviSpeed;
	}).click(function(){
		return false;
	});
	
	jQuery("#levaSipka")
	.mouseover(function(){
		productNaviDirection = -productNaviSpeed;
	}).mousedown(function(){
		productNaviDirection = -productNaviClickSpeed;		
	}).mouseup(function(){
		productNaviDirection = -productNaviSpeed;
	}).click(function(){
		return false;
	});
	
	
	productNaviCount = jQuery("#allBrandsBox>div").length - 4;
	productNaviPosition = Math.round(Math.random() * productNaviCount * 220);
	setTimeout(productNaviRun,productNaviTimeout);
}

function productNaviRun()
{
	productNaviPosition += productNaviDirection;
	if(productNaviPosition < 0) productNaviPosition += (220 * productNaviCount);
	productNaviPosition = productNaviPosition % (220 * productNaviCount);  
	jQuery("#allBrandsBox").css("left", -productNaviPosition);
	setTimeout(productNaviRun,productNaviTimeout);
	/*
	jQuery("#allBrandsBox").animate({
		left: -productNaviPosition
	},productNaviTimeout,"linear",productNaviRun)
	*/
}
function productNaviStop()
{
	 productNaviDirectionResumeDirection = productNaviDirection;
	 productNaviDirection = 0;
}
function productNaviResume()
{
	productNaviDirection = productNaviDirectionResumeDirection;
}
