//
// Functions for thumbnails view using mycollections_options.tpl
//

var current_piece = new Array();
var current_artist = new Array();

//position box option box on the page
function load_mc_options(mc_options_id, mycollections_item_id, piece_id, gallery_artist_id) {
	// get the optBox
	optBox = document.getElementById(mc_options_id);
	// make sure the option box is invisible
	optBox.style.visibility = 'hidden';
	//alert("Anchor pos: "+ancPos.x+"x "+ancPos.y+"y");
	// reset options box back to resting position so you can get get position on page.
	optBox.style.top="0px";
	optBox.style.left="0px";
	// get resting position of the options box
	optBoxPos = getAnchorPosition(mc_options_id);
	//alert("optBox pos: "+optBoxPos.x+"x "+optBoxPos.y+"y");
	// get position of the anchor that holds the piece
	ancPos = getAnchorPosition('image_div_mc_'+mycollections_item_id+'_p_'+piece_id+'_a_'+gallery_artist_id);
	// calculate new position of options box
	newOptPosY = ancPos.y - optBoxPos.y;
	newOptPosX = ancPos.x - optBoxPos.x;
	// move options box to anchor
	optBox.style.top=(newOptPosY)+"px";
	optBox.style.left=(newOptPosX+2)+"px";

	current_class = document.getElementById('image_div_mc_'+mycollections_item_id+'_p_'+piece_id+'_a_'+gallery_artist_id).className;
	current_piece[mc_options_id] = piece_id;
	current_artist[mc_options_id] = gallery_artist_id;

	if(current_class.indexOf('p_saved') != -1) {
		document.getElementById('optionSavePiece_'+mc_options_id).style.display='none';
		document.getElementById('optionUnsavePiece_'+mc_options_id).style.display='block';
	}else {
		document.getElementById('optionSavePiece_'+mc_options_id).style.display='block';
		document.getElementById('optionUnsavePiece_'+mc_options_id).style.display='none';
	}
	if(current_class.indexOf('p_removed') != -1) {
		document.getElementById('optionRemovePiece_'+mc_options_id).style.display='none';
		document.getElementById('optionUnremovePiece_'+mc_options_id).style.display='block';
	}else {
		document.getElementById('optionRemovePiece_'+mc_options_id).style.display='block';
		document.getElementById('optionUnremovePiece_'+mc_options_id).style.display='none';
	}

	if (gallery_artist_id > 0) {
		document.getElementById('mc_artist_options_'+mc_options_id).style.display='block'; // Reshow artist box incase it was turned off
		if(current_class.indexOf('a_saved') != -1) {
			document.getElementById('optionSaveArtist_'+mc_options_id).style.display='none';
			document.getElementById('optionUnsaveArtist_'+mc_options_id).style.display='block';
		}else {
			document.getElementById('optionSaveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnsaveArtist_'+mc_options_id).style.display='none';
		}
		if(current_class.indexOf('a_removed') != -1) {
			document.getElementById('optionRemoveArtist_'+mc_options_id).style.display='none';
			document.getElementById('optionUnremoveArtist_'+mc_options_id).style.display='block';
		}else {
			document.getElementById('optionRemoveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnremoveArtist_'+mc_options_id).style.display='none';
		}
	}else{
		document.getElementById('mc_artist_options_'+mc_options_id).style.display='none'; // Turnoff artist box for collectors
	}

	// show the option box now that it has moved and updated with the current options.
	optBox.style.visibility = 'visible';

}

