var global = this;

(function ($) {
	$.event.special.load = {
		add: function (hollaback) {
			if ( this.nodeType === 1 && this.tagName.toLowerCase() === 'img' && this.src !== '' ) {
				// Image is already complete, fire the hollaback (fixes browser issues were cached
				// images isn't triggering the load event)
				if ( this.complete || this.readyState === 4 ) {
					hollaback.handler.apply(this);
				}

				// Check if data URI images is supported, fire 'error' event if not
				else if ( this.readyState === 'uninitialized' && this.src.indexOf('data:') === 0 ) {
					$(this).trigger('error');
				}
				
				else {
					//$(this).bind('load', hollaback.handler);
				}
			}
		}
	};
}(jQuery));

(function() {

	var jumping = false;

	var selectOnMain = function(item, index) {

		util.log("Moving to " + index);

		if(getMainFlow()) {
			getMainFlow().moveTo(index);
		}
	};

	var getMainFlow = function() {
		return ContentFlowGlobal.Flows.length > 0 ? ContentFlowGlobal.Flows[0] : null;
	};

	var initPortfolioSlider = function() {	
		/**** Slide selector on portfolio pages ****/
		global.portfolioSlider = $('#portfolioBrowser').slideSelector({
			traySelector : ".tray",
			thumbSelector : ".item",
			hitSpotSize: 150
		}, function(index) {
			selectOnMain(this, index);
			jumping = true;
			//$('.flow').hide();
			//$('.loadIndicator').show();
		});

		global.portfolioSlider.init();

		if(util.queryString.contains("id")) {
			global.portfolioSlider.goto(util.queryString.get("id"), 0);
		}

	};

	// Grab all the images
	var container = $('#portfolioBrowser');
	var portfolioImages =  container.find('img');
	portfolioImages.hide();

	// Set a counter
	var counter = portfolioImages.length; 
	util.log("Loading: " + counter + " images"); 

	var cache = [];
	$(portfolioImages).each(function() {
		var img = document.createElement('img');

		// Load each image, init when all are loaded
		$(img).load(function() {
			util.log("Loading Image: " + counter); 
			counter--;
			if(counter == 0) {
				container.removeClass("loading");
				portfolioImages.show();
				initPortfolioSlider();
			}
		});

		img.src = $(this).attr("src");
		cache.push(img);
	});


	// Check for cache
	setTimeout(function() {
		var lastImg = $(portfolioImages).get($(portfolioImages).length - 1);
		if($(lastImg).width() > 1 && counter > 0 && counter == portfolioImages.length) {
			util.log($(lastImg).width()); 
			container.removeClass("loading");
			portfolioImages.show();
			initPortfolioSlider();
			counter = -1;
		}
	}, 8000);

	global.onMove = function() {
	};

	global.onArrive = function() {
		if(jumping) {
			getMainFlow().resize();
			jumping = false;
		}
		//$('.flow').show();
		//$('.loadIndicator').hide();
	};

})();


$(document).ready(function() {

	// Home page img switcher
	var sections = $('#home').find('.section a');

	sections.each(function() { 
		$(this).cycle({timeout: 1});
		$(this).cycle("pause");

		$(this).hover(function() {
			$(this).cycle("resume");
			$(this).cycle("next");
		}, function() {
			$(this).cycle("pause");
		});

	});

	/*******************************************/

	/**** Slide selector on bio page ****/
	var preButton = $("<div class='preButton'></div>");
	var nextButton = $("<div class='nextButton'></div>");

	$('#media-content').append(preButton).append(nextButton);

	global.bioSlider = $('#media-content').slideSelector({
		traySelector : ".tray",
		thumbSelector : ".item",
		hitSpotSize: 150,
		buttons: { pre: preButton, next: nextButton },
		mode: "buttons"
	}, function() {
		// nothing on click
		$('#media-content .current').removeClass("current");
		$(this).addClass("current");
	});

	setTimeout(global.bioSlider.init, 1000); // Hack timeout for fake "on image load"

	// Languages on bio
	$('#languages label').click(function() {
		$('#languages label').removeClass("current");
		$(this).addClass("current");
		var lang = $(this).text();

		$('.translation').hide();
		$('#'+lang).show();
	});

	$('.translation:gt(0)').hide();
	$('#languages label:eq(0)').click();

});

