//codemoutain.ca  copyrights 2008
// version 2.0 - pimiento custom functions for Pimiento.ca website


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 = "cms/vault/galery/gal-1.jpg";
		//$('gal-large').src = "cms/vault/galery/"+fileId+".jpg";
		Effect.Appear('gal-large_container',{duration:0.5});
		
	}
	
	
}

var locator = {
	
	init:function(countryID){
			allRegions = $$('.results_title');
			allVilles = $$('.villes');
			allPlaces = $$('.menulist');
			allStores = $$('.places');
			this.links();
			allStores.first().onclick;
			
			$("country_"+countryID).addClassName('active');
			
			this.showVilles(allVilles.first().id);
	},

	links:function(){
		
		////console.log("Categories : "+allCategories)	
		allRegions.each(function(el){
			el.observe('click',function(evt) {locator.showVilles(el.id) });	
		});
	
		allVilles.each(function(el){
			el.hide();
			el.observe('click',function(evt) {locator.showPlaces(el.id) });	
		});

	
	},
	
	showVilles:function(ville_id){
		var idville = ville_id.split("_")[1];
		//console.log('showVilles: .results_'+idville);
		allPlaces.invoke('hide');
		allVilles.invoke('hide');
		allRegions.each(function(el){
			el.removeClassName('list_active');
		
		});
		
		resultsVilles = $$('.results_'+idville);
		resultsVilles.invoke('show');
		$(ville_id).addClassName('list_active');
	},
	
	showPlaces:function(place_id){
		allPlaces.invoke('hide');
		$("liste_"+place_id).show();
		
	}

	
}


var catalog = {
	
	init:function(cataMainId, categoryId){
		allProducts = new Array();
		allCategories = new Array();
		allCategoryMenus = $$('.leftmenu2');
		allThumbs = new Array();
		overlaySection = new Array();
		this.categories(categoryId);
		this.previews();
		this.checkEvents();
		
	},
	
	categories:function(categoryId){
		allCategories = $$('.list_cat');
		//console.log("Categories : "+allCategories)	
		allCategories.each(function(el){
			//el.hide();
			el.observe('click',function(evt) {catalog.showCategory(el.id) });	
		});
		
		if(categoryId=="none"){
			this.showCategory(allCategories.first().id);
			this.showProduct($$('.leftmenu2 li').first().id);
		} else {
			this.showCategory(categoryId);
			this.showProduct($$('#'+categoryId+' .leftmenu2 li').first().id);
		}
		
		
	},
	
	showCategory:function(id){
		allCategoryMenus.invoke('hide');
		//console.log("id to showcategory function: "+id);
		$$("#"+id+" .leftmenu2").first().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='cms/vault/catalog/regular/'+fileId;
		
	},

	showLargeimg:function(){
		$('img_large_src').src='cms/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){
		prod_id = id.split("_")[0];
		$('preview_'+prod_id).show();
		//console.log("trace preview:"+ $('preview_'+prod_id).id)
	},
	
	hidePreview:function(id){
		prod_id = id.split("_")[0];
		$('preview_'+prod_id).hide();
	},
	
	showProduct2:function(id){
		//console.log("Show product info for : "+id);	
		//this.hidePreview(id);
		this.activeMenu(id);
		
	},
	showProduct:function(id){
			prod_id = id.split("_")[0];
		//console.log("Product id details call : "+prod_id);
		
		new Ajax.Updater('product_container', 'library/ajax/product_details.php',{
			parameters:{
				product_id:prod_id
			},
			evalScripts: true 
			//onComplete : catalog.checkEvents()
			}); 	
		
		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);
	}	
	
}
var news = {
	
	init:function(){
		allItems = $$('.leftmenu2 li');
		this.itemLinks();
		this.showItem(allItems.first().id);
		$('news').show();
		this.initMedia();
		//console.log("running news init on : " + allItems);
		$('news_header').observe('click',function(evt){
			$$('.leftmenu2').invoke('hide');
			$('news').show();
			news.activeHeader('news_header');
		})
		$('news-events_header').observe('click',function(evt){
			$$('.leftmenu2').invoke('hide');
			$('news-events').show();
			news.activeHeader('news-events_header');
		})
	},
	
	activeHeader:function(id){
		console.log(id);
			$$('.title1').each(function(el){
				el.removeClassName('active');
			});
			$(id).addClassName('active');
	},
	
	itemLinks:function(){
		
		//console.log("Categories : "+allCategories)	
		allItems.each(function(el){
			el.observe('click',function(evt) {news.showItem(el.id) });	
		});
		
	},
	showItem:function(id){
			news.hideAll();
			var titleID = $(id).up().previous().id;
			$('item_info').show();
			item_id = id.split("_")[1];
		//console.log("Product id details call : "+prod_id);
		
		new Ajax.Updater('item_info', 'library/ajax/news_details.php',{
			parameters:{
				news_id:item_id
			},
			evalScripts: true 
			
			}); 	
		
		this.activeMenu(id);
	
	},
	activeMenu:function(id){
		allItems.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	},
	
	showGallery:function(){
		news.hideAll();
		$$('.leftmenu2').invoke('hide');
		$('gallery_content').show();
		news.activeHeader('gallery_header');
	
	},
	showPress:function(){
		news.hideAll();
		$$('.leftmenu2').invoke('hide');
		$('press_content').show();
		news.activeHeader('press_header');
	
	},	
	hideAll:function(){
		$$('.media_sections').invoke('hide');
		
		$('item_info').hide();	
	
	},
	
	initMedia:function(){
	
	//init galery
	allGalery = $$('.gal1');
	////console.log("Categories : "+allCategories)	
	allGalery.each(function(el){
		el.observe('click',function(evt) {news.chgGaleryImg(el.src) });	
	});
	
	},
	
	chgGaleryImg:function(currentSrc){
		imgFile = currentSrc.split("/").last();
		////console.log("current img = "+imgFile);
		$('gal-large').src = path_media_gallery+imgFile;
		//$('gal-large').src = "cms/vault/galery/"+fileId+".jpg";
		Effect.Appear('gal-large_container',{duration:0.5});
		
	}
	
	
}


