/*
DezinerFolio.com Simple Accordians.

Author  : G.S.Navin Raj Kumar
Website : http://dezinerfolio.com

*/

/*
* The Variable names have been compressed to achive a higher level of compression.
*/
var scrollInt;
var scrTime, scrSt, scrDist, scrDur, scrInt;
// Prototype Method to get the element based on ID
function $(d){
	return document.getElementById(d);
}

// set or get the current display style of the div
function dsp(d,v){
	if(v==undefined){
		return d.style.display;
	}else{
		d.style.display=v;
	}
}

// set or get the height of a div.
function sh(d,v){
	// if you are getting the height then display must be block to return the absolute height
	if(v==undefined){
		if(dsp(d)!='none'&& dsp(d)!=''){
			return d.offsetHeight;
		}
		viz = d.style.visibility;
		d.style.visibility = 'hidden';
		o = dsp(d);
		dsp(d,'block');
		r = parseInt(d.offsetHeight);
		dsp(d,o);
		d.style.visibility = viz;
		return r;
	}else{
		d.style.height=v;
	}
}
/*
* Variable 'S' defines the speed of the accordian
* Variable 'T' defines the refresh rate of the accordian
*/
s=7;
t=10;

//Collapse Timer is triggered as a setInterval to reduce the height of the div exponentially.
function ct(d){
	d = $(d);
	if(sh(d)>0){
		v = Math.round(sh(d)/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)-v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,0);
		dsp(d,'none');
		clearInterval(d.t);
	}
}

//Expand Timer is triggered as a setInterval to increase the height of the div exponentially.
function et(d){
	d = $(d);
	if(sh(d)<d.maxh){
		v = Math.round((d.maxh-sh(d))/d.s);
		v = (v<1) ? 1 :v ;
		v = (sh(d)+v);
		sh(d,v+'px');
		d.style.opacity = (v/d.maxh);
		d.style.filter= 'alpha(opacity='+(v*100/d.maxh)+');';
	}else{
		sh(d,d.maxh);
		a = d.id;
        b = a.substr(0,a.indexOf('-'));
        //location.href='#'+b+'';
		
		
		scrollToAnchor(b);
		clearInterval(d.t);
	}
	
	
}

// Collapse Initializer
function cl(d){
	if(dsp(d)=='block'){
		clearInterval(d.t);
		d.t=setInterval('ct("'+d.id+'")',t);
	}
}

//Expand Initializer
function ex(d){
	if(dsp(d)=='none'){
		dsp(d,'block');
		d.style.height='0px';
		clearInterval(d.t);
		d.t=setInterval('et("'+d.id+'")',t);		

	}
}

// Removes Classname from the given div.
function cc(n,v){
	s=n.className.split(/\s+/);
	for(p=0;p<s.length;p++){
		if(s[p]==v+n.tc){
			s.splice(p,1);
			n.className=s.join(' ');
			break;
		}
	}
}
//Accordian Initializer
function Accordian(d,s,tc,e){
// get all the elements that have id as content
l=$(d).getElementsByTagName('div');
c=[];
for(i=0;i<l.length;i++){
h=l[i].id;
if(h.substr(h.indexOf('-')+1,h.length)=='content'){c.push(h);}
}
sel=null;
//then search through headers
for(i=0;i<l.length;i++){
h=l[i].id;
if(h.substr(h.indexOf('-')+1,h.length)=='header'){
d=$(h.substr(0,h.indexOf('-'))+'-content');
d.style.display='none';
d.style.overflow='hidden';
d.maxh =sh(d);
d.s=(s==undefined)? 7 : s;
h=$(h);
h.tc=tc;
h.c=c;
// set the onclick function for each header.
h.onclick = function(){
for(i=0;i<this.c.length;i++){
cn=this.c[i];
n=cn.substr(0,cn.indexOf('-'));
if((n+'-header')==this.id){
cn = $(n+'-header').className;
if (cn!=undefined && cn.indexOf(tc) != -1) {
collapse(n);
} else {
expand(n);
}
}else{
collapse(n);
}
}
}
if(h.className.match(/selected+/)!=undefined){ sel=h;}
}
}
if(sel!=undefined){sel.onclick();}
// initializes the first expanded tab:
if (e!=undefined) expand(e);
}

