/******** sifr ********/
var DIN = { src: 'din-bold.swf' };

sIFR.activate(DIN);

sIFR.replace(DIN, {
  selector: 'h3',
  css: '.sIFR-root {color: #000000; text-transform: uppercase}',
  wmode: 'transparent'
});

/******** theme drawer hider *********/
function hideDrawer() {
	$("#theme-drawer #theme-container").slideToggle("normal", function () {
		if ($("#theme-drawer #theme-container").is(':visible')) {
			$("#footer").css("margin-bottom", "150px");
		} else {
		$("#footer").css("margin-bottom", "20px");
		}
	});
}

/******** easing call ********/
jQuery.easing.def = 'easeOutQuint';

/******** clear field ********/
function clearText(field){
	if (field.defaultValue == field.value) field.value = '';
	else if (field.value == '') field.value = field.defaultValue;
}


/////// Theme selector ///////
var currentPosition = 0;
var slowScrollSpeed = 0;
var timer;
var totalWidth = 2000;
var outerWidth = 1000;
var leftLimit = outerWidth - totalWidth;

function updateLimits()
{				
	totalWidth = document.getElementById("theme-carousel").clientWidth;
	outerWidth = document.getElementById("theme-housing").clientWidth;
	
	leftLimit = outerWidth - totalWidth + 160;
}


function themeScroll(number)
{
	updateLimits();

	currentPosition += number;

	// Clamp
	if(currentPosition > 0)
	{
		currentPosition = 0;
	}
	
	//alert("currentPosition: " + currentPosition + ", leftLimit: " + leftLimit + ", totalWidth: " + totalWidth + ", outerWidth: " + outerWidth);
	
	$("#theme-carousel").animate({left: currentPosition + "pt"}, 500)
	
	updateButtons();
	
	return;
}

function scrollPrevious()
{
	themeScroll(133);
	
	return;
}


function scrollNext()
{
	themeScroll(-133);
	
	return;
}


function setSlowScroll(amount)
{
	//updateLimits();

	//slowScrollSpeed = amount;
}


function slowScroll()
{
	if(slowScrollSpeed)
	{
		currentPosition += slowScrollSpeed;

		document.getElementById("theme-carousel").style.left = currentPosition + "pt";
		
		updateButtons();
	}
}


function updateButtons()
{				
	if(currentPosition < 0)
	{
		document.getElementById("theme-previous").style.visibility = "visible";
	}
	else
	{
		document.getElementById("theme-previous").style.visibility = "hidden";
	}
						
	if(currentPosition > leftLimit)
	{
		document.getElementById("theme-next").style.visibility = "visible";
	}
	else
	{
		document.getElementById("theme-next").style.visibility = "hidden";
	}
	
	return;
}


window.onresize = function()
{
	updateLimits();
	updateButtons();
}


/******** let the fun begin.... ********/
$(document).ready(function(){

	$('#theme-drawer #theme-container').hide();

	/* pngfix */
	$(document).pngFix();

	$('#categories-preview ul').equalHeights(true);
	
	// class call to make links have target="_blank" and still validate
	$('.new-window').attr({target: '_blank'});
	
	// for the flickr thumbnails
	$('#hp-flickr ul li a').attr({target: '_blank'});
	
	// for comments
	$('.comment-list li h4 a').attr({target: '_blank'});
		
	// for we're loving
	$('#stuff-we-love ul li a').attr({target: '_blank'});
	
	// blog-roll
	$('#blog-roll ul li a').attr({target: '_blank'});
	
	// disabling links on lifestream [2,3,4,5,6 ]
	lifestreams = ".lifestream_feedid_2 .lifestream_events a, .lifestream_feedid_3 .lifestream_events a, .lifestream_feedid_4 .lifestream_events a, .lifestream_feedid_5 .lifestream_events a, .lifestream_feedid_6 .lifestream_events a"
	$(lifestreams).click(function() { return false; });
	$(lifestreams).mouseover(function(){window.status='Done';return true;});
	$(lifestreams).css("color","#222222");
	$(lifestreams).css("cursor","text");
	
	
	
	$('.searchtxt').attr ({
		onFocus: 'clearText(this)',
		onBlur: 'clearText(this)'
	})
	
	/* tooltip call */
	tooltip();
	
	/* Homepage Tabs */
	$('#hp-popular > ul').tabs({ fx: { opacity: 'toggle' } });
	$('#contact-options > ul').tabs({ fx: { opacity: 'toggle' } });

	/* main nav drop down */
	var timers = new Array();
	
	$('#main-nav ul li.dropdown').each(function (i) {
		$(this).hover(
			function() {
				clearTimeout(timers[i*2]);
				clearTimeout(timers[i*2+1]);
				var elem = $(this);
				elem.addClass('current');
				if (elem.find('.ro-container').is(':visible')) return;
				timers[i*2] = setTimeout(function() {
					//elem.find('.ro-container').show("slide", {direction: "up"}, 250);
					elem.find('.ro-container').show();
				}, 200);
			},
			function() {
				clearTimeout(timers[i*2]);
				clearTimeout(timers[i*2+1]);
				var elem = $(this);
				elem.removeClass('current');
				if (elem.find('.ro-container').is(':hidden')) return;
				timers[i*2+1] = setTimeout(function() {
					//elem.find('.ro-container').hide("slide", {direction: "up"}, 250);
					elem.find('.ro-container').fadeOut("def");
				}, 200);
			}
		);
	});
	
	/* Archive Box */
	$("#archive-box .month, #archive-box .year").change(function() {
		var c_time = new Date();
		var url = "?month=" + $("#archive-box .month").val() + "\&yr=" + $("#archive-box .year").val() + "\&time=" + c_time.getTime();
		$("#archive-box ul").load("/query/"+ url +" ul")
		return false;
	});
	
	$("#archive-box .month").trigger("change");


	/* Theme switcher madness */
	/* This is for the drawer */
	$("a.theme-thumb").click(function() {
		var themename = $(this).attr("rel");
		$("#anet-theme").attr({ href: "http://files.anidea.com/themes/" + themename + "/style.css"});
		hideDrawer();
		$.cookie('user_theme', themename, { path: '/', expires: 7 });
		return false;
	});

	/* this is for the post itself */
	$("a.theme-post").click(function() {
		var themename = $(this).attr("rel");
		$("#anet-theme").attr({ href: "http://files.anidea.com/themes/" + themename + "/style.css"});
		$.cookie('user_theme', themename, { path: '/', expires: 7 });
		return false;
	});
	
	$(".theme-toggle").click(function() {
		hideDrawer();
	});
	
	/*$('#theme-carousel').jCarouselLite({
        btnNext: ".theme-next",
        btnPrev: ".theme-previous",
		visible: "5"
		});*/
	
	$("#archives ul li:nth-child(odd)").addClass("odd");
	
	function killIt(){
			$(function(){
				$('#IE6-alert').fadeOut(1000);
			});
		};
	
	/* Form Validation */
	$("#commentform").validate({
		rules: {
			author: "required",
			email: "required"
		}
	});
	
	$("#commentform").validate ({
		messages: {
			author: "Oops, you forgot your Name!",
			email: "Please write down your e-mail. We won't display it."
		}
	}) 
	
	//timer = window.setInterval("slowScroll()", 50);
	
});	/****** ...and the fun ends ******/
