var phonesRail=null;
var currentKey=-1;

/******************************************** deal with position classes ****************************/

// returns the first visible phone in the list
function findTopPhone(){
	var r=phonesRail.each(function(val,key){
		if(getDisplay(val)!='none'){
			addClass(val,'first');
			return val.getAttribute('param')
		}
		return 'none'
	});
	while(r=='none'&&r!=null){
		r=phonesRail.next();
	}
	return (r?r:false);
}

function findLastPhone(){
	var k=-1;
	phonesRail.forEach(function(val,key){
		if(getDisplay(val)!='none'){k=key}
	});
	if(k!=-1){
		addClass(phonesRail[k],'last');
	}
	return ((k!=-1)?phonesRail[k].getAttribute('param'):false);
}
// find the currently visible phone above the active phone and add "above" class
function findAbovePhone(){
	var ak=phonesRail.filterKeys(function(val,key){
				return ((getDisplay(val)!='none')&&(key<currentKey))
			});
	if(ak.length>0){
		addClass(phonesRail[ak[ak.length-1]],'above');
		return phonesRail[ak[ak.length-1]].getAttribute('param');
	}
	return false;
}




/*  functions to run Drawers */
var drawers=[];
var drawersStatus=[];
var drawerEvents=[];


function setUpDrawers(){
	drawers=$css('#phoneDescContent div.drawers div.drawer');
	drawers.forEach(function(val,key){
		val.setAttribute('drawerCount',key);
		var d=val.$css('div.open; div.closed')[0];
		if(drawersStatus.length==drawers.length){
			setClass(d,drawersStatus[key]);
		}
		else{
			drawersStatus[key]=getClassName(d);
		}
		var h=val.$css('h3.drawerTop')[0];
		drawerEvents.push(Events.attach(h,'drawerClick'));
		if(bis.ie && (bis.v<7)){			
			drawerEvents.push(Events.attach(h,'ieFixMouseOver'));
			drawerEvents.push(Events.attach(h,'ieFixMouseOut'));
		}
	});
}

function doDrawerClick(e,el){
	el=el.$anc('div',{count:2});
	var k=el.getAttribute('drawerCount');
	var c=((drawersStatus[k]=="closed")?"open":"closed");
	drawersStatus[k]=c;
	var d=$(el).$css('div.open; div.closed')[0];
	setClass(d,c);	
}
	
function unloadDrawers(){
	drawerEvents.forEach(function(val){
		Events.detach(val);
	});
	drawerEvents.clear();
}


/* end Drawers */


/* Fixes for IE not recognizing hover psuedo-class */
function doHoverOverFix(e,el){
	addClass(el,'hover');
}

function doHoverOutFix(e,el){
	removeClass(el,'hover');
}
Events.add('ieFixMouseOver','mouseover');
Events.add('ieFixMouseOut','mouseout');
Events.inQ('ieFixMouseOver',doHoverOverFix);
Events.inQ('ieFixMouseOut',doHoverOutFix);
/* end Hover Fix */

Events.add('drawerClick','click');
Events.inQ('drawerClick',doDrawerClick);

//Events.inQ('DOMLoad',setUpDrawers);
