var columns;

$(document).ready(function(){
	columns=$("div.column");
	imgs_resize=columns.find('img').not('.noresize');
	columnprep();

	navprep();
	
	
     $("#slides_2 div.slide").attr("style","");
    $("#slides_2 img").attr("height",$("#slides_2").outerWidth()*.66);
	slideshow();
	
	
	$("#homework a").hover(function(){
		$(this).find('span.over').fadeIn(250);
		
	}, function(){	
		$(this).find('span.over').fadeOut(250);
		
	});
	
	
	
	
});





$(window).resize(function(){
    columns=$("div.column");
	imgs_resize=columns.find('img').not('.noresize');
	columnprep();

	navprep();
	
	
    
    
     $("#slides_2 div.slide").attr("style","");
    $("#slides_2 img").attr("height",$("#slides_2").outerWidth()*.66);
	slideshow();
	
	
});





function slideshow(){
   
    
	$('#slides_2').cycle('destroy').css('height','auto');
    
    
    
	$('#slides_2').cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 0, 
		next:   '#next', 
		prev:   '#prev' 
	}).css({'width':'100%'});
	
	
	$("#slides_2 div.slide img").live("click",function(){
		$("#next").trigger("click");	
	});
}




function navprep(){
	
	var nava=$("#nav ul a");
	var grida=$('ul.grid a');
	var gridli=$('ul.grid li');
	
	grida.hover(function(){
		$(this).find('span.over').stop().fadeTo(250,1);
		var classList = $(this).attr("class").split(/\s+/);
		
		//alert(classList.length);
		for (i = 0; i < classList.length; i++) {
		   s=classList[i];
		   nava.filter('.'+s).addClass("on");
		}
	}, function(){	
		$(this).find('span.over').fadeTo(250,0);
		nava.removeClass("on");	
	});

	nava.hover(function(){
		var classList = $(this).attr("class").split(/\s+/);
		
		//alert(classList.length);
		for (i = 0; i < classList.length; i++) {
		   s=classList[i];
		   
		   fades=grida.filter('.'+s).find('span.over');
		   fades.stop().fadeTo(250,1);//
		}
	}, function(){	
        var classList = $(this).attr("class").split(/\s+/);
		
		//alert(classList.length);
		for (i = 0; i < classList.length; i++) {
             s=classList[i];
            fades=grida.filter('.'+s).find('span.over');
            fades.stop().fadeTo(250,0);//
        }
		//grida.removeClass("on");	
	});
}



function columnprep(){
	
	wiff=$(window).width()-60;
	if (wiff <= 1260) {
		$("#wrapper, #copyright div").css("max-width",wiff + 'px');

        if (wiff<960) wiff=960; 
	} else {
		$("#wrapper, #copyright div").css("max-width",'1260px');
		wiff=1260;
	}
	//$("#copyright").css("top",($(window).height() - 32) + 'px');
	// see if it's firefox
	
	
	
	if (typeof grid == "undefined") { 
		imgs_resize.attr("height",((wiff/4)-20)*0.666); 
	} else {
		imgs_resize.attr("height",((wiff*.75)-20)*0.666); 
	}
	
	// find the tallest height and id of tallest
	var th=columns.tallestSize();
	var tallest=columns.tallest();
	tallest.addClass("tallest");
	

	
	// get window height
	var vp = $(window).height();
	
	// find the columns and fiddle with the widths
	var cols= new Array();
	var i =0;
	columns.each(function(){
		
		
		if($(this).height() > vp) $(this).removeClass("fixed");
		else $(this).addClass("fixed");
		if($(this).height() < th) {
			$(this).addClass("shift");
		
			cl=($(this).hasClass("shift")) ? true:false;
			fx=($(this).hasClass("fixed")) ? true:false;
		
			cols[i]=new Array($(this),$(this).height(),cl,fx);
			i++;
		}
		
		w=$(this).width() ;
		//$(this).width(w + 'px');
		
		//$(this).find('a.img').height((w*0.666))
	});
	
	
	
	var fixd=columns.filter(".fixed");
	

			 
			 
	var shifts = columns.filter(".shift");
	
	var sd=th-vp;

	
	
	
	 $(window).scroll(function () { 
		var st= $(window).scrollTop();
				
      
		//if(!ff) 
		//fixd.css("top", st + "px");
		fixd.animate({top:st},{queue:false, duration:300});
		
		
		for (i=0;i<cols.length; i++){
			if(cols[i][3]==false){
				diff=sd-(cols[i][1]-vp);
				ratio=diff/sd;
				//cols[i][0].css("top", (st*ratio) + "px");
				
				cols[i][0].animate({top:st*ratio },{queue:false, duration:300});
				
			} else {
				cols[i][0].animate({top:st},{queue:false, duration:300});
				
			}
			
		}
		
		
		
		
    });
		
	
}