function mc_send_to_list(mc_options_id,mycatalog_lists_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=save_piece&productid="+current_piece[mc_options_id]+"&mycatalog_lists_id="+mycatalog_lists_id;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=save_piece&productid="+current_piece[mc_options_id]+"&mycatalog_lists_id="+mycatalog_lists_id;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionSavePiece_'+mc_options_id).style.display='none';
			document.getElementById('optionUnsavePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionRemovePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionUnremovePiece_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('savePiece',current_piece[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;
}

function mc_remove_from_list(mc_options_id,mycatalog_lists_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unsave_piece&productid="+current_piece[mc_options_id]+"&mycatalog_lists_id="+mycatalog_lists_id;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unsave_piece&productid="+current_piece[mc_options_id]+"&mycatalog_lists_id="+mycatalog_lists_id;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionSavePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionUnsavePiece_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('unsavePiece',current_piece[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;
}

function mc_removePiece(mc_options_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=remove_piece&productid="+current_piece[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?remove=unsave_piece&productid="+current_piece[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionRemovePiece_'+mc_options_id).style.display='none';
			document.getElementById('optionUnremovePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionSavePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionUnsavePiece_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('removePiece',current_piece[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;
}

function mc_unremovePiece(mc_options_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unremove_piece&productid="+current_piece[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?unremove=unsave_piece&productid="+current_piece[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionRemovePiece_'+mc_options_id).style.display='block';
			document.getElementById('optionUnremovePiece_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('unremovePiece',current_piece[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;
}

function mc_saveArtist(mc_options_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=save_artist&gallery_artist_id="+current_artist[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=save_artist&gallery_artist_id="+current_artist[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionSaveArtist_'+mc_options_id).style.display='none';
			document.getElementById('optionUnsaveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionRemoveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnremoveArtist_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('saveArtist',current_artist[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;
}

function mc_unsaveArtist(mc_options_id) {

	// Ajax remove the artist from a list.
	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unsave_artist&gallery_artist_id="+current_artist[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unsave_artist&gallery_artist_id="+current_artist[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionSaveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnsaveArtist_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('unsaveArtist',current_artist[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;

}

function mc_removeArtist(mc_options_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=remove_artist&gallery_artist_id="+current_artist[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=remove_artist&gallery_artist_id="+current_artist[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionRemoveArtist_'+mc_options_id).style.display='none';
			document.getElementById('optionUnremoveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionSaveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnsaveArtist_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('removeArtist',current_artist[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;

}

function mc_unremoveArtist(mc_options_id) {

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unremove_artist&gallery_artist_id="+current_artist[mc_options_id];

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unremove_artist&gallery_artist_id="+current_artist[mc_options_id];
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			// Change links
			document.getElementById('optionRemoveArtist_'+mc_options_id).style.display='block';
			document.getElementById('optionUnremoveArtist_'+mc_options_id).style.display='none';
			// Close My Collections popup
			hidetab(mc_options_id, 'hidden');
			// Update piece image
			updatePieces('unremoveArtist',current_artist[mc_options_id]);
		}
	}

	xmlhttp.send(null);

	return;

}

//
// Functions for details view and product pages using my_catalog_product_page.tpl
//

function send_to_list(productid,mycatalog_lists_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=save_piece&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=save_piece&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			//update piece images on the page
			updatePieces('savePiece',productid);

		}
	}

	xmlhttp.send(null);

	return;
}

function remove_from_list(productid,mycatalog_lists_id){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unsave_piece&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unsave_piece&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			//update piece images on the page
			updatePieces('unsavePiece',productid);
		}
	}

	xmlhttp.send(null);
	return;
}

function clear_from_list(productid,mycatalog_lists_id,page,type) {

	// Ajax remove the piece from a list.
	var xmlhttp = start_ajax();

	url="/customer/shared_list.php?action=remove_piece&lists_url_rewrite=Y&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page+"&type="+type;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/shared_list.php?action=remove_piece&lists_url_rewrite=Y&productid="+productid+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page+"&type="+type;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			if(xmlhttp.responseText) {
				// Reload the lists with the given changes
				updateList(xmlhttp.responseText);
			}
		}
	}

	xmlhttp.send(null);
	return;

}

function removePiece(productid){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=remove_piece&productid="+productid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=remove_piece&productid="+productid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			//update piece images on the page
			updatePieces('removePiece',productid);
		}
	}

	xmlhttp.send(null);
	return;
}

function unremovePiece(productid){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unremove_piece&productid="+productid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unremove_piece&productid="+productid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			//update piece images on the page
			updatePieces('unremovePiece',productid);

		}
	}

	xmlhttp.send(null);

	return;
}

function saveArtist(artistid){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=save_artist&gallery_artist_id="+artistid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=save_artist&gallery_artist_id="+artistid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			updatePieces('saveArtist',artistid);
		}
	}

	xmlhttp.send(null);

	return;
}

