var miniSlideshow_images = new Array();
var miniSlideshow_captions = new Array();

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;
			};
		}
	};
}();

function getElementsByClass(searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}

function trim(str) {
  return str.replace(/^\s*|\s*$/g,"");
}

function toggleMenuItem(elem, b) {
  var x;
  var menus = getElementsByClass('secondary-menu', elem, 'ul');
  if ( menus ) {
    menus[0].style.display = (b == 1) ? 'block' : 'none';
  }
}

function toggleMenuItemOn(elem) {
  toggleMenuItem(elem, 1);
}

function toggleMenuItemOff(elem) {
  toggleMenuItem(elem, 0);
}

function silkscreenify(element) {
  var i, x;
  for ( i = 0 ; i < element.childNodes.length ; i++ ) {
    switch ( element.childNodes.item(i).nodeType ) {
      case 3:
        var span = document.createElement('span');
        for ( x = 0 ; x < element.childNodes.item(i).nodeValue.length ; x++ ) {
          if ( element.childNodes.item(i).nodeValue.substr(x,1).toLowerCase().match('[a-z\-\&]{1}') ) {
            var c = element.childNodes.item(i).nodeValue.substr(x,1).toLowerCase();
            switch ( c ) {
              case '-':
                c = 'Hyphen';
                break;
              case '&':
                c = 'Amp';
                break;
            }
            var img = document.createElement('img');
            img.setAttribute('src', '/' + 'silkscreen/px/' + ((element.getAttribute('title').indexOf(" :: ") == -1) ? 'large/' : '') + c + '.gif');
            img.setAttribute('alt', c);
            img.style.marginRight = '2px';
            span.appendChild(img);
          }
          else if ( (element.childNodes.item(i).nodeValue.substr(x,1) == ' ') && span.lastChild ) {
          span.lastChild.style.marginRight = '7px';
          }
        }
        element.replaceChild(span, element.childNodes.item(i));
        break;
      case 1:
        silkscreenify(element.childNodes.item(i));
        break;
    }
  }
}

function miniSlideshow_clicked(id, e) {
  var img = document.getElementById('miniSlideshow_img_' + id);
  var cap = document.getElementById('miniSlideshow_caption_' + id);
  var x;
  for ( x = 0 ; x < miniSlideshow_images[id].length ; x++ ) {
    if ( img.src.indexOf(miniSlideshow_images[id][x]) != -1 ) { // UGLY HACK
      break;
    }
  }

  switch ( e ) {
    case -2:
	  if ( document.all ) {
        var newImg = new Image();
        newImg.src = miniSlideshow_images[id][0];
        img.height = newImg.height;
      }
      img.src = miniSlideshow_images[id][0];
      cap.replaceChild(document.createTextNode(miniSlideshow_captions[id][0]), cap.childNodes[0]);
      break;
    case -1:
      if ( x > 0 ) {
		if ( document.all ) {
          var newImg = new Image();
          newImg.src = miniSlideshow_images[id][x - 1];
          img.height = newImg.height;
		}
        img.src = miniSlideshow_images[id][x - 1];
        cap.replaceChild(document.createTextNode(miniSlideshow_captions[id][x - 1]), cap.childNodes[0]);
      }
      break;
    case 1:
      if ( x < (miniSlideshow_images[id].length - 1) ) {
		if ( document.all ) {
          var newImg = new Image();
          newImg.src = miniSlideshow_images[id][x + 1];
          img.height = newImg.height;
		}
		img.src = miniSlideshow_images[id][x + 1];
        cap.replaceChild(document.createTextNode(miniSlideshow_captions[id][x + 1]), cap.childNodes[0]);
      }
      break;
    case 2:
	  if ( document.all ) {
        var newImg = new Image();
        newImg.src = miniSlideshow_images[id][ miniSlideshow_images[id].length - 1 ];
        img.height = newImg.height;
      }
      img.src = miniSlideshow_images[id][ miniSlideshow_images[id].length - 1 ];
      cap.replaceChild(document.createTextNode(miniSlideshow_captions[id][ miniSlideshow_captions[id].length - 1 ]), cap.childNodes[0]);
      break;
  }
}

