/*var SubNavigation = Class.create();
SubNavigation.prototype = {
*/	
var SubNavigation = {
	/* public var */
	id:'subnav',
	current:0,
	open:true,
	
	/* constructor 
	 * @context - the id of container
	**/
	initialize:function(id, current, context, _class){
		
		if (id!=undefined) this.id = id;
		if (current!=undefined) this.current = current;
		
		// grab all the grouped items inside 
		if (context!=undefined)
			this.es = document.getElementsByClassName(_class, $(context));
		else
			this.es = document.getElementsByClassName(_class);
		
		var i = 0;
		for (var i = 0; i < this.es.length; i++) {
			$(this.es[i].readAttribute("id")).setAttribute("onmouseover", "SubNavigation.SetAsCurrent("+i+")");
		}
			//var id = this.es[each].id;
			//this.es[each].setAttribute("id", "group"+(i++));
			//this.es[each].setAttribute("onmouseover", "SubNavigation.SetAsCurrent("+i+")");
		
//			this.es[each].onmouseover = this.SetCurrent.bindAsEventListener(this);
//			Event.observe('signinForm', 'submit', Checks.generic.bindAsEventListener(Checks));			
		
	},
	
	/* @dir = plus 1 or -1 
	**/
	Move:function(dir){
		
		//update the state
		this.current+=dir;
		
		// make sure the index is w/in range
		if(this.current < 0) this.current = this.es.length-1;
		if(this.current > this.es.length-1) this.current = 0;

		//
		this.es[this.current].scrollTo();
		
	},
	
	/* open and closes the subnav */
	Toggle:function(){
		
		/* is it open? */
		if(this.open){
			this.Close();
		}else{
			this.Open();
		}
		
		//set its property
		this.open=!this.open;
		
	},
	
	/* 
	**/
	Open:function(){
		
		$(this.id).setStyle({marginTop:'0px'});
		
	},
	
	/* 
	**/
	Close:function(){
		
		var h = (-1 * $(this.id).getHeight()) / 3;
		$(this.id).setStyle({marginTop:'-35px'});
		
	},
	
	/*
	**/
	SetAsCurrent:function(newID){
		
		this.current = newID;
		
	}
	
}

var Cards = {
	
	/* ref to the periodical executor */
	pe:null,
	/* holds all the 'card' divs */
	cards:null,
	/* current card */
	cc:0,
	
	/* constructor 
	 * @context - the id of container
	**/
	initialize:function(context,current){

		this.cards = document.getElementsByClassName('card', $(context));

		// Campfire style :-)
		this.pe = new PeriodicalExecuter(this.Scroll.bind(this), 3);
		
	},
	
	Scroll:function(){
		
		this.cards[this.cc].scrollTo();
		this.cc++;
		if (this.cc >= this.cards.length) this.cc = 0;
		
	}
	
}

/* generic mouseover for the buttons w/ background images
 * finds the 1/2 the height of the container
 * moves the background image minus that height
**/
var Rollover = function(btnObj){
	
	//make it's not supposed to be 'On'
	if(btnObj.id.indexOf("On") > -1) return;
	
	//calc half and move the background
	var half = $(btnObj).getHeight();	
	btnObj.setStyle({backgroundPosition:'0px -'+half+'px'});
	
}

/* generic mouseout for the buttons w/ backgrounds 
 * set the background position back to normal (0px 0px)
**/
var Rollout = function(btnObj){
	
	//make it's not supposed to be 'On'
	if(btnObj.id.indexOf("On") > -1) return;
	
	btnObj.setStyle({backgroundPosition:'0px 0px'});		
	
}

var updateLargeImage = function(obj,src){
	
	$(obj).addClassName('linkOn');
	
	$(obj).siblings().each(function(s){
		s.removeClassName('linkOn');
	});
	
	$(obj).up().previous().down().setAttribute("src",src);
	$(obj).up().previous().down().onload = function(){
		this.setAttribute("width",this.width);
		this.setAttribute("height",this.height);
	}
	
}

var numbersRollover = function(obj){
	if ($(obj).className == 'linkOn') return;
	
	$(obj).setStyle({background:'#FDE804',color:'#fff',cursor:'pointer'});
}

var numbersRollout = function(obj){
	$(obj).setStyle({background:'transparent',color:'#000',cursor:''});
}

var RollOver2 = function(obj){
	$(obj).setStyle({cursor:'pointer'});
}

var RollOut2 = function(obj){
	$(obj).setStyle({cursor:''});
}

var cssNum = function(cssVal){
	return cssVal.substr(0,cssVal.length-2);
}

var Toggle = function(ID){
	// find the orig window width
	var ww1 = $('container').scrollWidth;
	// toggle the content
	$(ID).className=($(ID).className == "closed") ? "opened" : "closed";
	
	// height of the window
	var wh = (window.innerHeight) ? (window.innerHeight) : (document.documentElement.clientHeight) ? (document.documentElement.clientHeight) : (document.body.clientHeight);

	// clear the height
	$('content').setStyle({height:wh+"px"});	
	$('clients').setStyle({height:""});
	
	// container div (ID) height
	var ch = $('clients').getDimensions().height;
	
	if (ch < wh) {
		$('clients').setStyle({height:wh+"px"});
		$('content').setStyle({height:wh+"px"});
	}
	$('content').setStyle({height:""});
	
	
	// now calc the difference
	var ww2 = $('container').scrollWidth;
	var diff = (ww1-ww2)/2;
	
	// find the position and move it
	var navPos = cssNum($('navigation').getStyle('left'));
	$('navigation').setStyle({left:(navPos-diff)+'px'});
	
}

var Toggle2 = function(ID){
	$(ID).className=($(ID).className == "closed") ? "opened" : "closed";
}

var init = function(){
	// hide the loader
	$('loading-container').setStyle({display:'none'});
	// size the container
	var wh = (window.innerHeight) ? (window.innerHeight) : (document.documentElement.clientHeight) ? (document.documentElement.clientHeight) : (document.body.clientHeight);
	$('container').setStyle({display:'block',overflow:'auto',height:wh+'px',zIndex:1000});
	// move the nav into place
	var ww = (window.innerWidth) ? (window.innerWidth) : (document.documentElement.clientWidth) ? (document.documentElement.clientWidth) : (document.body.clientWidth);
	var ww = $('container').getDimensions().width;
	var cw = $('content').getDimensions().width;
	var navOffset = 29;		//from content
	$('navigation').setStyle({display:'block',left:(((ww-15-cw)/2)+navOffset)+'px',zIndex:2000});
	
	// scroll to the proper position if there is something in the hash
	if(location.hash!=""){
		var id = location.hash.substr(1);
		if($(id)){
			var offset = (Position.cumulativeOffset($(id))[1]);
			$('container').scrollTop = offset;
		}
	}
	
}

Event.observe(window, "load", init);
Event.observe(window, "resize", init);