function unsaveArtist(artistid) {

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unsave_artist&gallery_artist_id="+artistid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unsave_artist&gallery_artist_id="+artistid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			updatePieces('unsaveArtist',artistid);
		}
	}

	xmlhttp.send(null);

	return;
}

function removeArtist(artistid){

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=remove_artist&gallery_artist_id="+artistid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=remove_artist&gallery_artist_id="+artistid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			updatePieces('removeArtist',artistid);
		}
	}

	xmlhttp.send(null);

	return;

}

function unremoveArtist(artistid) {

	var xmlhttp = start_ajax();

	url="/customer/mycollections.php?action=unremove_artist&gallery_artist_id="+artistid;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/mycollections.php?action=unremove_artist&gallery_artist_id="+artistid;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){
			updatePieces('unremoveArtist',artistid);
		}
	}

	xmlhttp.send(null);

	return;

}

function updatePieces(action, id) {
	if(action == "savePiece") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('p_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_none_a_none' || elements[i].className == 'title_box_white p_removed_a_none') {
						elements[i].className='title_box_white p_saved_a_none';
					}
					if(elements[i].className == 'title_box_white p_none_a_saved' || elements[i].className == 'title_box_white p_removed_a_saved') {
						elements[i].className='title_box_white p_saved_a_saved';
					}
					if(elements[i].className == 'title_box_white p_none_a_removed' || elements[i].className == 'title_box_white p_removed_a_removed') {
						elements[i].className='title_box_white p_saved_a_removed';
					}
					mc_style_thumbnail(current_id, "unremoved");
					button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
					button.style.display = "block";
					links = button.getElementsByTagName('a');
					links[0].style.background = "transparent";
				}
			}
		}
		// Change links in display_piece_detail
		if(document.getElementById('optionSavePiece_'+id) != null) {
			document.getElementById('optionSavePiece_'+id).style.display='none';
			document.getElementById('optionUnsavePiece_'+id).style.display='inline';
			document.getElementById('optionRemovePiece_'+id).style.display='inline';
			document.getElementById('optionUnremovePiece_'+id).style.display='none';
		}
		// Change links in product page
		if(document.getElementById('save_product_span_'+id) != null) {
			document.getElementById('save_product_span_'+id).style.display = "none";
			document.getElementById('unsave_product_span_'+id).style.display = "inline";
			document.getElementById('remove_product_span_'+id).style.display = "inline";
			document.getElementById('unremove_product_span_'+id).style.display = "none";
		}
	}
	if(action == "unsavePiece") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('p_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_saved_a_none') {
						elements[i].className='title_box_white p_none_a_none';
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						button.style.display = "none";
						links = button.getElementsByTagName('a');
						links[0].style.background = "";
					}
					if(elements[i].className == 'title_box_white p_saved_a_saved') {
						elements[i].className='title_box_white p_none_a_saved';
					}
					if(elements[i].className == 'title_box_white p_saved_a_removed') {
						elements[i].className='title_box_white p_none_a_removed';
						mc_style_thumbnail(current_id, "removed");
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						links = button.getElementsByTagName('a');
						links[0].style.background = "";
					}
				}
			}
		}
		// Change links in display_piece_detail
		if(document.getElementById('optionSavePiece_'+id) != null) {
			document.getElementById('optionSavePiece_'+id).style.display='inline';
			document.getElementById('optionUnsavePiece_'+id).style.display='none';
		}
		// Change links in product page
		if(document.getElementById('save_product_span_'+id) != null) {
			document.getElementById('save_product_span_'+id).style.display = "inline";
			document.getElementById('unsave_product_span_'+id).style.display = "none";
		}
	}
	if(action == "removePiece") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('p_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_none_a_none' || elements[i].className == 'title_box_white p_saved_a_none') {
						elements[i].className='title_box_white p_removed_a_none';
						button = current_id.replace("image_div_mc", "piece_options_button");
						getObject(button).style.display = "block";
					}
					if(elements[i].className == 'title_box_white p_none_a_saved' || elements[i].className == 'title_box_white p_saved_a_saved') {
						elements[i].className='title_box_white p_removed_a_saved';
					}
					if(elements[i].className == 'title_box_white p_none_a_removed' || elements[i].className == 'title_box_white p_saved_a_removed') {
						elements[i].className='title_box_white p_removed_a_removed';
					}
					mc_style_thumbnail(current_id, "removed");
					button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
					links = button.getElementsByTagName('a');
					links[0].style.background = "";
				}
			}
		}
		// Change links in display_piece_detail
		if(document.getElementById('optionSavePiece_'+id) != null) {
			document.getElementById('optionRemovePiece_'+id).style.display='none';
			document.getElementById('optionUnremovePiece_'+id).style.display='inline';
			document.getElementById('optionSavePiece_'+id).style.display='inline';
			document.getElementById('optionUnsavePiece_'+id).style.display='none';
		}
		// Change links in product page
		if(document.getElementById('save_product_span_'+id) != null) {
			document.getElementById('remove_product_span_'+id).style.display = "none";
			document.getElementById('unremove_product_span_'+id).style.display = "inline";
			document.getElementById('save_product_span_'+id).style.display = "inline";
			document.getElementById('unsave_product_span_'+id).style.display = "none";
		}
	}
	if(action == "unremovePiece") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('p_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_removed_a_none') {
						elements[i].className='title_box_white p_none_a_none';
						mc_style_thumbnail(current_id, "unremoved");
						button = current_id.replace("image_div_mc", "piece_options_button");
						getObject(button).style.display = "none";
					}
					if(elements[i].className == 'title_box_white p_removed_a_removed') {
						elements[i].className='title_box_white p_none_a_removed';
					}
					if(elements[i].className == 'title_box_white p_removed_a_saved') {
						elements[i].className='title_box_white p_none_a_saved';
						mc_style_thumbnail(current_id, "unremoved");
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						links = button.getElementsByTagName('a');
						links[0].style.background = "transparent";
					}
				}
			}
		}
		// Change links in display_piece_detail
		if(document.getElementById('optionRemovePiece_'+id) != null) {
			document.getElementById('optionRemovePiece_'+id).style.display='inline';
			document.getElementById('optionUnremovePiece_'+id).style.display='none';
		}
		// Change links in product page
		if(document.getElementById('remove_product_span_'+id) != null) {
			document.getElementById('remove_product_span_'+id).style.display = "inline";
			document.getElementById('unremove_product_span_'+id).style.display = "none";
		}
	}
	if(action == "saveArtist") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('a_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_saved_a_none' || elements[i].className == 'title_box_white p_saved_a_removed') {
						elements[i].className='title_box_white p_saved_a_saved';
					}
					if(elements[i].className == 'title_box_white p_removed_a_none' || elements[i].className == 'title_box_white p_removed_a_removed') {
						elements[i].className='title_box_white p_removed_a_saved';
					}
					if(elements[i].className == 'title_box_white p_none_a_none' || elements[i].className == 'title_box_white p_none_a_removed') {
						elements[i].className='title_box_white p_none_a_saved';
						mc_style_thumbnail(current_id, "unremoved");
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						button.style.display = "block";
						links = button.getElementsByTagName('a');
						links[0].style.background = "transparent";
					}
				}
			}
		}
		// Change links in display_piece_detail and product page
		var elements = document.getElementsByTagName('span');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('optionSaveArtist_'+id) != -1 || current_id.indexOf('save_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
				if (current_id.indexOf('optionUnsaveArtist_'+id) != -1 || current_id.indexOf('unsave_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionRemoveArtist_'+id) != -1 || current_id.indexOf('remove_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionUnremoveArtist_'+id) != -1 || current_id.indexOf('unremove_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
			}
		}
	}
	if(action == "unsaveArtist") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('a_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_saved_a_saved') {
						elements[i].className='title_box_white p_saved_a_none';
					}
					if(elements[i].className == 'title_box_white p_removed_a_saved') {
						elements[i].className='title_box_white p_removed_a_none';
					}
					if(elements[i].className == 'title_box_white p_none_a_saved') {
						elements[i].className='title_box_white p_none_a_none';
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						button.style.display = "none";
						links = button.getElementsByTagName('a');
						links[0].style.background = "";
					}
				}
			}
		}
		// Change links in display_piece_detail and product page
		var elements = document.getElementsByTagName('span');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('optionSaveArtist_'+id) != -1 || current_id.indexOf('save_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionUnsaveArtist_'+id) != -1 || current_id.indexOf('unsave_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
			}
		}
	}
	if(action == "removeArtist") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('a_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_saved_a_none' || elements[i].className == 'title_box_white p_saved_a_saved') {
						elements[i].className='title_box_white p_saved_a_removed';
					}
					if(elements[i].className == 'title_box_white p_removed_a_none' || elements[i].className == 'title_box_white p_removed_a_saved') {
						elements[i].className='title_box_white p_removed_a_removed';
					}
					if(elements[i].className == 'title_box_white p_none_a_none' || elements[i].className == 'title_box_white p_none_a_saved') {
						elements[i].className='title_box_white p_none_a_removed';
						mc_style_thumbnail(current_id, "removed");
						button = getObject(current_id.replace("image_div_mc", "piece_options_button"));
						button.style.display = "block";
						links = button.getElementsByTagName('a');
						links[0].style.background = "";
					}
				}
			}
		}
		// Change links in display_piece_detail and product page
		var elements = document.getElementsByTagName('span');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('optionRemoveArtist_'+id) != -1 || current_id.indexOf('remove_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
				if (current_id.indexOf('optionUnremoveArtist_'+id) != -1 || current_id.indexOf('unremove_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionSaveArtist_'+id) != -1 || current_id.indexOf('save_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionUnsaveArtist_'+id) != -1 || current_id.indexOf('unsave_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
			}
		}
	}
	if(action == "unremoveArtist") {
		// Change image class in display_piece_thumbnail
		var elements = document.getElementsByTagName('div');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('image_div') != -1 && current_id.indexOf('a_'+id) != -1) {
					if(elements[i].className == 'title_box_white p_saved_a_removed') {
						elements[i].className='title_box_white p_saved_a_none';
					}
					if(elements[i].className == 'title_box_white p_removed_a_removed') {
						elements[i].className='title_box_white p_removed_a_none';
					}
					if(elements[i].className == 'title_box_white p_none_a_removed') {
						elements[i].className='title_box_white p_none_a_none';
						mc_style_thumbnail(current_id, "unremoved");
						button = current_id.replace("image_div_mc", "piece_options_button");
						getObject(button).style.display = "none";
					}
				}
			}
		}
		// Change links in display_piece_detail and product page
		var elements = document.getElementsByTagName('span');
		for (var i=0; i < elements.length; i++) {
			var current_id = elements[i].getAttribute('id');
			if (current_id) {
				if (current_id.indexOf('optionRemoveArtist_'+id) != -1 || current_id.indexOf('remove_artist_span_'+id) != -1) {
					elements[i].style.display='inline';
				}
				if (current_id.indexOf('optionUnremoveArtist_'+id) != -1 || current_id.indexOf('unremove_artist_span_'+id) != -1) {
					elements[i].style.display='none';
				}
			}
		}
	}
}

