//
//  All this be by Jonathon Bellew for a Something Splendid gig.
//  Except for get_style, ripped lovingly from Marcel Laverdet and Facebook, inc.
//  See js/animation.js for full legals.
//  Let's party! jonno@somethingsplendid.com
//

var proc = '';
var origTop = '';
var origLeft = '';
var interval = 150;
var loaded = false;
var textscroll = 3;
var runscroll = true;
var loadedcount = 0;
var artistsVisible = false;
var artistsVisibleBottom = false;
var body_toggle = false;

// Preloader magicals!

function checkloaded() {
  if (loadedcount >= imagecount) {
    setTimeout('kickoff()', 1000);
  }
  else {
    for (i=0;i<imagecount;i++) {
      if (imagesLoaded[i] == false && images[i].complete) {
        imagesLoaded[i] = true;
        loadedcount++;
      }
    }
    setTimeout('checkloaded()', 10);
  }
}

// Show + hide body parts

function truth_toggle(id) {
  if (id != body_toggle) {
    new_el = document.getElementById(id);
    Animation(new_el).to('height', 'auto').from('0px').ease(Animation.ease.both).show().go();
    if (body_toggle) {
      old_el = document.getElementById(body_toggle);
      Animation(old_el).to('height', '0px').ease(Animation.ease.both).hide().go();
    }
    body_toggle = id;
  }
  else {
    body_toggle = false;
    old_el = document.getElementById(id);
    Animation(old_el).to('height', '0px').ease(Animation.ease.both).hide().go();
  }
  return false;
}

// Artist tooth

function toggleArtists() {
  stopShake('tooth-3');
  if (artistsVisible) {
    Animation(document.getElementById('artist-tooth')).to('height', '0px').from('150px').hide().go();    
    artistsVisible = false;
    startShake('tooth-3');
    stopShake('tooth-3');
  }
  else {
    Animation(document.getElementById('artist-tooth')).to('height', '150px').from('0px').show().go();
    artistsVisible = true;
  }
  return false;
}

// Ran out of time, old copy+pasta trick for other tooth. Yep, ashamed.

function toggleArtistsBottom() {
  stopShake('tooth-b3');
  if (artistsVisibleBottom) {
    Animation(document.getElementById('artist-tooth-bottom')).to('height', '0px').from('150px').hide().go();    
    artistsVisibleBottom = false;
    startShake('tooth-b3');
    stopShake('tooth-b3');
  }
  else {
    Animation(document.getElementById('artist-tooth-bottom')).to('height', '150px').from('0px').show().go();
    artistsVisibleBottom = true;
  }
  return false;
}



// Animation Sequence

function kickoff() {
  runscroll = false;
  Animation(document.getElementById('loader')).to('height','0').duration(800).ease(Animation.ease.both).hide().go();
  setTimeout('showUvula()',810);
}

function showUvula() {
  Animation(document.getElementById('uvula')).to('height','185px').duration(1000).ease(Animation.ease.both).go();
  setTimeout('showTongue()', 1500)
}

function showTongue() {
  Animation(document.getElementById('tongue')).to('height', '240px').duration(800).ease(Animation.ease.both).go();
  Animation(document.getElementById('bottom-lip')).to('width', '895px').duration(1).go();
  Animation(document.getElementById('bottom-lip')).to('width', '896px').duration(1).go();
  setTimeout('showTeeth()', 1200);
}

function showTeeth() {
  for (i=1;i<7;i++) {
    Animation(document.getElementById('tooth-' + i).getElementsByTagName('a')[0]).to('opacity', '1').from('0').duration(1000).show().go();
    Animation(document.getElementById('tooth-b' + i).getElementsByTagName('a')[0]).to('opacity', '1').from('0').duration(1000).show().go();
  }
  Animation(document.getElementById('spacer')).to('height', '20px').duration(1000).go();
  loaded = true;
  showContent();
}

