var lbServletUrl = "servlets/lightBox";

Event.observe(window, 'load', function() {

// get the current light box from the server
	if ($('lbimgs')) {
		new Ajax.Updater('lbimgs', "pvLightBox.do", {
			method: 'get',
			parameters:'',
			evalScripts: true,
			asynchronous: true});
	}


});

function showPreviewLB(assetId) {
	if ($('pvImg')) $('pvImg').src="";
    lightsOff();
    new Ajax.Updater($("overlay"), 'ajaxPreview.do?resource_id='+assetId);
}
function showCollectionLB(code) {
	if ($('pvImg')) $('pvImg').src="";
    lightsOff();
    new Ajax.Updater($("overlay"), 'ajaxCollection.do?collectionCode='+code, {
    	evalScripts:false,
		asynchronous:false
    });
}

function hideLb() {
	if($('lbcontent')) $('lbcontent').hide();
	if($('imagesDiv')) $('imagesDiv').setStyle({'bottom':'24px'});
	if($('lb')) $('lb').setStyle({height:'24px'});
	if($('lbHideDiv')) $('lbHideDiv').hide();
	if($('lbHideDiv')) $('lbShowDiv').show();
	document.cookie = "LBhide=true";
}
function showLb() {
	if($('imagesDiv')) $('imagesDiv').setStyle({'bottom':'110px'});
	if($('lb')) $('lb').setStyle({height:'110px'});
	if($('lbcontent')) $('lbcontent').show();
	if($('lbShowDiv')) $('lbShowDiv').hide();
	if($('lbHideDiv')) $('lbHideDiv').show();
	document.cookie = "LBhide=";
}
function toggleLb() {
	if($('lbcontent')) {
		if ($('lbcontent').visible()) {
			hideLb();
		} else {
			showLb();
		}
	}
}

function changeLb(no, img) {
	new Ajax.Updater('lbimgs', 'pvLightBox.do', {
		method: 'get',
		onComplete:function(request){
			imagesInLb = $$('img.lb-tn').size();
			updateLbImgCount();
		},
		parameters:'chgLbNo='+no,
		evalScripts:false,
		onSuccess: function(transport) {
			if (currentLbIcon) {
				currentLbIcon.className = "nalbi";
				currentLbIcon.src = "images/lb"+ currentLbNo +".gif";
			}
			img.className = "albi";
			img.src = "images/lb"+no+"a.gif";
			currentLbIcon = img;
			currentLbNo = no;
		},
		asynchronous: true
	});
}

// will be called first, does AJAX request
function addToLbRequest(productId, imgNo) {
	new Ajax.Request(lbServletUrl, {
		method: 'get',
		parameters:'action=add&id=' + encodeURIComponent(productId) +'&imgNo=' + imgNo,
		evalScripts:true,
		on400: function(transport) {
			alert(transport.responseText);
		},
		onSuccess: function(transport) {
			eval(transport.responseText);
		},
		asynchronous: true
	})
}



// will be called after AJAX
function addToLb(url, assetId, no) {
	var div = document.createElement("DIV");
	div.className='lbTnDiv';
	div.id = 'lbdiv'+assetId;
	var img = document.createElement("IMG");
	img.src = url;
	img.className='lb-tn';
	img.id = 'lbtn'+assetId;
//	img.hide(); // does not work with IE :-(
	div.appendChild(img);
	var but = document.createElement("img");
	but.src="images/closePreview.gif";
	but.className="lbTnBtn";
	div.appendChild(but);

/*
	var x = $('lbimgs').getElementsByTagName("img");
	if (x == null || x.length < 1) {
		$('lbimgs').innerHTML = "";
		$('lbimgs').appendChild(div);
	} else {
		$('lbimgs').insertBefore(div, $('lbimgs').firstChild);
	}
*/
	$('lbimgs').insertBefore(div, $('lbimgs').firstChild.nextSibling);

	imagesInLb++;
	updateLbImgCount();
	
	new Effect.Grow(img, {duration:0.5});
	Event.observe(but, 'click', function() {removeFromLbRequest2(assetId)});
	if (no.length < 6)
		Event.observe(img, 'click', function() {showCollectionLB(no)});
	else
		Event.observe(img, 'click', function() {showPreviewLB(assetId)});
}



function removeFromLbRequest(event) {
	removeFromLbRequest2(this.id);
}
function removeFromLbRequest2(id) {
	new Ajax.Request(lbServletUrl, {
		method: 'get',
		parameters:'action=remove&id=' + encodeURIComponent(id),
//		onLoading:function(request){Element.show('indicator')},
//		onComplete:function(request){Element.hide('indicator')},
		evalScripts:true,
		on400: function(transport) {
			alert(transport.responseText);
		},
		onSuccess: function(transport) {
			eval(transport.responseText);
		}
	});
}
// will be called after AJAX
function removeFromLb(id) {
	imagesInLb--;
	updateLbImgCount();
	
	new Effect.Shrink('lbtn'+$(id), {duration:0.5, afterFinish: function() {$('lbimgs').removeChild($('lbdiv'+$(id)))}});
}

function updateLbImgCount() {
	if ($("lbNumOfImgs"))
		$("lbNumOfImgs").innerHTML = imagesInLb;
}

// first call from website
function cleanLbRequest() {
if (confirm("Really clean LB and remove all images?"))
	new Ajax.Request(lbServletUrl, {
		method: 'get',
		parameters:'action=clean',
//		onLoading:function(request){Element.show('indicator')},
//		onComplete:function(request){Element.hide('indicator')},
		evalScripts:true,
		on400: function(transport) {
			alert(transport.responseText);
		},
		onSuccess: function(transport) {
			cleanLb();
		}
	});
}

// called after AJAX
function cleanLb() {
	$('lbimgs').immediateDescendants().each(function(el){ 
		el.remove(); 
	});
}


//
// flying to SC stuff
//
var shoppingCartDiv = $('lbimgs');
var flyingDiv = false;
var currentProductDiv = false;

function flyToLb(objId, productId, number) {
	showLb();

	if(!shoppingCartDiv) {
		shoppingCartDiv = $('lbimgs');
	}

	if(!flyingDiv){
		flyingDiv = document.createElement('DIV');
		flyingDiv.style.position = 'absolute';
		document.body.appendChild(flyingDiv);
	}
	
	currentProductDiv = $(objId);

	var shoppingContentCopy = document.createElement('IMG');
	shoppingContentCopy.src = tnBaseUrl + number +"c.jpg";
	shoppingContentCopy.id ='';
	flyingDiv.innerHTML = '';
	Position.clone(currentProductDiv, flyingDiv);
	flyingDiv.appendChild(shoppingContentCopy);
	flyingDiv.setStyle({
			'display':'block',
			'z-index':100
			,'max-height':'80px'
		});


// IE 7 can not handle scale !!!!
//	var p = (80.0 / shoppingContentCopy.getHeight()) * 100;
	new Effect.Parallel(
  		[ 
//  		  new Effect.Scale(flyingDiv, p , {scaleMode:'contents'}) ,
//  		  new Effect.Scale(shoppingContentCopy, p , {scaleMode:'contents'}) ,
  		  new Effect.Move(flyingDiv, { 
			x: Position.page(shoppingCartDiv)[0] + 1, 
			y: Position.page(shoppingCartDiv)[1] - 4, 
			mode: 'absolute',
	 		transition: Effect.Transitions.sinoidal 
		}) ],
  		{ duration: 0.7,
  		  afterFinish: function() {
				addToLbRequest(productId, number);
				flyingDiv.hide();
		  } 
		}
  	);
	
}