function mc_style_thumbnail(current_id, action) {
	if (action == "removed") {
		var new_display = "block";
		var new_background = "#F1F1F1";
		var new_color = "#F1F1F1";
	}else {
		var new_display = "none";
		var new_background = "";
		var new_color = "#000";
	}
	
	removed_image = current_id.replace("div", "removed");
	getObject(removed_image).style.display = new_display;
	details_div = current_id.replace("div", "details");
	getObject(details_div).style.background = new_background;
	details_spans = getObject(details_div).getElementsByTagName('span');
	for (var j=0; j < details_spans.length; j++) {
		details_spans[j].style.color = new_color;
	}
}

function mc_button(id, action) {
	if (action == "over") {
		getObject(id).style.display = "block";
	}else {
		div_id = id.replace("piece_options_button", "image_div_mc");
		if (getObject(div_id).className == 'title_box_white p_none_a_none') {
			getObject(id).style.display = "none";
		}
	}
}

function movePiece(productid,destination_list_id,mycatalog_lists_id,page) {

	// Ajax move piece to different list
	var xmlhttp = start_ajax();

	url="/customer/shared_list.php?action=move_piece&lists_url_rewrite=Y&productid="+productid+"&destination_list_id="+destination_list_id+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/shared_list.php?action=move_piece&lists_url_rewrite=Y&productid="+productid+"&destination_list_id="+destination_list_id+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){

			if (xmlhttp.responseText) {
				// Reload the lists with the given changes
				updateList(xmlhttp.responseText);
			}

		}
	}

	// Close the menu to create a new list
	hidePopup();

	xmlhttp.send(null);

	return;

}