function showContent() {
  Animation(document.getElementById('cavity')).to('height', 'auto').duration(1000).go();
  kickstart_zine();
}

function kickstart_zine() {
  loaded = true;
  el = document.getElementById('imageViewer');
  if (el) {
    setTimeout('init_zine()', 100);
    prepareViewer(el, 'tiles', 256);
    hookEvent('imageViewer', 'mousewheel', scrollHandle); 
  }
}

function init_zine() {
  el = document.getElementById('imageViewer');
  el.style.visibility = 'visible';
}

// Load text scroller

function loadtext() {
  if (runscroll) {
    if (textscroll == 4) {
      textscroll = 0;
    }
    text = 'Loading ';
    for (i=0;i<textscroll;i++) {
      text += '.';
    }
    document.getElementById('load-scroll').innerHTML = text;
    textscroll++;
    setTimeout('loadtext()', 500);
  }
}


// Closing up shop

function closeMouth(obj, id) {
  href = obj.href;
  Animation(document.getElementById('tongue')).to('height', '0').duration(800).ease(Animation.ease.both).hide().go();
  Animation(document.getElementById('uvula')).to('height','0').duration(800).ease(Animation.ease.both).hide().go();
  Animation(document.getElementById('cavity')).to('height', '0').duration(800).go();
  Animation(document.getElementById('spacer')).to('height', '338px').duration(800).go();
  for (i=1;i<7;i++) {
    tooth = 'tooth-' + i;
    if (tooth!=id) {
      Animation(document.getElementById('tooth-' + i).getElementsByTagName('a')[0]).to('opacity', '0').from('1').duration(800).show().go();
    }
    tooth = 'tooth-b' + i;
    if (tooth!=id) {
      Animation(document.getElementById('tooth-b' + i).getElementsByTagName('a')[0]).to('opacity', '0').from('1').duration(800).show().go();
    }
  }
  setTimeout('redirect(href)', 1200);
  return false;
}

function redirect(href) {
  window.location = href;
}


// Teeth shakers

function startShake(id) {
  if ((id == 'tooth-3' && artistsVisible) || (id == 'tooth-b3' && artistsVisible)) {
    return;
  }
  if (loaded) {
    el = document.getElementById(id);
    origTop = parseInt(get_style(el, 'top'), 10);
    origLeft = parseInt(get_style(el, 'left'), 10);
    shake(id);
  }
}

function shake(id) {
  if (loaded) {
    el = document.getElementById(id);
    randTop = (Math.random() * 4);
    randLeft = (Math.random() * 2) - 1;
    if (id.indexOf('b')!=-1) {
      randTop = randTop * -1;
    }
    el.style.top = (origTop + randTop) + 'px';
    el.style.left = (origLeft + randLeft) + 'px';
    proc = setTimeout(function() {shake(id);}, interval);
  }
}

function stopShake(id) {
  if (id == 'tooth-3' && artistsVisible) {
    return;
  }
  if (loaded) {
    clearTimeout(proc);
    el = document.getElementById(id); 
    el.style.top = (origTop) + 'px';
    el.style.left = (origLeft) + 'px';  
  }
} 

// Pop pop pop pop pop up

function pop() {
	newwindow = window.open('signup.php', 'name', 'height=670,width=407');
	return false;
}

function friendSend(destination) {
	newwindow = window.open(destination, 'name', 'height=670,width=407,scrollbars=yes');
	return false;
}

// Tools, stools, rules, Bruhls.

function get_style(obj, prop) {
  var temp;
  return (window.getComputedStyle && window.getComputedStyle(obj, null).getPropertyValue(prop.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase() }))) ||
         (document.defaultView && document.defaultView.getComputedStyle && (temp = document.defaultView.getComputedStyle(obj, null)) && temp.getPropertyValue(prop.replace(/[A-Z]/g, function(match) { return '-' + match.toLowerCase() }))) ||
         (obj.currentStyle && obj.currentStyle[prop]) ||
         obj.style[prop];
}