var initialized=false;

function getTextImageFileName(data)
{
	var ret=[];
	ret.push(data["text"]);
	ret.push(data["font"]);
	ret.push(data["color"]||'fff');
	ret.push(data["bgcolor"]||'none');
	ret.push(data["align"]||'left');
	ret.push(data["size"]||16);
	return encodeURI(ret.join("@")+".tdr.png");
}

function initSuzukiLayout()
{
	$("#layout.suzuki").each(function(){
		
		var obj=$(this);
		
		return;
		
		var setImage=function(obj,tim)
		{
			/*
			var span=obj.find('span.iblock');
			span.attr("style","background: url("+tim+")");
			*/
			var img=obj.find('img[src$='+tim.split('/').pop()+']');
			img.show().siblings().hide();
			/*
			span.width(img.width()).height(img.height());
			*/
			
		};
		
		var preload=function(obj,tim,text,set)
		{
			var img=$("<img/>").attr("src",tim);
			img.attr("alt",text);
			if (set)
			{
				obj.html('');
				img.load(function(){
					// var span=$("<span/>").addClass("iblock");
					// obj.append(span);
					setImage(obj,tim);
				});
			}
			$(obj).append(img.hide());
		};
		
		$("#right h1, #right h2")
		.each(function(){
			var obj=$(this);
			var a=obj.find('a');
			if (a.length) { obj=a; }
			var tim="/image/"+getTextImageFileName({
				text:obj.text(),
				font:"SuzukiPROHeadline.otf",
				size:24,
				color:"A59C87"
			});
			preload(obj,tim,obj.text(),true);
		});
		$("#main .bannery h1,#main .bannery h2")
		.each(function(){
			var obj=$(this);
			var a=obj.find('a');
			if (a.length) { obj=a; }
			var tim="/image/"+getTextImageFileName({
				text:obj.text(),
				font:"SuzukiPROHeadline.otf",
				size:24,
				color:"fff"
			});
			preload(obj,tim,obj.text(),true);
		});
		
		$("#main .panely h1,#main .panely h2")
		.each(function(){
			var obj=$(this);
			var a=obj.find('a');
			if (a.length) { obj=a; }
			var tim="/image/"+getTextImageFileName({
				text:obj.text(),
				font:"SuzukiPROHeadline.otf",
				size:24,
				color:"fff"
			});
			preload(obj,tim,obj.text(),true);
		});			
		
		$("#left a,#footer a")
		.each(function(){
			var obj=$(this).addClass("iblock");
			var current=obj.hasClass("current");
			var text=obj.text();
			if (text!="")
			{
				var tim="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:14,
					color:current?"ff6600":"A59C87"
				});
				var timh="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:14,
					color:'ccc'
				});
				preload(obj,tim,text,true);
				preload(obj,timh,text);
				obj
					.mouseover(function(){setImage(obj,timh)})
					.mouseout(function(){setImage(obj,tim)});
			}
		});
		
		$("#main .panely a")
		.each(function(){
			var obj=$(this).addClass("iblock");
			var current=obj.hasClass("current");
			var text=obj.text();
			if (text!="")
			{
				var tim="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:14,
					color:current?"ff6600":"fff"
				});
				var timh="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:14,
					color:'ccc'
				});
				preload(obj,tim,text,true);
				preload(obj,timh,text);
				obj
					.mouseover(function(){setImage(obj,timh)})
					.mouseout(function(){setImage(obj,tim)});
			}
		});
		
		$("#header .menu a").each(function(){
			var obj=$(this).addClass("iblock");
			var current=obj.hasClass("current");
			var text=obj.text();
			if (text!="")
			{
				var tim="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:16,
					color:current?"ff6600":"A59C87"
				});
				var timh="/image/"+getTextImageFileName({
					text:text,
					font:"SuzukiPROHeadline.otf",
					size:16,
					color:'fff'
				});
				preload(obj,tim,text,true);
				preload(obj,timh,text);
				obj
					.mouseover(function(){setImage(obj,timh)})
					.mouseout(function(){setImage(obj,tim)});
			}
		});
	});
}
initSuzukiLayout();

