/////////////////////////////////////////////////////////////////////////////////////////////////// BUTTON_RED LOADER
function showLoader($btnID){
	var div_btn = $($btnID)
	var div_loader = $($btnID+"_loader")
	setCss(div_loader, "display", "block")
	setClass(div_btn, "button_red disabled")
	
	// add spinner
	if(!div_btn.hasLoader){
		new Spinner({lines:9, length:0, width:3, radius:7, color:"#A32332", speed:1.5, trail:70, shadow:false}).spin(div_loader);
		div_btn.hasLoader = true
	}else{
		hideLoader($btnID)
	}
	
	div_btn.onclick_fc = div_btn.onclick
	div_btn.onclick = null
}

function hideLoader($btnID){
	var div_btn = $($btnID)
	var div_loader = $($btnID+"_loader")
	
	setCss(div_loader, "display", "none")
	setClass(div_btn, "button_red")
	
	if(div_btn.onclick_fc)	div_btn.onclick = div_btn.onclick_fc;
}


/////////////////////////////////////////////////////////////////////////////////////////////////// ADD BOOKMARK
function addBookmark($config){		// this checks if user is logged in, and fires submitBookmark
	var cookie = getCookie(USER_COOKIE_ID);		// is set in operations.php
	if(!cookie){
		//popup_login_onOK = {func:getURL, vars:"<?php echo PAGE_URL; ?>"}		// OLD: refresh page after loginOK
		
		popup_login_onOK = {func:addBookmark_afterLogin, vars:{item_id:$config.item_id, btn_id:$config.btn_id, type:$config.type}}		// trigger this function again after login
		openPopup("login");
			
	}else{
		submitBookmark($config.item_id, LANGUAGE, $config.type, $config.btn_id)		// see components.js
	}
}

function addBookmark_afterLogin($config){
	closePopup()
	addBookmark($config)
}

function submitBookmark($item_id, $language, $type, $btnID){		// type: "auction", "gallery"
	var json={};
	json.operation = 'add_favourite';
	json.item_id = $item_id;
	json.language = $language;
	json.type = $type;
	
	//--------------------------------- SUBMIT
	var loader = new Loader();
	loader.onComplete = function($response){
		$json = JSON.parse($response)
		
		// ERROR:
		if($json.error){
			if(DEVEL)	alert("error:\n"+JSON.stringify($json))
		// OK:
		}else{
			// push item-html in sidebar and do animation:
			if($($json.duplicate_id))	setCss($($json.duplicate_id), "display", "none")		// remove duplicate item, if present
			
			var current_amount = $("sidebar_items").getAttribute("amount")
			if(current_amount ==0){
				$("sidebar_items").innerHTML = "";		// remove "no bookmarks"-text
				$("sidebar_items").setAttribute("amount", 1)
			}
			$("sidebar_items").innerHTML = $json.favourite_html + $("sidebar_items").innerHTML;
			
			if(getCss($("sidebar_items"), "height") > 535){
				setCss($("sidebar_items"), "height", 535, true)		// limit to max height
			}
			proto.tween($($json.id), {prop:"opacity", start:0, end:1})
			proto.tween($($json.id), {prop:"margin-top", start:-getCss($($json.id), "height"), end:0})
		}
	}
	
	loader.onError = function($err){
		if(DEVEL)	alert("error:\n"+$err)
	}
	loader.load(PHP+'operations.php?json='+encodeURIComponent(JSON.stringify(json)));
	
	//--------------------------------- KILL BUTTON
	setCss($($btnID), "display", "none")
	setCss($($btnID+"_disabled"), "display", "block")
}


/////////////////////////////////////////////////////////////////////////////////////////////////// REMOVE BOOKMARK
function removeBookmark($config){
	var json={};
	json.operation = 'remove_favourite';
	json.id = $config.favourite_id;
	json.language = LANGUAGE;
	json.type = $config.type;
	
	var loader = new Loader();
	loader.onComplete = function($json){
		if($json.error && DEVEL)	alert("error:\n"+JSON.stringify($json))
	}
	loader.load(PHP+'operations.php?json='+encodeURIComponent(JSON.stringify(json)));
	
	//--------------------------------- REMOVE ITEM
	setCss($($config.id), "display", "none")
	/*proto.tween($($config.id), {prop:"opacity", end:0, onComplete:function(){
		setCss($($config.id), "display", "none")
	}})*/
}



