// How fast shall it move along?
// 4 is a good value (it's milliseconds)
var animstep = 4;

// Which item should be centered when view is called?
// This can be any number between 1 and the above
// items_in_coverflow variable.
var start_advance_to = 1;
var offset = 0;

var coverfolder = "/artista/img/uploads";
var miniid = "mini"+start_advance_to;

// Some stuff that shouldn't be changed, vital for Cover Flow flips and positions.
var current_comment = 0;
var current_center =	 		start_advance_to;

var items_available_on_right = items_in_coverflow-start_advance_to;
var items_available_on_left = start_advance_to-1;

var items_hide_on_right = items_in_coverflow-max_items;
var items_hide_on_left = 0;

if (is_ie())
	var viewpane_width = document.body.offsetWidth;
else
	var viewpane_width = window.innerWidth;

function is_ie()
{
	if (navigator.userAgent.indexOf('MSIE') != -1)
		return true;
	else
		return false;
}

////////////////////////////////////////////

// The jukebox view consists of three layers:
// Covers left, covers right, and the current displayed cover in the middle.
// As soon as a cover hits the center, the rest is faded out on both sides,
// creating the cover flow illusion.

function set_all_to_zero()
{
	document.getElementById('center').style.opacity = "0";
}

// Draw the centre piece

function draw_comment()
{
	
	for (i=1; i <= comments_in_coverflow; i++){
		document.getElementById(MapComment[i]+"_"+i).style.display = 'none';
		document.getElementById('nocomments').style.display = 'none';
		
		if(MapComment[i]==MapId[start_advance_to]){
			if(document.getElementById(MapComment[i]+"_"+i)!=null){
				document.getElementById('nocomments').style.display = 'none';
				document.getElementById(MapComment[i]+"_"+i).style.display = '';
			}
		}
	}	
}

function draw_center()
{
	if(bloc[start_advance_to]==1){
		document.getElementById('center').innerHTML = "<img src=\"/artista/img/under_block.png\">";
	} else {
		if(screen.availWidth < 1100){
			document.getElementById('center').innerHTML = "<img src="+coverfolder+"/item_"+MapId[start_advance_to]+".jpg style=\"max-width: 380px;max-height: 320px;\">";
		} else if(screen.availWidth < 1300){
			document.getElementById('center').innerHTML = "<img src="+coverfolder+"/item_"+MapId[start_advance_to]+".jpg style=\"max-width: 500px;max-height: 400px;\">";
		} else {
			document.getElementById('center').innerHTML = "<img src="+coverfolder+"/item_"+MapId[start_advance_to]+".jpg \">";
		}
	}
	
	
	/* 
	* per modificare l'id dei commenti
	*/
	document.getElementById('art_id').value=MapId[start_advance_to];
	draw_comment();
	
	document.getElementById(miniid).className="mini_on";
	document.getElementById('ItemName').innerHTML = "<b>"+ItemName[start_advance_to]+"</b>";
	document.getElementById('ItemDesc').innerHTML = ItemDescription[start_advance_to];

	if(ItemPDF[start_advance_to]==1){
		if(document.getElementById('icon_pdf')!=null){
			document.getElementById('icon_pdf').innerHTML = "<a href=\"/artista/files/f"+MapId[start_advance_to]+".pdf\" target=\"_blank\" alt=\"Get pdf\" ><img src=/artista/img/doc_pdf.png /></a>";
		}		
	} else if (ItemPDF[start_advance_to]==0){
		if(document.getElementById('icon_pdf')!=null){
			document.getElementById('icon_pdf').innerHTML = "";
		}		
	}
	if(ItemDoc[start_advance_to]==1){
		if(document.getElementById('icon_doc')!=null){
			document.getElementById('icon_doc').innerHTML = "<a href=\"/artista/files/f"+MapId[start_advance_to]+".doc\" target=\"_blank\" alt=\"Get doc\" ><img src=/artista/img/doc_doc.png /></a>";
		}			
	} else if (ItemDoc[start_advance_to]==0) {
		if(document.getElementById('icon_doc')!=null){
			document.getElementById('icon_doc').innerHTML = "";
		}	
	}
	if(ItemZip[start_advance_to]==1){
		if(document.getElementById('icon_zip')!=null){
			document.getElementById('icon_zip').innerHTML = "<a href=\"/artista/files/f"+MapId[start_advance_to]+".zip\" target=\"_blank\" alt=\"Get zip\" ><img src=/artista/img/doc_zip.png /></a>";
		}	
	} else if (ItemZip[start_advance_to]==0){
		if(document.getElementById('icon_zip')!=null){
			document.getElementById('icon_zip').innerHTML = "";
		}	
	}
	if(ItemJPG[start_advance_to]==1){
		if(document.getElementById('icon_jpg')!=null){
			document.getElementById('icon_jpg').innerHTML = "<a href=\"/artista/files/f"+MapId[start_advance_to]+".jpg\" target=\"_blank\" alt=\"Get jpg\" ><img src=/artista/img/doc_jpg.png /></a>";
		}		
	} else if (ItemJPG[start_advance_to]==0) {
		if(document.getElementById('icon_jpg')!=null){
			document.getElementById('icon_jpg').innerHTML = "";
		}	
	}	
}