function init()
{
	$(".bannery").each(function(){
		var obj=$(this);
		var list=obj.find('.banner');
		var i=0;
		var pid;
		var current;
		var nlist;
		
		var init=function()
		{
			current=$(list[i]);
			current.show();
			list.not(current).hide();
			play();
			if (list.length>1)
			{
				var nums=$('<div/>').addClass('nums');
				list.each(function(j){
					var num=$('<span/>')
						.addClass('num')
						.append(j+1)
						.click(function(){
							i=(j);
							animate();
						});
					nums.append(num);
				});
				obj.append(nums).mouseenter(stop).mouseleave(play);
				nlist=nums.find('.num');
				$(nlist[i]).addClass('num-current');
			}
		};
		
		var animate=function()
		{
			current=$(list[i]);
			list.not(current).fadeOut();
			if (nlist)
			{
				$(nlist[i]).addClass('num-current').siblings().removeClass('num-current');
			}
			current.fadeIn();
		};
		
		var next=function(){
			i++;
			if (i>=list.length)
			{
				i=0;
			}
			animate();
		};
		
		var prev=function()
		{
			i--;
			if (i==0)
			{
				i=list.length;
			}
			animate();
		};

		var play=function()
		{
			if (!pid)
			{
				pid=setInterval(next,5000);
			}
		};
		
		var stop=function()
		{
			clearInterval(pid);
			pid=false;
		}
		
		init();
	});

	if (initialized) return;
	
	$("#uvod").each(function(){
		var opened=!$('#uvod').is(":visible");
		
		var easing="easeInOutBack";
		var duration=1000;
		var body=$('body').css("overflow",opened?"auto":"hidden");
		var obj=$(this);

		var left=obj.find(".left");
		var right=obj.find(".right");
		var bottom=obj.find(".bottom");
		
		var open=function(){
			left.css({left:0});
			right.css({right:0});
			bottom.css({bottom:0});
			
			left.animate({left:-left.width()},{easing:easing,duration:duration,complete:function(){}});
			right.animate({right:-right.width()},{easing:easing,duration:duration,complete:function(){}});
			bottom.animate({bottom:-bottom.height()},{easing:easing,duration:duration,complete:function(){
					obj.hide();
					body.css("overflow","auto")}
			});
			opened=true;
			initLogo();
		};
		var close=function()
		{
			obj.show();
			body.css("overflow","hidden");	
			left.css({left:-left.width()});
			right.css({right:-right.width()});
			bottom.css({bottom:-bottom.height()});
			
			left.animate({left:0},{easing:easing,duration:duration,complete:function(){}});
			right.animate({right:0},{easing:easing,duration:duration,complete:function(){}});
			bottom.animate({bottom:0},{easing:easing,duration:duration,complete:function(){}});
			opened=false;
			initLogo();
		}
		
		var hash=""; //location.hash.toString();
		var hashTable={};
		var HashWatch=function()
		{
			if (hash!=location.hash)
			{
				hash=location.hash.toString();
				if (hashTable[hash])
				{
					hashTable[hash]();
				}
			}
		};
		setInterval(HashWatch,200);
		
		var initLogo=function()
		{
			var logo=$('body > a.logo').get(0);
			logo.href=self.location.href.toString();
			if(!opened)
			{
				logo.hash="home";
			}
			else
			{
				logo.hash="uvod";
			}
		};
		
		var ajax=obj.find("a.ajax").each(function(){
			var obj=$(this);
			obj.data('href',this.href);
			var hash=this.href.toString().replace(self.location.origin,'').replace(/[^a-z0-9]*/i,'-').replace('/','');
			hashTable['#'+hash]=function(){
				var href=obj.data('href');
				$.ajax({
					url:href,
					data:{ajax:true},
					success:function(html){
						$('#layout').replaceWith(html);
						open();
					},
					error:function(){
						// self.location=href;
					}
				});
			};				
			this.href=self.location.href.toString();
			this.hash=hash;
		});

		hashTable[""]=hashTable["#"]=hashTable["#uvod"]=function(){
			close();
		};
		hashTable["#home"]=function()
		{
			$.ajax({
				url:'/',
				data:{ajax:true},
				success:function(html){
					$('#layout').replaceWith(html);
					open();
				},
				error:function(){
					// self.location=href;
				}
			});
		};

		initLogo();
	});
	
	var fpList=$('a.flowplayer');
	if (fpList.length>0)
	{
		$.getScript('/flowplayer.min.js',function(){
			fpList.each(function(j){
				var obj=$(this);
				var id='fp'+j;
				var div=$('<div/>').attr('id',id);
				obj.wrap(div);
				flowplayer(id,{src:'/flowplayer.swf',width:496,height:310},obj.attr('href'));
			});
		});
	}
	
	$('.products').each(function(){
		var obj=$(this);
		var hslider=obj.find('.hslider').parent();
		var back=obj.find('.back');
		var next=obj.find('.next');
		var prods=obj.find('.product');
		var length=prods.length;
		var p=1;
		var swidth=900;
		
		prods.each(function(){
			var self=$(this);
			var id=self.find('input[name=id]').val();
			var thumbs=self.find('.thumbs');
			var tslider=thumbs.parent();
			var vslider=self.find('.vslider');
			var mainImg=self.find('img');
			var p=1;
			var height=506;
			var tList;
			var mList;
			
			var animate=function()
			{
				var i=p-1;
				vslider.parent().stop(true,false).animate({scrollTop:i*height},{easing:"easeInOutBack",duration:"slow"})
				var ct=$(tList[i]);
				ct.addClass('current').siblings('.current').removeClass('current');
				tslider.stop(true,false).animate({scrollLeft:i*102-450});
			}
			
			var moveUp=function()
			{
				p--;
				if(p<=0)
				{
					p=length;
				}
				animate();				
			}
			
			var moveDown=function()
			{
				p++;
				if (p>length)
				{
					p=1;
				}
				animate();				
			}
			
			if (product[id])
			{
				var k;
				var img;
				var src;
				for(k in product[id].image.thumb)
				{
					src=product[id].image.thumb[k];
					img=$('<img/>').attr("src",src);
					// img=$('<div/>').attr("style","width: 90px; float: left; height: 51px; background: url("+src+")");
					thumbs.append(img);
					src=product[id].image.detail[k];
					if (src!=mainImg.attr("src"))
					{
						img=$('<img>/').attr("src",src);
						// img=$('<div/>').attr("style","width: 900px; height: 506px; background: url("+src+")");
						mainImg.parent().append(img);
					}
					/*
					else
					{
						img=$('<div/>').attr("style","width: 900px; height: 506px; background: url("+src+")");
						mainImg.parent().append(img);
						mainImg.remove();
						mainImg=img;
					};
					*/
				}
			}
			
			tList=thumbs.find('img');
			mList=mainImg.siblings('img');
			
			tList.each(function(i){
				var self=$(this);
				self.click(function(){
					p=i+1;
					animate();
				});
			})
			
			animate();
			
		});

		var animate=function(){
			hslider.stop(true,false).animate({scrollLeft:(p-1)*swidth},{easing:"easeInOutBack",duration:"slow"});
		}
		
		var moveNext=function()
		{
			p++;
			if (p>length)
			{
				p=1;
			}
			animate();
		}
		
		var moveBack=function()
		{
			p--;
			if(p<=0)
			{
				p=length;
			}
			animate();
		}
		
		next.click(moveNext);
		back.click(moveBack);
		
		$(next).add(back).mouseenter(function(){
			var self=$(this);
			self.find('.ico').stop(true,true).fadeIn();
		}).mouseleave(function(){
			var self=$(this);
			self.find('.ico').stop(true,true).fadeOut();
		});
	});

	initialized=true;
}

$(function(){
	init();
	
	if (typeof messages!="undefined")
	{
		var window=$('<div id="message"/>');
		var close=$('<a/>').html("Zavřít");
		var href="?";
		var i=0;
		for(var key in messages)
		{
			href+="close["+key+"]&";
			window.append("<span>"+messages[key]+"</span>");
			i++;
		}
		close.attr("href",href);
		window.append(close);
		if (i>0)
		{
			$('body').append(window);
			var blink=function(o){
				var obj=o || $(this);
				obj.animate({color:"rgb(256,0,0)"},{complete:function(){
					obj.animate({color:"rgb(256,128,0)"},{complete:function(){
						blink(obj);
					}});
				}})
			};
			window.find('.blink').each(blink);
		}
	}
});