function copyPiece(productid,destination_list_id,mycatalog_lists_id,page) {

	// Ajax move piece to different list
	var xmlhttp = start_ajax();

	url="/customer/shared_list.php?action=copy_piece&lists_url_rewrite=Y&productid="+productid+"&destination_list_id="+destination_list_id+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page;

	try{
		xmlhttp.open("GET",url+"&ajax_loaded=Y");
	} catch(E){
		//call to open failed, should redirect the user to the appropriate page anyways
		window.location="http://"+location.host+"/customer/shared_list.php?action=copy_piece&lists_url_rewrite=Y&productid="+productid+"&destination_list_id="+destination_list_id+"&mycatalog_lists_id="+mycatalog_lists_id+"&page="+page;
		return;
	}

	xmlhttp.onreadystatechange=function(){
		if(xmlhttp.readyState==4){

			if (xmlhttp.responseText) {
				// Reload the lists with the given changes
				updateList(xmlhttp.responseText);
			}

		}
	}

	// Close the menu to create a new list
	hidePopup();

	xmlhttp.send(null);

	return;

}

function updateList(list_template) {

	document.getElementById("list_content").innerHTML = list_template

}

/*

	This function takes the notes and productid and changes the appropriate hidden
	field for this form.

*/

function selectPieceNotes(notes,productid) {

	document.getElementById('form_piece_notes_'+productid).notes.value = notes;

}
// for family filter
function toggle_af (enabled) {

	// Go through all the divs and find the adult filter image and adult filter settings divs
	// If the adult filter is enabled set both as block if it's not enabled hide both.
	var divs = document.getElementsByTagName('div');
	for (var i=0; i < divs.length; i++) {
		var current_id = divs[i].getAttribute('id');
		if (current_id) {
			if (enabled) {
				// Show all the adult filter images
				if (current_id.indexOf('afi_') != -1) {
					document.getElementById(current_id).style.display = 'block';
				}
				// Hide all the adult filter settings and the product images
				if (current_id.indexOf('afs_') != -1 || current_id.indexOf('afp_') != -1) {
					document.getElementById(current_id).style.display = 'none';
				}
			}else{
				// Show all the product images
				if (current_id.indexOf('afp_') != -1) {
					document.getElementById(current_id).style.display = 'block';
				}
				// Hide all the adult filter images and settings
				if (current_id.indexOf('afi_') != -1 || current_id.indexOf('afs_') != -1) {
					document.getElementById(current_id).style.display = 'none';
				}
			}

		}
	}

	// Set the cookie that will know whether family filter is on or not.
	// Store for a year.
	if (enabled) {
		var af_value = 'enabled';
	}else{
		var af_value = 'disabled';
	}
	var af_date = new Date();
	af_date.setTime(af_date.getTime()+(365*24*60*60*1000));
	var af_expires = af_date.toGMTString();
	document.cookie = "settings_adult_filter="+af_value+";expires="+af_expires+";path=/";

}

// for changing between thumbnails and details piece view
function change_view(view_type) {
	var dv_date = new Date();
	dv_date.setTime(dv_date.getTime()+(365*24*60*60*1000));
	var dv_expires = dv_date.toGMTString();
	document.cookie = "settings_piece_view="+view_type+";expires="+dv_expires+";path=/";
	location.reload(true);
}

// for changing between thumbnails and artist provider view
function change_provider_view(view_type) {
	var dv_date = new Date();
	dv_date.setTime(dv_date.getTime()+(365*24*60*60*1000));
	var dv_expires = dv_date.toGMTString();
	document.cookie = "settings_provider_view="+view_type+";expires="+dv_expires+";path=/";
	location.reload(true);
}