// ENAGIC.COM

if (window.location.href.test(/enagic-latest-news/))
	AjaxHash = false;
else
	AjaxHash = true;

sContentAreaID = 'AjaxContent';
oContentTarget = $(sContentAreaID);
jsOnComplete = "AjaxComplete('AjaxContent');";

var oLeftNav;

var AjaxComplete = function(sContentAreaID)
{
	if (isValid('.productStart'))	new Accordion('.productStart', 'div.productContainer', ProductAccordionOptions);
	if (window.ParseTwitterGitter) ParseTwitterGitter();
	MooTip();
	HoverFade();
	new HoverSwap();
	PowerFormsParse();
	ParseSmoothLinks();
	Mediabox.scanBlock(sContentAreaID);
	
	if (isValid('.faqStart'))
		new CreateAccordion('.faqStart', '.faqContainer', FAQAccordionOptions);

	if (isValid('.compStart'))
		new CreateAccordion('.compStart', '.compContainer', COMPAccordionOptions);

	
	if (isValid('.sectionLeft .sectionToggler') && !Browser.Engine.trident4)
		new CreateAccordion('.sectionLeft .sectionToggler', '.sectionLeft .sectionContent', SECTIONAccordionOptions);

	if (isValid('.sectionRight .sectionToggler') && !Browser.Engine.trident4)
		new CreateAccordion('.sectionRight .sectionToggler', '.sectionRight .sectionContent', SECTIONAccordionOptions);
	
	$$('.ajaxify').each(function(e)
	{
		var sTag = e.get('tag');
		
		if (sTag == 'form')
			AjaxifyForm(e, sContentAreaID, jsOnComplete);
		else 
		if (sTag == 'a')		
			AjaxifyLink(e, sContentAreaID, jsOnComplete);
	});
	
	if (isValid('.AjaxProductContent')) Mediabox.scanBlock('.AjaxProductContent');
	if (isValid('.AjaxWaterContent')) Mediabox.scanBlock('.AjaxWaterContent');
	
	if (isValid('.qandaStart'))
		new CreateAccordion('.qandaStart', '.qandaContainer', QANDAAccordionOptions);
	
	if (isValid('.sizzlingStart'))
		new CreateAccordion('.sizzlingStart', '.sizzlingContainer', SIZZLINGAccordionOptions);
	
	if (isValid('subtabs'))
	{
		new TabSwapper({
			selectedClass: 'subtabs_on',
			mouseoverClass: 'subtabs_over',
			deselectedClass: '',
			mouseoutClass: '',
			tabs: $$('#subtabs li'),
			clickers: $$('#subtabs li a'),
			sections: $$('.subtabs_content'),
			cookieName: 'enagic_subtabs'
		});
	}
	
	if (isValid('formtabs'))
	{
		new TabSwapper({
			selectedClass: 'formtabs_on',
			mouseoverClass: 'formtabs_over',
			deselectedClass: '',
			mouseoutClass: '',
			tabs: $$('#formtabs li'),
			clickers: $$('#formtabs li a'),
			sections: $$('.formtabs_content'),
			cookieName: 'enagic_formtabs'
		});
	}



	
	if (AjaxOnLoadArray.length > 0)
	{
		AjaxOnLoadArray.each(function(func, index){func();});
		AjaxOnLoadArray = [];
	}
};


/*
 * GOOGLE GEOCODE MAPPING SYSTEM
 */
// define base classes
if (window.google && window.google.maps)
{
	var mapGeocoder = new google.maps.Geocoder();
	var mapOptions = {
	  zoom: 15,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	};
}
// initialize a map from a particular element in a particular x position
var initMap = function(el, x)
{
	var map = new google.maps.Map(el, mapOptions);
	var address = $$('.maps_address')[x].get('text').replace(new RegExp( "\\n|\\t", "g" ), ' ');
	//if (window.console) console.log(address);
	mapGeocoder.geocode( { 'address': address}, function(results, status) 
	{
        if (status == google.maps.GeocoderStatus.OK) 
        {
			map.setCenter(results[0].geometry.location);
			var marker = new google.maps.Marker({
				  title: 'Enagic',
			      map: map, 
			      position: results[0].geometry.location
			});
			
			var lat = results[0].geometry.location.lat();
			var lng = results[0].geometry.location.lng();
			
			var thumb = $$('.map_marker_thumb')[x]; 
			if (x != 3 && x != 6)
				if (thumb) thumb.set('src', 'http://cbk0.google.com/cbk?output=thumbnail&w=90&h=68&ll='+lat+','+lng);
			else
				if (thumb) thumb.destroy();
			
			var infoContent = $$('.map_marker')[x].get('html');
			var infowindow = new google.maps.InfoWindow({
				    content: infoContent
			});
		
			google.maps.event.addListener(marker, 'click', function() {
			  infowindow.open(map,marker);
			});
			
			setTimeout(function(){infowindow.open(map,marker)}, 3500);
          
        } else {
          // geocode doesn't like rapid queries, so if we hit an over query limit, lets delay it and try again
          if (status == 'OVER_QUERY_LIMIT')
          {
          	setTimeout(function(){
				initMap(el,x);
          	}, 1000);
          } else
          {
          	//alert("Geocode was not successful for the following reason: " + status);
          	el.set('html', '<a href="http://maps.google.com/?q=to+'+address+'" target="_blank"><img src="/images/maps_'+x+'.jpg" alt="map" /></a>');
          }
        }
	});
};

