window.addEvent('domready', function() {
	makeMenus();
	topMenus = new horizontalMenus();
	if($('therefersite')) $('therefersite').value = Cookie.read('saveReferrer');
	if($('s')){$('s').addEvents({
		'focus':function(){
			this.store('ovalue',this.value);
			this.value="";
		},
		'blur':function(){
			if(this.value=="") this.value=this.retrieve('ovalue','Search');
		}
	});}
	if($('control_EMAIL')){$('control_EMAIL').addEvents({
		'focus':function(){
			this.store('ovalue',this.value);
			this.value="";
		},
		'blur':function(){
			if(this.value=="") this.value=this.retrieve('ovalue','Enter your e-mail address');
		}
	});}
	$$('a').each(function(a){
		if(a.href){
			/*
			var href=new URI(a.href),l=new URI(document.location).getData(),d=Object.merge(l,href.getData());
			if(!href.get('host').contains('flavorseal')){
				a.target="_blank";
			}else{
				a.href=href.setData(d).toString();
			}
			*/
			if(checkHost(a.href,'flavorseal')){
				a.target=="_blank";
			}else if(!/^(mailto)/.test(a.href)){
				a.href=mergeQS(a.href);
			}
		}
	});
	$$('form').each(function(f){
		if(f.get('action')){
			f.set('action',mergeQS(f.get('action')));
		}
	});
	pageGal = new shadowbox();	
});

function checkHost(a,b){
	var c=a.slice(0,a.indexOf("?")),d=c.indexOf(b);
	return d==-1;
}

function mergeQS(a){
	var b=a.indexOf("?"),c=(b==-1)?a.length:b,d=a.slice(0,c),e=a.slice(c),f={},g={},h=[];
	e.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"),function($0,$1,$2,$3){f[$1]=$3;});
	location.search.replace(new RegExp("([^?=&]+)(=([^&]*))?","g"),function($0,$1,$2,$3){g[$1]=$3;});
	for(var i in g){if(f[i]===undefined)f[i]=g[i];}
	for(var j in f){h.push(j+"="+f[j]);}
	if(h.length) return d+"?"+h.join("&");
	else return d;
}

window.addEvent('load',function(){
	sameHeightby2('div.searchresult');
	sameHeightby2('div.relatedProducts');
	sameHeight('div.recommendedproducts');
});

function sameHeightby2(selector){
	for(var i=0,d=$$(selector),l=d.length,p=d.getStyle('padding');i<l;i=i+2){
		if(d[i+1]){
			var pa=p[i].split(' '),pat=pa[0].toInt(),pab=pa[2].toInt(),pb=p[i+1].split(' '),pbt=pb[0].toInt(),pbb=pb[2].toInt(),a=d[i].getSize().y-pat-pab,b=d[i+1].getSize().y-pbt-pbb,h=(a>b)?a:b;
			d[i].setStyles({height:h});
			d[i+1].setStyles({height:h});
		}
	}
}

function sameHeight(selector){
	var h=0;
	$$(selector).each(function(s){
		var y=s.getSize().y,pt=s.getStyle('paddingTop').toInt(),pb=s.getStyle('paddingTop').toInt(),p=pt+pb,yt=y-p;
		h=(h>yt)?h:yt;
	});
	$$(selector).setStyles({height:h});
}

if(!Cookie.read('saveReferrer')){
	referrer = (document.referrer != "") ? document.referrer : "Unknown or blocked";
	referrer = unescape(referrer);
	referrer = referrer.replace("&"," - ");
	referrer = referrer.replace("q=","keywords=");
	Cookie.write('saveReferrer',referrer,{path:'/'});
}

function makeMenus(){
	if(document.URL.indexOf('cgi-bin')>-1||document.URL.indexOf('shop.flavorseal.com')>-1){
		sections = {
			"General":"040092cc755caefe",
			"Foodservice":"c06478da80146203",
			"Food_Processing":"2eb1fb96d522ea1d",
			"Consumer":"fb22c56625df356e"
		};
		var URL = new URI();		
		var nav = URL.getData('nav');
		if($type(nav)!="string" || nav==""){
			$$('ul.navsystem').each(function(ul){
				if(ul.getStyle('display')=='block'){
					nav=ul.get('class').split(" ")[1];
				}
			});
			nav=(nav=="")?"General":nav;
		}
		//new Element('span',{'html':'http://www.flavorseal.com/cgi-bin/nav_json.pl?max_parent_level=3&max_child_level=4&content_id'+sections[nav],'styles':{'display':'none'}}).inject('header');
		var j = new Request.JSON({
			url: 'http://www.flavorseal.com/cgi-bin/nav_json.pl',
			onSuccess: function(json,text){
				var base = $$('ul.navsystem.'+nav)[0];
				base.empty();
				json.nav.each(function(j,i){
					la = new Element('li').inject(base);
					if(j.highlight) la.addClass('thisnav');
					aa = new Element('a',{'href':mergeQS(j.href),'target':'_parent','html':j.html}).inject(la);
					if(j.children){
						ub = new Element('ul').inject(la);
						j.children.each(function(cone,i){
							lb = new Element('li').inject(ub);
							if(cone.highlight) lb.addClass('thisnav');
							ab = new Element('a',{'href':mergeQS(cone.href),'target':'_parent','html':cone.html}).inject(lb);
						});
					}
				});
			}
		}).get({'content_id':sections[nav],'max_parent_level':'3','max_child_level':'4'});
	}
}


var horizontalMenus = new Class({
	initialize: function(options){
		this.options= $extend(
			{
				topID: 'wrapper',
				ulClass: "navsystem",
				highlight: "viewnav",
				fxduration:	0
			}, options || {}
		);
		
		this.pageID = -1;
		/*
		$$('div#'+this.options.topID+' ul.'+this.options.ulClass+' li a').each(function(a,i){
			if(location.pathname.test(a.get('href'))){
				//a.getParent('li').addClass(this.options.highlight);
				this.pageID=i;
			}
		},this);
		*/
		
		
		this.lis = $$('div#'+this.options.topID+' ul.'+this.options.ulClass+' > li');
		this.lis.each(function(li,i){
			li.fx = false;
			if(li.getFirst('ul')){
				li.fx = new Fx.Morph(li.getFirst('ul'), {
					duration: this.options.fxduration,
					transition: Fx.Transitions.Sine.easeOut,
					link: 'cancel'
				});
			}
			if(!$(this.options.topID).hasClass('hidemenus')){
				li.addEvents({
					'mouseenter':function(){
						this.showMenu(i);
					}.bind(this)
				});
			}
			li.getElements('a').each(function(a){
				if(location.pathname.test(a.get('href'))){
					this.pageID=i;
					this.showMenu(this.pageID);
				}
			},this);
			if(li.hasClass(this.options.highlight)) this.showMenu(i);
		},this);
		
		
		$(this.options.topID).addEvents({
			'mouseleave':function(){
				this.showMenu(this.pageID);
			}.bind(this)
		});
	},
	
	showMenu: function(m){
		this.hideMenus(m);
		if(m > -1){
			li = this.lis[m];
			li.addClass(this.options.highlight);
			if(li.fx)li.fx.start({'opacity': 1});
		}
	},
	
	hideMenus: function(m){
		this.lis.each(function(li,i){
			if(li.fx){
				li.fx.cancel();
				if(i!=m) li.fx.set({visibility:'hidden','opacity': 0});
			}
			li.removeClass(this.options.highlight);
		},this);
	}
});



/*


fHover = function() {
	var sfEls = document.getElementById("navsystem").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

*/