window.onload = function () {
  /* Parameters GODDAMN INTERNET EXPLORER. BITCH-ASS, MOTHERFUCKING PIECE OF FUCKING SHIT! */
  var control_prefix = '/img/controls/';

  function css_container(style) {
    style.backgroundColor = '#aaa';
    style.width = '250px';
    style.textAlign = 'center';
    style.cssFloat = style.styleFloat = 'right';
    style.display = 'block';
  }

  function css_controls(style) {
    style.height = '15px';
	style.width = '250px';
    style.padding = '0';
    style.textAlign = 'center';
  }

  function css_control(style) {
    style.borderStyle = 'outset';
    style.borderWidth = '2px'
    style.width = '58px';
    style.verticalAlign = 'middle';
    style.textAlign = 'center';
    style.display = 'block';
    style.cssFloat = style.styleFloat = 'left';
  }

  function css_caption(style) {
    style.fontSize = '70%';
	style.width = '250px';
  }
  
  function css_cont_img(style) {
  }

  function css_image(style) {
  }

  // Find all elements with the class name
  var i, id, entireDocument = document.getElementsByTagName("*");

  for ( i = id = 0 ; i < entireDocument.length ; i++ ) {
    if ( entireDocument[i].className.match(/ ?miniSlideshow ?/) ) {
      var slideshow = entireDocument[i];

      // Store an images in a global array so we can access them later.
      miniSlideshow_images.push(new Array());
      miniSlideshow_captions.push(new Array());
      var x;
      for ( x = 0 ; x < slideshow.childNodes.length ; x++ ) {
        if ( slideshow.childNodes[x].tagName && slideshow.childNodes[x].tagName.toLowerCase() == 'li' ) {
          var y;
          for ( y = 0 ; y < slideshow.childNodes[x].childNodes.length ; y++ ) {
            if ( slideshow.childNodes[x].childNodes[y].nodeType == 1 ) { // IE is strange...
              miniSlideshow_images[id].push(slideshow.childNodes[x].childNodes[y].src);
              miniSlideshow_captions[id].push(slideshow.childNodes[x].childNodes[y].alt);
            }
          }
        }
      }

      var ssElem   = document.createElement('div');
        css_container(ssElem.style);

      var img      = document.createElement('img');
        img.setAttribute('src', miniSlideshow_images[id][0]);
        css_image(img.style);
        img.setAttribute('id', 'miniSlideshow_img_' + id);
      ssElem.appendChild(img);

      // Create caption box
      var caption = document.createElement('div');
        css_caption(caption.style);
        caption.setAttribute('id', 'miniSlideshow_caption_' + id);
        caption.appendChild(document.createTextNode(miniSlideshow_captions[id][0]));
        ssElem.appendChild(caption);

      // Create controls
      var controls = document.createElement('div');
        controls.setAttribute('id', 'miniSlideshow_set_' + id);
		css_controls(controls.style);
        var fback_cont    = document.createElement('div');
          var fback_img   = document.createElement('img');
          fback_img.setAttribute('src', control_prefix + 'fast_back.gif');
          fback_cont.appendChild(fback_img);
          css_cont_img(fback_img.style);
        css_control(fback_cont.style);
        fback_cont.setAttribute('onmouseover', 'this.style.borderStyle="inset";');
        fback_cont.setAttribute('onmouseout', 'this.style.borderStyle="outset"');
        fback_cont.setAttribute('onmousedown', 'this.style.backgroundColor="#666"');
        fback_cont.setAttribute('onmouseup', 'this.style.backgroundColor="transparent"');
        fback_cont.setAttribute('onclick', 'miniSlideshow_clicked(' + id + ', -2);');
        controls.appendChild(fback_cont);
        var prev_cont     = document.createElement('div');
          var prev_img   = document.createElement('img');
          prev_img.setAttribute('src', control_prefix + 'previous.gif');
          prev_cont.appendChild(prev_img);
          css_cont_img(prev_img.style);
        css_control(prev_cont.style);
        prev_cont.setAttribute('onmouseover', 'this.style.borderStyle="inset"');
        prev_cont.setAttribute('onmouseout', 'this.style.borderStyle="outset"');
        prev_cont.setAttribute('onmousedown', 'this.style.backgroundColor="#666"');
        prev_cont.setAttribute('onmouseup', 'this.style.backgroundColor="transparent"');
        prev_cont.setAttribute('onclick', 'miniSlideshow_clicked(' + id + ', -1);');
        controls.appendChild(prev_cont);
        var next_cont     = document.createElement('div');
          var next_img   = document.createElement('img');
          next_img.setAttribute('src', control_prefix + 'next.gif');
          next_cont.appendChild(next_img);
          css_cont_img(next_img.style);
        css_control(next_cont.style);
        next_cont.setAttribute('onmouseover', 'this.style.borderStyle="inset"');
        next_cont.setAttribute('onmouseout', 'this.style.borderStyle="outset"');
        next_cont.setAttribute('onmousedown', 'this.style.backgroundColor="#666"');
        next_cont.setAttribute('onmouseup', 'this.style.backgroundColor="transparent"');
        next_cont.setAttribute('onclick', 'miniSlideshow_clicked(' + id + ', 1);');
        controls.appendChild(next_cont);
      ssElem.appendChild(controls);
        var fforward_cont = document.createElement('div');
          var fforward_img   = document.createElement('img');
          fforward_img.setAttribute('src', control_prefix + 'fast_forward.gif');
          fforward_cont.appendChild(fforward_img);
          css_cont_img(fforward_img.style);
        css_control(fforward_cont.style);
        fforward_cont.setAttribute('onmouseover', 'this.style.borderStyle="inset"');
        fforward_cont.setAttribute('onmouseout', 'this.style.borderStyle="outset"');
        fforward_cont.setAttribute('onmousedown', 'this.style.backgroundColor="#666"');
        fforward_cont.setAttribute('onmouseup', 'this.style.backgroundColor="transparent"');
        fforward_cont.setAttribute('onclick', 'miniSlideshow_clicked(' + id + ', 2);');
        controls.appendChild(fforward_cont);

      var par = slideshow.parentNode;
      par.replaceChild(ssElem, slideshow);

      id++;
    }
  }

  // IE
  if ( document.all ) {
    // Fix IE's stupidity re: unknown elements (ie, abbr) in the html namespace
    document.documentElement.setAttribute("xmlns:acc", "http://www.w3.org/1999/xhtml");
    document.body.innerHTML = document.body.innerHTML.replace(/<abbr/gi, "<acc:abbr").replace(/<\/abbr/gi, "</acc:abbr");

    if ( !window.XMLHttpRequest ) { // IE < 7
      var menus = getElementsByClass("secondary-menu");
      for ( i = 0 ; i < menus.length ; i++ ) {
      }
    }
  }

  silkscreenify(document.getElementById('menu'));
}
