/**
 *	js/home.js			$id$
 *
 *	O'Shea-Getz homepage animations script
 *
 */
var oh = {

	patents: [],
	patent_index: 0,
	patent_speed: 2500,

	blurbs: [],
	blurb_index: 0,
	blurb_timer: null,
	blurb_busy: false,
	blurb_do_loop: false,

	init_blurb: function() {
		$(".blurb").each(function(index,elem) {
			oh.blurbs.push(elem);
		});
		$(".blurb .content:not(:first-child)").hide();

		$("#next-blurb").click(function(e) {
			if (!oh.blurb_busy) {
				oh.next_blurb();
				if (this.blurb_do_loop) {
				  oh.start_blurb_timer();
				}
			}
		});

		$("#prev-blurb").click(function(e) {
			if (!oh.blurb_busy) { oh.prev_blurb(); }
		}).hide();

		this.start_blurb_timer();
	},

	start_blurb_timer: function() {
		if (this.blurb_timer) clearInterval(this.blurb_timer);
		this.blurb_timer = setInterval("oh.next_blurb()", 6000);
	},

  stop_blurb_timer: function() {
    if (this.blurb_timer) {
	    clearInterval(this.blurb_timer);
	    this.blurb_timer = null;
	  }
  },

	next_blurb: function() { this.go_blurb(1); },
	prev_blurb: function() { this.go_blurb(-1); },

	go_blurb: function(dir) {
		this.blurb_busy = true;
		var len = this.blurbs.length;
		var cb = this.blurb_index;
		var nb = (cb + dir + len) % len;
		this.blurb_index = nb;
		if (!this.blurb_do_loop && dir == 1 && nb == len - 1) {
		  this.stop_blurb_timer();
		}
		var elem1 = this.blurbs[cb];
		var elem2 = this.blurbs[nb];
		$(elem2).css({left:400*dir,top:40}).show();
		$(elem1).animate({left:-400*dir}, 400, 'linear',
			function() {
				$(this).hide();
				oh.blurb_busy = false;
				if (dir == 1) {
			    $("#prev-blurb").show();
				  if (nb == len-1) {
				    $("#next-blurb").hide();
				  }
				}
				else {
				  $("#next-blurb").show();
				  if (nb == 0) {
				    $("#prev-blurb").hide();
				  }
				}
			}
		);
		$(elem2).animate({left:0}, 400, 'linear');
	},

	init_patent: function() {
		$("#patent-list li").each(function(index,elem) {
			oh.patents.push($(elem).text());
		});
		this.next_patent();
		setInterval("oh.next_patent()", this.patent_speed);
	},

	set_patent_number: function(val) {
		for (var index = 0; index < 7; index++) {
			digit = val % 10;
			val = Math.floor(val / 10);
			$("#digit"+index).animate({backgroundPosition: "(0 "+(-30*digit)+"px)"}, 500, 'linear');
		}
	},

	next_patent: function() {
		var pn = this.patent_index;
		this.set_patent_number(this.patents[pn]);
		this.patent_index = (pn + 1) % this.patents.length;
	}

};

// Document Ready
$(function() {
	oh.init_blurb();
	oh.init_patent();
});

