// global variables
var currentTroubleTab = "";

var isActiveX = (window.ActiveXObject) ? true : false;

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

addEvent(window,'unload',EventCache.flush);

// Retrieve the first ancestor of a given element that matches the given tag name.
function getAncestorByTagName(obj, tag) {
    var s = obj ? obj : null;
    if (s == null) return s;
    var tagName;
    do {
        s  = s.parentNode;
        tagName = s.tagName.toLowerCase();
        if (tagName=='body') return null;
    }
    while (tagName != tag);
    return s;   
}

// Retrieve the first ancestor of a given element that matches the given class name.
document.getAncestorByClassName = function(obj, className) {
	var s = obj ? obj : null;
    if (s == null) return s;
    var tagName;
    do {
        s = s.parentNode;
        tagName = s.tagName.toLowerCase();
        if (tagName == "body") return null;
    }
    while (!Element.hasClassName(s, className) );
    return s;
}

// Detect links with a rel value of "external" and cause them to open in a popup window
function externalLinks() {
	$$('a[rel~="external"]').each(function(item) {
		item.title += " [ Opens in a new window ]";
		item.onclick = function() {
			newWin(this.href,'960','600','yes','yes','yes','yes','yes');
			return false;
		}
	});
}

// Detect links with a rel value of "popup" and cause them to open in a popup window
function popupLinks() {
	$$('a[rel~="popup"]').each(function(item) {
		item.title += " [ Opens in a new window ]";
		item.onclick = function() {
			newWin(this.href,'700','500','yes','yes','yes','yes','yes');
			return false;
		}
	});
}

// Detect links with a rel value of "gallery" and cause them to open in a popup window
function galleryLinks() {
	$$('a[rel~="gallery"]').each(function(item) {
		item.title += " [ Opens in a new window ]";
		item.onclick = function() {
			newWin(this.href,'500','350','no','no','no','yes','yes');
			return false;
		}
	});
}

// Create a popup window with passed parameters
function newWin(url,w,h,menubar,toolbar,location,scrollbar,resizable) {  
	var appWindow = window.open(url, "appWin", "menubar=" + menubar + ", toolbar=" + toolbar + ",location=" + location + ",resizable=" + resizable + ",scrollbars=" + scrollbar + ",left=20,top=20,width=" + w + ",height=" + h);
}


// Create popups for Flash movies from Service & Maintenance home page
function flashpops() {
	var flashPopLinks = $$('#auto-systems-guide-wrap a');
	flashPopLinks.each(function(item) {
		item.onclick = function() {
			newWin(this.href,'700','400','no','no','no','no','no');
			return false;
		}
	})
}


function setSubNav() {
	
 	var options = $('acdelco-subnav');
 	
 	Element.hide(options);
 	
    options.style.position = 'absolute';
    
	var trigger = $('options-button');
	trigger.style.display = 'block';
 	
	addEvent(trigger, 'mouseover', function() { subNav.showMenu(); return false;  },false );
	addEvent(trigger, 'mouseout', function() { subNav.hideMenu(); return false; },false );
	addEvent(trigger, 'click', function() { return false; },false );
	addEvent(options, 'mouseover', function() { subNav.showMenu(); return false;  },false ); 
	addEvent(options, 'mouseout', function() { subNav.hideMenu(); return false;  },false ); 
}

var subNav = {
    timeout : null,
    showMenu : function(){
    clearTimeout(this.timeout);
        if($('acdelco-subnav').style.display == 'none'){
            this.timeout = setTimeout(function(){new Effect.Appear('acdelco-subnav', {from: 0.0, to: 1, duration:0.4, fps:200})},500);
            $('options-button').parentNode.id = 'subnav-active';
        } 
    },
    hideMenu : function(){
        if($('acdelco-subnav').style.display == 'none'){
            clearTimeout(this.timeout);
            addEvent($('acdelco-subnav'), 'mouseover', function() { subNav.showMenu(); return false;  },false );
      		$('options-button').parentNode.id = $('options-button').parentNode.id.replace('subnav-active','');
        }else{
        	 $('options-button').parentNode.id = 'active';
        	this.timeout = setTimeout(function(){new Effect.DropOut('acdelco-subnav', {duration:0.4, fps:200});  $('options-button').parentNode.id = '';}, 500);
        	addEvent($('options-button'), 'click', function() { subNav.hideMenu(); return false; },false );
 			$('options-button').parentNode.id = 'subnav-active';
        }
    }    
}



