// JavaScript Document for www.freedomkart.com
// designed and coded by www.Local-PC-Guy.com

var fk = {

	init : function () {
		if (!document.getElementById) {return;}
		fk.navMenuinit();
		fk.mainImageSize();
		fk.addEvent(window,'resize',fk.mainImageSize);
		fk.addCaption('imagewrapl');
		fk.addCaption('imagewrapr');
		fk.offSiteLinks();
	},

	offSiteLinks : function () {
		var osls = fk.getElementsByClass('offsitelink');
		var oslsimg = fk.getElementsByClass('offsitelinkimg');
		oslslen = osls.length;
		oslsimglen = oslsimg.length;
		for (var i=0; i<oslslen; i++) {
			osls[i].target = '_blank';
			osls[i].title = 'This link will open in a new window';
		}
		for (var j=0; j<oslsimglen; j++) {
			oslsimg[j].target = '_blank';
			oslsimg[j].title = 'This image link will open in a new window';
		}
	},
	
	mainImageSize : function () {
		var body = document.getElementsByTagName('body')[0];
		if (body.className != 'home') {return;}
		var content = document.getElementById('content');
		var mainImage = document.getElementById('racewinner');
		var mainImageDiv = document.getElementById('icimagewrapl0');
		miwidth = mainImage.width;
		if(content.offsetWidth > 1001) {
    	mainImage.width = 500;
			mainImage.height = 350;
			if(mainImageDiv) {mainImageDiv.style.width = '500px';}
		}
		if((content.offsetWidth < 1002) && (content.offsetWidth > 952)) {
			mainImage.width = 450;
			mainImage.height = 315;
			if(mainImageDiv) {mainImageDiv.style.width = '450px';}
		}
		if(content.offsetWidth <= 952 && (content.offsetWidth > 902)) {
			mainImage.width = 400;
			mainImage.height = 280;
			if(mainImageDiv) {mainImageDiv.style.width = '400px';}
		}
		if(content.offsetWidth <= 902 && (content.offsetWidth > 852)) {
			mainImage.width = 350;
			mainImage.height = 245;
			if(mainImageDiv) {mainImageDiv.style.width = '350px';}
		}
		if(content.offsetWidth <= 852 && (content.offsetWidth > 802)) {
			mainImage.width = 300;
			mainImage.height = 210;
			if(mainImageDiv) {mainImageDiv.style.width = '300px';}
		}
		if(content.offsetWidth <= 802) {
			mainImage.width = 250;
			mainImage.height = 175;
			if(mainImageDiv) {mainImageDiv.style.width = '250px';}
		}
		
	},
	
	addCaption : function(xClass) {
		var allImages = fk.getElementsByClass(xClass, document, 'img');
		for (var i=0; i < allImages.length; i++) {
			var imageCaption = document.createTextNode(allImages[i].title);
			var imageContainer = document.createElement("div");
			var imagePara = document.createElement("p");
			var imageWidth = allImages[i].getAttribute("width");
			var spareSpan = document.createElement("span");
			imagePara.appendChild(imageCaption);
			allImages[i].parentNode.insertBefore(imageContainer, allImages[i]);
			imageContainer.appendChild(allImages[i]);
			if (allImages[i].title != "" ) {
				imageContainer.appendChild(imagePara);
			}
			imageContainer.appendChild(spareSpan);
			imageContainer.className = xClass;
			imageContainer.id = 'ic' + xClass + i;
			spareSpan.className = "spareSpan";
			allImages[i].className = "img";
			imageContainer.style.width = imageWidth + "px";
		}
	},

	openMenus : [],   // array for menu levels currently displayed
  pressedNav : [],  // array tracking which item is pressed at each level
  timer : null,     // timer for when to close menus

  // array of id values for submenus, which correspond to the link
  // sequence in the whole navigation sequence
  subMenus : ['','sub_sched','','sub_points','sub_drivers'],

  navMenuinit : function() {

    if (!document.getElementById || !document.getElementsByTagName) { return; }
    var navHolder = document.getElementById('nav');
    var navItems = navHolder.getElementsByTagName('a');
    var lists = navHolder.getElementsByTagName('ul');

    // disabling the visibility of the sub menus
    for (var i=1, allLists = lists.length; i<allLists; i++) {
        lists[i].style.visibility = 'hidden';
        lists[i].style.position = 'absolute';
    }

    // adding a number property to each nav link, to synchronize
    // them with the subMenus[] array; adding event handlers
    for (var i=0, allNavItems = navItems.length; i<allNavItems; i++) {
        navItems[i].number = i;
        fk.addEvent(navItems[i],'mouseover', fk.display);
        fk.addEvent(navItems[i],'focus', fk.display);
        fk.addEvent(navItems[i],'mouseout', fk.setTimer);
        fk.addEvent(navItems[i],'blur', fk.setTimer);
    }
  },

  display : function() {

     // a new link has been moused over or given focus, so erase the countdown
     if (fk.timer) { clearTimeout(fk.timer); }

     var menuLvl, menuToShow, num = this.number;

     // Determine menu level
     if (num <= 9) { menuLvl = 1; }
     if (num > 9) { menuLvl = 2; }

     // stop if the user is mousing over or tabbing to the same item again
     if (fk.openMenus[menuLvl] && fk.openMenus[menuLvl] == fk.subMenus[num]) { return; }

     // shut off any other submenus
     if (fk.openMenus[menuLvl]) { fk.closeAllMenus(menuLvl); }

     // if there is no item in subMenus[] at that position, then do nothing
     // if there is an item in subMenus[] at that position, change the indicated
     // sub menu's visibility
     if (!fk.subMenus[num]) {}
     else {
        menuToShow = document.getElementById(fk.subMenus[num]).style;
        menuToShow.visibility = 'visible';
     }

     // assign that open menu to the openMenus[] array, with the position
     // in the array the same as the menu level (1 or 2)
     fk.openMenus[menuLvl] = fk.subMenus[num];

     // alter visual display if there is no class assigned to that item
     // if there is already a class assigned, then we assume it is "over" and do nothing
     if (this.className) { return; }
     this.className = 'over';

     // replace whatever item is in the pressedNav[] array with the one just activated
     // remove the class from that previous item
     if (fk.pressedNav[menuLvl]) { fk.pressedNav[menuLvl].className = ''; }
     fk.pressedNav[menuLvl] = this;

   },

   // setting a 5 second timer
   setTimer : function() {
     if (fk.timer) { clearTimeout(fk.timer); }
     fk.timer = setTimeout('fk.closeAllMenus(1)',1300);
   },

   // shutting down all menus, wiping all "over" classes and emptying out all
   // the items in openMenus[] and pressedNav[]
   closeAllMenus : function(lvl) {

     for (var i=fk.openMenus.length - 1; i>=lvl; i--) {
        if (fk.openMenus[i]) {
            var menuToHide = document.getElementById(fk.openMenus[i]).style;
            menuToHide.visibility = 'hidden';
        }
        fk.openMenus[i] = null;
        if (fk.pressedNav[i]) {
            fk.pressedNav[i].className = '';
            fk.pressedNav[i] = null;
        }
     }
  },
  
	getElementsByClass : function (searchClass,node,tag) {
    // this array will hold the nodes that have the desired class
    var classElements = [];
    // if we did not pass the node parameter, assume document
    if (node == null) {node = document;}
    // if we did not pass the tag parameter, grab every node
    if (tag == null) {tag = '*';}
    // gather all the element nodes to look through; by default is everything in document
    var els = node.getElementsByTagName(tag);
    // to improve loop performance, determine the length ahead of time
    var elsLen = els.length;
    // establish the pattern to search for within className
    var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
    // look through all the class properties to see if there is a match
    // the j variable is the counter variable that increments each time a
    // match is found and becomes the next item in classElements
    for (var i = 0, j = 0; i < elsLen; i++) {
        if (pattern.test(els[i].className)) {
           classElements[j] = els[i];
           j++;
        }
    }
    // send back the array of elements to whatever variable called this function
    return classElements;
	},
	
	// associate nodes with events and functions
  addEvent : function(obj, type, func) {
       // W3C approach for non-IE modern browsers
			 if (obj.addEventListener) {obj.addEventListener(type, func, false);}
       // Windows IE approach
       else if (obj.attachEvent) {
           obj["e" + type + func] = func;
           obj[type + func] = function() {obj["e" + type + func] (window.event);}
					 obj.attachEvent("on" + type, obj[type + func]);
       }
			 // browsers supporting neither W3C or IE methods
       else {obj["on" + type] = func;}
   }

}
fk.init();
