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

// PATHS variables are defines in the inc_constants.php to map against PHP application
// path_xyz patterns

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.src) });	
	});
	
	},
	
	chgGaleryImg:function(currentSrc){
		imgFile = currentSrc.split("/").last();
		////console.log("current img = "+imgFile);
		$('gal-large').src = path_media_gallery+imgFile;
		//$('gal-large').src = "admin/vault/galery/"+fileId+".jpg";
		Effect.Appear('gal-large_container',{duration:0.5});
		
	}
	
	
}



var catalog = {
	
	init:function(){
		allProducts = new Array();
		allCategories = new Array();
		allCategoryMenus = new Array();
		allThumbs = new Array();
		
		overlaySection = new Array();
		this.categories();
		this.previews();
	},
	
	categories:function(){
		allCategories = $$('.list_cat');
		allCategoryMenus = $$('.leftmenu2');
		allCategoryMenus.invoke('hide');
		////console.log("Categories : "+allCategories)	
		allCategories.each(function(el){
			//el.hide();
			el.observe('click',function(evt) {catalog.showCategory(el.id) });	
		});
		

		
	},
	
	showFirst:function(id){
		this.showCategory(id);
		firstProd = $$('#'+id+'_menu li').first().id;
		//console.log("fisrt in list : "+firstProd);
		this.showProduct(firstProd);
	},
	
	showCategory:function(id){
		allCategoryMenus.invoke('hide');
		//console.log("id to showcategory function: "+id);
		$$("#"+id+" .leftmenu2").first().show();
		
		
	},
	
	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').stopObserving('click');
		$('btnBuy').observe('click',function(evt) { alert('Coming Soon ! ( '+$('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,e.id)});			
		});
		
	},

	showMainimg:function(fileId,thumbId){
		$('product_main_img').src=path_regular+fileId;
		var hiddenThumb = $$('#product_thumbs img.thumbs_hide').first().id;
		var currentClass = $(thumbId).className;
		
		$(hiddenThumb).className = currentClass;
		$(thumbId).className="thumbs_hide";
		
	},

	showLargeimg:function(){
		$('img_large_src').src=path_large+$('product_main_img').src.split("/").last();
		var imgPos = $('product_main').viewportOffset();
		//console.log(imgPos[0]);
		imgPosLeft = imgPos[0]+18;
		imgPosTop = imgPos[1]-80;
		$('img_large').style.left = imgPosLeft;
		$('img_large').style.top = imgPosTop;
		
		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();
		
	},
	
	hidePreview:function(id){
		prod_id = id.split("_")[0];
		$('preview_'+prod_id).hide();
	},
	
	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');
	}	
	
}


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

	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');
		resultsVilles = $$('.results_'+idville);
		resultsVilles.invoke('show');
	},
	
	showPlaces:function(place_id){
		allPlaces.invoke('hide');
		$("liste_"+place_id).show();
		
	}

	
}


//check out pimiento 2 sctins function hash for reference
current_comm = 0;
current_dossier = 1;
current_bio = 2;
current_photo = 3;
current_img = 4;

var current = new Array();
var totalpages = new Array();
current = [1,1,1,1,1];
totalpages = [1,1,1,1,1];

var presse = {
	
	goto:function(type,id){
		presse.hideall();
		section = type+"_content_"+id;
		Effect.Appear(section);
		currentName = String("current_"+type);
		currentNumber = window[currentName];
		
		
		
		//set link styles
		//if(totalpages[currentNumber]>=2){
			linkid = "link-"+type+"-"+current[currentNumber];
			////console.log("linkid = "+linkid);
			$(linkid).className = "";
			
			current[currentNumber] = id;
			
			linkid = "link-"+type+"-"+current[currentNumber];
			$(linkid).className = "currentpage";
		//}
		//alert("current = "+current[currentNumber]+"  of total"+totalpages[currentNumber]);
		
	},
	
	next:function(type){
		goto_number = 1;
		
		currentName = String("current_"+type);
		currentNumber = window[currentName];
		
		//alert("total pages = "+totalpages[currentNumber]+" current number = "+current[currentNumber]);
		
		if(current[currentNumber]>=totalpages[currentNumber]) {
			presse.goto(type, goto_number);	
		} else {
			goto_number = current[currentNumber] + 1;
			presse.goto(type, goto_number);	
		}
		
		
	},
	
	hideall:function(){
		$$('.layers').invoke('hide');
	}
	
	
	
}


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;
	}
}
