Cufon.set('fontFamily', 'TradeGothic').replace('#headline h1')('#primaryMenu h3')('#primaryTitle h3')('#primaryNav h3')('#mainNav h3', {hover:true})('#applyNowBoxes h3')('#neTabs h1')('#neTabs h3', {hover:true})('#slimContent h1');

var qlIsOpen = false;

$(document).ready(function() {
	if(document.getElementById('primaryMenu')) {
	    renderFlash_Home("/edu_HomeFlash.ashx");
		var pm = new primaryMenu();
	}
	if(document.getElementById('quickLinks')) {
		var ql = new quickLinks();
	}
/*	if(document.getElementById('searchResults')) {
		var sr = new searchResults();
	}
*/	if(document.getElementById('facSlider')) {
		var fs = new facSlider();
	}
	if(document.getElementById('newsFlyout')) {
		var nf = new newsFlyout();
	}
	if(document.getElementById('dateSlider')) {
		var ds = new dateSlider();
	}
	if(document.getElementById('eventList')) {
		var el = new eventList();
	}
	if(document.getElementById('monthSlider')) {
	    var ms = new monthSlider();
	}
	if(document.getElementById('calendarTips')) {
		calendarTips();
	}
	if(document.getElementById('faq')) {
		faq();
	}
	if(document.getElementById('alumStoriesSlider')) {
		alumStoriesSlider();
	}
	if(document.getElementById('degreeMenu')) {
		var dm = new degreeMenu();
	}
	if(document.getElementById('grid')) {
		var gb = new grid();
	}
	
	// Temporary Javascript for Debugging
	if(window.location.href.substr(7,9)=="localhost") {
        $('img').each(function(index) {
            var src = $(this).attr('src');
            $(this).attr('src','http://174.121.28.98'+src);
        });
    }
    
	
});

function decodeEmail() {
    $('span.obfs').each(function() {
        var email = this.innerHTML.replace(/[\/]/g,'.').replace(/[\|]/g,'@');
        $(this).html('<a href="mailto:'+email+'" title="'+email+'">'+email+'</a>');
    });
}

function primaryMenu() {
	var hover = [];
	var tab = [];
	var open = [];
	var lis = $('#primaryMenu>ul>li');
	var ids = [];
	ids['ug'] = 1;
	ids['pgs'] = 2;
	ids['grts'] = 3;
	
	function slideUp(cls) {
		var elem = lis.filter('.'+cls);
		elem[0].style.backgroundPosition = 'left -30px';
		var height = elem.height();
		open[cls] = true;
		elem.stop().animate({top: 20 - height},{duration: 350,easing: 'easeInOutSine',queue: false, complete: function() {
	        $('#headline div').hide().filter('.'+cls).show();
		    changeSection(ids[cls]);
		}});
	}
	function slideDown(cls) {
		var elem = lis.filter('.'+cls);
		var dur = parseInt(elem.css('top'),10) * -5 + 20;
		elem.stop().animate({top: 0},{duration: dur,easing: 'easeInOutSine',queue: false,complete: function() {open[this.className] = false;this.style.backgroundPosition = 'left top';}});
	}
	
	lis.hover(function() {
		var cls = this.className;
		this.style.backgroundPosition = 'left -30px';
		if(!tab[cls] && !open[cls] && !qlIsOpen) {
			tab[cls] = true;
			clearTimeout(hover[cls]);
			hover[cls] = null;
			hover[cls] = setTimeout(function() { slideUp(cls); }, 250);
		}
		else if(qlIsOpen) {
	        $('#headline div').hide().filter('.'+cls).show();
		    changeSection(ids[cls]);
		}
	},
	function() {
		var cls = this.className;
		if(!open[cls]) {
			this.style.backgroundPosition = 'left top';
		}
		tab[cls] = false;
		clearTimeout(hover[cls]);
		hover[cls] = null;
		slideDown(cls);
	});
	$('#logo').mouseover(function() {
	    changeSection(0);
	    $('#headline div').hide().filter('.home').show();
	});
}

