var fn = {
	el: {
		get: function(id) {
			var el = document.getElementById(id);
			if (el)
				return el;
			else
				return null;
		},
		style: function(id,st,value) {
			var obj = fn.el.get(id);
			obj.style[st] = value;
		},
		attribute: function(id,attr,value) {
			var obj = fn.el.get(id);
			obj.setAttribute(attr,value);
		}
	},
	strings: {
		aText: new Array(),
		clear: function() {
			aText = new Array();
		},
		add: function(txt) {
			aText.push(txt);
		},
		concat: function(sep) {
			return aText.join(sep);
		}
	},
	popup: {
		show: function(position) {
			var p = fn.el.get('popup');
			for (var a in position) {
				p.style[a] = position[a];
			}
			return p;
		},
		hide: function() {
			var p = fn.el.get('popup');
			p.style.display = 'none';			
		}
	},
	images: {
		preLoad: function(filename,destImageID,onLoadCallback) {
			return (new rmLoadImage(filename,destImageID,onLoadCallback));
		}
	}
}

function rmLoadImage(filename,destImage,onLoadCallback) {
	var destination = destImage;
	var callback = onLoadCallback;
	
	var img = new Image();
	if (callback) {
		img.onload = function() {
			if (callback)
				callback();
			if (destination) {
				var el = fn.el.get(destination);
				el.src = img.src;
			}
		}
		img.src = filename;
	}
	else {
		var el = fn.el.get(destination);
		el.src = filename;
	}
	return img;	
}
function setFocus(obj) {
	obj.style.border='1px solid #ff0';
	obj.style.backgroundColor='#FC890A';
}
function unFocus(obj) {
	obj.style.border='1px solid #008';
	obj.style.backgroundColor='#fff';
}
function showMessage() {
	fn.popup.show({left:'50%',top:'68px',width:'480px',marginLeft:'-20px',height:'380px',display:'block'});
	var content = '<div style="margin-bottom:10px;"><b style="color:#fff;font-size:85%;">Please enter your name:</b><input id="txtName" type="text" onfocus="setFocus(this)" onblur="unFocus(this)" style="margin-left:58px;width:201px;font-weight:bold;border:1px solid #008;" value="'+messageDetails.name+'"/></div>'+
		'<div style="margin-bottom:10px;"><b style="color:#fff;font-size:85%;">Please enter your email address:</b><input id="txtEmailAddress" type="text" onfocus="setFocus(this)" onblur="unFocus(this)" style="margin-left:10px;width:201px;font-weight:bold;border:1px solid #008;" value="'+messageDetails.emailAddress+'"/></div>'+
		'<b style="color:#fff;font-size:85%;">Please type your message in the box below:</b><textarea id="txtMessage" onfocus="setFocus(this)" onblur="unFocus(this)" wrap="virtual" style="margin-top:5px;width:99%;font-weight:bold;border:1px solid #008;height:185px;"></textarea>'+
		'<div style="text-align:right;margin-top:10px;"><button onclick="sendMessage()" type="button">Send</button><button onclick="fn.popup.hide()" type="button">Cancel</button></div>';
	fn.el.get('popupContent').innerHTML = content;
	setTimeout(function(){fn.el.get('txtName').focus();},50);
}
function sendMessage() {
	var name = fn.el.get('txtName').value;
	var emailAddress = fn.el.get('txtEmailAddress').value;
	var message= fn.el.get('txtMessage').value;
	
	if (name=='') {
		alert('Please enter your Name');
		fn.el.get('txtName').focus();
	} else if (emailAddress=='') {
		alert('Please enter your email address so we can contact you');
		fn.el.get('txtEmailAddress').focus();
	} else if (message=='') {
		alert('Please enter your message to us before sending');
		fn.el.get('txtMessage').focus();
	} else {
		ajax.request('','ajax/sendMessage.asp?name='+escape(name)+'&emailAddress='+escape(emailAddress)+'&message='+escape(message),function(result) {
			alert('Your message has been successfully sent to Kenny Mckaig Photography.');
			fn.popup.hide();
		});
	}
}
function showLogin(refreshAlbumsList,showAlbumID) {
	fn.popup.show({left:'50%',top:'68px',width:'260px','marginLeft':'235px',height:'160px',display:'block'});
	var content = '<div style="margin-bottom:5px;"><b style="color:#fff;font-size:85%;">Username:</b><input id="userName" type="text" onfocus="setFocus(this)" onblur="unFocus(this)" style="margin-left:10px;width:100px;font-weight:bold;border:1px solid #008;"/></div>'+
				  '<b style="color:#fff;font-size:85%;">Password:</b><input id="password" type="password" onfocus="setFocus(this)" onblur="unFocus(this)" style="margin-left:11px;width:100px;border:1px solid #008;"/>'+
				  '<div style="text-align:right;margin-top:10px;margin-right:5px;"><button onclick="login('+refreshAlbumsList+','+showAlbumID+')" type="button">Login</button><button onclick="fn.popup.hide()" type="button">Cancel</button></div>';
	fn.el.get('popupContent').innerHTML = content;
	setTimeout(function(){fn.el.get('userName').focus();},50);
}
function login(refreshAlbumsList,showAlbumID) {
	var username = fn.el.get('userName').value;
	var password = fn.el.get('password').value;
	
	if (username=='') {
		alert('Please enter the user name given to you');
		fn.el.get('userName').focus();
	} else if (password=='') {
		alert('Please enter the password given to you');
		fn.el.get('password').focus();
	} else {
		ajax.request('','ajax/checkLogin.asp?user='+username+'&pwd='+password,function(result) {
			if (result=='error') {
				alert('Your username or password was not recognized, please try again.');
				fn.el.get('userName').focus();
				fn.el.get('userName').select();
			} else {
				fn.el.get('shade').style.display='none';
				fn.popup.hide();
				fn.el.get('loginButton').style.display='none';
				fn.el.get('welcome').innerHTML = result+', <a href="#" onclick="logout('+refreshAlbumsList+');return false" class="logout">click here to logout</a>';
				fn.el.get('welcome').style.display='block';
				
				if (refreshAlbumsList)
					selectCategory(current.categoryName,current.categoryID);
					
				if (showAlbumID!=-1)
					showAlbum(showAlbumID);
			}
		 });
	}
	
}
function logout(refreshAlbumsList) {
	ajax.request('','ajax/logout.asp',function() {
		fn.el.get('loginButton').style.display='block';
		fn.el.get('welcome').innerHTML = '';
		fn.el.get('welcome').style.display='none';
		
		if (refreshAlbumsList)
			selectCategory(current.categoryName,current.categoryID);
	});
}
function showBuy() {
	fn.el.get('shade').style.display='block';
	fn.popup.show({left:'50%',top:'120px',width:'490px','marginLeft':'-250px',height:'450px',display:'block'});
	var dropdown = '<option value="0">0</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option>';
	var content =  '<div style="text-align:center;font-size:150%;background-color:#4976D1;color:#fff;border-top:1px solid #264A95;border-bottom:1px solid #264A95;padding:5px 0;margin-bottom:20px;">Buy this photograph</div>'+
					'<img src="'+path+'images/photos/'+currentAlbum+'/'+currentImage+'a.jpg" style="background-color:#fff;border:1px solid #555;padding:5px;"/>'+
					'<div style="position:absolute;top:67px;right:10px;height:190px;">'+
						'<div style="border-bottom:1px solid #264A95;background-color:#A7BCE9;font-weight:bold;height:18px;padding:2px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">Print sizes</div>'+
							'<div style="float:left;width:78px;text-align:center;">Price</div>'+
							'<div style="float:left;width:90px;text-align:center;">Quantity</div>'+
						'</div>'+
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">6" x 4"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;10.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size6">'+dropdown+'</select></div>'+
						'</div>'+
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">7" x 5"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;15.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size7">'+dropdown+'</select></div>'+
						'</div>'+
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">8" x 6"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;20.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size8">'+dropdown+'</select></div>'+
						'</div>'+
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">10" x 8"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;23.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size10">'+dropdown+'</select></div>'+
						'</div>'+						
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">12" x 8"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;30.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size12">'+dropdown+'</select></div>'+
						'</div>'+
						'<div style="border-bottom:1px solid #264A95;background-color:#D8E1F5;font-weight:normal;height:23px;padding:3px 0;">'+
							'<div style="float:left;width:90px;text-align:center;">16" x 11"</div>'+
							'<div style="float:left;width:78px;text-align:center;">&pound;50.00</div>'+
							'<div style="float:left;width:90px;text-align:center;"><select id="size16">'+dropdown+'</select></div>'+
						'</div>'+						
					'</div>'+
					'<div style="position:absolute;right:10px;bottom:60px;height:50px;color:#fff;font-size:85%;"><li>All sizes are approximate</li><li>Other sizes are available by request</li><li>Prices include postage and secure envelope</li></div>'+
					'<div style="position:absolute;right:10px;bottom:10px;"><button type="button"onclick="addToBasket()">Add to basket</button><button type="button" onclick="fn.el.get(\'shade\').style.display=\'none\';fn.popup.hide()">Cancel</button></div>';
	fn.el.get('popupContent').innerHTML = content;
//	setTimeout(function(){fn.el.get('userName').focus();},50);	
}
var basketHistory = new Array();
function addToBasket() {
	var totalQty = 0;
	totalQty += parseInt(fn.el.get('size6').value);
	totalQty += parseInt(fn.el.get('size7').value);
	totalQty += parseInt(fn.el.get('size8').value);
	totalQty += parseInt(fn.el.get('size10').value);
	totalQty += parseInt(fn.el.get('size12').value);
	totalQty += parseInt(fn.el.get('size16').value);
	
	if (totalQty>0) {
		var url = 'ajax/addToBasket.asp?album='+currentAlbum+'&img='+currentImage
					 +'&size6='+fn.el.get('size6').value
					 +'&size7='+fn.el.get('size7').value
					 +'&size8='+fn.el.get('size8').value
					 +'&size10='+fn.el.get('size10').value
					 +'&size12='+fn.el.get('size12').value
					 +'&size16='+fn.el.get('size16').value;
		basketHistory.push(url);
		ajax.request('',url,function() {
			fn.el.get('shade').style.display='none';
			fn.popup.hide();
		 });
	} else
		alert('Please select a quantity beside the Print Sizes you would like to order');
}
function getBasket() {
	ajax.request('','ajax/getBasket.asp',function(result) {
		if (result=='timeout') {
			if (confirm('Your session has timed out and for security reasons your basket has been emptied.\n\nWe can, if you like, place the items you had ordered back into your basket.\n\nIf you don\'t want us to do this, please press the Cancel button.')) {
				for (var url in basketHistory) {
					ajax.request('',basketHistory[url]);
				}
			}
		} else {
			fn.el.get('shade').style.display='block';
			fn.popup.show({left:'50%',top:'40px',width:'650px','marginLeft':'-325px',height:'600px',display:'block'});
			var content =  '<div style="text-align:center;font-size:150%;background-color:#4976D1;color:#fff;border-top:1px solid #264A95;border-bottom:1px solid #264A95;padding:5px 0;margin-bottom:20px;">Your basket</div>';
			var basket = eval('('+result+')');
			if (basket.length==0) {
				content += '<div style="text-align:center;font-weight:bold;font-size:120%;">There are no items in your basket</div>'+
						   '<div style="position:absolute;right:10px;bottom:10px;"><button onclick="fn.el.get(\'shade\').style.display=\'none\';fn.popup.hide()">Continue shopping</button></div>';
			} else {
				content += '<div style="border-top:1px solid #264A95;border-bottom:1px solid #264A95;background-color:#A7BCE9;font-weight:bold;height:18px;padding:2px 0;">'+
									'<div style="position:relative;float:left;width:100px;text-align:left;color:#A7BCE9;">.</div>'+
									'<div style="position:relative;float:left;width:165px;text-align:left;">Photograph</div>'+
									'<div style="position:relative;float:left;width:185px;text-align:left;">Details</div>'+
									'<div style="position:relative;float:left;width:90px;text-align:right;">Cost</div>'+
									'<div class="spacer">&nbsp;</div>'+
								'</div>'+
								'<div style="position:relative;height:380px;overflow:auto;background-color:#D8E1F5;">';
				var total = 0;
				for (var line in basket) {
						var orderLine = basket[line];
						content +=  '<div style="border-bottom:1px dotted #264A95;background-color:#D8E1F5;font-weight:normal;padding:3px 0;">'+
										'<div style="position:relative;float:left;width:100px;text-align:center;"><button onclick="removeBasketItem('+orderLine.id+')">Remove</button></div>'+
										'<div style="position:relative;float:left;width:165px;text-align:left;"><img src="'+path+'images/photos/'+orderLine.album+'/'+orderLine.image+'a.jpg" style="background-color:#fff;border:1px solid #555;padding:5px;"/></div>'+
										'<div style="position:relative;float:left;width:185px;text-align:left;">'+
											'<div style="float:left;width:80px;">Size: </div><div style="float:left;width:70px;">'+orderLine.printSize+'</div>'+
											'<div style="float:left;width:80px;">Price:</div><div style="float:left;width:70px;">&pound;'+orderLine.price+'</div>'+
											'<div style="float:left;width:80px;">Quantity: </div><div style="float:left;width:70px;">'+
												'<select onchange="changeQuantity(this,'+orderLine.id+')">';
						for (var c=1; c<6; c++)
							content += '<option value="'+c+'"'+(orderLine.quantity==c?' selected':'')+'>'+c+'</option>';
						content += '</select></div></div>'+
									'<div style="position:relative;float:left;width:90px;text-align:right;">&pound;'+(orderLine.price*orderLine.quantity)+'</div>'+
									'<div class="spacer">&nbsp;</div>'+
									'</div>';
						total += (orderLine.price*orderLine.quantity);					
				}
				content += '</div><div style="border-bottom:1px solid #264A95;border-top:1px solid #264A95;background-color:#A7BCE9;font-weight:bold;height:18px;padding:2px 0;text-align:right;padding-right:32px;">Total cost: &pound;'+total+'</div>'+
							'<div style="position:absolute;right:10px;bottom:10px;"><button type="button" onclick="fn.el.get(\'shade\').style.display=\'none\';fn.popup.hide()">Continue shopping</button><button type="button" onclick="checkout()">Proceed to checkout</button></div>';
			};
			fn.el.get('popupContent').innerHTML = content;						
		}
	 });
}
function removeBasketItem(which) {
	ajax.request('','ajax/removeBasketItem.asp?which='+which,function() {
		getBasket();
	});
}
function changeQuantity(obj,which) {
	ajax.request('','ajax/updateBasket.asp?which='+which+'&qty='+obj.value,function() {
		getBasket();
	});	
}



