
	jQuery(function($) {
		
		/* font replace */
		
		Cufon.replace('#menu li a.menu, h1,h2,h3', { fontFamily: 'MyriadPro', hover: true });
		
		/* center all ads */
		
		$('#categories a').each(function() { 
			var i = $(this).find('img').attr('src');
			$(this).find('img').remove();
			$(this).css({'background-image':'url(' + i + ')'});
		});
		
		/* top images */
		
		$('#topimg img').hide().eq(0).addClass('show').show();
		
		if($('#topimg img').length > 1) {
		
			window.setInterval(function () {
				var thisi = $('#topimg img.show');
				var nexti = thisi.next('img');
				if(nexti.length == 0) nexti = $('#topimg img:first');
				thisi.removeClass('show').stop().fadeOut(1000);
				nexti.addClass('show').stop().fadeIn(1000);
			},5000);
			
		}
		
		/* ads */
				
		window.setInterval(function () {
			
			if($('.cat1 .adholder a').length > 1) {
				$('.cat1 .adholder').fadeOut(1000,function() {
					$('.cat1 .adholder a').slice(0,1).detach().appendTo($('.cat1 .adholder'));
					$('.cat1 .adholder').fadeIn(1000);
				});
			}
			
			if($('.cat2 .adholder a').length > 2) {
				$('.cat2 .adholder').fadeOut(1000,function() {
					$('.cat2 .adholder a').slice(0,2).detach().appendTo($('.cat2 .adholder'));
					$('.cat2 .adholder').fadeIn(1000);
				});
			}
			
			if($('.cat3 .adholder a').length > 2) {
				$('.cat3 .adholder').fadeOut(1000,function() {
					$('.cat3 .adholder a').slice(0,2).detach().appendTo($('.cat3 .adholder'));
					$('.cat3 .adholder').fadeIn(1000);
				});
			}

			if($('.cat4 .adholder a').length > 4) {			
				$('.cat4 .adholder').fadeOut(1000,function() {
					$('.cat4 .adholder a').slice(0,4).detach().appendTo($('.cat4 .adholder'));
					$('.cat4 .adholder').fadeIn(1000);
				});
			}
			
		},5000);
			
		/* menu */
				
		var aw = $('#menu > ul > li.active').width();
		var ao = $('#menu > ul > li.active').position();
		var aa = Math.floor(aw/2) + ao.left + 7;
		
		$('#menu > ul').css('background-position', aa + 'px 73px');
		
		$('#menu > ul > li').hover(
			function() {
				$(this).addClass('hover');
				$('#menu ul li:not(.hover) ul').stop(true,true).fadeOut();
				$(this).find('ul').stop(true,true).fadeIn();
				var nw = $(this).width();
				var no = $(this).position();
				var na = Math.floor(nw/2) + no.left + 7;				
				$('#menu > ul').stop(true,true).animate({'background-position': na + 'px 73px'},200);
			},
			function() {
				$(this).removeClass('hover');
				if(!$(this).hasClass('active')) $(this).find('ul').stop(true,true).fadeOut();
			}
		);
		
		$('#menu > ul').hover(
			function() {},
			function() { 
				$('#menu > ul').stop(true,true).animate({'background-position':aa + 'px 73px'},200); 
				$('#menu .active ul').stop(true,true).fadeIn();
			}
		);
		
		/* scroll */
		
		if($('#scroll li').length > 5) {
			$('#scroll .scrollDown span').addClass('can');
			var spg = 0;
			var spgs = Math.floor($('#scroll li').length / 5); 
			$('#scroll .scrollDown').click(function() { 
				if(spg < spgs) {
					spg++;
					$('#scroll .scroller ul').animate({'top': - spg * 115}, 300);
					$('#scroll .scrollUp span').addClass('can');
					if(spg == spgs) $('#scroll .scrollDown span').removeClass('can');
				}
				return false;
			});
			$('#scroll .scrollUp').click(function() {
				if(spg > 0) {
					spg--;
					$('#scroll .scroller ul').animate({'top': - spg * 115}, 300);
					$('#scroll .scrollDown span').addClass('can');
					if(spg == 0) $('#scroll .scrollUp span').removeClass('can');
				}
				return false;
			});
		}	
    
		/* footer stick */
		
		var ws = $(window).height();
		var bs = $('#site-inner').height();
				
		if(ws > bs) {
			$('#wrapper').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
			$('#container').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
			$('#site').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
		}
	
		
		$(window).resize(function() {
  			ws = $(window).height();
			bs = $('#site-inner').height();
					
			if(ws > bs) {
				$('#wrapper').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
				$('#container').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
				$('#site').css({'min-height': ws + 'px', 'height': 'auto !important', 'height': ws + 'px' });
			}
			
		});
		
		/* interactive map */
		
		// create universities
		var $mapt = '';
		var $mapu = '';
		$.each(universitylist,function($key,$val) {
			$mapt += '<li rel="#u' + $key +'">' + $val + '</li>';
			$mapu += '<ul id="u' + $key +'"></ul>';
		});
		$('#mapt ul').html($mapt).find('li:first').addClass('mapta');
		$('#mapscroll').html($mapu);
				
		// create tabs and maps
		var $btou = new Array;
		var $mapc = new Array;
		var $maptabs = '';
		var $mapmaps = '';
		var $mpz  = new Array;
		$.each(buildinglist,function($key,$val) {
			$btou[$val.building_id] = $val.university_id;
			$mapc[$val.university_id] = '';
			$maptabs += '<li><a rel="#map' + $val.building_id + '">' + $val.building_name + '</a><span></span></li>';
			$mapmaps += '<div id="map' + $val.building_id + '" class="mphol"><img src="' + $val.building_picture + '" alt="" usemap="#xmap' + $val.building_id + '" /><map name="xmap' + $val.building_id + '" id="xmap' + $val.building_id + '"></map></div>';
			$mpz[$val.building_id] = '';
		});
		$('#maptabs ul').html($maptabs);
		$('#mapmaps').html($mapmaps);
				
		// create company list
		$companylist = new Array;
		$cps = new Array;
		$.each(companylist,function($key,$val) {
			$companylist[$val.id] = $val;
			var $cname = $val.name;

			
			
			
			var $cid = $val.id;//$key;
			$.each($val.locations,function($key2,$val2) {
				$mapc[$btou[$val2.building_id]] += '<li><a rel="' + $cid +'">' + $cname + '</a></li>';
				if($val2.mapcoords != '' && $val2.building_id > 0) $mpz[$val2.building_id] += '<area rel="' + $cid +'" shape="poly" coords="' + $val2.mapcoords + '" alt="" />'
			});
			$cps += '<div id="ca' + $cid + '">';
			if($val.logo != '') $cps += '<em style="background-image:url(' + $val.logo + ');"></em>';
			$cps += '<h2>' + $cname + '</h2>';
			if($val.url != '') $cps += '<a class="murl" href="' + $val.url + '">' + $val.url.replace('http://','') + '</a>';
			if($val.announcement != '') $cps += '<span class="announcement">' + $val.announcement + '</span>';
			$cps += '</div>';
		});
		$('#mapdata').append($cps).find('div:first').show();
		
		$.each($mpz,function($key,$val) { 
			$('#xmap' + $key).html($val);
		});
		
		$.each($mapc,function($key,$val) { 
			$('#u' + $key).html($val);
		});
		$('#mapscroll > ul:first').show();
		
		// set tabs functionality
		$('#maptabs li').hover(
			function() { $(this).addClass('mhover'); },
			function() { $(this).removeClass('mhover'); }
		);
		$('#maptabs li').live('click',function() {
			$(this).addClass('mactive').siblings().removeClass('mactive');
			var $map = $(this).find('a').attr('rel');
			
			if(!$('#mapt li[rel=#u' + $btou[$map.substr(4)] + ']').hasClass('mapta')) {
				$('#mapt li[rel=#u' + $btou[$map.substr(4)] + ']').click(); 
			}
			
			$('#mapdata').removeClass('lock');
			$('#mapdata div:visible').stop(true,true).fadeOut();
			$('#ca0').stop(true,true).fadeIn();
			$('#mapabout').stop(true,true).fadeOut();
			$('#mapwrap .mclose').stop(true,true).fadeOut();
			$('#mapwrap .mclose2').stop(true,true).fadeOut();
			$('#mapmaps > div.mphol').stop(true,true).fadeOut();
			$($map).stop(true,true).fadeIn();
			return false;
		});
		$('#mapt li').live('click',function() {
			$(this).addClass('mapta').siblings().removeClass('mapta');
			var $mu = $(this).attr('rel');
			$('#mapscroll ul').hide(function() {
				$($mu).show(function() {
					$('#mapscroll').jScrollPane();
				});
			});
			return false;
		}).hover(
			function() { $(this).addClass('mapth'); },
			function() { $(this).removeClass('mapth'); }
		);
		
		$('#mapscroll a,#mapmaps area').live('click',function() {
			var $id = $(this).attr('rel'); 
			$.ajax({
  				url: '/ajax.php',
  				data: 'id=' + $id,
  				success: function(data) {
  					if($('#mapdata').hasClass('lock')) {
						$('#mapdata div:visible').stop(true,true).fadeOut();
						$('#ca0').stop(true,true).fadeOut();
						$('#ca' + $id).stop(true,true).fadeIn();
					}
  					$('#mapdata').addClass('lock');
  					var $gcp = $('#mapabout').data('jsp');
  					$gcp.getContentPane().html('<div class="minner">' + data + '</div>');
  					Cufon.replace('#mapabout h3', { fontFamily: 'MyriadPro', hover: true });
  					$('#mapmaps div.mphol').stop(true,true).fadeOut();
  					$('#mapabout').stop(true,true).fadeIn();
  					$('#mapwrap .mclose').stop(true,true).fadeIn();
  					$('#mapwrap .mclose2').stop(true,true).fadeIn();
  					$gcp.reinitialise();
  					$gcp.scrollTo(0,0);
  				}
  			});
			return false;
		}).hover(
			function() { 
				var $clr = $(this);
				var $cid = $(this).attr('rel');
				if(!$('#mapdata').hasClass('lock')) {
					$('#mapdata div:visible').stop(true,true).fadeOut();
					$('#ca0').stop(true,true).fadeOut();
					$('#ca' + $cid).stop(true,true).fadeIn();
				}
				$.each($companylist[$cid].locations,function($key,$val) {
					if($val.centcoords.length > 2) {
						$('#maptabs a[rel=#map' + $val.building_id + ']').closest('li').addClass('itshere');
					}
					
					if($('#maptabs a[rel=#map' + $val.building_id + ']').closest('li').hasClass('mactive')) {
						var $coords = $val.centcoords.split(','); 
						
						$('#point').fadeOut(600,function() { $(this).remove(); });
						
						if( $coords.length > 1 ) {
						
							if(!$clr.is('area')) {
								$('#map' + $val.building_id + ' > div').append('<div id="point"></div>');
								$('#point').css({'top':parseInt($coords[1])-14+'px','left':parseInt($coords[0])-14+'px'}).fadeIn(600);
							}
						
							$('#arrow').fadeOut(600,function() { $(this).remove(); });
							$('#map' + $val.building_id + ' > div').append('<div id="arrow"></div>');
							$('#arrow').css({'left':parseInt($coords[0])-10+'px'}).fadeIn(600);
						}
					}
					
				});
			},
			function() { 
				if(!$('#mapdata').hasClass('lock')) {
					$('#mapdata div:visible').stop(true,true).fadeOut();
					$('#ca0').stop(true,true).fadeIn();
				}
				$('#maptabs li').removeClass('itshere');
				$('#point').remove();
				$('#arrow').remove();
			}
		);
		
		$('#mapwrap .mclose,#mapwrap .mclose2').live('click',function() {
			$('#mapdata').removeClass('lock');
			$('#mapdata div:visible').stop(true,true).fadeOut();
			$('#ca0').stop(true,true).fadeIn();
			$('#mapabout').stop(true,true).fadeOut();
			$('#mapwrap .mclose').stop(true,true).fadeOut();
			$('#mapwrap .mclose2').stop(true,true).fadeOut();
			var $map = $('#maptabs li.mactive').find('a').attr('rel');
			$('#mapmaps > div.mphol').stop(true,true).fadeOut();
			$($map).stop(true,true).fadeIn();
			return false;
		});
		
		Cufon.replace('#maplist h4,#mapt li,#mapscroll a,#mapwrap .mclose,#mapdata a,#mapdata span', { fontFamily: 'MyriadPro', hover: true });
		
		$('#mapscroll').jScrollPane({ showArrows: true, maintainPosition: false });
		$('#mapabout').jScrollPane({ showArrows: true, maintainPosition: false });
		
		$('img[usemap]').maphilight({
			fill: true,
			fillColor: '000000',
			fillOpacity: 0.33,
			stroke: false
		});
			
		$('#mapmaps .mphol').hide();
		$('#maptabs li').eq(0).click();
		
	});
