$(document).ready(function(){
	$.preload = function(){
		for(var i = 0; i <arguments.length; i++){
			$("<img>").attr("src", arguments[i]);
			console.log( "Preloading: " + arguments[i] );
		}
	}

	//Megamenu hover events
	$("#nav li.has-menu>a").mouseenter(function(){
		$(this).parent().addClass("show-menu");
	});
	$("#nav li.has-menu ul li").mouseenter(function(){
		$(this).parent().parent().addClass("show-menu");
	});
	$("#nav li.has-menu").mouseleave(function(){
		$(this).removeClass("show-menu");
	});

	// Add class "odd" to odd-numbered list items and table rows
//	$("table,ul").find("tr:odd,li:odd").addClass("odd");

	
	// Some search field eye candy
	$("#auto-search")
	.css("opacity",.7)
	.focus(function(){
		if( $(this).attr("value") == $(this).attr("title") )
			$(this).attr("value", "").css("opacity",1);
	})
	.blur(function(){
		if( $(this).attr("value") == "" )
			$(this).attr( "value", $(this).attr("title") ).css("opacity",.7);
	});

	// Expanding Articles
	$('.expanding li h4').click(function() {
		$(this).parent().toggleClass('open').find('.expand-content').slideToggle();
	});
	
	$('table.expanding td').click(function() {
		$(this).toggleClass('open').find('.expand-content').slideToggle();
	});

	// Clickable list item hack (there, I said it)
	$("ul.resource-list li").hover(function(){
		link = $(this).find("a.clickable");
		if( link.html() == null ){
			//console.log("a.clickable not found. Click event was not added");
		} else {
			//console.log("Click event added");
			$(this).addClass("hover clickable").css({cursor:"pointer"}).click(function(){
				window.location = link.attr("href");
			});
		}
	},function(){
		$(this).removeClass("hover clickable");
	});

	$('li.teacher').click(function(){
		var url = $(this).find('span.name a').attr('href')
		window.location = url;
	});
	
	/* Thumbnail Shadow */
	
	$(".shadow-contain").each(function() {
		var shadowWidth = $(this).children("img").width();
		$(this).width(shadowWidth);
	});
	
	/* Z-INDEX test */
	
	$(function() {
		var zIndexNumber = 1000;
		$('#wrap div').each(function() {
			$(this).css('zIndex', zIndexNumber);
			zIndexNumber -= 10;
		});
	});
	
	/* Q-TIP TOOLTIPS  */
	
	/* GRIDS */
	   $('.grid li .qtip').each(function()
		   {
		      $(this).qtip({
		      	content: $(this).find('.tip'),
				
				position: {
						corner: {target: 'rightMiddle', tooltip: 'leftMiddle'}, /* rightMiddle 40px 30px */
						adjust: { x: -35, y: 0 }
 				},
				
				hide: { when: 'mouseout', fixed: true },
				style: {
						width: 300,
						tip: 'leftMiddle',
				        border: {
							width: 1,
							radius: 5,
							color: '#DDDDDD'	
						}
					
				}
		      });
		
		   });

	$("a.view-cart-button").click(function(){
		$("#cart-dropdown").hide();
//		tb_show( "", "#WHYDOESTHISNOTWORK", "height=400&width=620" );
		//TB_iframe=true&amp;height=400&amp;width=620
		return false;
	});
	
	$('ul.cycle').each(function() {
		var cycleTimeout = ( $(this).attr('timeout') !== undefined ) ? parseInt($(this).attr('timeout')) * 1000 : 8000
		$(this).cycle({
			fx: 'fade',
			speed: 800,
			// speedIn: 800,
			// speedOut: 1600,
			pause: 1,
			easing: 'linear',
			timeout: cycleTimeout,
			manualTrump: true,
			pager: '.cycle-control'
		});
	});
	var numOfSlides = $('ul.cycle').children().size()*16+10+"px";
	$('.cycle-control').css("width",numOfSlides);//should be number of slides * 16, but for some reason it needs more...
	
});

function openPopupWindow(url, w, h){
	//"toolbar=no,menubar=no,scrollbars=no,statusbar=no,height="+strHeight+",width="+strWidth+",left="+leftStr+",top="+topStr+"";
	var params =  "width="+w+",height="+h+",toolbar=no,location=no,status=no,scrollbars=no,menubar=no,resizable=yes"
	var w = window.open(url, "pop_window", params);
	w.focus();
}