function quickLinks() {
	var time = null;
	
	function close_hover() {
		$('#quickLink').hide();
	}
	
	$('#ql_open').click(function(e) {
		e.preventDefault();
		if($.browser.msie) {
			$('#quickLinks').show();
		}
		else {
			$('#quickLinks').fadeIn();
		}
		$(this).addClass('active');
		qlIsOpen = true;
	});
	$('#ql_over, #ql_close').click(function(e) {
		e.preventDefault();
		if($.browser.msie) {
			$('#quickLinks').hide();
			$('#ql_open').removeClass('active');
		}
		else {
			$('#quickLinks').fadeOut('def',function() { $('#ql_open').removeClass('active'); });
		}
		qlIsOpen = false;
	});
	$('#ql_links a').mouseover(function() {
		var pos = $(this.parentNode).position();
		$('#quickLink').html('<a href="'+this.href+'" title="'+this.title+'" target="'+this.target+'">'+this.innerHTML+'</a>').css('left',pos.left + 11).show();
	});
	$('#quickLink').mouseout(function() {
		time = setTimeout(function() { close_hover(); },50);
	}).mouseover(function() {
		clearTimeout(time);
		time = null;
	});
}

function searchResults() {
	var searchResults = $('#searchResults');
	var globalSearch = $('#globalSearch');
	
	
    function setTrigger() {
        if(globalSearch.val().length > 0 && !globalSearch.hasClass('defaultValue')) {
            $('#triggerSearch').val(globalSearch.val()).addClass('userValue');
        }
        else {
            $('#triggerSearch').val('Search').removeClass('userValue');
        }
    }
	function closeCompact() {
		if(searchResults.hasClass('compactSearch')) {
		    setTrigger();
			searchResults.hide();
			$('body').unbind();
		}
	}
	function exeSearch() {
	    var SearchText = globalSearch.val().trim();
	    if(SearchText.length > 0 && !globalSearch.hasClass('defaultValue')) {
	        // Clears the searchbox of the current searchs
			$('body').unbind();
	        globalSearch.val('Search').removeClass('userValue').addClass('defaultValue');
		    searchResults.removeClass('compactSearch').addClass('fullSearch');
		    btnGo_Click(SearchText, $('#globalSearch_lang').val());
		}
	}
	
	$('#triggerSearch').focus(function() {
	    searchResults.removeClass('fullSearch').addClass('compactSearch').show();
	    globalSearch.focus().select();
	    $('body').click(function() {
	        closeCompact();
	    });
	}).click(function(e) {
		e.stopPropagation();
	});
	
	globalSearch.defaultVal('Search').keypress(function(e) {
		if(e.which == 13) {
			e.preventDefault();
			exeSearch();
			globalSearch.blur();
		}
	}).click(function(e) {
		e.stopPropagation();
	});
	
	$('#globalSearch_go').click(function(e) {
		e.preventDefault();
		e.stopPropagation();
		exeSearch();
	});
	
	$('#globalSearch_close').click(function(e) {
		e.preventDefault();
		$('#searchResults .paging').html('');
		$('#searchResults .results').html('');
		setTrigger();
		searchResults.hide();
	});
}

function facSlider() {
	var cursor = $('#facSliderArrow');
	var bar = $('#facSliderBar');
	var spot = 26;
	var currentSpot = 26;
	
	function calculateLeftPos(mouseX) {
		var barPos = bar.offset();
		return Math.floor((mouseX - barPos.left + 3) / 19);
	}
	function moveCursorTo(num) {
		var left = 19*num + 1;
		if(num > 25) {
			left = 542;
			spot = 26;
		}
		else if(num < 0) {
			left = 1;
			spot = 0;
		}
		else {
			spot = num;
		}
		cursor.css('left',left);
	}
	function resetContent() {
		if(currentSpot != spot) {
			currentSpot = spot;
			SendServerRequest(spot);
		}
	}
	
	$('#facSlider a').each(function(i) {
		$(this).click(function(e) {
			e.preventDefault();
			moveCursorTo(i);
			resetContent();
		});
	});
	bar.click(function(e) {
		e.preventDefault();
		var leftPos = calculateLeftPos(e.pageX);
		moveCursorTo(leftPos);
		resetContent();
	});
	cursor.mousedown(function() {
		document.onselectstart = function() { return false; }; 
		$(document).mousemove(function(e) {
			var leftPos = calculateLeftPos(e.pageX);
			moveCursorTo(leftPos);
		}).mouseup(function(e) {
			$(document).unbind();
			document.onselectstart = null;
			resetContent();
		});
	}).click(function(e) {
		e.stopPropagation();
	});
}