function checkout() {
	fn.popup.hide();
	fn.popup.show({left:'50%',top:'40px',width:'800px','marginLeft':'-400px',height:'600px',display:'block'});	
	ajax.request('','ajax/paymentOptions.asp',function(result) {
		fn.el.get('popupContent').innerHTML = result;
		setTimeout('fn.el.get("firstname").focus()',100);
	});
}
function paymentComplete() {
	fn.popup.show({left:'50%',top:'40px',width:'600px','marginLeft':'-300px',height:'600px',display:'block'});	
	ajax.request('','ajax/orderComplete.asp',function(result) {
		fn.el.get('popupContent').innerHTML = result;
	});
}
function paymentFailed() {
	fn.popup.show({left:'50%',top:'40px',width:'600px','marginLeft':'-300px',height:'600px',display:'block'});	
	ajax.request('','ajax/orderFailed.asp',function(result) {
		fn.el.get('popupContent').innerHTML = result;
	});
}
var scenes = Array();
var actors = Array();

function Scene(name,frameLength)
{
	scenes[name] = this;
	
	this.frames = Array();
	
	this.name = name;
	this.numFrames = 0;
	this.frameLength = frameLength;
	
	this.action = action;
	this.nextFrame = nextFrame;
	this.moveActors = moveActors;
	this.addActor = addActor;
	this.instructActor = instructActor;
	this.debug=debug;
}
/*
	Actor instructions
	
	style property to change
		
		property
		startValue
		endValue
		startFrame
		endFrame
		addPX
		
*/
function addActor(id)
{
	if (!actors[id])
		actors[id] = document.getElementById(id);		
}
function instructActor(id,instructions)
{
	this.addActor(id);
	
	if (instructions.endFrame)
	{
		if (instructions.endFrame>this.numFrames)
			this.numFrames = instructions.endFrame;
	}
	else
	{
		if (instructions.frame>this.numFrames)
			this.numFrames = instructions.frame;
	}
	
	if (instructions.type=='setStyle' || instructions.type=='setAttr')
	{
		if (!this.frames['f'+instructions.frame])
			this.frames['f'+instructions.frame] = '';
			
		this.frames['f'+instructions.frame] += ';'+id+':'+instructions.type+':'+instructions.property+':'+instructions.value;
	}
	else
	{
		var numFrames = instructions.endFrame-instructions.startFrame+1;
		
		var distance = (instructions.endValue-instructions.startValue);
		var px = (instructions.addPX?'px':'');
		
		var nStartFrame = instructions.startFrame;
		var nEndFrame = instructions.endFrame;
		
		var constSpeed = {
			stepSize: distance/numFrames
		};

		var nValue = instructions.startValue;
		var speed = 0;
		var frame = nStartFrame-1;
		var nStartPos = instructions.startValue;
		var nEndPos = instructions.endValue;
		
		if (instructions.accelerate)
		{
			var increment = constSpeed.stepSize/1000;
			while (Math.abs(increment)<Math.abs(constSpeed.stepSize))
			{
				frame += 1;
				increment *= 2;
				nValue += increment;
				
				if (!this.frames['f'+frame])
					this.frames['f'+frame] = '';
					
				this.frames['f'+frame] += ';'+id+':'+instructions.type+':'+instructions.property+':'+parseInt(nValue)+px;
			}		
			nStartFrame = (frame+1);
			nStartPos = nValue+increment;
		}
		if (instructions.decelerate)
		{
			nValue = instructions.endValue;
			var frame = instructions.endFrame+1;
			var increment = -constSpeed.stepSize/1000;
			while (Math.abs(increment)<Math.abs(constSpeed.stepSize))
			{
				frame -= 1;
				increment *= 2;
				nValue += increment;
				
				if (!this.frames['f'+frame])
					this.frames['f'+frame] = '';

				this.frames['f'+frame] += ';'+id+':'+instructions.type+':'+instructions.property+':'+parseInt(nValue)+px;
			}
			
			nEndFrame = (frame-1);
			nEndPos = nValue+increment;
		}
		constSpeed.stepSize = (nEndPos-nStartPos+1)/(nEndFrame-nStartFrame+1);
		nValue = nStartPos;
		for (var frame=nStartFrame;frame<=nEndFrame; frame++)
		{
			nValue += constSpeed.stepSize;
			
			if (!this.frames['f'+frame])
				this.frames['f'+frame] = '';
			
			this.frames['f'+frame] += ';'+id+':'+instructions.type+':'+instructions.property+':'+parseInt(nValue)+px;
		}
	}
}
var doWhatNext;
function action(afterEffects)
{
	if (afterEffects)
		doWhatNext = afterEffects;
	else
		doWhatNext = null;
		
	this.frame = 0;
	setTimeout('scenes[\''+this.name+'\'].nextFrame()',10);
}
function debug()
{
	for (var f=1; f<=this.numFrames; f++)
	{
		if (this.frames['f'+f])
			__debug('Frame '+f+': '+this.frames['f'+f]);
		else
			__debug('Frame '+f+': no movemet');
	}
}
function nextFrame()
{
	if (this.frame!=this.numFrames)
	{
		this.frame++;
		this.moveActors();
		setTimeout('scenes[\''+this.name+'\'].nextFrame()',this.frameLength);	
	}
	else
	{
		if (doWhatNext!=null)
			doWhatNext();
	}
}
function moveActors()
{	
	if (this.frames['f'+this.frame])
	{
		var frameDetails = this.frames['f'+this.frame];
		var instructions = frameDetails.split(';');
		for (var i=1; i<instructions.length; i++)
		{
			if (instructions[i]!='')
			{
				var details = instructions[i].split(':');
				var actor = actors[details[0]];
				
				if (details[1]=='position')
					actor.style[details[2]] = details[3];
					
				else if (details[1]=='transparency')
				{
					actor.style['opacity'] = parseInt(details[3])/100;
					actor.style['-moz-opacity'] = parseInt(details[3])/100;
					if (actor.filters)
						actor.filters.alpha['opacity'] = parseInt(details[3]);
				}
				else if (details[1]=='setStyle')
					actor.style[details[2]] = details[3];
					
				else if (details[1]=='setAttr')
					actor.setAttribute(details[2],details[3]);
			}
		}
	}
}


function __debug(txt)
{
	document.getElementById('debug').innerHTML += txt+'<br />';
}

function showPopup(left,top,width,height) {
	
}