/**
 * brigitta horvat "gallery"
 * jk 2009
 */

var artistPreviewActive = true;
var artistPreviewImage = "";
$(document).ready(function() {
	var config = {    
		 sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
		 interval: 200, // number = milliseconds for onMouseOver polling interval    
		 over: showArtistPreview, // function = onMouseOver callback (REQUIRED)    
		 timeout: 500, // number = milliseconds delay before onMouseOut    
		 out:  function () {}// function = onMouseOut callback (REQUIRED)    
	};
	$("#subNavi .menuItem a").hoverIntent(config);
	$("#subNavi").hover(function () {},function () {hideArtistPreview();});
 });
function showArtistPreview() {
	if (artistPreviewImage == "" || !artistPreviewActive) return;
	if (!artistPreviewActive) return;
	$("#imagePreviewContainer").stop().animate({opacity: 1}, 200 );
	$("#imageContainer").stop().animate({opacity: 0}, 200 );
}

function loadArtistPreview($img) {
	artistPreviewImage = $img;
	if (artistPreviewImage == "" || !artistPreviewActive) return;
	$("#imagePreviewContainer").stop().remove();
	$("#container").append("<div id='imagePreviewContainer'></div>");
	$("#imagePreviewContainer").append("<img id='previewImage' src='"+  $img +"' alt='' />");
	$("#imagePreviewContainer").css("opacity","0");
	$('#previewImage').load();
}

function hideArtistPreview() {
	if (!artistPreviewActive) return;
	$("#imagePreviewContainer").stop().animate({opacity: 0}, 200);
	$("#imageContainer").stop().show().animate({opacity: 1}, 200);
}

function stopArtistPreview() {
	artistPreviewActive = false;
}

function Gallery ($data) {
	this.data = JSON.parse($data);
    if (this.data.dir == "rev") {
    	this.index = this.data.images.length - 1;
    	if ((this.data.images[this.index].format =="portrait") && (this.data.images[this.index-1] && (this.data.images[this.index-1].format == "portrait"))) {
    		this.index -= 1;
    	}
    } else {
    	this.index = 0;
    }
    $("#container").append("<div id='imageContainer'></div>");
    this.displayImages = displayImages;
    this.nextImage = nextImage;
    this.prevImage = prevImage;
    this.isDoubleImage = false;
    this.displayImages();
}

function nextImage() {
	this.index += 1;
	if (this.index >= this.data.images.length || this.data.images.length == 1) {
		if (this.data.lastClickAction == "repeat") {
			this.index = 0;
			this.displayImages();
		} else {
			window.location.href = this.data.lastClickAction;
		}
	} else {
		this.displayImages();
	}
}
function prevImage() {
	if (this.isDoubleImage) {
		this.index -= 2;
	} else {
		this.index -= 1;	
	}
	if ((this.data.images[this.index] && (this.data.images[this.index].format =="portrait")) && (this.data.images[this.index-1] && (this.data.images[this.index-1].format == "portrait"))) {
		this.index -= 1;
	}
	if (this.index < 0) {
		if (this.data.firstClickAction == "repeat") {
			this.index = this.data.images.length - 1;
			this.displayImages();
		} else {
			window.location.href = this.data.firstClickAction;
		}
	} else {
		this.displayImages();
	}
}
function displayImages() {
	var instance = this;
	var $width = 0;
	this.isDoubleImage = false;
	this.nextPreloadInitiated = false;
	// remove container first
	$("#imageContainer").remove();
	
	// images
	$("#container").append("<div id='imageContainer'></div>");
	if (this.data.images[this.index].format == "portrait") {
		if (this.data.images[this.index+1] && (this.data.images[this.index+1].format == "portrait")) {
			$("#imageContainer").append("<img width="+this.data.images[this.index].width+" height="+this.data.images[this.index].height+" class='image' style='margin-right:1px;' src='"+ this.data.images[this.index].file +"' alt='Gallery image' />");
			$("#imageContainer").append("<img id='imageRight' width="+this.data.images[this.index+1].width+" height="+this.data.images[this.index].height+" class='image' src='"+  this.data.images[this.index+1].file +"' title='Click for next image!' alt='Image preview' />");
			$width = this.data.images[this.index].width + this.data.images[this.index+1].width + 1;
			$('#imageRight').css("left",this.data.images[this.index].width+1);
			this.isDoubleImage = true;
			this.index += 1;
		} else {
			$("#imageContainer").append("<img width="+this.data.images[this.index].width+" height="+this.data.images[this.index].height+" class='image' style='margin-right:1px;' src='"+  this.data.images[this.index].file +"' alt='Gallery image' />");
			$("#imageContainer").append("<div class='imageBlank' style='width:"+this.data.images[this.index].width+"px;height:"+this.data.images[this.index].height+"px;' title='Click for next image!' alt='Click for next image!' /></div>");
			$width = this.data.images[this.index].width * 2 + 1;
		}
	} else {
		$("#imageContainer").append("<img width="+this.data.images[this.index].width+" height="+this.data.images[this.index].height+" class='image' src='"+  this.data.images[this.index].file +"' alt='Gallery image' />");
		$width = this.data.images[this.index].width;
	}
	$("#imageContainer").width($width);
	
	// buttons
	if (this.data.images.length > 1) {
		if (this.index < this.data.images.length-1) {
			$("#imageContainer").append("<div id='nextButton' class='imageButton' title='Click for next Image'></div>")
		}
		if (this.index > 0 && !(this.index == 1 && (this.data.images[this.index].format =="portrait") && (this.data.images[this.index-1] && (this.data.images[this.index-1].format == "portrait")))) {
			$("#imageContainer").append("<div id='prevButton' class='imageButton' title='Click for previous Image'></div>");
		}	
	}
	
	// set image heigth (css ie flaw - workaround)
	$("#imageContainer").height(this.data.images[this.index].height);
	$('.imageButton').width($width/2);
	$('#nextButton').css("left",$width/2);
	
	$('.imageButton').height(this.data.images[this.index].height);
	$('#nextButton').css("left",$width/2);
	$('.imageBlank').hide().fadeIn(200);
	
	$('.image').hide().load(function() {
		$(".image").fadeIn(200);
		
		// preload next image (only once)
		if (instance.data.images[instance.index+1] && !instance.nextPreloadInitiated) {
			$("#container").append("<div id='preload'></div>");
			$("#preload").append("<img src='"+  instance.data.images[instance.index+1].file +" />");
			$("#preload").hide().load();
			instance.nextPreloadInitiated = true;
		}
	}); 
	
	// set button action
	$("#prevButton").click(function (e) {
		$("#imageContainer").fadeOut(200,function () { 
			instance.prevImage();
		});
	});
	$("#nextButton").click(function () { 
		$("#imageContainer").fadeOut(200,function () { 
			instance.nextImage();
		});
	});
}