//codemoutain.ca  copyrights 2008
// version 2.0 - pimiento custom functions for Pimiento.ca website
var formit = {
	
	sendcard1:function(){
		$$('.form_content').invoke('hide');
		Effect.Appear($$('.form_result').first());
		
	},
	sendcard2:function(){
		$$('.form_content').invoke('hide');
		$$('.form_result').invoke('show');
		
	},
	calcvip:function(nocards,cardvalue){
		$('card_total').value = nocards*cardvalue;	
	}
}

var cartemenu = {
	
	init:function(){
		allItems = $$('.vertimenu1 li','.vertimenu2 li');
		allPages = $$('.menupages', '.menu_info');
		allWines = $$('.menuwine');
		allWineImg = $$('.wines');
		this.itemLinks();
		//console.log("running news init on : " + allItems);
		this.showItem('page-1');
		
	},
	
	itemLinks:function(){
		allItems.each(function(el){
			el.observe('click',function(evt) {cartemenu.showItem(el.id) });	
		});

		allWines.each(function(el){
			el.observe('click',function(evt) {cartemenu.showWine(el.id) });	
		});


	},
	showItem:function(id){
		console.log("show item : "+id);
		this.activeMenu(id);
		var pageid = this.getId(id);
		allPages.invoke('hide');
		if(pageid[1]!='v1'){
			//not wine list
			Effect.Appear('img-'+pageid[1]);
			Effect.Appear('menutext-'+pageid[1]);
		} else {
			//wine list div
			Effect.Appear('winelist');
		}
	},
	
	showWine:function(id){
		console.log("show item : "+id);
		allWines.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
		
		allWineImg.invoke('hide');
		Effect.Appear('wine-'+id);
	},
	getId:function(id){
		var idReturn = id.split("-");
		//console.log(idReturn);
		return idReturn;
	},
	activeMenu:function(id){
		allItems.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	}
	
}

var promos = {
	
	init:function(){
//		alert('running init js');
		
		allItems = $$('.vertimenu3 li');
		allInfos= $$('.promo_details');
		
		this.itemLinks();
		//console.log("running news init on : " + allItems);
		this.showItem('promo-2');
		
	},
	
	itemLinks:function(){
		allItems.each(function(el){
			//el.observe('click',function(evt) {promos.showItem(el.id) });	
			el.observe('click',function(evt) {promos.showItem(el.id) });	

});
		
	},
	showItem:function(id){
	//	alert("show item : "+id);
		//console.log("show item : "+id);
		var restoid = this.getId(id)[1];
		$('restoname').src = "../img/"+nom_promos[restoid];
		allInfos.invoke("hide");
		$('info-'+restoid).show();
		
	},
	getId:function(id){
		var idReturn = id.split("-");
		//console.log(idReturn);
		return idReturn;
	},
	activeMenu:function(id){
		allItems.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	}
	
}

