// gui javascript

window.addEvent('domready', function() {
	gui = new GUI();	
});

var GUI = new Class({

	bubble: Array,
    promptUsername: 'Nameholder ID',
    promptPassword: 'Password',
	  
	initialize: function() {
		
		$$('ul.nav.primary li a').each(function(el, index) { // loop elements
			if(window.location.href.contains(el.href)) {
				el.addClass('active');
			} else {
				el.removeClass('active');
			}
		}.bind(this));
		
		$$('.bubble.cta').each(function(cta, index) { // loop elements
			if(cta.type != 'submit') {
				cta.grab(new Element('span', {'class': 'pointer sml top-right'}));
			}
		});

		$$('.nav.lower ul').each(function(ul, index) {
			this.bubble[index] = ul.getElement('li.bubble');
			this.bubble[index].set('tween', {transition: Fx.Transitions.Circ.easeOut});
			ul.addEvents({
			    mouseover: function(){
				  ul.addClass('hover');
				  this.bubble[index].tween('top', -20);
				}.bind(this),
			    mouseout: function(){
			      ul.removeClass('hover');
				  this.bubble[index].tween('top', -10);
				}.bind(this)
			});
		}.bind(this));

		$$('.nav.upper a.handle').each(function(handle, index) {
			var drawer = handle.getNext('.drawer');
			if(drawer != null) {
				var inner = drawer.getElement('.inner');
				var close = inner.getElement('.close');
				handle.addEvent('click', function(e) {
					inner.reveal(); return false;
				});
				close.addEvent('click', function(e) {
					inner.dissolve(); return false;
				});
			}
			
		}.bind(this));

		// old legacy function
		
		if($('dmzLogin') != undefined) {
			$('dmzLogin').addEvent('click', function(e) {
				var hostname = (window.location.hostname == 'domainz.jon.mit' || window.location.hostname == 'domainz.stage.mit' || window.location.hostname == 'ote.domainz.net.nz' || window.location.hostname == 'dnzdrs01.domainz.net.nz') ? 'dnzdrs01.domainz.net.nz' : 'www.domainz.net.nz';
				window.open("https://" + hostname + "/Facility/Login/ssl/Login.asp","Login","scrollbars=no,status=no,width=450,height=300,screenX=200,screenY=300,top=200,left=300");
				return false;
			});
		}
		
		// new login function
		
		if($('frmHdrLogin') != undefined) {

			var inputUsername = $$('#frmHdrLogin input.username')[0];
			
			if (inputUsername.value == '' || inputUsername.value == this.promptUsername) { // add prompt to input
				inputUsername.value = this.promptUsername;
				inputUsername.setStyles({color: '#999', fontSize: '16px'});
			}		
			
			inputUsername.addEvent('focus', function(e) { // clear prompt on focus
				if(inputUsername.value == this.promptUsername) {
					inputUsername.value = '';
				}
				inputUsername.setStyles({color: '#000', fontSize: '18px'});
			}.bind(this));			

			var inputPassword = $$('#frmHdrLogin input.password')[0];

			if (inputPassword.value == '' || inputPassword.value == this.promptPassword) { // add prompt to input
				inputPassword.value = this.promptPassword;
				if(!Browser.ie && !Browser.opera) {
					inputPassword.type = 'text';
				}
				inputPassword.setStyles({color: '#999', fontSize: '16px'});
			}
			
			inputPassword.addEvent('focus', function(e) { // clear prompt on focus
				if(inputPassword.value == this.promptPassword) {
					inputPassword.value = '';
				}
				if(!Browser.ie && !Browser.opera) {
					inputPassword.type = 'password';
				}
				inputPassword.setStyles({color: '#000', fontSize: '18px'});
			}.bind(this));

			
			$$('#frmHdrLogin .username').addEvents({
				mouseover: function(){
					$('frmHdrLogin').getElement('label.username').setStyle('backgroundPosition', '-0px -14px');
				},
				mouseout: function(){
					$('frmHdrLogin').getElement('label.username').setStyle('backgroundPosition', '0px 0px');
				}
			});

			$$('#frmHdrLogin .password').addEvents({
				mouseover: function(){
					$('frmHdrLogin').getElement('label.password').setStyle('backgroundPosition', '-15px -14px');
					$('frmHdrLogin').getElement('input.password.btn').setStyle('color', '#666');
				},
				mouseout: function(){
					$('frmHdrLogin').getElement('label.password').setStyle('backgroundPosition', '-15px 0px');
					$('frmHdrLogin').getElement('input.password.btn').setStyle('color', '#ccc');
				}
			});
		}
		
		if($('frmHdrSearch') != undefined) {
			$$('#frmHdrSearch .q').addEvents({
			    mouseover: function(){
					$('frmHdrSearch').getElement('label.q').setStyle('backgroundPosition', '-30px -14px');
					$('frmHdrSearch').getElement('input.q.btn').setStyle('color', '#666');
				},
			    mouseout: function(){
					$('frmHdrSearch').getElement('label.q').setStyle('backgroundPosition', '-30px 0px');
					$('frmHdrSearch').getElement('input.q.btn').setStyle('color', '#ccc');
				}
			});
		}


		if($$('.price-list table').length > 0) {
			$$('.price-list table').each(function(table, tableIndex) { // loop elements
				table.getElements('td').each(function(td, tableIndex) { // loop elements
					if(!td.getParent().hasClass('spacer')) {
						td.addEvents({
						    mouseover: function(){
								td.getParent().addClass('hover');
							}.bind(this),
						    mouseout: function(){
								td.getParent().removeClass('hover');
							}.bind(this)
						});
					}
				});
			});
		}
		
		if($$('.price-list .header').length > 0) {
			$$('.price-list .header').each(function(header, tableIndex) { // loop elements
				
				var help = new Element('span', {'class': 'help'});
				var icon1 = new Element('span', {'class': 'icon'});
				var icon2 = new Element('span', {'class': 'icon'});
				var pointer = new Element('span', {'class': 'pointer sml top-right'});
				var div = new Element('div', {'class': 'info bubble sky-blue'});
				var h4 = new Element('h4', {'text': 'Help is just one click or phone call away'});
				var p1 = new Element('p', {'class': 'phone', 'text': '0800 366 2469'});
				var p2 = new Element('p', {'class': 'email', 'text': '4service@domainz.net.nz'});
				
				p1.grab(icon1);
				p2.grab(icon2);
				
				div.grab(h4);
				div.grab(p1);
				div.grab(p2);
				div.grab(pointer);

				help.addEvents({
				    click: function(){
						header.getElement('div.info').setStyle('display', ((header.getElement('div.info').getStyle('display') == 'none') ? 'block' : 'none'));
					}.bind(this)
				});
				
				header.grab(help);
				header.grab(div);
			});
		}
		
		if($('ftr-slide') != undefined) {
			var gallery = new slideGallery($('ftr-slide'), {
				mode: 'circle',
				direction: 'horizontal',
				transition: Fx.Transitions.Sine.easeInOut,
				autoplay: true,
				speed: 1000,
				duration: 5000
			});
		}
		
		if($$('#banner.homepage').length == 1) {	
			this.gallery = new slideGallery($('banner'), {
				direction: 'horizontal',
				mode: 'callback',
				paging: true,
				transition: Fx.Transitions.Sine.easeInOut,
				autoplay: false,
				speed: 1000,
				duration: 5000,
				stopOnHover: true
			});
		}
		
		if($('c2t-open') != undefined) {
			var fx = new Fx.Reveal($('click-to-talk'), {
				duration: 500,
				mode: 'vertical',
				onComplete: function() {
					$('c2t-close').setStyle('display', 'block');
				}
			});
			
			$('c2t-open').addEvent('click', function(e) {
				$('c2t-close').setStyle('display', 'none');
				fx.reveal();
				return false;
			});
			$('c2t-close').addEvent('click', function(e) {
				$('c2t-close').setStyle('display', 'none');
				fx.dissolve();
				return false;
			});
		}
		
		if($$('table.fancy').length > 0) {
			$$('table.fancy').each(function(table, tableIndex) { // loop elements
				table.getElements('tr').each(function(tr, rowIndex) { // loop elements
					if (rowIndex == 0) {
						tr.getElements('h2').each(function(h2) { // loop elements
							h2.set('tween', {transition: Fx.Transitions.Circ.easeOut});
						});
					}
					if (tr.hasClass('cta')) {
						tr.getElements('a').each(function(a) { // loop elements
							a.set('tween', {transition: Fx.Transitions.Circ.easeOut});
							a.addEvents({
								mouseover: function() {
									console.log("in here");
									a.getElement('span').setStyle('background-position', '-60px 0');
									a.tween('top', 10);								
								},
								mouseout: function() {
									a.getElement('span').setStyle('background-position', '-180px 0');
									a.tween('top', 0);								
								}
							});								
						});
					}
					if (rowIndex > 0) { // skip header row
						tr.addClass((rowIndex % 2) ? 'odd' : 'even');
						tr.addEvents({
							mouseover: function() {tr.addClass('active');},
							mouseout: function() {tr.removeClass('active');}
						});
					}
					tr.getElements('th, td').each(function(cell, colIndex) { // loop elements
						if (colIndex > 0) {
							cell.addEvents({
								mouseover: function() {
									this.highlightCol(table, colIndex, true);
								}.bind(this),
								mouseout: function() {
									this.highlightCol(table, colIndex, false);
								}.bind(this)
							});
						}
					}.bind(this));
				}.bind(this));
			}.bind(this));
		}
 	},

 	highlightCol: function(table, colIndex, state) {
 		table.getElements('tr').each(function(tr, rowIndex) { // loop elements
 			tr.getElements('th, td').each(function(cell, index) { // loop elements
 	 			if (rowIndex == 0) {
 	 				cell.getElements('h2').each(function(h2) { // loop elements
 	 					h2.tween('paddingBottom', ((index == colIndex && state == true) ? '28' : '18'));
 	 				});
 	 			}
 				if (index == colIndex && state == true && !tr.hasClass('cta')) {
 					cell.addClass('active-col');
 				} else {
 					cell.removeClass('active-col');
 				}
 			});
 		});
 	}
 		
});

function pause() { // called by flash when movie starts
	var expiry = new Date(); expiry.setDate(expiry.getDate() + 28);
	document.cookie= '_domainz_mov=true; expires=' + expiry.toUTCString();
}

function resume() { // called by flash when movie ends
	gui.gallery.begin();	
}