// initialize all maps on the page with class map_canvas
var initAllMaps = function() 
{
	if (!isValid('.map_canvas')) return;
	
	
	$$('.map_canvas').each(function(el,x){

		initMap(el, x);
		
		var address = $$('.maps_address')[x].get('text').replace(new RegExp( "\\n|\\t", "g" ), ' ').urlencode();
		$$('.maps_dir')[x].set('href', 'http://maps.google.com/?q=to+' + address);
		$$('.maps_marker_dir')[x].set('href', 'http://maps.google.com/?q=to+' + address);
		
	});
	
};


window.addEvent('load', initAllMaps);
/*
** DOMREADY CODE EXECUTION
** When the document is done loading, execute this code
*/
window.addEvent('domready', function() 
{
		//PreloadImage(['/images/menu_home_hover.gif', '/images/menu_enagic_hover.gif', '/images/menu_water_hover.gif', '/images/menu_technology_hover.gif', '/images/menu_faq_hover.gif', '/images/menu_contact_hover.gif']);
		
		/*
		** SMOOTHSCROLL
		*/
		//new SmoothScroll({ duration: 1000 })
		
		
		
		/*
		** MOUSEOVER TRANSPARENCY FADER
		** HoverFade(beginOpacityPercent, endOpacityPercent, milliseconds, classname, bClickNStick);
		** defaults HoverFade(65, 100, 500, 'HoverFade', true);
		*/	
		//HoverFade();
		
		
		/*
		 * SETUP LANGUAGE LINKS
		 * We do this here to avoid problems with the translation system 
		 * trying to convert the translation links into translated stuffs
		 */
//		var uri = location.pathname.replace(/\/translate\/[a-z]{2,2}\//, '/');
//		if (uri == '/') uri = '/index.php';
//		$$('.flags a').each(function(el, x){
//			arel = el.get('rel');
//			//if (window.console) console.log(arel + ' - ' + uri);
//			if (arel == null) arel = '';
//			el.set('href', arel + uri);
//		});
	
		
		/*
		** ACCORDION STARTUPS
		*/
		if (isValid('leftnav') && !Browser.Engine.trident4)
		{
			if ($('leftnav').hasClass('acchover')) LeftNavAccordionOptions.trigger = 'mouseover';
			oLeftNav = new CreateAccordion('.toggler', '.togglerContent', LeftNavAccordionOptions);
			
			if ($('leftnav').hasClass('acchover'))
			oLeftNav.elements.each(function(el, x){
				oLeftNav.togglers[x].addEvent('mouseup', function(){
					ela = el.getElement('a');
					if (ela.retrieve('ajaxparsed') != true)
					{
						var ahref = el.getElement('a').get('href');
						window.location.href = ahref;
					} else
						el.getElement('a').fireEvent('click');
				});
			});
		}
		
		if (isValid('leftnavhover') && !Browser.Engine.trident4)
		{
			LeftNavAccordionOptions.trigger = 'mouseover';
			oLeftNav = new CreateAccordion('.toggler', '.togglerContent', LeftNavAccordionOptions);
			
		}


		//if (isValid('.faqStart'))	oFaqAcc = new CreateAccordion('.faqStart', 'div.faqContainer', FAQAccordionOptions);



		//if (isValid('.formsStart'))
			//var oFORMSAccordion = new CreateAccordion('.formsStart', '.formsContainer', FORMSAccordionOptions);
		
		


		///if (isValid('.productStart'))
			//var oProductAccordion = new Accordion('.productStart', 'div.productContainer', ProductAccordionOptions);
			
					
		
		
		/*
		** AJAX STUFF
		*/
		if (isValid('.AjaxLink'))
		{
			$$('.AjaxLink').each(function(el){
				AjaxifyLink(el, 'AjaxContent', jsOnComplete);
			});
		}
		
		if (isValid('.AjaxWaterLink'))
		{
			$$('.AjaxWaterLink').each(function(el){
				AjaxifyLink(el, 'AjaxContent', jsOnComplete);
			});
		}		
		
		AjaxComplete(sContentAreaID);
		
		
		
		
}); // end domready