var reserve = {
	
		init:function(){
		allRestos = $$('.restonav-reserv');
		imgGallery_laval = new Array();
		imgGallery_rosemere = new Array();
		imgGallery_boucherville = new Array();
		imgGallery_toronto = new Array();
		imgGallery_aeroport = new Array();
		
		navPrev = "";
		navNext = "";
		
		//console.log("Categories : "+allCategories)	
		allRestos.each(function(el){
			//el.hide();
			el.observe('click',function(evt) {reserve.showForm(el.id) });	
		});
		
		allThumbs = $$('.imgrestos');
		allThumbs.each(function(el){
			//el.hide();
			el.observe('click',function(evt) {reserve.showLarge(el.id) });
			//build array for image names for each resto;
			var resto = el.id.split("-")[0];
			var imgArray = eval("imgGallery_"+resto);
			imgArray.push(el.id) ;
		});		
	},
	
	showLarge:function(id){
		
		$('large_img').src = "../img/clear.gif";
		$('large_img').src = "../img/restos/"+id+".jpg";
		Effect.Appear("large_container");
		this.imgNav(id);
	},
	
	imgNav:function(id){
		
		var imgArray = eval("imgGallery_"+id.split('-')[0]);
		imgPos = imgArray.indexOf(id);
		
		var imgNext = imgPos + 1;
		var imgPrev = imgPos-1;
		(imgNext >= imgArray.length) ?  imgNext = 0 : imgNext = imgNext ;
		(imgPrev <= -1) ?  imgPrev = (imgArray.length-1) : imgPrev = imgPrev ;
		
		
		navPrev =imgArray[imgPrev];
		navNext = imgArray[imgNext];;	
		
		
	},
	
	showForm: function(id){
		$('adresses').hide();
		var resto = "";
		var emailresto = "";
		var emailend = "@houstonresto.com";
		if(id=="resto-laval"){
			resto = "Laval";
			emailresto="laval"+emailend;
		}
		if(id=="resto-rosemere"){
			resto = "Rosem&Egrave;re";
			emailresto="rosemere"+emailend;
		}
		if(id=="resto-rosemere"){
			resto = "Rosem&Egrave;re";
			emailresto="rosemere"+emailend;
		}
		if(id=="resto-boucherville"){
			resto = "Boucherville";
			emailresto="boucherville"+emailend;
		}
		if(id=="resto-toronto"){
			resto = "Toronto";
			emailresto="yonge"+emailend;
		}
		if(id=="resto-aeroport"){
			resto = "Aeroport P.-E.-Trudeau";
			emailresto="aeroport"+emailend;
		}

		$('nomresto').innerHTML = resto;
		$('resto_reserve').value = resto;
		$('to').value = emailresto;
		Effect.Appear('reserveForm');
		console.log("reserve for : "+id);	
	}
}

var sections = {
	
	init:function(){
		allLinks = $$('.menulist li a');
		//activate js evetns for li links
		allLinks.each(function(e){
			e.observe('click',function(evt) {sections.show(this.id) });					   
							   
		});
	
		
	},
	
	show:function(id){
		var pageId = sections.getId(id)[0] ;
		var sectionId = sections.getId(id)[1] ;
		//hide sections by class = pageId_sections
		this.hideall(pageId);
		//show the clicked on section id = sectionId_content
		Effect.Appear(sectionId+"_content");
		this.activeMenu(sectionId);
	},
	
	hideall:function(pageId){
		$$('.'+pageId+'_sections').invoke('hide');
	},
	
	getId:function(id){
		var idReturn = id.split("_");
		//console.log(idReturn);
		return idReturn;
	},
	
	activeMenu:function(id){
		var links = $$('.menulist li');
		links.each(function(e){
			e.removeClassName('active');
		});
		$(id+'_link').addClassName('active');
	},
	showFirst:function(){
		//find first menu a href id to run sections.show on first link
		var menuId = $$('.menulist li a').first();
		this.show(menuId.id);
	},
	
	initMedia:function(){
	
	//init galery
	allGalery = $$('.gal1');
	//console.log("Categories : "+allCategories)	
	allGalery.each(function(el){
		el.observe('click',function(evt) {sections.chgGaleryImg(el.id) });	
	});
	
	},
	
	chgGaleryImg:function(fileId){
		$('gal-large').src = "admin/vault/galery/gal-1.jpg";
		//$('gal-large').src = "admin/vault/galery/"+fileId+".jpg";
		Effect.Appear('gal-large_container',{duration:0.5});
		
	}
	
	
}

var locator = {
	
	init:function(){
		
		allRegions = new Array();
		allStores= $$('.leftmenu2 li');
		this.locatorLinks();
		showAddress('45.898340,-74.157695','St-Sauveur - Parc JH Molson','yoga023_link');
		this.showRegion('crew_menu');
	
		
	},
	
	locatorLinks:function(){
		allRegions = $$('.leftmenu2');
		//console.log("Categories : "+allCategories)	
		allRegions.each(function(el){
			el.hide();
			el.parentNode.observe('click',function(evt) {locator.showRegion(el.id) });	
		});
		
		//this.showCategory('crew_menu');
		//this.showProduct('yoga023_link');
		
	},
	showRegion:function(id){
		//console.log(id);
		allRegions.invoke('hide');
		$(id).show();
		
		$$('p.list_active').each(function(el){
			el.removeClassName('list_active');
		
		});
		$$('#'+$(id).parentNode.id+" p").first().addClassName('list_active');
	
	},
	activeMenu:function(id){
		allStores.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	}
	
}

