var gallery = null;

$(document).ready(function() {
	var pages = {};
	$('#menu a').each(function(i) {
		pages[this.id] = i++;
		$(this).click(function() {
			pageTracker._trackPageview(this.id); 
			$("#pageContainer").stop().animate({ 
		        left: pages[this.id] * -800 - 800
		    }, 1200, "easeOutElastic");
			return false;
		});
	});
	
	gallery = new Carousel(".carousel");
	$('#preview a img').each(function(i) {
		$(this).css('position', 'absolute');
		gallery.add(this.id, $(this).attr('title'), $(this).attr('alt'));
	});
	$('#galleryPage').removeClass('hide');
});


/******************
 * Carousel Class
 ******************/
var Carousel = (function (el) {
	var speed = 800;
	var sites = {
			size : 0
	};
	var container = el;
	
    this.add = function(id, title, url) {
    	sites[id] = {
    		id : id,
    		title : title,
    		url : url,
    		position : sites.size
    	};

    	$(container + ' #' + id).addClass("position" + sites.size).addClass("z-position" + sites.size++).click(function(e) {
			e.preventDefault();
			pageTracker._trackPageview(this.alt); 
			rotateStage(this);
		});
    }
    
    var rotate = function(elem, max) {
    	var lastPos = sites[elem].position;
    	sites[elem].position -= 1;
    	if (sites[elem].position < 0) {
    		sites[elem].position = max-1;
		}
    	$('#'+elem).removeClass("z-position"+lastPos)
    		.addClass("z-position"+sites[elem].position)
    		.switchClass("position"+lastPos, "position"+sites[elem].position, speed);
    }
    
    var rotateStage = function(obj) {
		if (sites[obj.id].position != 0) {
			$(container + ' a img').each(function() {
				rotate(this.id, sites.size);
			});
			setTimeout(function() {rotateStage(obj);}, speed+50);
		} else {
			var link = "<span> :: <a href='http://"+$("#"+obj.id).attr('alt')+"' rel='external'>load site</a></span>"
			$.fn.colorbox({
				href : $("#"+obj.id).parent().attr('href'),
				open : true,
				width : "50%",
				overlayClose : true,
				opacity : 0.85,
				title : $("#"+obj.id).attr('title') + link,
				speed : 600
			}); 
		}
    }
});