// check if there is a hash in the url indicating we should redirect
var hash = window.location.hash.replace('#', '');
if(hash != ''){
	if(hash == 'thehome'){
		window.location.href = base_url;
	} else {
		if(hash != 'comments') {
			window.location.href = base_url + hash;
		}
	}
}

function pollHash() {
    if(hash !== location.hash.replace('#', '')) {
        hash = location.hash.replace('#', '');
        window.location.reload();
    }
}

setInterval(pollHash, 100);

jQuery.noConflict();
jQuery(function($){
	highlightDisabled = false;
	viewHeight = $(window).height();
	
	// setup all pages heights
	setHeight = function(i, elem){
		$elem = $(elem);
		
		if($elem.outerHeight() < viewHeight){
			var height = $.browser.msie ? 'height' : 'min-height';
			$elem.css(height, viewHeight - 80);
		}
	}
	$('.content-slide').each(setHeight);
	$(window).resize(function(){
		$('.content-slide').each(setHeight);
	});
	
	// cufon setup
	Cufon.replace('h1,h2,h1 a,h2 a,h3,h4,h5,h6..s3sliderImage span');
	
	// portfolio setup
	
	curve = function(el, inOut){
		$('img', el).stop().animate({
			left: inOut == 'out' ? '320px' : '0px', 
			top: inOut == 'out' ? '240px' : '0px'
		}, {
			specialEasing: {
				left: 'easeOutQuad', top: 'easeInQuad'
			},
			queue: false,
			duration: 400
		});
	}
	
	left = function(el, inOut){
		$('img', el).stop().animate({
			left: inOut == 'out' ? '-320px' : '0px'
		}, {
			specialEasing: {
				left: 'easeOutQuad'
			},
			queue: false,
			duration: 400
		});
	}
	
	right = function(el, inOut){
		$('img', el).stop().animate({
			left: inOut == 'out' ? '320px' : '0px'
		}, {
			specialEasing: {
				left: 'easeOutQuad'
			},
			queue: false,
			duration: 400
		});
	}
	
	up = function(el, inOut){
		$('img', el).stop().animate({
			top: inOut == 'out' ? '-240px' : '0px'
		}, {
			specialEasing: {
				top: 'easeInOutQuad'
			},
			queue: false,
			duration: 400
		});
	}
	
	down = function(el, inOut){
		$('img', el).stop().animate({
			top: inOut == 'out' ? '240px' : '0px'
		}, {
			specialEasing: {
				top: 'easeInOutQuad'
			},
			queue: false,
			duration: 400
		});
	}
	
	$('.portfolio-item').each(function(){
		var func = $('.anim', this).text();
		$(this).hover(function(){
			eval(func+"(this, 'out')")
		}, function(){
			eval(func+"(this, 'in')")
		});
	});
	
	// submenu
	$('#menu>li').not('.current_page_item,:has(.current_page_item)').find('ul').slideUp();
	
	// setup ajax
	$("#ajax-loader").ajaxStart(function(){
		$(this).show();
	});
	$("#ajax-loader").ajaxStop(function(){
		$(this).hide();
	});
	
	$('.s3sliderImage span').css('opacity', 0.7);
	
	$('#s3slider').s3Slider({
		timeOut: window.interval
	});
	
	// create container for the content loaded
	$('#home').before("<div id='scroller' class='content-slide'></div>");
	$('#scroller').css("left", "-760px");
	$('#home').css("left", "-760px");
	
	$('#post-pager a,.post-breadcrumbs a,.post-info a').addClass('ajaxify');
	
	if($.browser.msie && $.browser.version < 8){
		$("#scroller").css('visibility', 'hidden');
		if($.browser.version == 6){
			$('#home').css('margin-top', '-'+$('#left-column').height());
		}
	}
	
	// hook up menu ajax loading
	$('#menu a, a.ajaxify').live("click", function(e){
		e.preventDefault();
		$(this).blur();
		
		if($.browser.msie && $.browser.version == 6){
			$('#menu>li.current_page_item>a,#menu>li.current_page_parent>a,#menu>li.current_page_ancestor>a,').css('background', "url("+template_url+"/images/tab-inactive-bg.png)");
		}
		
		if($(this).parents('#menu').size() > 0){
			$("#menu li")
				.removeClass("current_page_item")
				.removeClass("current_page_ancestor")
				.removeClass("current_page_parent");
			$(this).parent().addClass("current_page_item");
		}
		
		if($.browser.msie && $.browser.version == 6){
			if($(this).parent().parent().is('#menu')){
				if(theme == 'bright'){
					$(this).css('background', "url("+template_url+"/images/tab-active-bg.png)");
				} else {
					$(this).css('background', "url("+template_url+"/images/theme-" + theme + "/tab-active-bg.png)");
				}
			}
			$(document).pngFix();
		}
		
		var url = $(this).attr('href');
		
		window.location.hash = hash = url.replace(base_url, '');
		if(hash == '') window.location.hash = hash = 'thehome';
		
		$.ajax({
			type: "GET",
			url: url,
			success: function(data){
				if($(data).find('#maincontent').size() > 0){
					data = $(data).find('#maincontent .content-slide').children().get();
				}
				$("#scroller").html(data);
				$('.content-slide').each(setHeight);
				
				// refresh cufon
				Cufon.refresh();
				
				$('#menu>li').not('.current_page_item,:has(.current_page_item)').find('ul').slideUp();
				
				animtime = 1000;
				if($.browser.msie && $.browser.version < 8){
					animtime = 0;
				}
				
				$('.content-slide').animate({
						left: "0px"
					}, animtime, easing, 
					function(){
						if($.browser.msie && $.browser.version < 8){
							$('#scroller').css('visibility', 'visible');
							$('#home').css('visibility', 'hidden');
							$('#maincontent,#maincontent-wrapper').css({
								'width': '760px',
								'height': $('#scroller').height(),
								'overflow': 'hidden'
							});
							if($.browser.version == 6){
								$('#scroller').css('margin-top', '-'+$('#left-column').height());
								$(document).pngFix();
							}
						} else {
							$('#scroller').css("left", "-760px").html('');
							$('#home').css("left", "-760px").html(data);
						}
						
						// refresh cufon
						Cufon.refresh();
						
						$('#post-pager a,.post-breadcrumbs a,.post-info a').addClass('ajaxify');
						
						$('.portfolio-item').each(function(){
							var func = $('.anim', this).text();
							$(this).hover(function(){
								eval(func+"(this, 'out')")
							}, function(){
								eval(func+"(this, 'in')")
							});
						});
											
						$('.content-slide').each(setHeight);
						
						$('#s3slider').s3Slider({
							timeOut: window.interval
						});
						
						uBillboard();
					}
				);
			}
		});
		
		return false;
	});
	
	$('#menu>li').hover(function(){
		$(this).find('ul').slideDown();
	},function(){
		$(this).not('.current_page_item,:has(.current_page_item)').find('ul').slideUp();
	});
	
	// setup fancybox
	//$('.portfolio-item a').fancybox();
});