var catalog = {
	
	init:function(){
		allProducts = new Array();
		allCategories = new Array();
		allThumbs = new Array();
		overlaySection = new Array();
		this.categories();
		this.previews();
		this.checkEvents();
		
	},
	
	categories:function(){
		allCategories = $$('.leftmenu2');
		//console.log("Categories : "+allCategories)	
		allCategories.each(function(el){
			el.hide();
			el.parentNode.observe('click',function(evt) {catalog.showCategory(el.id) });	
		});
		
		this.showCategory('crew_menu');
		this.showProduct('yoga023_link');
		
	},
	
	showCategory:function(id){
		allCategories.invoke('hide');
		$(id).show();
		
		$$('p.list_active').each(function(el){
			el.removeClassName('list_active');
		
		});
		$$('#'+$(id).parentNode.id+" p").first().addClassName('list_active');
	
	},
	
	stopEvents:function(){
		allThumbs.each(function(e){
			var srcName= $A(e.src.split("/")).last();
			//remove observe incase
			e.stopObserving('click');			
		});	
		
	},
	
	checkEvents:function(){
		//various javascript observe setups
		//$('btnBuy').observe('click',function(evt) { alert('go to store for item : '+$('productId').value) });
		
		//enlarge div click behavior
		/*overlaySection = $$('.overimg');
		$('product_main_enlarge').observe('click',function(evt) {catalog.showLargeimg()});
		overlaySection.each(function(e){
		//remove observe incase
			e.stopObserving('click');			
			//activate new on click
			e.observe('click',function(evt) { overlaySection.invoke('hide') });								 
		});
		*/
		//thumbs onlcik beahviors
		allThumbs = $$('#product_thumbs img');
		allThumbs.each(function(e){
			var srcName= $A(e.src.split("/")).last();
			//remove observe incase
			e.stopObserving('click');			
			//activate new on click
			e.observe('click',function(evt) {catalog.showMainimg(srcName)});			
		});
		
	},

	showMainimg:function(fileId){
		$('product_main_img').src='admin/vault/catalog/regular/'+fileId;
		
	},

	showLargeimg:function(){
		$('img_large_src').src='admin/vault/catalog/large/'+$('product_main_img').src.split("/").last();
		overlaySection.invoke('show') 
		
	},
	previews:function(){
		allProducts = $$('.leftmenu2 li');
		//console.log(allProducts);
		allProducts.each(function(el){
			el.observe('mouseover',function(evt) { catalog.showPreview(this.id) });	
								  
			el.observe('mouseout',function(evt) { catalog.hidePreview(this.id) });	
								 
			el.observe('click',function(evt) { catalog.showProduct(this.id) });	
		});					  
	},
	
	showPreview:function(id){
		var preview = new Element('div',{ 'id':'preview'+id, 'class':'cat_preview'});
		preview.update('<img src="img/product_temp-preview-1.jpg" width="90" height="115"/>');
		$(id).appendChild(preview);
	},
	
	hidePreview:function(id){
		$('preview'+id).remove();	
	},
	
	showProduct:function(id){
		//console.log("Show product info for : "+id);	
		//this.hidePreview(id);
		this.activeMenu(id);
		
	},
	
	activeMenu:function(id){
		allProducts.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	},
	
	showFirst:function(){
		//find first menu a href id to run sections.show on first link
		var menuId = $$('.menulist li a').first();
		this.show(menuId.id);
	}	
	
}


function register(){
	//ajax call to plug in doc link if valid email;
		
		email2send = $("email").value;
		
	
	
	new Ajax.Updater('container_register', 'library/ajax/register.php',{
			parameters:{
				email:$('email').value
			},
			evalScripts: true 
			
			}); // end Ajax.Updater	
	
	
}


//email click disappear text functions
function clickclear(thisfield, defaulttext) {
	if (thisfield.value == defaulttext) {
	thisfield.value = "";
	}
}

function clickrecall(thisfield, defaulttext) {
	if (thisfield.value == "") {
	thisfield.value = defaulttext;
	}
}