// Align and locate the cover flow divs, and scrollbar

function align_covers() {
	
	if(screen.availWidth < 1100){
		var pos_scrollbar = viewpane_width / 2 - 190;
	} else {
		var pos_scrollbar = viewpane_width / 2 - 250;
	}
	//var pos_scrollbar = viewpane_width / 2 - 200;
	document.getElementById('coverscroller').style.left = pos_scrollbar;
}

// Draw the left side, like, left from center piece

function draw_left_side()
{	
	if (items_available_on_left == 0) {
			document.getElementById('left_flip').style.visibility = 'hidden';
			return;
		}		
	else {
		document.getElementById('left_flip').style.visibility = 'visible';
		//var item_to_draw_1 = start_advance_to-1;
		return;
	}	
}

// Draw the right side.

function draw_right_side()
{	
	if (items_available_on_right == 0) {
			document.getElementById('right_flip').style.visibility = 'hidden';
			return;
		}	
	else {
		document.getElementById('right_flip').style.visibility = 'visible';
		//var item_to_draw_1 = start_advance_to+1;
		return;
	}	
}

function draw_mini()
{	
	var tmp;
	for (i=1; i <= items_in_coverflow; i++){
		tmp = "mini"+i;
		document.getElementById(tmp).style.display = 'none';
	}
	
	if (items_hide_on_left >= 0) {
		for (j=1; j <= max_items; j++){
			tmp = "mini"+(j+items_hide_on_left);
			document.getElementById(tmp).style.display = '';
		}
	} else {
		for (j=1; j <= max_items; j++){
			tmp = "mini"+(j-items_hide_on_right);
			document.getElementById(tmp).style.display = '';
		}
	}	
	
}

function draw_mini_left_side()
{	
	if (items_hide_on_left <= 0) {
			document.getElementById('left_flip2').style.visibility = 'hidden';
			return;
	} else {
		document.getElementById('left_flip2').style.visibility = 'visible';
		return;
	}	
}

function draw_mini_right_side()
{	
	if (items_hide_on_right <= 0) {
			document.getElementById('right_flip2').style.visibility = 'hidden';
			return;
	} else {
		document.getElementById('right_flip2').style.visibility = 'visible';
		return;
	}	
}


///funzioni per i commenti///
function comment()
{
	
	if (current_comment == 0) {
		document.getElementById('comments').style.display = '';
		document.getElementById('plus_img').src = "/artista/img/minus_off.jpg";
		current_comment = 1;
	} else {
		document.getElementById('comments').style.display = 'none';
		document.getElementById('plus_img').src = "/artista/img/plus_off.jpg";	
		current_comment = 0;
	}
}

function show_comment(cond)
{
	
	if (current_comment == 1) {
		
		if(cond){
			document.getElementById('plus_img').src = "/artista/img/minus_off.jpg";
		} else {
			document.getElementById('plus_img').src = "/artista/img/minus.jpg";
		}
	} else {
		if(cond){
			document.getElementById('plus_img').src = "/artista/img/plus_off.jpg";
		} else {		
			document.getElementById('plus_img').src = "/artista/img/plus.jpg";
		}
	}
}