var brand = {
	
	init:function(){
		allItems = $$('.leftmenu2 li');
		this.itemLinks();
		this.showItem(allItems.first().id);
		//console.log("running news init on : " + allItems);
		
	},
	
	itemLinks:function(){
		
		//console.log("Categories : "+allCategories)	
		allItems.each(function(el){
			el.observe('click',function(evt) {brand.showItem(el.id) });	
		});
		
	},
	showItem:function(id){
			item_id = id;
		//console.log("Product id details call : "+prod_id);
		
		new Ajax.Updater('item_info', 'library/ajax/brand_details.php',{
			parameters:{
				news_id:item_id
			},
			evalScripts: true 
			
			}); 	
		
		this.activeMenu(id);
	
	},
	activeMenu:function(id){
		allItems.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	}
	
}



slideshow_index= 0;
var slideshow = {
	
	init:function(){
		allThumbs = $$('.slidenav');
		allThumbs_total = allThumbs.length-1;
		this.itemLinks();
		//console.log("running news init on : " + allItems);
		slideshow_index= 0;
		allThumbs.last().addClassName('last');
		this.showImg(allThumbs.first());
		
	},
	
	itemLinks:function(){
		
		//console.log("Categories : "+allCategories)	
		allThumbs.each(function(el){
			el.observe('click',function(evt) {slideshow.showImg(el) });	
		});
		
	},
	showImg:function(el){
			img_src = el.alt;
			thumb_id = el.id;
		//console.log("image to gol : "+item_id+" for id"+img_file);
		$('item_slide_img').src = "cms/vault/gallery/"+img_src;
		slideshow_index = thumb_id.split("_")[1];
		
		this.activeMenu(el.id);
	
	},
	activeMenu:function(id){
		allThumbs.each(function(e){
			e.removeClassName('active');
		});
		$(id).addClassName('active');
	},
	prev:function(){
	
		if(slideshow_index==0){
			thumb_id = "thumb_"+(allThumbs_total);
		} else {
			
			thumb_id = "thumb_"+(parseInt(slideshow_index)-1);
		}
		//console.log("next image:"+thumb_id);
		
		this.showImg($(thumb_id));
	
	},
	
	next:function(){
	
		if(slideshow_index>=allThumbs_total){
			thumb_id = "thumb_0";
		} else {
			
			thumb_id = "thumb_"+(parseInt(slideshow_index)+1);
		}
		//console.log("next image:"+thumb_id);
		
		this.showImg($(thumb_id));
	
	}
	
}




function register(){
	//ajax call to plug in doc link if valid email;
		
		email2send = $("email").value;
		
	
	
	new Ajax.Updater('register_box', '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;
	}
}






var terms = {
	
	init:function(){
		//allItems = $$('.leftmenu2 li');
		//this.itemLinks();
		this.showItem();
		//console.log("running news init on : " + allItems);
		
	},
	
	showItem:function(){
			item_id = 'terms';
		//console.log("Product id details call : "+prod_id);
		
		new Ajax.Updater('terms_info', 'library/ajax/terms_details.php',{
			parameters:{
				news_id:item_id
			},
			evalScripts: true 
			
			}); 	
			
	}
	
}


