function collapseDirections()
{
    var link=$('directions_link');
    var body=$('directions_body');
    if (!body)
    {
        return;
    }
    body.style.display='none';
    link.className='collapsed';
    //link.innerHTML='<a href="#" onclick="toggleDirections()">' + link.innerHTML + '</a>'
}

function toggleDirections()
{
    var body=$('directions_body');
    var link=$('directions_link');
    if (body.style.display == 'none')
    {
        link.className='expanded';
    }
    else
    {
        link.className='collapsed';
    }
    new Effect.toggle('directions_body','appear');
    return false;
    
}

function setMaxHeight(item, subs)
{
    var maxHeight = 0;
    //Find the height of the tallest sub.
    for (var i = 0; i < subs.length; i++)
    {
        var sub = subs[i];
        if (sub.offsetHeight > maxHeight)
        {
            maxHeight = sub.offsetHeight;
        }
    }
    item.style.height=maxHeight + 'px';
 
}

function createTabSheet(container, fader)
{
    if (!fader)
    {
        fader = 0;
    }
    container.tabs=new Array();
    container.currentTabIndex=0;
    container.backgroundX = -10;
    container.currentTab = null;
    container.addTab=function(link,tab)
    {
        this.tabs[this.tabs.length]=tab;
        tab.link=link;
        
        var text=link.innerHTML;
        link.innerHTML = "<a href=\"#\" onclick=\"$('" + this.id + "').selectTab('" + tab.id + "'," + fader + ");return false\" >" + text + '</a>';

    }
    
    container.selectTab=function(tab_id, fader)
    {
        for (var i = 0; i < this.tabs.length; i++)
        {
            var tab = this.tabs[i];
            if (tab.id == tab_id)
            {
                tab.style.display='block';
                tab.link.className += ' selected';
                if (fader)
                {
                    tab.link.style.background='url(/extension/womenforhire/design/womenforhire/images/feature_employer_bkgd.jpg) no-repeat -10px -10px';
                    clearInterval(this.timer);
                    this.timer=setInterval("$('promo_control_switches').nextTab(true)", 8000);
                }
                this.currentTabIndex = i;
                this.backgroundX = -10;
                this.currentTab = tab;
            }
            else
            {
                tab.style.display='none';
                tab.link.className = tab.link.className.replace('selected','');
                if (fader)
                {
                    tab.link.style.background='none';
                }
            }
        }
    };

    container.prevTab=function(fader)
    {
        this.currentTabIndex--;
        if (this.currentTabIndex < 0 )
        {
            this.currentTabIndex = this.tabs.length - 1;
        }
        this.selectTab(this.tabs[this.currentTabIndex].id, fader);
    };
    
    container.nextTab=function(fader)
    {
        this.currentTabIndex++;
        if (this.currentTabIndex >= this.tabs.length)
        {
            this.currentTabIndex = 0;
        }
        this.selectTab(this.tabs[this.currentTabIndex].id, fader);
    };
    
}

function enableExpoList()
{
    var container=$('expo_list');
    if (!container)
    {
        return false;
    }
    
    
    var list = $('expo_seasons_list');
    var seasons = list.getElementsByTagName('ul');

    setMaxHeight(list, seasons);
    
    //Hide non-current seasons
    //Make season names into links

    var container = $('expo_season_names');
    if(!container)
    {
	return;
    }

    createTabSheet(container);
    
    var names=container.getElementsByTagName('li');
    for (var i = 0; i < names.length; i++)
    {
        var name=names[i];
        var tab_id=name.id.replace('_name','');
        var tab=$(tab_id);
        container.addTab(name, tab);
        
    }
    container.selectTab(container.tabs[0].id);
}

