jQuery(document).ready(function(){

//jQ IE cleartype fixed
jQuery.fn.fadeIn = function(speed, callback) { 
    return this.animate({opacity: 'show'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 
 
jQuery.fn.fadeOut = function(speed, callback) { 
    return this.animate({opacity: 'hide'}, speed, function() { 
        if (jQuery.browser.msie)  
            this.style.removeAttribute('filter');  
        if (jQuery.isFunction(callback)) 
            callback();  
    }); 
}; 


//Menu
jQuery('#menu > li > a').hover(function(){
	jQuery('#menu > li > ul').hide();
	jQuery(this).parent().find('ul').show();
}, function(){
	jQuery('#menu > li > a').removeClass('hover');
	jQuery(this).parent().find('ul').hide();
	jQuery(this).parent().find('ul').hover(function(){
		jQuery(this).parent().find('a').addClass('hover');
		jQuery(this).show();
	}, function(){
		jQuery(this).hide();
		jQuery(this).parent().find('a').removeClass('hover');
	});
});

//Search
jQuery('#searchbar input').focus(function(){
		jQuery(this).parent().addClass('focus');
		if (jQuery(this).val() == "Search") {
		jQuery(this).attr("value","");
		}
		}).blur(function(){
		if (jQuery(this).val() == "") {
			jQuery(this).attr("value","Search");
		}
			jQuery(this).parent().removeClass('focus');
		});








//ui.table
jQuery('.ui-table').each(function(){
	jQuery(this).find('thead th:first').addClass('first');
	jQuery(this).find('tbody tr:odd').addClass('odd');
});

//ui-filter
jQuery('.ui-filter').each(function(){
	jQuery(this).find('li:first').addClass('first');
	jQuery(this).find('li:last').addClass('last');
	jQuery('.ui-filter a').click(function(){
		jQuery(this).parent().siblings().find('a').removeClass('filter');
		jQuery(this).addClass('filter');
		return false;
		});
	});


	
	

//Faculty Detail
jQuery('.tab-holder:first').show();
jQuery('.ui-tab-menu a').click(function(){
	if(jQuery(this).parents('.ui-tab-menu:first').hasClass('click')){
		return true;
	}else{
		jQuery('.tab-holder').hide();
		jQuery('.ui-tab-menu a').removeClass('tabNow');
		jQuery(this).addClass('tabNow');
		var adNow = jQuery(this).attr("href");
		jQuery(adNow).fadeIn();
		return false;
	}
});

	

// Focus Research
jQuery('#focusList li').hover(function(){
	jQuery(this)
	.addClass('hover')
	.append('<span></span>');
}, function(){
	jQuery(this)
	.removeClass('hover')
	.children('span').remove();
});
jQuery('#focusList li').click(function(){
	var url = jQuery(this).find('a').attr('href');
	location.href = url;
});

jQuery('.research-reference').each(function(){
	jQuery(this).find('a').click(function(){
		jQuery(this).parent().next('.holder').toggle();
		jQuery(this).parent().toggleClass('close');
		return false;
		});
	
});

jQuery('.research-content .content-holder').each(function(){
	var y = jQuery(this).children(':not(br):first').outerHeight(true);
	var x = jQuery(this).children(':not(br):first').position();
	var z = jQuery(this).height();
	var ot = jQuery(this).children(':not(br):first');
	
	ot.append('<a class="toggle" href="">more</a>');
	jQuery(this).css('height', x.top + y);
	
	jQuery(this).find('.toggle').toggle(
		function(){
			var target = jQuery(this).parents(':eq(1)');
			jQuery(this).parents(':eq(1)').css('height',z).removeAttr("style");
			jQuery(this).detach().appendTo(target);
			jQuery(this).text('fold');
		},
		function(){
			jQuery(this).detach().appendTo(ot);
			jQuery(this).parents(':eq(1)').css('height', x.top + y);
			jQuery(this).text('more');
		});
	jQuery(this).find('.toggle').click(function(){
		jQuery(this).toggleClass('collapse');
		return false;
	});
	
});


// Hardware Detail
var fw = jQuery('.hardware-detail-overview > img').width();
var fh = jQuery('.hardware-detail-overview > img').height();
var pp = jQuery('.hardware-detail-overview').height();
jQuery('.hardware-detail-overview li').css('paddingLeft', fw +116 );
jQuery('.hardware-detail-overview img').css('paddingBottom', pp - fh );


// Equalheight function by Rob Glazebrook @ www.cssnewbie.com
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = jQuery(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}

equalHeight(jQuery('#focusList h3, #focusList p, #researchFaculty li')); // set equal height


// Table-sorter
jQuery.getScript('js/metadata.js');
jQuery.getScript('js/pager.js');
jQuery.getScript('js/tablesorter.js',function() {
		jQuery('tbody:not(:has(tr))').each(function() {
			var tmp = jQuery(this).parents('table:first').find('thead th').length;
			var trhtml='<tr>';
			for(var i=0;i<tmp;i++){
				trhtml+='<td></td>';
			}
			trhtml+='</tr>';
			jQuery(this).html(trhtml);
		})
		
		
		//sorter
		jQuery('table.sort').each(function() {
			var sortby = jQuery(this).find('thead th').hasClass('bythis');
			var H = jQuery(this).find('thead th.bythis').prevAll().length;
			var N = jQuery(this).find('thead th:first').hasClass('{sorter: false}');
			var i = 0;
			if (sortby) {
				i = H;
			} else if (N) {
				i = 1;
			} else {
				i = 0;
			}
			
			
			//Add parser
			jQuery.tablesorter.addParser({
				id: 'monthYear',
				is: function(s) {
					return false;
				},
				format: function(s) {
				
				if (s == ''){
					return 0;
				}
				
				var monthNames = {};
				monthNames["Jan"] = "01";
				monthNames["Feb"] = "02";
				monthNames["Mar"] = "03";
				monthNames["Apr"] = "04";
				monthNames["May"] = "05";
				monthNames["Jun"] = "06";
				monthNames["Jul"] = "07";
				monthNames["Aug"] = "08";
				monthNames["Sep"] = "09";
				monthNames["Oct"] = "10";
				monthNames["Nov"] = "11";
				monthNames["Dec"] = "12";
				
				var date = s.match(/^(\w{3})*[ ]*(\d{4})/);
				var m = monthNames[date[1]];
				var y = date[2];
				return '' + y + m;
				},
				type: 'numeric'
			});
			
			//Add parser
			jQuery.tablesorter.addParser({
				id: 'digiY',
				is: function(e) {
					return false;
				},
				format: function(e) {
				
				var nothing = e.match(/\D/);
				var yes = e.match(/\d{2,4}/);
				return '' + yes;
				},
				type: 'numeric'
			});

						
			jQuery(this).tablesorter({
				debug :  false,
				widgets : [ 'zebra' ]
				/*,
				sortList : [ [i,0 ] ]*/
			});

			
			//Sort loading...
			jQuery(this).bind('sortStart', function(){
				var sw = jQuery(this).width();
				var sp = jQuery(this).offset();
				jQuery('#loading')
					.css('top',sp.top)
					.css('left',sp.left)
					.css('width',sw)
					.fadeIn(200);
			}).bind('sortEnd', function(){
				jQuery('#loading').delay(300).fadeOut();
			});
			
			jQuery(this).find('thead th.unbind').click(function(){
				jQuery('.sort').trigger('sortEnd');
			 });
			 


		});
		jQuery('table.pager').tablesorterPager({
			container :jQuery('.tablePager')
		});

	});

// Index Banner
// Cycle-slide

jQuery('.cycle-slide').each(function(){
	jQuery(this).find('.cycle-slide-show').cycle({
		fx: 'fade',
		pager: '.cycle-slide-nav',
		speed: 2000,
		timeout: 7000,
		pause: 1,
		cleartype:  1,
		pagerAnchorBuilder: function(idx, slide) { 
			// return selector string for existing anchor 
			return '.cycle-slide-nav li:eq(' + idx + ') a'; 
		} 
	});
});

// index
// Event switch

jQuery('.col').each(function(){
	jQuery(this).find('.panelholder').hide();
	jQuery(this).find('.panelholder:first').show();
	jQuery(this).find('.eventThumb a:first').addClass('now');
	jQuery(this).find('.eventThumb a').click(function(){
		var targetP = jQuery(this).attr('href');
		var who = jQuery(this).parentsUntil('.col');
		jQuery(who).find('a').removeClass('now');
		jQuery(this).addClass('now');
		jQuery(who).siblings('.panelholder').hide();
		jQuery(who).siblings(targetP).show();
		return false;
	});
});

// Archives List Sidebar

jQuery('.archives > li > a').click(function(){
	jQuery(this).parent().toggleClass('expand');
	jQuery(this).parent().find('ul').delay(200).slideToggle();
	jQuery(this).parent().siblings().removeClass('expand').find('ul').slideUp();
	return false;
});


// Float Description
jQuery('.floatdes').mousemove(function(e){
	var title = jQuery(this).html();
	var note = jQuery(this).attr('note');
	if(note){
		var fh = jQuery('#floatDes').height();
		var st = jQuery(document).scrollTop();
		var f = e.pageY - st;
		if ( f < fh ){
			fh = -16;
			jQuery('.arr-top').show();
			jQuery('.arr-bottom').hide();
		} else {
			f = e.pageY - st;
			jQuery('.arr-top').hide();
			jQuery('.arr-bottom').show();
		}
		jQuery('#floatDes .holder h3').html(title);
		jQuery('#floatDes .holder p').html(note);
		
		jQuery('#floatDes')
		.show()
		.css( "left",e.pageX + 16 )
		.css( "top",e.pageY - fh );
	}

}).click(function(){
	return false;
}).mouseout(function(){
	jQuery('#floatDes').hide();
});
	
	



});
