var emailRegEx = new RegExp("/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/");

document.observe("dom:loaded", function() {	
	
	$$('#left_nav > a').each(function(element) {	
			
		element.observe('mouseover', function(event) {
			Event.stop(event);
			Effect.Queues.get(this.identify()).each(function(effect) { effect.cancel(); });
			this.setStyle({	backgroundColor:'#FFF', backgroundPosition:'10px', color:'#999', paddingLeft:'30px' });
			new Effect.Morph(this, {
				style: { backgroundColor:'#EEE', color:'#000', backgroundPosition:'20px', paddingLeft:'40px' },
				queue: { scope: this.identify() },
				duration: 0.2
			});			
		}.bindAsEventListener(element));
		
		element.observe('mouseout', function(event) {
			Event.stop(event);
			Effect.Queues.get(this.identify()).each(function(effect) { effect.cancel(); });			
			new Effect.Morph(this, {
				style: { backgroundColor:'#FFF', color:'#999', backgroundPosition:'10px', paddingLeft:'30px' },
				queue: { scope: this.identify() },
				duration: 0.2
			});	
		}.bindAsEventListener(element));
		
	});
	
	$$('#btm_right .content > a').each(function(element) {
	
		var styleAtLoad = $H({width:'0px', height:'0px'});
		styleAtLoad.set('right', element.next('img').getStyle('right'));
		styleAtLoad.set('top', element.next('img').getStyle('top'));
		
		element.observe('mouseover', function(event) {
			Event.stop(event);
			Effect.Queues.get(this.identify()).each(function(effect) { effect.cancel(); });
			new Effect.Morph(element.next('img'), {
				style: { height:'124px', width:'200px', right:'5px', top:'5px' },
				queue: { scope: this.identify() },
				duration: 0.5,
				beforeStart: function() {					
					element.next('img').show();
				}
			});			
		}.bindAsEventListener(element));
		
		element.observe('mouseout', function(event) {
			Event.stop(event);
			Effect.Queues.get(this.identify()).each(function(effect) { effect.cancel(); });
			new Effect.Morph(element.next('img'), {
				style: styleAtLoad,
				queue: { scope: this.identify() },
				duration: 0.5,
				afterFinish: function() {
					element.next('img').hide();
				}
			});
		}.bindAsEventListener(element));
		
	});
	
	$$('a').each(function(element) {
		var LIVE = (element.href.indexOf("cablestogo-oem") != -1)
		var DEV = (element.href.indexOf("developoem.cablestogo") != -1)
		var DEV2 = (element.href.indexOf("dev2oem.cablestogo") != -1)
		var MAIL = (element.href.indexOf("mailto:") != -1)
		if ( !LIVE && !DEV && !DEV2 && !MAIL ) {
			element.observe('click', function(event) {
				Event.stop(event);
				window.open(this.href, '_blank');
			}.bindAsEventListener(element));
		}			
	});
	
	$$('.refresh').each(function(element) {	
		element.observe('click', function(event){
			Event.stop(event);
			var objImage = $('imgCaptcha');
			if (objImage != undefined) {
				var now = new Date();
				objImage.src = objImage.src.split('?')[0] + '?x=' + now.toUTCString();
			}			
		}.bindAsEventListener(element));
		element.show();
	});
	
	$$('.tree').each(function(element) {
		new TreeView(element.identify());
	});
	
	$$('form').each(function(element) {
		element.getElements().each(function(element) {
			element.observe('focus', function(event) {
				Event.stop(event);
				this.addClassName('focus')
			}.bindAsEventListener(element));
			element.observe('blur', function(event) {
				Event.stop(event);
				this.removeClassName('focus')
			}.bindAsEventListener(element));
		});
	});

	var elem = $('hear_about');
	if (elem) {
		elem.observe('change', function(event) {
			event.stop();
			if ($F(elem) == 'other') {
				elem.up('p').next('p').show();
			} else {				
				elem.up('p').next('p').hide();
			}
		});
	}
});

var TreeView = Class.create({
	initialize: function(id){
		$$('#'+id+' p').each(function(element, i){
			if (element.next() && element.next().tagName == 'DIV') {
				element.addClassName('parent');
				element.observe('click', function(event){
					Event.stop(event);
					if ( Effect.Queues.get(this.identify()).size() == 0 ) {
						element.toggleClassName('open');
						new Effect.toggle(element.next(), 'blind', { duration: 0.5, queue: { scope: this.identify() } });
					}
				}.bindAsEventListener(element));
			}
		});		
		$$('#'+id+' > p').each(function(element, i){
			if ($H(window.location.toString().toQueryParams()).get('tab') == i) { element.addClassName('open'); }
		});	
		this.links = $$('.parent');
		this.links.each(function(element) { 
			if (!element.hasClassName('open')) { element.next().hide() }
		});
	}			
});