var portImages = [];
var imageList;
var curIndex;
var lastIndex;
$(document).ready(function() {
	//Flash
	$('.flash').hide().slideDown('slow');
	setTimeout("$('.flash').slideUp('slow');",5000);
	
	id = $('body').attr('id');
	
	//Tweet box for about me
	if(id=='default-aboutMe') {
		var cache = cookie('tweetCache');
		if(cache.get()) {
			$('#twitter .latest').html(cache.get());
		} else {
			$('#twitter .latest').hide();
			$('#twitter .loader').show();
			$.ajax({
				type: 'POST',
				url: 'ajax/',
				data: 'url='+escape('http://twitter.com/statuses/user_timeline/cloudedvision.xml?count=1'),
				success: function(xml) {
					tweet = $(xml).find('text').text();
					hrefRegex = /http:\/\/[^\.:~\)\?\]\* ]+([\.:~\)\?\]\*][^\.:~\)\?\]\* ]+)+/
					tweet = replaceAll(/http:\/\/[^\.:~\)\?\]\* ]+([\.:~\)\?\]\*][^\.:~\)\?\]\* ]+)+/, tweet, '<a href=":match:" rel="nofollow">:match:</a>');
					tweet = replaceAll(/[a-zA-Z0-9_]+(?=@)/, tweet.reverse(), '<a href="http://www.twitter.com/:hctam:/" rel="nofollow">:hctam:</a>'.reverse()).reverse(); //Weird hack for positive lookbehind
					tweet = replaceAll(/[a-zA-Z0-9_]+(?=#)/, tweet.reverse(), '<a href="http://search.twitter.com/search?q=:hctam:" rel="nofollow">:hctam:</a>'.reverse()).reverse();
					cache.set(tweet,60*5); //Cache, but only for five minutes
					$('#twitter .loader').slideUp();
					$('#twitter .latest').html(tweet).slideDown();
				}
			});
		}
	}
});

Image.prototype.isLoaded = function() {
	if(!this.complete) {
		return false;
	}
	
	if(typeof this.naturalWidth != 'undefined' && this.naturalWidth == 0) {
		return false;
	}
	
	return true;
}

if (typeof String.prototype.reverse!="function") {
   String.prototype.reverse=function() {
      var str=this.split(/.??/);
      for (var min=0,max=str.length-1; min<max; min++,max--) {
         var temp=str[min];
         str[min]=str[max];
         str[max]=temp;
      }
      return str.join('');
   }
}


function replaceAll(regex, str, replace) {
	matches = [];
	tmpStr = str;
	for(var i = 0; matches[i] = regex.exec(tmpStr);i++) {
		matches[i] = matches[i][0];
		tmpStr = tmpStr.replace(regex, '');
	}
	delete matches[i];
	for(var i in matches) {
		tmpReplace = replace;
		while(tmpReplace.search(':match:')>-1) {
			tmpReplace = tmpReplace.replace(':match:',matches[i]);
		}
		str = str.replace(matches[i], tmpReplace);
	}
	return str;
}

var cookie = function(cName) {
	return {
		name: cName,
		get: function() {
			if (document.cookie.length>0) {
				c_start=document.cookie.indexOf(this.name + "=");
				if (c_start!=-1) {
					c_start=c_start + this.name.length+1;
					c_end=document.cookie.indexOf(";",c_start);
					if (c_end==-1) c_end=document.cookie.length;
    			return unescape(document.cookie.substring(c_start,c_end));
    		}
  		}
			return false;
		},
		
		set: function(value, expires) {
			expires = (typeof expires == 'undefined') ? 86400000 : expires*1000;
			exdate = time()+expires;
			var exdate = new Date(exdate);
			document.cookie=this.name+ "=" +escape(value)+
			((expires==null) ? "" : ";expires="+exdate.toGMTString());
			return this;
		},
		
		remove: function() {
			this.set('',-86400);
			return this;
		}
	};
}

function time() { //Returns date in unix timstamp (I think)
	return Date.parse(new Date());
}
