var Shampoo = {
  init: function() {
  	if (arguments.callee.done) return;
  	arguments.callee.done = true;
  	if (Shampoo.khtmltimer) window.clearInterval(Shampoo.khtmltimer);

  	Shampoo.albumarray = Shampoo.$i('albumarray');
  	Shampoo.thumbarray = Shampoo.$i('thumbarray');
  	Shampoo.albums = Shampoo.$i('albums');
  	Shampoo.bod = document.body;
  	if (Shampoo.ie) Shampoo.ieFixes.makeThumbsClickable();
  	if (window.addEventListener) {
    	window.addEventListener('load', Shampoo.thumbFill, false);
    	window.addEventListener('resize', Shampoo.thumbFill, false);
  	}
  	else if (window.attachEvent) {
    	window.attachEvent('onload', Shampoo.thumbFill);
    	window.attachEvent('onresize', Shampoo.thumbFill);
  	}
    Shampoo.animateBox();
    Shampoo.makeSettingsDynamic();
    Shampoo.viewDynamics();

    if (Shampoo.albumarray && typeof Thickbox !== 'undefined') Thickbox.init();
  },

  thumbFill: function() {
    if (!Shampoo.albumarray) return;
    var holders = Shampoo.$t('div', Shampoo.albumarray), thumbs, thumbswidth = 0, i, j, k;
    var holderwidth = holders[0].clientWidth || holders[0].offsetWidth;
    for (i = 0, j = holders.length; i < j; i++) {
      thumbs = Shampoo.$t('a', holders[i]);
      thumbswidth = 0;
      for (k = 0, l = thumbs.length; k < l; k++) {
        thumbswidth += thumbs[k].firstChild.width + 14;
        if (thumbswidth < holderwidth - 10) Shampoo.removeClass(thumbs[k], 'thumbhide');
        else Shampoo.addClass(thumbs[k], 'thumbhide');
      }
    }
  },

  animateBox: function() {
    if (Shampoo.ie6) return;
    var moveBoxText = document.createElement('a');
    moveBoxText.className = 'menumover';
    moveBoxText.appendChild(document.createTextNode(''));
    Shampoo.albums.appendChild(moveBoxText);
    Shampoo.moveBox();
    moveBoxText.onclick = function(e) {
      if (!e) window.event.returnValue = false;
      else e.preventDefault();
      Shampoo.ajax('fixed=' + Shampoo.moveBox(true));
      Shampoo.thumbFill();
    }
  },

  moveBox: function(swap) {
    var fixd = Shampoo.hasClass(Shampoo.bod, 'fixed');
    if (swap) {
      if (fixd) removeClass(Shampoo.bod, 'fixed');
      else addClass(Shampoo.bod, 'fixed');
      fixd = !fixd;
    }
    Shampoo.albums.lastChild.firstChild.nodeValue = fixd ? 'Unstick' : 'Stick';
    Shampoo.albums.lastChild.setAttribute('title', fixd ? 'Maximise space by collapsing this box into a little tab at the top' : 'Fix this box to the page and stop it collapsing into the compact green thing.');
    Shampoo.albums.lastChild.href = fixd ? '#unfix' : '#fix';
    return Number(!!fixd);
  },

  makeSettingsDynamic: function() {
    var bar = Shampoo.$i('bar');
    if (Shampoo.ie6) Shampoo.ieFixes.thumbsPagination(bar);
    if (!bar || Shampoo.ie6) return;
    var sel = Shampoo.$t('select', bar)[0];
    if (sel) {
      var opt = document.createElement('option');
      opt.appendChild(document.createTextNode('Custom ...'));
      opt.value = 'custom';
      if (hasClass(sel, 'custom')) {
        opt.setAttribute('selected', 'selected');
      }
      sel.appendChild(opt);
      if (Shampoo.ie) { // workaround for IE7 rendering bug (when sel changes width due to new opt)
        sel.style.width = '1px';
        sel.style.width = '';
      }
      sel.onchange = function() {
        if (this.value === 'custom') {
          var cpp = Shampoo.$n('customperpage');
          if (cpp.length === 0) {
            var cpp = document.createElement('input');
            cpp.type = 'text';
            cpp.name = 'customperpage';
            cpp.setAttribute('maxlength', '3');
            sel.parentNode.insertBefore(cpp, sel.nextSibling);
          }
          Shampoo.addClass(sel, 'custom');
          try{sel.nextSibling.focus()} catch(e) {}
        }
        else {
          Shampoo.removeClass(sel, 'custom');
        }
      }
    }
  },

  viewDynamics: function() {
    var v = Shampoo.$i('view');
    if (v) {
      var sp = document.createElement('a');
      var remove = 'Remove album info', show = 'Album info', initial, showhref = '#showinfo', hidehref = '#hideinfo';
      if (Shampoo.hasClass(Shampoo.bod, 'h')) {
        initial = show;
        sp.href = showhref;
      }
      else {
        initial = remove;
        sp.href = hidehref;
      }
      sp.appendChild(document.createTextNode(initial));
      sp.id = 'topswitch';
      v.appendChild(sp);
      sp.onmouseover = function() {
        Shampoo.bod.style.backgroundColor = '#FFFFFF';
      }
      sp.onmouseout = function() {Shampoo.bod.style.backgroundColor = ''}
      sp.onclick = function(e) {
        if (!e) window.event.returnValue = false;
        else e.preventDefault();
        if (Shampoo.hasClass(Shampoo.bod, 'h')) {
          Shampoo.removeClass(Shampoo.bod, 'h');
          Shampoo.ajax('topbit=0');
          this.firstChild.nodeValue = remove;
          this.href = hidehref;
        }
        else {
          Shampoo.addClass(Shampoo.bod, 'h');
          Shampoo.ajax('topbit=1');
          this.firstChild.nodeValue = show;
          this.href = showhref;
        }
      }
      var exif = Shampoo.$c('exif')[0];
      if (exif) {
        var a = document.createElement('a'), exifh = Shampoo.$t('h4', exif)[0], loc = window.location.href;
        a.href = loc + '#expand';
        a.appendChild(document.createTextNode('EXIF data'));
        exifh.title = 'Expand/collapse EXIF data for this image';
        exifh.removeChild(exifh.firstChild);
        exifh.appendChild(a);
        Shampoo.addClass(exif, 'collapsed');
        exifh.onclick = function(e) {
          if (!e) window.event.returnValue = false;
          else e.preventDefault();
          if (Shampoo.hasClass(exif, 'collapsed')) {
            Shampoo.removeClass(exif, 'collapsed');
            this.firstChild.href = loc + '#collapse';
          }
          else {
            Shampoo.addClass(exif, 'collapsed');
            this.firstChild.href = loc + '#expand';
          }
        }
      }
    }
  },
  
  ieFixes: {
    makeThumbsClickable: function() {
     	var lis = Shampoo.$t('li', Shampoo.thumbarray);
     	if (lis && lis[0].currentStyle['display'] === 'inline') {
     	  var th = Shampoo.$t('img', Shampoo.thumbarray);
     	  for (var i = 0; i < th.length; i++) {
           th[i].onclick = function() {
       	    window.location = this.parentNode.parentNode.href;
           }
         }
      }
    },
    thumbsPagination: function(bar) {
      if (!bar) return;
      var div = bar.getElementsByTagName('div')[0];
      if (!div) return;
      div.onmouseover = function() {
        Shampoo.addClass(div, 's');
      }
      div.onmouseout = function() {
        Shampoo.removeClass(div, 's');
      }
    }
  },

  ajax: function(data, callback) {
    var req, base = Shampoo.$t('base')[0];
    basebits = base.href.split('/');
    basebits.pop();
    base = basebits.join('/');
    if (window.XMLHttpRequest) req = new XMLHttpRequest();
    else if (window.ActiveXObject) {
      var msxml = ['Msxml2.XMLHTTP', 'Msxml3.XMLHTTP', 'Microsoft.XMLHTTP'];
      for (var i = 0; i < 3; i++) {
        try {req = new ActiveXObject(msxml[i])}
        catch (e) {continue;}
        break;
      }
    }
    req.open('POST', base + '/settings.php', true);
    req.onreadystatechange = function(e) {
      if (callback && req.readyState === 4 && req.status === 200) callback(req.responseText);
    }
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.send(data);
  },

  $i: function(e, p) {
    if (!p) p = document;
    return p.getElementById(e);
  },

  $t: function(e, p) {
    if (p === null) return p;
    if (!p) p = document;
    return p.getElementsByTagName(e);
  },

  $n: function(e) {
    return document.getElementsByName(e);
  },

  $c: function(c, p) {
  	var children = Shampoo.$t('*', p);
  	var els = [];
  	for (var i = 0; i < children.length; i++) {
    	if (Shampoo.hasClass(children[i], c)) els.push(children[i]);
  	}
  	return els;
  },

  hasClass: function(el, c) {
    var hasC = false;
    if (!el || !el.className.length) return;
    var bits = el.className.split(' ');
    for (var j = 0; j < bits.length; j++) {
    	if (bits[j] === c) hasC = true;
    }
    return hasC;
  },

  addClass: function(el, c) {
    if (!el || Shampoo.hasClass(el, c)) return;
    if (el.className.length) el.className += ' '+c;
    else el.className = c;
  },

  removeClass: function(el, c) {
    if (!el || !el.className.length) return;
    el.className = el.className.replace(c, '');
  }
}

// Mozilla/Opera
if (document.addEventListener) document.addEventListener("DOMContentLoaded", Shampoo.init, false);

// IE
/*@cc_on @*/
/*@if (@_win32 || @_win64)
  Shampoo.ie = true;
  @if (@_jscript_version < 5.7) Shampoo.ie6 = true @end;
  document.write('<script id="ie_onload" defer src="javascript:void(0);"></script>');
  document.getElementById('ie_onload').onreadystatechange = function() {
    if (this.readyState == 'complete') Shampoo.init();
  }
/*@end @*/

// Safari
if (/KHTML|Webkit|iCab/i.test(navigator.userAgent)) {
	Shampoo.khtmltimer = window.setInterval(function() {
		if (/loaded|complete/.test(document.readyState)) Shampoo.init();
	}, 10);
}