function newsFlyout() {

	var isOpen = false;
	$nf = $('#newsFlyout');
	$overlay = $('#overlay');
	var story = 0;
	var timer = null;
	
	function close() {
		isOpen = false;
		$(document).unbind();
	    $overlay.unbind();
		$nf.stop().animate({ width:159 }, 500, 'easeInOutSine', function() {
	        if($.browser.msie) {
		        $overlay.hide();
	        }
	        else {
		        $overlay.fadeOut(400);
	        }
			$nf.appendTo('#homeBanner');
		});
	}
	function open() {
		isOpen = true;
	    $overlay.mouseover(function() {
		    close();
	    });
		//$(document).keypress(function(e) {
		//	if(e.which === 0) {
		//		close();
		//	}
		//});
		$nf.appendTo('body').stop().animate({ width:920 }, 600, 'easeInOutCirc', function() {
		    if($.browser.msie) {
		        $overlay.show();
		    }
		    else {
		        $overlay.fadeIn(600);
		    }
		});
	}
	$nf.hover(function() {
		if(!isOpen) {
			timer = setTimeout(function() { open(); }, 250);
		}
	},function() {
	    clearTimeout(timer);
	    timer = null;
	    close();
	}).find('div.title').click(function(e) {
		if(isOpen) {
			e.stopPropagation();
			close();
		}
	});
	$nf.find('div.menu a').each(function(i) {
	    $(this).click(function(e) {
	        e.preventDefault();
	        $nf.find('div.menu li').removeClass('selected');
	        $('#story'+story).hide();
	        story = i;
	        $(this).parent().addClass('selected');
	        $('#story'+story).show();
	    });
	});
	
	// ie6 hack
	if($.browser.msie && document.documentElement.offsetWidth % 2 > 0) { $('body').css('paddingRight','1px'); }
	
}


function monthSlider() {
	var cursor = $('#monthSliderArrow');
	var bar = $('#monthSliderBar');
	var anchors = $('#monthSlider a');
	var spot = 0;
	var currentSpot = 0;
	var max = anchors.length - 1;
	
	function calculateLeftPos(mouseX) {
		var returnVal = 0;
		var barPos = bar.offset();
		var left = mouseX - barPos.left;
		if(left < 0) {
			returnVal = 0;
		}
		else if(left <= 495) {
			returnVal = Math.floor(left / 45);
		}
		else {
			returnVal = 11;
		}
		if(returnVal > max) {
			returnVal = max;
		}
		return returnVal;
	}
	function moveCursorTo(num) {
		if(num != spot) {
			left = (num * 45) + 17;
			anchors.eq(spot).removeClass('selected');
			spot = num;
			anchors.eq(num).addClass('selected');
			cursor.css('left',left);
		}
	}
	function resetContent() {
		if(currentSpot != spot) {
			currentSpot = spot;
			//SendServerRequest(spot);
			anchors.eq(spot)[0].onclick();
		}
	}
	
	anchors.each(function(i) {
	    if(this.className == "selected") {
	        currentSpot = i;
	        moveCursorTo(currentSpot);
	    }
		$(this).click(function(e) {
			e.preventDefault();
			moveCursorTo(i);
			currentSpot = spot;
			//resetContent();
		});
	});
	bar.click(function(e) {
		e.preventDefault();
		var leftPos = calculateLeftPos(e.pageX);
		moveCursorTo(leftPos);
		resetContent();
	});
	cursor.mousedown(function() {
		document.onselectstart = function() { return false; }; 
		$(document).mousemove(function(e) {
			var leftPos = calculateLeftPos(e.pageX);
			moveCursorTo(leftPos);
		}).mouseup(function(e) {
			$(document).unbind();
			document.onselectstart = null;
			resetContent();
		});
	}).click(function(e) {
		e.stopPropagation();
	});
}


