var ssTotals = {};
ssTotals['about'] = 1;
ssTotals['jobs'] = 1;
ssTotals['news'] = 3;
ssTotals['services'] = 3;
ssTotals['welcome'] = 1;
ssTotals['work-advertising'] = 7;
ssTotals['work-brochures'] = 15;
ssTotals['work-direct-mail'] = 6;
ssTotals['work-logos'] = 27;
ssTotals['work-packaging'] = 2;
ssTotals['work-web-design'] = 10;

var ssAbout, ssWork, ssServices;
var ie6 = false;

function highlightOn(ele, force) {
  if (ie6) return;
  if (!force && ele.hasClassName('selected')) return;
  ele.src = ele.src.replace(/\.png$/, '_h.png'); 
}

function highlightOff(ele, force) {
  if (ie6) return;
  if (!force && ele.hasClassName('selected')) return;
  ele.src = ele.src.replace(/_h\.png$/, '.png'); 
}

function toWorkSection(section) {
  $$('#work-submenu img').each(function(ele) {
    ele.removeClassName('selected');
    highlightOff(ele, true);
  });
  
  highlightOn($(section));
  $(section).addClassName('selected');
  
  ssWork.toSection(section);
}

function boot() {
  
  if (typeof(correctPNG) != 'undefined') { ie6 = true; }
  
  $$('.rollover').each(function(ele) {
    Event.observe(ele, 'mouseover', function() {highlightOn(this);});
    Event.observe(ele, 'mouseout', function() {highlightOff(this);});
  });
  
  $$('#work-submenu .selected').each(function(ele) {
    highlightOn($(ele), true);
  });
  
  var as = document.getElementsByTagName("a"), m;
  for (var i = 0; i < as.length; i++) {
    if (as[i].href && (m = /#(\w+)$/.exec(as[i].href)) && !as[i].onclick) {
      as[i].onclick = new Function("scroll('" + m[1] + "'); return false;");
    }
  }
 
  as = document.getElementById('menu').getElementsByTagName('img');
  for (var i = 0; i < as.length; i++) {
    var bits = as[i].src.split('.');
    bits[bits.length-2] += '_h';
    as[i].onmouseover = new Function("this.src = '" + bits.join('.') + "';");
    as[i].onmouseout = new Function("this.src = '" + as[i].src + "';");
  }

  

  ssAbout = new Slideshow('about-container', 'about');
  ssWork = new Slideshow('work-container', 'work-logos');
  ssServices = new Slideshow('services-container', 'services');
  ssNews = new Slideshow('news-container', 'news');
  ssJobs = new Slideshow('jobs-container', 'jobs');

}

function scroll(id) { new Scroller(id); }

function submitContact() {
	var fields = [ "name_text", "email_text", "message_text" ], i = 0;
	for (i = 0; i < fields.length; i++) {
		if ($(fields[i]).value.length == 0) {
			$('send-notification').innerHTML = "Please complete all fields";
			$('send-notification').style.display = 'block';
			return;
		}
	}
	new Ajax.Request(
		'contact.php',
		{ parameters: Form.serialize($('contact_form'), true),
		  onSuccess: function() {
			  new Blip('blip');
				$('send-notification').innerHTML = "Thank you, your message has been sent";
				$('send-notification').style.display = 'block';
			  for (i = 0; i < fields.length; i++) {
				  Form.Element.clear($(fields[i]));
			  }
			}
		}
	);
}

function Slideshow(id, section) {
	var container = $(id);
	this.main = document.createElement("div");
	this.main.className = "item-1";
	this.main.innerHTML = container.innerHTML;
	this.off = document.createElement("div");
	this.off.className = "item-2";
	container.innerHTML = "";
	container.appendChild(this.main);
	container.appendChild(this.off);
	this.width = this.main.offsetWidth;
	this.height = this.main.offsetHeight;
	this.setSection(section);
	this.busy = false;
}

Slideshow.prototype.setSection = function(section) {
  this.section = section;
  this.root = 'page.php?section=' + section + '&page=';
  this.curr = 1;
  this.size = ssTotals[section] || 0;
}

Slideshow.prototype.toSection = function(section) {
  if (this.busy || section == this.section) return;
  this.setSection(section);
  this.curr = 0;
  this.next();
}

Slideshow.prototype.prev = function() {
	if (this.busy) return;
	this.curr -= 1;
	if (this.curr < 1) this.curr = this.size;
	this.start = -this.width;
	this.fire();
}
	
Slideshow.prototype.next = function() {
	if (this.busy) return;
	this.curr += 1;
	if (this.curr > this.size) this.curr = 1;
	this.start = this.width;
	this.fire();
}

Slideshow.prototype.fire = function() {
	this.busy = true;
	this.off.style.left = "" + this.start + "px";
	this.off.style.display = 'block';
	this.progress = 0;
	me = this;
	new Ajax.Updater(this.off, this.root + this.curr, {onComplete: function() {
		me.timer = window.setInterval(function(){me.tick();}, 40);
	}});
}

Slideshow.prototype.tick = function() {
	this.progress += 0.05;
	if (this.progress > 1) this.progress = 1;
	var progress = this.progress * this.progress;
	this.off.style.left = "" + (this.start + (progress * -this.start)) + "px";
	this.main.style.left = "" + (progress * -this.start) + "px";
	if (this.progress >= 1) {
		var tmp = this.main;
		this.main = this.off;
		this.off = tmp;
		this.off.style.display = 'none';
		window.clearInterval(this.timer);
		this.busy = false;
	}
}

function Scroller(id, options) {
  var me = this;
  this.start = this.getCurrentScrollPos();
  this.end = this.getElementPos(id);
  this.end[1] = Math.max(0, this.end[1] - 100);
  this.delta = [this.end[0]-this.start[0], this.end[1]-this.start[1]];
  this.progress = 0;
  this.timer = window.setInterval(function(){me.tick();}, 20);
}

Scroller.prototype.tick = function() {
  this.progress += 0.02;
  var progress = this.ease(this.progress);
  if (this.progress > 1) this.progress = 1;
  window.scrollTo(this.start[0] + this.delta[0] * progress,
                  this.start[1] + this.delta[1] * progress);
  if (this.progress >= 1) window.clearInterval(this.timer);
}

Scroller.prototype.getElementPos = function(id) {
  var ele = document.getElementById(id);
  var out = [ ele.offsetLeft, ele.offsetTop];
  while (ele = ele.offsetParent) {
    out[0] += ele.offsetLeft;
    out[1] += ele.offsetTop;
  }
  return out;
}

Scroller.prototype.getCurrentScrollPos = function() {
	return [window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0,
				  window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0];
}

Scroller.prototype.ease = function(c) {
  if (c < 1/2.75) {
    return (7.5625*c*c);
  } else if (c < 2/2.75) {
    return (7.5625*(c-=(1.5/2.75))*c+.75);
  } else if (c < (2.5/2.75)) {
    return (7.5625*(c-=(2.25/2.75))*c + 0.9375);
  } else {
    return (7.5625*(c-=(2.625/2.75))*c + 0.984375);
  }
}

function Blip(id) {
	var me = this;
	this.element = document.getElementById(id);
	this.progress = 0.7;
	this.timer = window.setInterval(function(){me.tick();}, 40);
}

Blip.prototype.tick = function() {
	this.progress -= 0.05;
	if (this.progress < 0) this.progress = 0;
	this.element.style.opacity = this.progress;
	this.element.style.filter = "alpha(opacity=" + (this.progress * 10) + ")";
	if (this.progress == 0) { window.clearInterval(this.timer); }
}
