//##################################################
//
//	EJN N-Go. 09/09/2009. Some scripts to do various [ajax] things on pages in the site. Mostly prototype specific.
//
//##################################################
Event.observe(window, 'load', function() 
{
	if ( $('logo') != null )
	{
		$('logo').observe('mouseover', showHover);
	}
	
	if ( $('cap') != null )
	{
		$('cap').observe('mouseout', hideHover);
	}
	
	if ( $('whychoose') != null )
	{
		$('whychoose').observe('click', showChoose);
	}
	
	if ( $('takechallenge') != null )
	{	
		$('takechallenge').observe('click', showChallenge);
	}
	
	if ( $('attendevent') != null )
	{
		$('attendevent').observe('click', showAttendEvent);
	}
	
	if ( $('changer') != null )
	{
		$('changer').observe('click', goToChange);
	}
	
	setWidths();
});

function showHover(event) 
{
	$('cap').show();
	$('logo').hide();
}

function hideHover(event) 
{
	$('cap').hide();
	$('logo').show();
}

function showChoose(event)
{
	if ($('choose').style.display == 'none' && $('clist').style.display == 'none' && $('cevent').style.display == 'none')
	{
		fadeInElement('choosebg', 2, 0, .8);
		fadeInElement('choose', 2, 0, 1);
	}
}

function hideChoose(event) 
{
	fadeOutElement('choose', 2, 1, 0);
	fadeOutElement('choosebg', 2, .8, 0);
}

function showChallenge(event) 
{
	if ($('choose').style.display == 'none' && $('clist').style.display == 'none' && $('cevent').style.display == 'none')
	{
		fadeInElement('clist', 2, 0, 1);
	}
}

function hideChallenge(event) 
{
	var p = parent.document.getElementById('clist');
	
	fadeOutElement(p, 2, 1, 0);
}

function showAttendEvent(event) 
{
	if ($('choose').style.display == 'none' && $('clist').style.display == 'none' && $('cevent').style.display == 'none')
	{
		fadeInElement('cevent', 2, 0, 1);
	}
}

function hideAttendEvent(event) 
{
	var p = parent.document.getElementById('cevent');
	
	fadeOutElement(p, 2, 1, 0);
}

function goToChange(event)
{
	if ( $('changelist').value != '' )
	{
		window.location.href = $('changelist').value;
	}
}

Event.observe(document.onresize ? document : window, 'resize', function() 
{
	setWidths();
});

function setWidths()
{
	var iW = 0;
    
    if(window.innerWidth) // Mozilla etc...
    {
        iWcap = window.innerWidth > 1020 ? (window.innerWidth / 2) - 150 : 300;
        iWchoo = window.innerWidth > 1020 ? (window.innerWidth / 2) - 260 : 200;
        iWchal = window.innerWidth > 1020 ? (window.innerWidth / 2) - 300 : 60;
        // alert(iWcap + ', ' + iWchoo);
    }
    else // IE etc...
    {
        iWcap = document.body.clientWidth > 1020 ? (document.body.clientWidth / 2) - 135 : 300;  
        iWchoo = document.body.clientWidth > 1020 ? (document.body.clientWidth / 2) - 245 : 200;  
        iWchal = document.body.clientWidth > 1020 ? (document.body.clientWidth / 2) - 285 : 50; 
    }
    
    // Positions the BG on the why choose flyout...
    var iChooBG = iWchoo - 20;
    var sChooBG = iChooBG + "px"
    
    var sCap = iWcap+"px"
    var sChoo = iWchoo+"px"
    var sChal = iWchal+"px"
    
    if($('choose') != null)
    {
    	    $('choose').style.left = sChoo;
    }
    
    if($('choose') != null)
    {
    	    $('choosebg').style.left = sChooBG;
    }
    if($('clist') != null)
    {
    	    $('clist').style.left = sChal;
    }
    
    if($('cevent') != null)
    {
    	    $('cevent').style.left = sChal;
    }
}

function fadeInElement(elementID, fd, ff, ft)
{
	Effect.Appear(elementID, { duration: fd, from: ff, to: ft });
}

function fadeOutElement(elementID, fd, ff, ft)
{
	Effect.Fade(elementID, { duration: fd, from: ff, to: ft });
}