
// ===== Virtual Host Flash Module =====================================================================================
window.addEvent('domready',function(){
	if($('virtualHost')){
		var so = new SWFObject('flash/virtualHost.swf', 'oeIndexModule', '908', '333', '8');
		so.addParam("wmode","transparent");
		so.addParam("allowScriptAccess","always");
		so.write("virtualHost");
	}
});
// ===== Visit Virtual Host Flash Module ===============================================================================
window.addEvent('domready',function(){
	if($('visitVirtualHost')){
		var so = new SWFObject('flash/visitVirtualHost.swf', 'oeSideBarModule', '248', '163', '8');
		so.addParam("wmode","transparent");
		so.write("visitVirtualHost");
	}
});
// ========== ANCHOR TAGS: Add Fade FX =================================================================================
window.addEvent('load',function(){
	$$('a.fade').each(function(element){
		var fx = new Fx.Style(element,'color',{duration:500});
		element.addEvent('mouseenter',function(){
			fx.stop(); fx.start("FFFFFF")
		});
		element.addEvent('mouseleave',function(){
			fx.stop(); fx.start("6091F2");
		});
	});
});
// ========== Switch Tabs ====================================================================================
window.addEvent('domready',function(){
	if(!$E('ul#tabs')) return;
	if($E('.googleMapContainer')){
		$E('.googleMapContainer').removeClass('hide').setStyles({'height':0,'overflow':'hidden'});
	}
	var activeTab = false;
	var findOutMore = $E('.findOutMore');
	var startHeight = $('contentArea').offsetHeight;
	var tabBtn = $$('ul#tabs li');
	var tabItem = $$('.tabcontent');
	tabBtn.each(function(tab,i){
		tab.addEvent('click',function(e){
			new Event(e).stop();
//			tabItem.each(function(item){ item.addClass('hide'); if(findOutMore) fader.clear() });
			tabItem.each(function(item){ item.addClass('hide'); });
			tabBtn.each(function(tab){ tab.removeClass('active'); });
			tab.addClass('active');
			tabItem[i].removeClass('hide');
			// ===== Google Map <div> needs to be visible to load successfully =====
			if($('googleMap')){
				if(tabItem[i].hasClass('googleMapContainer')){
					tabItem[i].setStyles({'height':625,'overflow':'visible'})
					$('googleMap').setStyle('left','0px');
				} else {
					$('googleMap').setStyle('left','-9000px');
				}
			};
			/*
			var endHeight = tabItem[i].offsetHeight;
			tabItem[i].addClass('hide');
			if(startHeight != endHeight){
				$('contentArea').effect('height', {duration: 500, transition:Fx.Transitions.Back.easeInOut,
					onComplete:function(){
						tabItem[i].removeClass('hide');
						startHeight = tabItem[i].offsetHeight;
					}
				}).start(endHeight+100);
			} else {
				tabItem[i].removeClass('hide');
				startHeight = tabItem[i].offsetHeight; 
			};
			*/
			//if(findOutMore) fader.begin(tabItem[i].getElement('.findOutMore img').removeClass('hide'));
		});
		// ===== Display '#' Hash Item =====
		var href = window.location.href.toLowerCase();
		if(href.indexOf(tab.getText().toLowerCase())>0){
			tab.addClass('active');
			tabItem[i].removeClass('hide');
			activeTab = tabItem[i];
			if($('googleMap')){
				if(tabItem[i].hasClass('googleMapContainer')){
					tabItem[i].setStyles({'height':625,'overflow':'visible'})
					$('googleMap').setStyle('left','0px');
				} else {
					$('googleMap').setStyle('left','-9000px');
				}
			};
		};
	});
	if(!activeTab){
		activeTab = tabItem[0];
		tabBtn[0].addClass('active');
		activeTab.removeClass('hide');
	};

//	if(findOutMore){
//		var fader = new pulseFade();
//		fader.begin(activeTab.getElement('.findOutMore img').removeClass('hide'));
//	}
});
// ========== SERVICES: Find Out More Fade FX =================================================================================
function pulseFade(){
	var loop;
	this.begin = function(element){
		var fade = new Fx.Style(element, 'opacity', {duration:750, wait:false, transition:Fx.Transitions.Quad.easeIn}).set(0);
		// ===== Set Up Chain =====
		var fadeLoop = new Chain();
		var runLoop = function() {
			fadeLoop.callChain();
			fadeLoop.chain( function() {
				fade.start(0,0.75).chain(function() { fade.start(0.75,0); });
			});
		}
		loop = runLoop.periodical(1500);
	};
	this.clear = function(){
		$clear(loop);
	};
};
// ========== FAQ: Enable Multi-level Accordion ===============================================================================
var nMainCat = null;
var nSubCat = null;
var openItem = false;
var mainCategory = [];
var subCategory = [];