//////////////// INITIAL DRAWING COMPLETE /////////////////
///////////////////////////////////////////////////////////

// Flip functions. Giving anything the rockstar treatment.

function direct_flip(id)
{
	offset = id - start_advance_to;
	//alert(offset);

	if(offset<0){ //id minore di start, si è cliccati a sinistra
		if (items_available_on_left == 0) {
			document.getElementById('left_flip').style.visibility = 'hidden';
			return;
		}	
		document.getElementById('center').innerHTML = "";
		document.getElementById('left_flip').style.visibility = 'visible';
		
		items_available_on_right = items_available_on_right-offset;
		items_available_on_left = items_available_on_left+offset;
		
	} else {
		if (items_available_on_right == 0) {
			document.getElementById('right_flip').style.visibility = 'hidden';
			return;
		}	
		document.getElementById('center').innerHTML = "";
		document.getElementById('right_flip').style.visibility = 'visible';
		
		items_available_on_right = items_available_on_right-offset;
		items_available_on_left = items_available_on_left+offset;
	}
	start_advance_to = id;
	deselect_mini(miniid);
	miniid = "mini"+id;
	
	align_covers();
	draw_left_side();
	draw_center();
	draw_right_side();
}

function flip_forward()
{
	
	// Check if there are still items in flow. If not, well, don't flip, leave function.
	if (items_available_on_right == 0) {
		document.getElementById('right_flip').style.visibility = 'hidden';
		return;
	}

	document.getElementById('center').innerHTML = "";
	document.getElementById('right_flip').style.visibility = 'visible';
	
	items_available_on_right = items_available_on_right-1;
	items_available_on_left = items_available_on_left+1;
	start_advance_to = start_advance_to+1;
	deselect_mini(miniid);
	miniid = "mini"+start_advance_to;

	align_covers();
	draw_left_side();
	draw_center();
	draw_right_side();
	mini_forward();
}

function flip_back()
{
	
	// Check if there are still items in flow. If not, well, don't flip, leave function.
	if (items_available_on_left == 0) {
		document.getElementById('left_flip').style.visibility = 'hidden';
		return;
	}

	document.getElementById('center').innerHTML = "";
	document.getElementById('left_flip').style.visibility = 'visible';
	
	items_available_on_right = items_available_on_right+1;
	items_available_on_left = items_available_on_left-1;
	start_advance_to = start_advance_to-1;
	deselect_mini(miniid);
	miniid = "mini"+start_advance_to;

	align_covers();
	draw_left_side();
	draw_center();
	draw_right_side();
	mini_back();
		
}

function mini_forward()
{
	
	// Check if there are still items in flow. If not, well, don't flip, leave function.
	if (items_hide_on_right <= 0) {
		document.getElementById('right_flip2').style.visibility = 'hidden';
		return;
	}
	document.getElementById('right_flip2').style.visibility = 'visible';
	items_hide_on_right = items_hide_on_right-1;
	items_hide_on_left = items_hide_on_left+1;

	draw_mini();
	draw_mini_left_side();
	draw_mini_right_side();
}

function mini_back()
{
	
	// Check if there are still items in flow. If not, well, don't flip, leave function.
	if (items_hide_on_left <= 0) {
		document.getElementById('left_flip2').style.visibility = 'hidden';
		return;
	}
	document.getElementById('left_flip2').style.visibility = 'visible';
	items_hide_on_right = items_hide_on_right+1;
	items_hide_on_left = items_hide_on_left-1;

	draw_mini();
	draw_mini_left_side();
	draw_mini_right_side();
}

// When window is resized, redraw the stuff so that it's centered.
function resize_redraw() {
	
	if (is_ie())
		viewpane_width = document.body.offsetWidth;
	else
		viewpane_width = window.innerWidth;
	
	align_covers();
	draw_left_side();
	draw_center();
	draw_right_side();
}

function restore_mini() {
	document.getElementById(miniid).className="mini_on";
}

function deselect_mini(id) {
	document.getElementById(id).className="mini_off";
}

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}