// Setup JS events when the DOM is ready
$(document).ready(function(){
	if (!$("body#enews").is("body")) {
	  xfade_imgs = new Array();
	  xfade_current = 0;
	  so_init();
  }

	$("#cv2_explanation").hide();
	$("a#cv2").click(function(){
		$("#cv2_explanation").show();
		return false;
	})
	$("a#hide_cv2").click(function(){
		$("#cv2_explanation").hide();
		return false;
	})

	$(".course_submit").hide();
    $(".search_box").focus(clearbox);
    $(".search_box").blur(clearbox);
	$("a.popup").click(function()
	{
		this.target = "_blank";
	});
	$("#payment_add_address").click(function()
	{
		$("#add_address").toggle();	
	});

	$('.styleswitch').click(function()
        {
                switchStylestyle(this.getAttribute("rel"));
                return false;
        });
        var c = readCookie('style');
        if (c) switchStylestyle(c);
});

// Reset default text in input box
function clearbox(){
    if (!this.default_value) this.default_value = this.value;
    
    if (this.value == ''){
        this.value = this.default_value;
    } else if (this.value == this.default_value){
        this.value = '';
    }
}


// JQuery Style Switcher
function switchStylestyle(styleName)
{
        $('link[@rel*=style]').each(function(i)
        {
                this.disabled = true;
                if (this.getAttribute('title') == styleName) this.disabled = false;
        });
        createCookie('style', styleName, 365);
}

// cookie functions http://www.quirksmode.org/js/cookies.html
	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 so_init() 
{
	var d = document;

	if(!d.getElementById || !d.createElement) return;

	var imgs = d.getElementById("images");

	if (imgs)
	{
		xfade_imgs = imgs.getElementsByTagName("img");
		for(i=1;i<xfade_imgs.length;i++)
		{ 
			xfade_imgs[i].className = "hide";
			xfade_imgs[i].xOpacity = 0;
		}
		xfade_imgs[0].style.display = "block";
		xfade_imgs[0].xOpacity = .99;

		setTimeout(so_xfade, 3000);
	}
}

function so_xfade() 
{
	cOpacity = xfade_imgs[xfade_current].xOpacity;
	nIndex = xfade_imgs[xfade_current+1]?xfade_current+1:0;
	nOpacity = xfade_imgs[nIndex].xOpacity;

	cOpacity-=.05; 
	nOpacity+=.05;

	xfade_imgs[nIndex].style.display = "block";
	xfade_imgs[xfade_current].xOpacity = cOpacity;
	xfade_imgs[nIndex].xOpacity = nOpacity;

	setOpacity(xfade_imgs[xfade_current]); 
	setOpacity(xfade_imgs[nIndex]);

	if(cOpacity<=0) 
	{
		xfade_imgs[xfade_current].style.display = "none";
		xfade_current = nIndex;
		setTimeout(so_xfade,3000);
	} 
	else 
	{
		setTimeout(so_xfade,55);
	}

	function setOpacity(obj) 
	{
		if(obj.xOpacity>.99) 
		{
			obj.xOpacity = .99;
			return;
		}
		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	}

}	