function dateSlider() {
	var cursor = $('#dateSliderArrow');
	var bar = $('#dateSliderBar');
	var anchors = $('#dateSlider a');
	var spot = 0;
	var currentSpot = 0;
	var max = anchors.length - 1;
	
	function calculateLeftPos(mouseX) {
		var returnVal = 0;
		var barPos = bar.offset();
		var left = mouseX - barPos.left;
		if(left <= 40) {
			returnVal = 0;
		}
		else if(left <= 148) {
			returnVal = Math.floor((left - 40) / 12) + 1;
		}
		else {
			returnVal = Math.floor((left - 148) / 18) + 10;
		}
		if(returnVal > max) {
			returnVal = max;
		}
		return returnVal;
	}
	function moveCursorTo(num) {
		if(num != spot) {
			if(num === 0) {
				left = 13;
			}
			else if(num < 10) {
				left = 41 + ((num - 1) * 12);
			}
			else {
				left = 152 + ((num - 10) * 18);
			}
			anchors.eq(spot).removeClass('selected');
			spot = num;
			anchors.eq(num).addClass('selected');
			cursor.css('left',left);
		}
	}
	function resetContent() {
		if(currentSpot != spot) {
			currentSpot = spot;
			SendServerRequest(spot);
		}
	}
	
	anchors.each(function(i) {
		$(this).click(function(e) {
			e.preventDefault();
			moveCursorTo(i);
			resetContent();
		});
	});
	bar.click(function(e) {
		e.preventDefault();
		var leftPos = calculateLeftPos(e.pageX);
		moveCursorTo(leftPos);
		resetContent();
	});
	cursor.mousedown(function() {
		document.onselectstart = function() { return false; }; 
		$(document).mousemove(function(e) {
			var leftPos = calculateLeftPos(e.pageX);
			moveCursorTo(leftPos);
		}).mouseup(function(e) {
			$(document).unbind();
			document.onselectstart = null;
			resetContent();
		});
	}).click(function(e) {
		e.stopPropagation();
	});
}

function eventList() {
	
	function open(object) {
		$('#eventList dl').removeClass('open').addClass('fade');
		$(object).parents('dl').removeClass('fade').addClass('open');
	}
	function close() {
		$('#eventList dl').removeClass('fade').removeClass('open');
	}
	
	$('#eventList dl').find('a.eventShow').bind('click', function(e) {
		e.preventDefault();
		open(this);
	})
	.end()
	.find('a.eventHide').bind('click', function(e) {
		e.preventDefault();
		close();
	})
	.end()
	.find('h2.title a').bind('click', function(e) {
		e.preventDefault();
		if($(this).parents('dl').hasClass('open')) {
			close();
		}
		else {
			open(this);
		}
	});
}

function calendarTips() {
	
	$('#calendarTips a').each(function() {
		$a = $(this);
		if(this.rel && this.rel.length > 0 && document.getElementById(this.rel)) {
			var $tip = $('#'+this.rel);
			var apos = $a.position();
			$tip.css({
				left: apos.left - 221,
				top: apos.top - 25
			});
			if($.browser.msie) {
				$a.hover(function() {
					$tip.show();
				},function() {
					$tip.hide();
				});
			}
			else {
				$a.hover(function() {
					$tip.stop().fadeTo(300, 1);
				},function() {
					$tip.stop().fadeTo(200, 0,function() {
						$tip.hide();
					});
				});
			}
		}
	}).click(function(e) {
	    e.preventDefault();
	});
	
}

function faq() {
	$('#faq dt').click(function() {
		$(this).parent().toggleClass('open');
	});
}

function alumStoriesSlider() {
    if($('#alumStoriesSlider li').length > 1) {
        var frame = $('#alumStoriesSlider');
        if(frame.outerHeight() < frame.children('ul').outerHeight()) {
            var down = document.getElementById('alumStoriesDown');
            down.style.display = "block";
		    var up = document.getElementById('alumStoriesUp');
		    up.style.display = "block";
	        $('#alumStoriesSlider').slider({
		        next: down,
		        previous: up,
		        direction: 1,
		        scrollWheel: true
	        });
	    }
	}
}