window.addEvent('domready', function(){
	if(!$E('ul#FAQ')) return; // no FAQ list
	mainCategory = ['Cable','OnDemand','Phone','Internet','Service','Install'];
	// ===== Add Accordion Events to Main List ====================================================
	var accordion = new Accordion('div.toggle', 'dl.list', {
		opacity: true, alwaysHide: true, display: -1, wait: false, transition:Fx.Transitions.Circ.easeInOut,
		onActive: function(toggler, element){
			toggler.getParent().setStyle('height','auto');
			toggler.removeClass('arrow_horz').addClass('arrow_vert');
			nMainCat = toggler.getProperty('rel');
		},
		onBackground: function(toggler, element){
			element.setStyle('height',element.offsetHeight);
			toggler.removeClass('arrow_vert').addClass('arrow_horz');
		},
		onComplete: function(){
			if(openItem && nSubCat !== null && $(subCategory[nMainCat]).getElements('dt').length > nSubCat){
				$(subCategory[nMainCat]).getElements('dt')[nSubCat].fireEvent('click');
			};
			openItem = false;
		}
	}, $E('ul#FAQ'));
	$$('div.toggle').each(function(div,idx){ div.setProperty('rel',idx)});
	// ===== Add Accordion Events to All Subsets ==================================================
	var subAccordion = [];
	subCategory = ['faq_Cable','faq_OnDemand','faq_Phone','faq_Internet','faq_Service','faq_Install'];
	subCategory.each(function(element,index){
		subAccordion[index] = new Accordion($(element).getElements('dt'), $(element).getElements('dd'), {
			opacity: true, alwaysHide: true, display: -1, wait: false, transition:Fx.Transitions.Circ.easeInOut,
			onActive: function(toggler, element){
				toggler.getParent().setStyle('height','auto');
				toggler.removeClass('arrow_horz').addClass('arrow_vert');
				nSubCat = index;
			},
			onBackground: function(toggler, element){
				element.setStyle('height',element.offsetHeight);
				toggler.removeClass('arrow_vert').addClass('arrow_horz');
			}
		}, $(element));
	});
	// ===== If URL has hash(#), parse then open FAQ ==============================================
	if(window.location.hash.contains('#')){
		$$('div.toggle').each(function(link, index){
			var urlHash = window.location.hash;
			var hash = (urlHash.contains('#'))? urlHash.replace('#',''):null;
			if(hash && hash.contains(':')){ 
				// ===== Match List ID then set nSubCat to index found =====
				nSubID = hash.split(':')[1];
				hash = hash.split(':')[0];
				nMainCat = mainCategory.indexOf(hash);
				var question = $(subCategory[nMainCat]).getElements('dt');
				question.each(function(dt,n){ if(dt.getProperty('id') == nSubID) nSubCat = n });
			};
			if(link.getNext().getProperty('id').indexOf(hash)>-1){
				nMainCat = index;
				openItem = true;
				link.fireEvent('click');
			};
		});
	};
});
// ========== Add Click to open FAQ or redirect to FAQ page ====================================================================
window.addEvent('domready',function(){
	$$('.FAQs a.btnFAQ').each(function(btn){
		btn.addEvent('click',function(e){
			new Event(e).stop();
			openItem = true;
			if(!$E('ul#FAQ')){
				window.location = btn.getProperty('href')+'#'+btn.getProperty('rel');
			} else {
				var hash = btn.getProperty('rel').split(':')[0];
				nMainCat = mainCategory.indexOf(hash);
				// ===== Match List ID then set nSubCat to index found =====
				nSubID = btn.getProperty('rel').split(':')[1];
				var question = $(subCategory[nMainCat]).getElements('dt');
				if($defined(nSubID)){
					question.each(function(dt,n){ if(dt.getProperty('id') == nSubID) nSubCat = n });
				}
				if($$('dl.list')[nMainCat].offsetHeight > 0){
					$(subCategory[nMainCat]).getElements('dt')[nSubCat].fireEvent('click');
				} else {
					$$('div.toggle')[nMainCat].fireEvent('click');
				}
			}
		});
	});									
});
// ========== Add Click to open Service tab or redirect to Service page ====================================================================
window.addEvent('domready',function(){
	$$('.Services a.btnFAQ').each(function(btn){
		btn.addEvent('click',function(e){
			new Event(e).stop();
			openItem = true;
			if(!(/_active/).test($('Nav_Services').src)){
				window.location = btn.getProperty('href')+'#'+btn.getProperty('rel');
			} else {
				var tabBtn = $$('ul#tabs li');
				var tabItem = $$('.tabcontent');
				tabItem.each(function(item){ item.addClass('hide'); });
				tabBtn.each(function(tab,i){
					tab.removeClass('active');
					// ===== Display '#' Hash Item =====
					if(btn.getProperty('rel').indexOf(tab.getText().toLowerCase())>=0){
						tab.addClass('active');
						tabItem[i].removeClass('hide');
					};
				});
			}
		});
	});									
});
// ========== FOOTER: FAQs Show Questions SLIDER ===============================================================================
window.addEvent('domready',function(){
	var FAQs = $E('#footer .FAQs');
	var items = FAQs.getElements('li');
	var controls = FAQs.getElement('.controls');
	var FAQsList = new FAQsInFooter(FAQs.getElement('ul'),{
		pageSize:4,
		items:items,
		minHeight:90,
		display:controls.getElement('.galleryCount'),
		prev:controls.getElement('.btnPrev'),
		next:controls.getElement('.btnNext')
	});
});
var FAQsInFooter = new Class({
	initialize: function(container, options) {
		this.setOptions(options);
		this.active = false;
		this.activeCount = 1;
		this.container = container;
		this.aItems = $A(this.options.items);
		this.nDisplay = this.options.display;
		this.pageSize = this.options.pageSize;
		this.itemsTotal = this.aItems.length;
		this.pageTotal = (this.itemsTotal%this.pageSize==0)?this.itemsTotal:(this.pageSize-(this.itemsTotal%this.pageSize)+this.itemsTotal);
		this.totalPages = Math.floor(this.pageTotal / this.pageSize);
		this.nDisplay.setHTML(this.activeCount +" of "+ this.totalPages);
		if(this.totalPages > 1){
			this.prevLink = this.options.prev;
			this.nextLink = this.options.next;
			this.prevLink.addClass('btnDisabled');
			this.nextLink.addClass('btnEnabled');
			this.prevLink.addEvent("click", function(e) {new Event(e).stop(); this.previous()}.bind(this));
			this.nextLink.addEvent("click", function(e) {new Event(e).stop(); this.next()}.bind(this));
		} else { 
			this.options.prev.remove();
			this.options.next.remove();
			this.options.display.remove();
		};
		this.slider = new Fx.Slide(this.container, {
			duration: 750,
			transition:Fx.Transitions.quartInOut,
			onStart: function(){
				// disable both buttons
				this.prevLink.removeClass("btnEnabled");
				this.nextLink.removeClass("btnEnabled");
				this.prevLink.addClass("btnDisabled");
				this.nextLink.addClass("btnDisabled");
				this.active = true;
			}.bind(this),
			onComplete: function(){
				if(this.container.getStyle('margin-top').toInt() < 0){
					this.aItems.each(function(li,i){
						li.addClass('hide');
						if(i >= this.range().min && i <= this.range().max){
							li.removeClass('hide');
						};
					}.bind(this));
					var height = (this.container.getCoordinates().height > this.options.minHeight)? 'auto' : this.options.minHeight+'px';
					this.container.setStyle('height',height);
					this.slider.slideIn();
				} else {
					if(this.activeCount > 1){
						this.prevLink.removeClass("btnDisabled");
						this.prevLink.addClass("btnEnabled");
					}
					if(this.activeCount < this.totalPages){
						this.nextLink.removeClass("btnDisabled");
						this.nextLink.addClass("btnEnabled");
					}
					this.active = false;
					this.container.effect('border-bottom-color', {duration: 200, transition:Fx.Transitions.linear}).start('#0d366f','#002752');
				};
			}.bind(this)
		});
	},
	previous: function() {
		if(this.active) return;
		return this.changeImage(this.activeCount-1);
	},
	next: function() {
		if(this.active) return;
		return this.changeImage(this.activeCount+1);
	},
	range: function(){
		var nMin = ((this.activeCount-1)*this.pageSize);
		var nMax = ((this.activeCount*this.pageSize)-1);
		return {min:nMin,max:nMax}
	},
	changeImage: function(n) {
		if((n <= 0) || (n > this.totalPages)) return false;
		var dir = (n > this.activeCount)? -1 : 1;
		this.activeCount = n;
		this.slider.slideOut();
		this.nDisplay.setHTML(this.activeCount +" of "+ this.totalPages);
		this.container.effect('border-bottom-color', {duration: 200, transition:Fx.Transitions.linear}).start('#002752','#0d366f');
	}
});
FAQsInFooter.implement(new Options);
// ========== FOOTER: FAQs Links Bounce FX =================================================================================
window.addEvent('load',function(){
	var list = $$('#footer .FAQs li a');
	list.each(function(element) {
		var fx = new Fx.Styles(element,{duration:500, wait:false, transition:Fx.Transitions.Bounce.easeOut});
		element.addEvent('mouseenter',function(){
			fx.stop(); fx.start({'padding-left':5, 'color':'#FFF'});
		});
		element.addEvent('mouseleave',function(){
			fx.stop(); fx.start({'padding-left':0, 'color':'#3A6ED2'});
		});
	});
});
// ========== FOOTER: Service Icons Bounce FX =================================================================================
window.addEvent('load',function(){
	var service = $$('#footer .Services ul.icon img');
	var reflect = $$('#footer .Services ul.reflect img');
	
	service.each(function(icon, i){
		var fx = new Fx.Styles(icon,{duration:400, wait:false, transition:Fx.Transitions.Bounce.easeOut});
		var fx2 = new Fx.Styles(reflect[i],{duration:400, wait:false, transition:Fx.Transitions.Bounce.easeOut});
		icon.addEvent('mouseenter',function(){
			fx.stop(); fx.start({'padding-top': 0 });
			fx2.stop(); fx2.start({'padding-top': 5 });
		});
		
		icon.addEvent('mouseleave',function(){
			fx.stop(); fx.start({'padding-top': 5 });
			fx2.stop(); fx2.start({'padding-top': 0 });
		});
	});
});
// ============================================================================================================================