/** Initialize variables */
var current = 0;
var colorsUrl = "http://www.digitaltableteur.com/wp-content/themes/digitaltableteur/script/colors/colors.txt";
var colorsArray = loadColorsToArray(true);

$(document).ready( function() {
	
	/* Show error when image is not found */
	$("img").error(function () {
		$(this).unbind("error").attr("src", baseurl() + "image_not_found.png");
	});
	
	/* Add tooltips to work content images */
	$(".work-content img, .add-tooltip").each(function() {
		var title = $(this).attr("title");
		$(this).removeAttr("title");
		addTooltip($(this), title)
	});
	
	/* Open all links to a new window in footer */
	$(".content-footer a").bind("click", function() { 
		var href = $(this).attr("href");
		window.open(href, 'newwin'); 
		return false;
	});
	
	/** Init image boxes
	$(".image-box a").each(function() {	
		var self = $(this);
		
		// Get work title
		var title = self.attr("title");
		self.removeAttr("title");
		
		// Get image
		var img = $("img.picture", self);
		
		// Add tooltip
		addTooltip(self, title);
		
		$(img).ready(function () {
			initImage(self);
			$(img).show();
		});
		
		$(img).bind("load", function () {
			initImage(self);
			$(img).show();
		});
	});
	 */
});

/** Array shuffle 
Array.prototype.shuffle = function() {
var s = [];
while (this.length) s.push(this.splice(Math.random() * this.length, 1));
while (s.length) this.push(s.pop());
return this;
}
*/

function shuffle(ary) {
var s = []; while (ary.length) s.push(ary.splice(Math.random() * ary.length, 1)); while (s.length) ary.push(s.pop());
}


/** Init image */
function initImage(self)
{
	var img2 = $("img.on-hover-picture", self);	
	$(img2).css("opacity", 0).fadeTo(500, 0);	
	// Add hoverIn/hoverOut event handlers				
	self.hoverIntent({    
		sensitivity: 15, // number = sensitivity threshold (must be 1 or higher)    
		interval: 50, // number = milliseconds for onMouseOver polling interval    
		over: function() {
				$(img2).fadeTo(300, 1);							
			},  
		timeout: 2, // number = milliseconds delay before onMouseOut    
		out: function() {
				$(img2).fadeTo(300, 0);
			}  
	});
}

/** Is image loaded */
function isImageLoaded(img) {
	if (!img.complete) 
	{
		return false;
	}
	if (typeof img.naturalWidth != "undefined" && img.naturalWidth == 0) 
	{
		return false;
	}
	return true;
}

/** Add tooltip to image */
function addTooltip(self, title)
{
	var backgroundColor = getBackgroundColor();
	var tooltipLocation = "body";
	$(self).mouseover(function(e) {
		if($(".tooltip").size() == 0)
		{
			$(tooltipLocation).append("<div class='tooltip'>" + title + "</div>");
		}
		$(".tooltip").css("background-color", backgroundColor);		
		setMousePosition(e, ".tooltip");		
		$(".tooltip").show();
		return false;
	}).mouseout(function(e){
		$(".tooltip").remove();
		return false;
    });	
	$(tooltipLocation).mousemove(function(e) {
		setMousePosition(e, ".tooltip");
		return false;
	});
}

/** Set mouse position */
function setMousePosition(e, element)
{
	var x = e.pageX + 20;
	var y = e.pageY;
	$(element).css("top", y + "px");
	$(element).css("left", x + "px");
}

/** Get current background color from array*/
function getBackgroundColor()
{		
	var result = "";
	if(current < colorsArray.length-1) 
	{
		result = colorsArray[current];
		current = current + 1;
	}
	else 
	{
		result = colorsArray[current];
		current = 0;
	}
	return result;
}

/** Load colors to array */
function loadColorsToArray(rand)
{
	rand = typeof(rand) != 'undefined' ? rand : false;
	var result = $.ajax({
		async: false,
		dataType: 'text',
		url: colorsUrl
	}).responseText;
	colorsArray = result.split(",");
	if (rand){
		shuffle(colorsArray);
	}
	return colorsArray;
}

/** Baseurl of site images */
function baseurl()
{
	return "http://www.digitaltableteur.com/wp-content/themes/digitaltableteur/images/";
}

/**
 * Get the milliseconds since epoch
 */
function timestamp(){    
    return new Date().getTime();
}