/**
 *	uBillboard
 *	
 *	these are animation functions used by the animate() function
 */
var animFunc1 = function(rows, cols){
	var n = rows*cols;
	for(i = 0; i < n; i++){
		setTimeout(
			"jQuery('#square'+"+i+").stop().animate({'opacity': 1}, "+anim_speed+");", 
			Math.round(Math.random() * anim_speed)
		);
	}
}

var animFunc2 = function(rows, cols){
	var n = rows*cols;
	for(i = 0; i < n; i++){
		setTimeout(
			"jQuery('#square'+"+i+").stop().animate({'opacity': 1}, "+anim_speed+");", 
			i * 70
		);
	}
}

var animFunc3 = function(rows, cols){
	var n = 0;
	for(i = 0; i < cols; i++){
		for(j = 0; j < rows; j++){
			setTimeout(
				"jQuery('#square'+"+(j*10+i)+").stop().animate({'opacity': 1}, "+anim_speed+");", 
				n * 70
			);
			n++;
		}
	}
}

/**
 *	this function is in charge of swapping images and running the animation functions
 */
var animate = function(rows, cols){
	// move current image to background
	jQuery('#billboard').css({
		'background-image': 'url('+images[cur_img]+')'
	});
	
	// set next image as bg to all the animation divs, and set their opacity to 0
	jQuery('#billboard div').css({
		'background-image': 'url('+images[nxt_img]+')',
		'opacity': '0'
	});
	
	// run the effect function
	eval("animFunc"+cur_eff+"("+rows+", "+cols+");");
	
	if(cur_eff == 3) cur_eff = 1; else cur_eff++; // loop effect pointer
	if(images.length - 1 == cur_img) cur_img = 0; else cur_img++; // loop current image pointer
	if(images.length - 1 == nxt_img) nxt_img = 0; else nxt_img++; // loop next image pointer
		
	if(jQuery('#billboard a').size() > 0){
		jQuery('#billboard').unbind('click').click(function(){
			if(jQuery('#billboard a.link-'+cur_img).size() > 0){
				window.location = jQuery('#billboard a.link-'+cur_img).attr('href');
			}
		});
	}
};

jQuery(window).load(uBillboard = function($){
	// no images to swith between, returning
	if(jQuery('#billboard img').size() < 2){
		return;
	}
	
	if(jQuery.browser.msie && jQuery.browser.version == 6){
		jQuery('#billboard').cycle({
			fx: 'fade'
		});
		return;
	}
	
	if(typeof window.interval != 'number' || window.interval < 3000){
		window.interval = 5000;
	}
	
	window.squares = [];
	window.images = [];
	window.cur_img = 0;
	window.nxt_img = 1;
	window.cur_eff = 1;
	window.anim_speed = 1000;
	var variant = 68;
	var rows = 340 / variant;
	var cols = 680 / variant;
	var billboard = jQuery('#billboard');
	
	// parse images within the billboard, add their sources to an array and remove them
	jQuery('#billboard img').each(function(){
		images.push(jQuery(this).attr('src'));
		jQuery(this).remove();
	});
	
	// set billboard background
	jQuery('#billboard').css({
		'background-image': 'url('+images[cur_img]+')'
	});
	
	// create divs for animation
	var n = 0;
	for(y = 0; y < rows; y++){
		for(x = 0; x < cols; x++){
			var current = document.createElement('div');
			current.style.backgroundPosition = -x*variant+'px '+(-y*variant)+'px';
			current.id = 'square'+n;
			billboard.append(current);
			squares.push(current.id);
			n++;
		}
	}
	
	jQuery('#billboard div').css({
		'opacity': '0',
		'width': variant+'px',
		'height': variant+'px',
		'float': 'left',
		'position': 'relative',
		'z-index': 1
	});
	
	// setup links
	if(jQuery('#billboard a').size() > 0){
		jQuery('#billboard').css('cursor', 'pointer');
	}
	
	if(jQuery('#billboard a.link-0').size() > 0){
		jQuery('#billboard').click(function(){
			window.location = jQuery('#billboard a.link-0').attr('href');
		});
	}
	
	// and we're off
	clearInterval(window.t);
	window.t = setInterval("animate("+rows+", "+cols+");", interval + anim_speed);
});