Array.prototype.random=function(){return this[Math.floor(this.length*Math.random())]};

function Set_Cookie( name, value, expires, path, domain, secure ){
	document.cookie=name+"="+escape(value)+((expires)?";expires="+new Date(new Date().valueOf()+expires*1000*60*60*24).toUTCString():"")+((path)?";path="+path:"")+((domain)?";domain="+domain:"")+((secure)?";secure":"");
}

function mysave(o){
	page=window.location.search.toQueryParams().page?window.location.search.toQueryParams().page:'home';
	new Ajax.Request('save.php5',{parameters:{file:page,content:o.getContent()},onSuccess:function(response){alert(response.responseText);}});
}

function show_large(event){
	var filename=event.element().src;
	if(event.type=='mouseover'){
		if($(filename))var img=$(filename);
		else $(window.document.body).insert(img=new Element('img',{id:filename,src:filename}).addClassName('product_image'));
		var resize=function(){
			if(!img.naturalWidth)img.naturalWidth=img.getWidth();
			if(!img.naturalHeight)img.naturalHeight=img.getHeight();
			var boxwidth=Math.max(1,event.element().viewportOffset().left);
			var boxheight=Math.max(1,event.element().viewportOffset().top);
			var widthfactor=boxwidth/img.naturalWidth;
			var heightfactor=boxheight/img.naturalHeight;
			var imagewidth=img.naturalWidth;
			var imageheight=img.naturalHeight;
			var maxwidth=$(window.document.viewport).getWidth();
			var maxheight=$(window.document.viewport).getHeight();
			if(widthfactor>heightfactor){
				if(img.naturalWidth>boxwidth)
				if(img.naturalHeight*widthfactor>maxheight){
					imagewidth=(maxheight/img.naturalHeight)*img.naturalWidth;
					imageheight=maxheight;
				}else{
					imagewidth=boxwidth;
					imageheight=img.naturalHeight*widthfactor;
				}
			}else{
				if(img.naturalHeight>boxheight)
				if(img.naturalWidth*heightfactor>maxwidth){
					imagewidth=maxwidth;
					imageheight=(maxwidth/img.naturalWidth)*img.naturalHeight;
				}else{
					imagewidth=img.naturalWidth*heightfactor;
					imageheight=boxheight;
				}
			}
			img.setStyle({width:imagewidth+'px',height:imageheight+'px'});
			if(img.effect)img.effect.cancel();
			img.effect=new Effect.Appear(img,{duration:0.5});
		}
		if(img.getWidth())resize();else img.observe('load',resize);
	}else{
		if($(filename).effect)$(filename).effect.cancel();
		$(filename).effect=new Effect.Fade($(filename),{duration:0.5});
	}
}

var translation={fr:{calendar:'calendrier',diary:'agenda'},nl:{calendar:'kalender',diary:'agenda'},de:{calendar:'kalender',diary:'kalender'}};
function choose_product(element){
		var tag=window.tag_list.get_objects('number',parseInt(element.id.substr(4))).first();
		var product=product_list.findAll(function(product){return product.type==tag.type&&product['tag list']['include'](tag)&&product['image list'].length&&((img=element.firstDescendant())?img.product!=product:true);}).random();
		if(product){
			function resize(event){
				var container=this.parentNode.getDimensions();
				$A(this.parentNode.getElementsByTagName('img')).each(function(img){if(img!=event.element())new Effect.Fade(img.absolutize(),{afterFinish:function(){img.remove();}});});
				new Effect.Appear(this.setStyle({width:(this.getWidth()>this.getHeight()?container.width:(container.height/this.getHeight())*this.getWidth())+'px',height:(this.getWidth()>this.getHeight()?(container.width/this.getWidth())*this.getHeight():container.height)+'px'}));
			};
			element.insert(i=new Element('img').hide().observe('load',resize));
			var language=window.location.pathname.substring(1,3);
			i.src=translation[language][tag.type].replace(/ /g,'-')+'/'+tag['name '+language].replace(/ /g,'-')+'/'+product['name '+language].replace(/ /g,'-')+'-'+product['image list'].random()+'.jpg?product='+product['number'];
			i.product=product;
		}
}

document.observe('dom:loaded',function(){
	$$('.product_thumb').invoke('observe','mouseover',show_large).invoke('observe','mouseout',show_large);
	$$('.popup').invoke('hide');
	$$('.background').invoke('setOpacity',0.9);
	$$('.page_gifts .tag').each(function(e){
		var popup=e.select('.popup').first();
		e.observe('mouseover',function(event){
			if(popup.fadeEffect){popup.fadeEffect.cancel();popup.fadeEffect=undefined;}
			if(!popup.appearEffect)popup.appearEffect=new Effect.Appear(popup,{duration:0.5});
		});
		e.observe('mousemove',function(event){
			popup.setStyle({left:event.pointerX()-popup.getOffsetParent().offsetLeft+(event.pointerX()<document.viewport.getWidth()/2?8:-8-popup.getWidth())+'px'});
			popup.setStyle({top:event.pointerY()-popup.getOffsetParent().offsetTop-popup.getHeight()+'px'});
		});
		e.observe('mouseout',function(){
			if(popup.appearEffect){popup.appearEffect.cancel();popup.appearEffect=undefined;}
			if(!popup.fadeEffect)popup.fadeEffect=new Effect.Fade(popup,{duration:0.5});
		});
	});
	$$('li.product').invoke('observe','click',function(event){window.location.href=($A($A(event.findElement('li').getElementsByTagName('h2')).first().getElementsByTagName('a')).first().getAttribute('href'));});
});			

Event.observe(window,'load',function(){
	$$('.tag .tag_link').collect(choose_product);
	if($$('.tag .tag_link').length)new PeriodicalExecuter(function(){choose_product($$('.tag .tag_link').random());},3);	
	if($('bar'))$('bar').setStyle({height:Math.max($(window.document.body).getHeight(),$(window.document.viewport).getHeight())+'px'});
	window.focus();
	$('query').focus();
});