$(document).ready(function() {
	// commonly used elements to reduce js lookups
	var categories = $('ul#categories');
	var main_content = $('div.main-content');
	var doc = $(window);
	docWidth = doc.width();

	// for home page
	if ($('.site-container').hasClass('home')) {

		// randomize logo, logo changer
		var numberOfLogos = 6;
		var currentLogo = Math.ceil(Math.random()*numberOfLogos);
		$('.home #logo img').attr('src','images/logo_'+currentLogo+'.jpg');
		$('.home #logo').click(function(e){
			e.preventDefault();
			if (currentLogo == numberOfLogos) currentLogo = 1;
			else currentLogo++;
			$(this).children('img').attr('src','images/logo_'+currentLogo+'.jpg');
		})

		$('body,html').css('overflow','hidden');

		$('#slider-container').css({'width':5*docWidth,'left':-4*docWidth});
		$('#outer-container').css({'width':docWidth,'left':4*docWidth});
		$('#thmvmnt-site').css({'width':docWidth});
		$('#blank').css({'width':3*docWidth, 'left':docWidth});
		$('#thmvmnt-logo').click(function(){
			$('#slider-container').animate({'left':0},1200,'swing',function(){
				$('#tc-logo').fadeIn('fast');
			})
		})
		$('#tc-logo').click(function(){
			$(this).fadeOut('fast',function(){
				$('#slider-container').animate({'left':-4*docWidth},1200,'swing');
			})
		})
		
		if (navigator.userAgent.indexOf("MSIE")>=0) {
			$(window).resize(function(){ 
				if ($(window).height()>590) $('.email').css('margin-top',$(window).height()-590);
				if ($('.site-container').hasClass('home')) {
					docWidth=doc.width();
					$('#slider-container').css({'width':5*docWidth,'left':-4*docWidth});
					$('#outer-container').css({'width':docWidth,'left':4*docWidth});
					$('#thmvmnt-site').css({'width':docWidth});
					$('#blank').css({'width':3*docWidth, 'left':docWidth});
				}
			});
		} else {
			$(window).resize(function(){ 
				if ($(window).height()>590) $('.email').css('margin-top',$(window).height()-590);
				if ($('.site-container').hasClass('home')) {
					docWidth=doc.width();
					$('#slider-container').css({'width':5*docWidth,'left':-4*docWidth});
					$('#outer-container').css({'width':docWidth,'left':4*docWidth});
					$('#thmvmnt-site').css({'width':docWidth});
					$('#blank').css({'width':3*docWidth, 'left':docWidth});
				}
			});
		}
	}
	
	if ($('body').hasClass('portfolio')){
		// for scrolling positions
		var vert_positions = new Array(); 
		for (var i=0; i<main_content.children().length; i++) vert_positions[i] = main_content.children(':eq('+i+')').offset().top+300;

		var rounded_position = Math.round((main_content.scrollTop()/591))+1;
		
		$('html,body').scrollTop(0);
		categories.find('ul').css("opacity","1");              // needed for slideups in IE
		$('ul#categories li.selected').children('ul').slideDown("normal");
		categories.find('li.selected li.1').addClass("selected");
		$('.image-and-desc:first .mult-img-nav').fadeIn('fast');
		
		// change categories
		categories.children('li').children('p').click(function(){
			var sel_category = $(this).parent().attr("class").split(" ")[1];
			main_content.css('opacity',0);
			categories.children('li.selected').children('ul').slideUp("normal",function(){
				categories.children('li.selected').children('ul').find('li.selected').removeClass('selected');
				categories.children('li.selected').removeClass('selected');
				categories.children('li.'+sel_category).addClass('selected').children('ul').slideDown("normal",function(){
					$('html,body').scrollTop(0);
					$('this li').removeClass("selected");
					main_content.load("main_content_normal.php?category="+sel_category,{},function(){
						// on loading of new category
						$('.main-content img.multiview').hide();
						$('.main-content img.multiview:nth-child(2)').show();
						for (var i=0; i<main_content.children().length; i++) vert_positions[i] = main_content.children(':eq('+i+')').offset().top+300;
						main_content.css('opacity',1);
						categories.find('li.selected li.1').addClass("selected");
						$('.image-and-desc:first .mult-img-nav').fadeIn('fast');
					});
				});
			});		
		});
		
		categories.find('ul.pieces li').click(function(){        // scrolling function for pieces
			var i = parseInt($(this).attr("class"));
			$('html,body').scrollTo('.image-and-desc:eq('+(i-1)+')',600);
		})
		function f_scrollTop() {
			return f_filterResults (
				window.pageYOffset ? window.pageYOffset : 0,
				document.documentElement ? document.documentElement.scrollTop : 0,
				document.body ? document.body.scrollTop : 0
			);
		}
		function f_filterResults(n_win, n_docel, n_body) {
			var n_result = n_win ? n_win : 0;
			if (n_docel && (!n_result || (n_result > n_docel)))
				n_result = n_docel;
			return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
		}

		doc.scroll(function(){        	   // highlight displayed piece in navigation
			rounded_position = 0;
			var temp = f_scrollTop();
			while (temp>vert_positions[rounded_position]) rounded_position++;
			$('.image-and-desc:not(:eq('+rounded_position+')) .mult-img-nav').hide();
			$('.image-and-desc:eq('+rounded_position+') .mult-img-nav').fadeIn('fast');
			rounded_position++;
			categories.find('li.selected li.selected').removeClass("selected");
			categories.find('li.selected li.'+rounded_position).addClass("selected");
		})

		$('.main-image').livequery('click',function(){
			var i = $('.main-image:visible').index(this);
			$('html,body').scrollTo('.image-and-desc:eq('+i+')',600);	
		})
		
		$('.main-content img.multiview').livequery('click',function(){
			if($(this).parents('.image-and-desc').find('.mult-img-nav').is(':visible')){
				var temp = $(this).parent().children('img.multiview').index(this)+1;
				if ($(this).is(":last-child")) { 
					$(this).hide().parent().children("img:first").show();
					$(this).parents('.image-and-desc').find('.mult-img-nav li:not(:eq('+temp+'))').removeClass('active');			
					$(this).parents('.image-and-desc').find('.mult-img-nav li:eq(0)').addClass('active');
				} else {
					$(this).hide().next().show();
					$(this).parents('.image-and-desc').find('.mult-img-nav li:not(:eq('+temp+'))').removeClass('active');			
					$(this).parents('.image-and-desc').find('.mult-img-nav li:eq('+temp+')').addClass('active');
				}
			}
		})
		$('.main-content ul.mult-img-nav li').livequery('click',function(){
			$parent = $(this).parent();
			var temp = $parent.children('li').index(this);
			$parent.children('.active').removeClass('active');
			$(this).addClass('active');
			$(this).parents('.image-and-desc').find('img.multiview:not(:eq('+(temp)+'))').hide();
			$(this).parents('.image-and-desc').find('img.multiview:eq('+(temp)+')').show();
		})
	}
	
	if ($('body').hasClass('daily-image') || $('body').hasClass('blog')){
		$('.post-meta-data a').fadeTo(1, 0.5);
		$('.post-meta-data a').hover(function(){
			$(this).fadeTo('fast',1.0);
		},function(){
			$(this).fadeTo('fast',0.5);	
		});
		
		$('.post-meta-data a').click(function(){
			$(this).parent().parent().siblings('.comments-template').slideToggle('fast');
			return false;
		})
	}

	$('.header .main-nav li:eq(0) a').attr('title',"A selection of my past work.");
	$('.header .main-nav li:eq(1) a').attr('title',"Trying to create a cool image each day to get the ol' <em>jus de creatif</em> flowing.");
	$('.header .main-nav li:eq(2) a').attr('title',"Mish-mash of thoughts, photos, links, etc.");
	$('.header .main-nav li:eq(3) a').attr('title',"Nigerian businessmen who need help transferring money: look no further, I'm your guy!");
	$('.category a, .header a').tooltip({ 
			track: true, 
			delay: 0, 
			showURL: false, 
			fade: 250 
	});	
})
