$(document).ready(function(){
	$('#main').css({ opacity : 0 });

	//hide titles on hover
	$("a,img").removeAttr("title");

	//hide youtube videos behind the header.
	$("embed").attr("wmode", "transparent");
	var embedTag;
	$("embed").each(function(i) {
		   embedTag = $(this).attr("outerHTML");
		   if ((embedTag != null) && (embedTag.length > 0)) {
				   embedTag = embedTag.replace(/embed /gi, "embed wmode='transparent' ");
				   $(this).attr("outerHTML", embedTag);
		   } else {
				   $(this).wrap("<div></div>");
		   }
	});
	
	
	$('.case-thumb').css({ opacity : .2 });
	$('.case-hover').css({ opacity : 0 });
	$(".footer-icons").css({ opacity : .75});
	$('#pager').css({ opacity : .7 });
	//$('#prev, #next').css({ opacity : .4});
	$(window).load(
		function() {
		  $('#main').delay(200).animate({ opacity : 1}, 200);
		}
	);
	
	//add arrow to read more links
	$('.read-more').append('<img src="' + templateUrl + '/img/more-end.jpg" class="read-more-after" />');
	
	var wid = $(window).width();
	var hite = $(window).height();
	var conWid = 200;
	var conHite = 200;
	var $allTubes = jQuery(".oddBoxLink");
	
	//$("#popupvideo").width(wid).height(hite).css({ "position" : "absolute"});	
	//lighbox click
	//$('#lightbox-container-image-box').css({ 'max-width' : '800px'});
	$('.oddBoxLink').click(function(){
		OddBox.init();
		showVid(this.href, "something",wid ,hite);
		//$('html, body').css({'overflow' : 'hidden'});
		return false;
	});
	
	//slideshow
	$('#slideshow').cycle({ 
		pagerAnchorBuilder: function(index, el) {
			return '<div class="pager"></div>'; // whatever markup you want
		},	
		fx:     'fade', 
		timeout: 5000,
		next:   '#next', 
		prev:   '#prev', 
		pager:  '#pager', 
		after:   onAfter 
	});
	
	$('#sortby li').first().addClass('current-menu-item');
	
	$('#sortby li').click(function(){
		$('#sortby li').removeClass('current-menu-item');
		//var old = $('#menu-portfolio li.current-menu-item').index();
		//var old = $("#menu-portfolio li").index($(".current-menu-item")); 
		$('#sortby li').each(function(i,v){
			//var old = $(this).hasClass("current-menu-item");
					var matchingDiv = $('#menu-portfolio li').closest('.current-menu-item');
		});
			
		$(this).addClass('current-menu-item');

		 var n = $("#sortby li").length;
		 var curr = $("#sortby li").index(this) + 1;
		 //alert(curr + ' out of ' + n);

	});
	
	//sort work page by category ajax call and re bind events on load
	$('#sortby a').click(function(e) {
		var thisurl = $(this).attr('href');
		$('.case').animate({ opacity : 0 }, 200);
		$('.all-cases').load(thisurl + " .all-cases", function() {
			Cufon.refresh();
			$('.case-hover').css({ opacity : 0 });
			$('.case-hover').hover(caseover, caseout);
		});
		e.preventDefault();  
	});
	
	
	//events
	$('.case-hover').hover(caseover, caseout);
	$('.oddfellows .case').hover(oddcaseover, oddcaseout);
	$(".read-more").hover(readover, readout);	
	$(".footer-icons").hover(footerover, footerout);	
	
});
//hovers on footer links
	function footerover() {
		$(this).stop().animate({ opacity : 1}, 200);
	} 
	function footerout() {
		$(this).stop().animate({ opacity : .75}, 200);
	}
//hovers on read more links
	function readover() {
		$('img', this).attr('src', templateUrl + '/img/more-end-hover.jpg');
		$(this).attr('src', templateUrl + '/img/more-end-hover.jpg');
	} 
	function readout() {
		$('img', this).attr('src', templateUrl + '/img/more-end.jpg');
		$(this).attr('src', templateUrl + '/img/more-end.jpg');
	}
//hovers on cases
	function caseover(){
		$(this).stop().animate({opacity :'1'},{queue:false,duration:160});
	}
	function caseout() {
		$(this).stop().animate({opacity :'0'},{queue:false,duration:160});
	}
//hovers on oddfellows
	function oddcaseover(){
		$(".case-title", this).stop().animate({top:'0'},{queue:false,duration:160});
	}
	function oddcaseout() {
		$(".case-title", this).stop().animate({top:'195px'},{queue:false,duration:160});
	}

	
//add lightbox functionality
	function showVid(vidPath, caption, conWid, conHite){
		OddBox._start(vidPath, caption, Number(conWid), Number(conHite) );
	}
			

function onAfter(){
	//do something
	return true;
	//$('#main').css({ background : '#000'})
}