function enableHomePromos() 
{
    var container = $("home_promos");
    if (!container) {
        return false;
    }
    var promo_controls=$("promo_controls");
    //promo_controls.innerHTML="<ul id=\"promo_control_switches\"></ul>";
    var promos = document.getElementsByClassName("home_promo");
    setMaxHeight(container, promos);
    var ul = $("promo_control_switches");
    ul.innerHTML = "";
    createTabSheet(ul,1);
    var prev = document.createElement("li");
    prev.id = "home_promos_prev";
    prev.className = "prev";
    prev.innerHTML = "<a href=\"#\" onclick=\"$('promo_control_switches').prevTab(true);return false\"> &laquo; previous</a>";
    ul.appendChild(prev);
    for (var j = 0; j < promos.length; j++) {
        var promo = promos[j];
        var item_id = promo.id + "_link";
        var li = document.createElement("li");
        li.id = item_id;
        li.innerHTML = j + 1;
        ul.appendChild(li);
        ul.addTab(li, promo);
    }
    var next = document.createElement("li");
    next.id = "home_promos_next";
    next.innerHTML = "<a href=\"#\" onclick=\"$('promo_control_switches').nextTab(true);return false\"> next &raquo;</a>";
    ul.appendChild(next);
    ul.selectTab(ul.tabs[0].id);
    
    ul.timer=setInterval("$('promo_control_switches').nextTab(true)", 8000);
    ul.backgroundTimer=setInterval("updateThrobber($('promo_control_switches'))",250);   
}

function updateThrobber(ul)
{
    ul.backgroundX = ul.backgroundX - 3;
    ul.currentTab.link.style.background='url(/extension/womenforhire/design/womenforhire/images/feature_employer_bkgd.jpg) no-repeat ' + ul.backgroundX + 'px -10px';
}

function enableSponsorRotation()
{
    var list = $('expo_sponsor_list');
    if (!list)
    {
        return false;
    }

    var divs = list.getElementsByTagName('div');
    setMaxHeight(list, divs);    
    //hide all the divs
    for( var i = 1; i < divs.length; i++)
    {
        divs[i].style.display='none';
        //divs[i].style.visibility='visible';
    }
    
    list.sponsors = divs;
    list.sponsorIndex = 0;
    
    //start the rotation timer    
    setTimeout('rotateSponsors()',5000);
}

function rotateSponsors()
{
    var list = $('expo_sponsor_list');
    var sponsor = list.sponsors[list.sponsorIndex];
    
    // Fade out the current sponsor
    new Effect.Fade(sponsor);
    
    // select the next sponsor
    list.sponsorIndex++;
    if (list.sponsorIndex >= list.sponsors.length)
    {
        list.sponsorIndex = 0;
    }
    sponsor = list.sponsors[list.sponsorIndex];
    
    // Fade in the new sponsor
    setTimeout('new Effect.Appear("' + sponsor.id + '")',2000);
    
    // Rinse, repeat
    setTimeout('rotateSponsors()', 5000);
}

function enableFAQExpanders()
{
  var blocks = document.getElementsByClassName('question_line');
  for(var i=0; i < blocks.length ; i++)
  {
    var block = blocks[i];
    var question = block.getElementsByTagName('h2')[0];
    var answer = block.getElementsByTagName('div')[0];
    question.innerHTML = "<a href=\"#\">" + question.innerHTML + "</a>";
    var href = question.getElementsByTagName('a')[0];
    href.answer = answer;
    href.onclick=function(){
			if (this.answer.style.display=='none')
			{

				Effect.BlindDown(this.answer);

			}
			else
			{
				Effect.BlindUp(this.answer);

			}
			return false;      
    };
    answer.style.display='none';
  }
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


function enableNetworkSection()
{
  var d = new Date();
  d.getTime();
  var t = Date.parse((d.getMonth()  + 1) + '/' + d.getDate() + '/' + d.getFullYear() + ' 00:00:00');
  var s = Date.parse('1/1/' + d.getFullYear() + ' 00:00:00');
  var day = Math.round((t - s) / 86400000);
  
  var pass=(day + 1) * (d.getDay() + 1) * 333;
  if(document.location.pathname.indexOf('/network')==0)
  {
    if (document.location.search == '?pass=' + pass)
    {
      createCookie('wfhnetwork',pass);
    }
      if (readCookie('wfhnetwork') != pass)
    {
      document.location="http://network.womenforhire.com/premium_features.php";
    }
  }
}
enableNetworkSection();


function bindBehaviors()
{
    // quit if this function has already been called
    if (arguments.callee.done) return;

    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;
    
    document.getElementsByTagName('body')[0].className=document.getElementsByTagName('body')[0].className.replace('jsloading','jsactive');

    collapseDirections();
    enableSponsorRotation();
    enableHomePromos()
    enableExpoList();
	enableFAQExpanders();
}

window.onload=bindBehaviors;