var ChangeHeader = function(sNewImgURL)
{
	if ($type(sNewImgURL) !== 'string') sNewImgURL = '/images/subheader_3a.jpg';
	
	var eHeader = $('subheader');
	if ($type(eHeader) !== 'element') return;
	var eHeaderImg = eHeader.getElements('img')[0];
	if ($type(eHeaderImg) !== 'element') return;
	
	eNewImg = new Element('img', {src: sNewImgURL, styles: {position: 'absolute'}}).fade('hide');
	
	
	eHeaderImg.setStyle('position', 'absolute');
	
	eHeader.grab(eNewImg);
	
	eHeaderImg.fadenix();
	eNewImg.fade('in');
};






// enagic.com's custom accordion options
var LeftNavAccordionOptions = {

	show: 'nothing',						// integer, the Index of the element to show at start.
	display: 0,					// integer, the Index of the element to show at start (with a transition). defaults to 0.
	fixedHeight: false,		// integer, if you want the elements to have a fixed height. defaults to false.
	fixedWidth: false,		// integer, if you want the elements to have a fixed width. defaults to false.
	height: true,				// boolean, will add a height transition to the accordion if true. defaults to true.
	opacity: false,				// boolean, will add an opacity transition to the accordion if true. defaults to true.
	width: false,				// boolean, will add a width transition to the accordion if true. defaults to false, css mastery is required to make this work!
	alwaysHide: false,		// boolean, will allow to hide all elements if true, instead of always keeping one element shown. defaults to false.
	hover: false,
	
	// the below options are part of fx.Base, and thus, a part of accordion
	duration: 500,															// the duration of the effect in ms; 500 is the default.
	transition: Fx.Transitions.Sine.easeInOut,		// the equation to use for the effect see Fx.Transitions; default is <Fx.Transitions.Sine.easeInOut>
	unit: 'px',																		// the unit is px by default (other values include things like em for fonts or %).
	wait: false,																	// boolean: to wait or not to wait for a current transition to end before running another of the same instance. defaults to true.
	fps: 50,																		// on mouse over, activate accordion element
	togglerClassA: 'togglerActive',
	togglerClassB: 'togglerInactive',
	togglerClassHover: 'togglerHover'
	
	// below are the events.  onActive and onBackground are what accordion mainly uses, but others are possible


	
	/*
	** OTHER POSSIBLE EVENTS
	onStart						the function to execute as the effect begins; nothing (Class.empty) by default.
	onComplete				the function to execute after the effect has processed; nothing (Class.empty) by default.
	onCancel					the function to execute when you manually stop the effect.
	*/
	
};



var AutoYear = function(yearPast, yearFuture) 
{
	var time = new Date();
	var year = time.getYear();
	 
	if (year < 1900) {
		year = year + 1900;
	}
	
	var date = year - yearPast; 
	var future = year + yearFuture;
	
	do {
		document.write ("<option value=\"" +date+"\">" +date+ "</option>");
		date++;
	}
	while (date < future)
};


var FAQAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	onActive: function(toggler, element){
		element.setStyle('display', 'block');
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title_hover.jpg)'); 
	},
	onBackground: function(toggler, element){
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title.jpg)'); 
	}
};



var COMPAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	onActive: function(toggler, element){
		element.setStyle('display', 'block');
		toggler.setStyle('background-image', 'url(/approved/images/bg_cell.jpg)'); 
	},
	onBackground: function(toggler, element){
		toggler.setStyle('background-image', 'url(/approved/images/bg_cell.jpg)'); 
	}
};



var SIZZLINGAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	onActive: function(toggler, element){
		element.setStyle('display', 'block');
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title_hover.jpg)'); 
	},
	onBackground: function(toggler, element){
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title.jpg)'); 
	}
};



var QANDAAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	onActive: function(toggler, element){
		element.setStyle('display', 'block');
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title_hover.jpg)'); 
	},
	onBackground: function(toggler, element){
		toggler.setStyle('background-image', 'url(images/bg_faqmenu_title.jpg)'); 
	}
};




var SECTIONAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	togglerClassA: 'sectionTogglerActive',
	togglerClassB: 'sectionTogglerInactive'
};

var FORMSAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	togglerClassA: 'formsTogglerActive',
	togglerClassB: 'formsTogglerInactive'
};





var ProductAccordionOptions = {
	opacity: false,
	duration: 800,
	show: 'none',
	alwaysHide: true,
	hover: true,
	onActive: function(toggler, element){
		toggler.setStyle('background-image', 'url(images/bg_productinfo_title_hover.jpg)'); 
		element.setStyle('display', 'block');
	},
	onBackground: function(toggler, element){
		toggler.setStyle('background-image', 'url(images/bg_productinfo_title.jpg)'); 
	}
};