function degreeMenu() {
	var current = "";
	
	function reset() {
		$('#degreeModal div.body').hide();
		$('#degreeModal_tabs li').removeClass('selected');
	}
	function select(type) {
		$('#degreeModal_'+type).show();
		$('#degreeModal_tabs .'+type).addClass('selected');
		current = type;
	}
	
	$('#degreeMenu a').click(function(e) {
		e.preventDefault();
		if($.browser.msie) {
		    $('#overlay').show();
		}
		else {
		    $('#overlay').fadeIn();
		}
		$('#overlay').click(function() {
			reset();
			$('#degreeModal').hide();
			if($.browser.msie) {
			    $(this).hide().unbind();
			}
			else {
			    $(this).fadeOut().unbind();
			}
		});
		$('#degreeModal').show();
		select(this.rel);
	});
	$('#degreeModal_tabs a').click(function(e) {
		e.preventDefault();
		if(current != this.rel) {
			reset();
			select(this.rel);
		}
	});
	$('#degreeModal_close').click(function(e) {
		e.preventDefault();
		reset();
		$('#degreeModal').hide();
		if($.browser.msie) {
		    $('#overlay').hide().unbind();
		}
		else {
		    $('#overlay').fadeOut().unbind();
		}
	});
}

function grid() {
	$('body>form:first').append('<div id="gridfloat" class="gridfloat"></div>');
	
	var gridfloat = $('#gridfloat');
	var grid = $('#grid');
	var timer = null;
	
	function clearHover(remClass) {
		grid.append(gridfloat.children());
		if(remClass) {
			grid.removeClass('gridActive');
		}
		gridfloat.hide();
	}
	
	grid.find('div.gridhover').live('mouseover',function() {
		clearTimeout(timer);
		timer = null;
		grid.addClass('gridActive');
		if(gridfloat.children().length) {
			clearHover(false);
		}
		var pos = $(this).unbind().offset();
		// ie6 hack
		if($.browser.msie && $.browser.version < 7 && document.documentElement.offsetWidth % 2 > 0) { pos.left = pos.left + 1; }
		var topPos = pos.top - 14;
		if($(this).hasClass('hoverW1H2')) {
			topPos = topPos - 86;
		}
		if($(window).width() < 956) {
			gridfloat.css({ top: topPos, left: pos.left - 14, marginLeft: 0});
		}
		else {
			var pagePos = $('#page').offset();
			var leftPos = 492 - pos.left + pagePos.left;
			gridfloat.css({ top: topPos, marginLeft: -leftPos, left: '50%'});
		}
		// ie hack
		if($.browser.msie) { timer = setTimeout(function() { clearHover(true); }, 50); }
		gridfloat.append(this).show();
	});
	gridfloat.hover(function() {
		clearTimeout(timer);
		timer = null;
	},
	function() {
		timer = setTimeout(function() { clearHover(true); }, 50);
	});
}


function btnNiMonth_Click(tid, langType, month, year){
    $('#newsList').html('<div class="loading"><img src="/webimages/ajaxLoader.gif" alt="Loading..." /></div>');
    var request = {};
    request.TaxonomyID = tid;
    request.LangType = langType;
    request.Month = month;
    request.Year = year;
    
    var DTO = { 'request' : request };
    
    doJson(
        '/webservices/edu_NewsIndex.asmx/GetNewsIndexUI',
        JSON.stringify(DTO),
        function(result, textStatus){
            $('#newsList').html(result);
        },
        function(request, status, err){
            if (request.readyState == 4) {
                switch (request.status) {
                // Page-not-found error
                case 404:
                    alert('Error: Not Found. The requested URL could not be found.');
                    break;
                // Internal server error
                case 500:
                    if (request.responseText.Message) {
                        alert(request.responseText.Message);
                    }
                    else {
                        alert(request.responseText);
                    }
                    break;
                default:
                    break;
                }
            }
        },
        function(XMLHttpRequest, textStatus){
            return false;
        });
        
    return false;
}
