/**
 * DOM Load
 */
$(document).ready(function() {
	$("body").css({backgroundPosition: "50% -" + (randomY)+"px"})
	pageEnhance();
});

/**
 * General "init" function for progressively enhancing the page on DOM load
 */
function pageEnhance() {
	newWindowLinks();
	clickable();
	$(".sheader").prepend('<span class="slashes">\\\\\\</span>');
	$("#viewSwitcher a").click(function(){
		var h = $(this).attr("href").split("?view=")[1];
		$("#switchList").attr("class","cfx "+h);
		return false
	})
	$(window).scroll(function(){
		offsety = $(window).scrollTop() + randomY;
		$("body").css({backgroundPosition: "50% -" + (offsety)+"px"})
	})
	$("header h1").click(function(){ window.location = "/"})
}


/* Clickable blocks */
function clickable() {
	$(".clickable:has(a):not(.disabled)").live("click",function() {
		var link = $("a",this).eq(0).attr("href");
		window.location = link;
	}).hover(function(){
		if(!$(this).hasClass("disabled")) {
			$(this).addClass("hover").css({"cursor":"pointer"});
		}
	},function(){
		$(this).removeClass("hover").css({"cursor":"default"});
	})
}


/* Un-obfuscate email */
function fnE(inarr,text) {
	inarr = inarr.split(",");
	var out = "";
	for(i=inarr.length-1;i>=0;i--) {
		out += inarr[i];
	}
	if(text==false || text=="" || text==undefined) {
		text = out;
	}
	document.write('<a href="mailto:'+out+'">'+text+'</a>');
}

/**
 * animate page to a #elementid
 */
function inPageLinksScrollTo() {
	$(".scrollTo").click(function(){
		var href 	= $(this).attr("href");
		var	target	= $(href);
		if(target.size()>0) {
			var offset 	= target.offset()
			$('html,body').animate({scrollTop:offset.top},1000)
			return false;
		} else {
			return true;
		}
	})
}



/**	
 * Target new windows
 */
function newWindowLinks(){
	$('a[rel=external]').live("click",function(){
		window.open($(this).attr('href'));
		return false;
	});
}



/* Enable array searching in IE - From: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf */
if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(searchElement /*, fromIndex */)
  {
    "use strict";

    if (this === void 0 || this === null)
      throw new TypeError();

    var t = Object(this);
    var len = t.length >>> 0;
    if (len === 0)
      return -1;

    var n = 0;
    if (arguments.length > 0)
    {
      n = Number(arguments[1]);
      if (n !== n) // shortcut for verifying if it's NaN
        n = 0;
      else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0))
        n = (n > 0 || -1) * Math.floor(Math.abs(n));
    }

    if (n >= len)
      return -1;

    var k = n >= 0
          ? n
          : Math.max(len - Math.abs(n), 0);

    for (; k < len; k++)
    {
      if (k in t && t[k] === searchElement)
        return k;
    }
    return -1;
  };
}


function jsdebug(message) {
	try {
		if(window.console) {
			console.log(message);
		}
	} catch(e) {}	
}