function expand(n) {
ex($(n+'-content'));
n=$(n+'-header');
cc(n,'__');
n.className=n.className+' '+n.tc;
}

function collapse(n) {
cl($(n+'-content'));
cc($(n+'-header'),'');
}











// Found this script at http://blog.firetree.net/2005/07/04/javascript-find-position/
// Just used here to provide offsetTop functionality for IE
function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }

function replaceAnchorLinks()
{
	var anchors, i, targ, targarr;

	if (!document.getElementById)
		return;
	
	// get all anchors
	anchors = document.getElementsByTagName("a");
	
	for (i=0;i<anchors.length;i++)
	{
		// check if href links to an anchor on this page
		if ( anchors[i].href.indexOf("#") != -1)
		{
			// get name of target anchor
			targ = anchors[i].href.substring( anchors[i].href.indexOf("#")+1 );
			
			// find target anchor
			targarr = document.getElementsByName( targ );
			
			if (targarr.length)
			{
				anchors[i].className = (targarr[0].offsetTop < anchors[i].offsetTop) ? "up" : "down";
				anchors[i].id = "__" + targ;	// save target as id with prefix (used in onclick function below)
				anchors[i].onclick = function () {
					try {
						scrollToAnchor( this.id.substring( 2 ) );
					}
					catch (e) {}
					return false;
				};
			}
		}
		
	}
}


/*
SCROLL FUNCTIONS
*/




function scrollPage()
{
	scrTime += scrInt;
	if (scrTime < scrDur) {
		window.scrollTo( 0, easeInOut(scrTime,scrSt,scrDist,scrDur) );
	}else{
		window.scrollTo( 0, scrSt+scrDist );
		clearInterval(scrollInt);
	}
}

function scrollToAnchor(aname)
{
	var anchors, i, ele, elePosY, heightCorrection;

	if (!document.getElementById)
		return;
	
	// get anchor
	anchors = document.getElementsByTagName("a");
	for (i=0;i<anchors.length;i++) {
		if (anchors[i].name == aname) {
			ele = anchors[i];
			i = anchors.length;
		}
	}
	
	// Find anchor's Y position
	elePosY = findPosY(ele);
	
	//	The following is just to give some vertical space above where the anchor lands, 
	//	in case you think it stops too close to the top of the window.  Set to 0 if unnecessary.
	heightCorrection = 30;
	
	// set scroll target
	if (typeof (window.pageYOffset) == 'number') {
		// Non-IE modern browsers
		scrSt = window.pageYOffset;
		scrDist = elePosY - heightCorrection - scrSt;
		scrDur = 500;
	} else if (document.documentElement) {
		// IE in Standards Compliance mode
		scrSt = document.documentElement.scrollTop;
		scrDist = elePosY - scrSt;
		if (window.XMLHttpRequest) {
			// IE7
			scrDur = 500;
		} else {
			// IE6
			scrDur = 700;
		}
	} else if (document.body && (document.body.scrollLeft || document.body.scrollTop) ) {
		// DOM compliant method, IE Quirks Mode
		scrSt = document.body.scrollTop;
		scrDist = elePosY - scrSt;
		scrDur = 500;
	}

	scrTime = 0;
	scrInt = 10;
	
	// set interval
	clearInterval(scrollInt);
	scrollInt = setInterval( scrollPage, scrInt );
}




/*
EASING FUNCTIONS
*/

function easeInOut(t,b,c,d)
{
	return c/2 * (1 - Math.cos(Math.PI*t/d)) + b;
}