function toggleLocators(){
	//if (document.getElementsByTagName('body').item(0).className == 'homepage'){
		document.getElementsByClassName('toggle-locator').each(function(item) {
		//alert(item.parentNode.parentNode.childNodes[1].id);
		var triggerId = item.hash.slice(1);
		$(triggerId).style.display = "none";
		Event.observe(item, 'click', function(evt) { new Effect.toggle(triggerId, 'blind'); Event.stop(evt); if (item.parentNode.id == "") { item.parentNode.id = "active"; } else { item.parentNode.id = ""; } }, false); } );
	//} 
}



/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 1.1 2006-05-06
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
 
 var Fabtabs = Class.create();


Fabtabs.prototype = {
initialize : function(element) {
		this.element = $(element);
		var options = Object.extend({}, arguments[1] || {});
		this.menu = $A(this.element.getElementsByTagName('a'));
		this.show(this.getInitialTab());
		this.menu.each(this.setupTab.bind(this));
		// next 2 lines added by jamos@campbell-ewald.com to ensure css is not controlling the hidden div - turn js off and the divs are still visible
		this.panels = document.getElementsByClassName('panel');
		this.panels.each(function(option) { option.style.position = 'absolute'; if (option.className.indexOf('active-tab-body') == -1) { option.style.opacity = 0; option.style.top = 0; option.style.left = 0;  } else { option.style.opacity = 1; option.style.top = -999; } });
	},
	setupTab : function(elm) {
	Event.observe(elm,'click',this.activate.bindAsEventListener(this),false);
		// new effects not required here
		//Event.observe(elm,'mouseover',this.shift.bindAsEventListener(this),false);
		//Event.observe(elm,'mouseout',this.shiftback.bindAsEventListener(this),false);	
	},
	activate :  function(ev) {
		var elm = Event.findElement(ev, "a");
		this.show(elm);
		Event.stop(ev);
		this.menu.without(elm).each(this.hide.bind(this));
		// next 2 lines added by jamos@campbell-ewald.com > bookmarkable url
		//newloc = elm.hash.slice(1);
		//location.href = "#"+newloc;

	},
	hide : function(elm) {
		$(elm).removeClassName('active-tab');
		$(this.tabID(elm)).removeClassName('active-tab-body');
		//added by jamos@campbell-ewald.com
		new Effect.Fade($(this.tabID(elm)), { fps:250, duration:0.5, mode: 'absolute' });
	},
	show : function(elm) {
		$(elm).addClassName('active-tab');
		$(this.tabID(elm)).addClassName('active-tab-body');
		
		// next line added by jamos@campbell-ewald.com
		new Effect.Appear($(this.tabID(elm)), { fps:250, duration:0.8, delay:0.3, mode: 'absolute' });
	},
	// next 2 methods added by jamos@campbell-ewald 
	shift : function(ev) {
		var elm = Event.findElement(ev, "a");
		new Effect.Morph(elm, { style: 'margin-left:10px; background-color:#ccc;', fps:250, duration:0.1 });
	},	
	shiftback : function(ev) {
		var elm = Event.findElement(ev, "a");
		new Effect.Morph(elm, { style: 'margin-left:0px; background-color:#eee;', fps:250, duration:0.1 });
	},	
	tabID : function(elm) {
		return elm.href.match(/#(\w.+)/)[1];
	},
	getInitialTab : function() {
		if(document.location.href.match(/#(\w.+)/)) {
			var loc = RegExp.$1;
			var elm = this.menu.find(function(value) { return value.href.match(/#(\w.+)/)[1] == loc; });
			return elm || this.menu.first();
		} else {
			return this.menu.first();
		}
	}
}

function showTabs(){
Element.hide('tab-wrap');
var trigger = $('show-tabs');
Event.observe(trigger,'click',function(evt){ Event.stop(evt); Effect.toggle('tab-wrap','Appear'); },false);
Event.observe($('close-tabs'),'click',function(evt) { Event.stop(evt); Effect.Fade('tab-wrap'); },false);
}

// Name: Blur Anchors
// Language: JavaScript
// Author: Travis Beckham | squidfingers.com
// Description: Remove anchor outlines from all links in the document
// --------------------------------------------------

function blurAnchors(){
	if(document.getElementsByTagName){
		// Fetch links within #header-forms div to limit blurAnchors effect to that area
		var a = $$('div#header-forms a');
//    var a = document.getElementsByTagName("a");
		for(var i = 0; i < a.length; i++){
      	a[i].onfocus = function(){this.blur()};
		}
	}
}


var initLocatorReveal = new domFunction(function() {
	//toggleLocators();
}, { 'global-header' : 'id' } );

var initCareerOptions = new domFunction(function() {
	setSubNav();
}, { 'acdelco-subnav' : 'id'} );	

var initFlashpops = new domFunction(function() {
	flashpops();
}, { 'auto-systems-guide-wrap' : 'id' } );

var initGallery = new domFunction(function() {
	galleryLinks();
}, { 'photo-gallery' : 'id' } );
	
var initSwapLists = new domFunction(function() {
	externalLinks();
	popupLinks();
	blurAnchors();
	if ($('popular')) {
		new Element.hide('popular');
		}
	
}, { 'global-footer' : 'id' } );



var initRacingTabMenu = new domFunction(function() {
		showTabs();
		new Fabtabs('racing-tabs');
	}, { 'racing-tabs' : 'id' } );

var initTabMenu = new domFunction(function() {
		new Fabtabs('tabs');
	}, { 'tabs' : 'id' } );
	

microsurveys = new Array(
	{location:"/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F5F7BDF47"},
	{location:"/parts/filters/air-filters.jsp",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F7A297D2E"},
	{location:"/parts/belts-hoses/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F3F5CD58D"},
	{location:"/racing/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F14C97A80"},
	{location:"/parts/battery/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F58BAECA3"},
	{location:"/service/troubleshooting.jsp",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F37B9E71F"},
	{location:"/about/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F2A366EF0"},
	{location:"/parts/sparkplugs/rapidfire-platinum.jsp",link:"	http://www.gmsurveys2.com/se.ashx?s=7C7FD94F43C7DA33"},
	{location:"/parts/shocks-struts/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F7023CC05"},
	{location:"/news/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F6D5A7932"},
	{location:"/search/SearchServer/wwwtemplates/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F7D5A9855"},
	{location:"/service/",link:"http://www.gmsurveys2.com/se.ashx?s=7C7FD94F3AA4EAF7"}
	)
	
function pupupsurvey()	{
	if(GetCookie("microsurvey") == null ){
		var pageName = location.pathname;
		for (var i = microsurveys.length; i != 0; i--) 	{
			if ((microsurveys[i-1].location == pageName) || (microsurveys[i-1].location + "index.jsp" == pageName )) {			
				newWin(microsurveys[i-1].link,'600','400','no','no','no','yes','yes');
				SetCookie("microsurvey","popped",0.5);
				
			}
	
		}
	}
		
}

var exp_hours = 10000;	
function SetCookie (name, value, hours) {		//if hours = -1 it's a session cookie
	if (hours == null) {
		hours = exp_hours;
	}
	exp = new Date(); 
	exp.setTime(exp.getTime() + (hours*60*60*1000));
	if (hours != -1) {
		expstring = '; expires=' + exp.toGMTString();
	} else {
		expstring = '';
	}
	document.cookie = name + '=' + escape (value) + expstring + '; path=/';
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
		return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}


function openVideo(url){
	window.open(url,'video','menubar=no,toolbar=no,location=no,scrollbars=no,resizable=no,width=300,height=350,top=20,left=50');
}  
//window.onload = pupupsurvey;
