function UJPF_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.UJPF_p) d.UJPF_p=new Array();
    var i,j=d.UJPF_p.length,a=UJPF_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.UJPF_p[j]=new Image; d.UJPF_p[j++].src=a[i];}}
}

function UJPF_swapImgRestore() { //v3.0
  var i,x,a=document.UJPF_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function UJPF_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=UJPF_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function UJPF_swapImage() { //v3.0
  var i,j=0,x,a=UJPF_swapImage.arguments; document.UJPF_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=UJPF_findObj(a[i]))!=null){document.UJPF_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//-->


document.ondragstart = function () { return false; }; 


var ujpf = {};

ujpf.rootURL = "http://www.unjourparfees.com";
ujpf.pageWidth = 1000;
ujpf.pageHeight = 580;
ujpf.padding = 10;
ujpf.easeOutFactor = 10;
ujpf.topSpeed = 50;
ujpf.panIntervalInMilliseconds = 20;

ujpf.gotoPath = function(path){
	ujpf.gotoScreen(path.pageId, path.parameters);
}

ujpf.gotoScreen = function(pageId, parameters){
	ujpf.wasManuallyDragged = false;
	ujpf.historyController.addToHistory({pageId: pageId, parameters: parameters}, this);
	
	ujpf.currentPageId = pageId;
	
    //ujpf.panCallback = callback;
	var width =document.viewport.getWidth();
	var height = document.viewport.getHeight();
	
	var element = $(pageId);
	var pageDimensions = Element.getDimensions(element);
	ujpf.offsetLeft = (width-pageDimensions.width)/2;
	ujpf.offsetTop = (height-pageDimensions.height)/2;
	
	
	

	ujpf.canvas = $('canvas');
	
	if(element==null)
		return;
	
	//var offset = Element.cumulativeOffset(element);
	ujpf.targetPosition = {top: -element.offsetTop+ujpf.offsetTop, left: -element.offsetLeft+ujpf.offsetLeft};
	ujpf.currentPosition  = Element.cumulativeOffset(ujpf.canvas);
	
	//if another interval still exists then remove it...memory issues otherwise
	if(ujpf.panInterval!=null)
		window.clearInterval(ujpf.panInterval);
		
	ujpf.panInterval = window.setInterval(ujpf.panToScreen, ujpf.panIntervalInMilliseconds);
	
	ujpf.ySpeed = (ujpf.targetPosition.top-ujpf.currentPosition.top)/30;
	ujpf.xSpeed = (ujpf.targetPosition.left-ujpf.currentPosition.left/30);
	
	//ujpf.loadPage($(ujpf.currentPageId+"Container"), "work", {index:2});
}

ujpf.panToScreen = function(){
	ujpf.wasManuallyDragged = false;
	var speedY = ((ujpf.targetPosition.top-ujpf.currentPosition.top)/ujpf.easeOutFactor);
	var speedX = ((ujpf.targetPosition.left-ujpf.currentPosition.left)/ujpf.easeOutFactor);
	
	if(Math.abs(speedY)>ujpf.topSpeed){
		if(speedY>0)
			speedY=ujpf.topSpeed;
		else
			speedY=-ujpf.topSpeed;
	}
	
	if(Math.abs(speedX)>ujpf.topSpeed){
		if(speedX>0)
			speedX=ujpf.topSpeed;
		else
			speedX=-ujpf.topSpeed;
	}
		
	ujpf.currentPosition.top = ujpf.currentPosition.top+speedY;
	ujpf.currentPosition.left = ujpf.currentPosition.left+speedX;

	ujpf.canvas.style.top = ujpf.currentPosition.top+"px";
	ujpf.canvas.style.left = ujpf.currentPosition.left+"px";
	
	if(Math.abs(Math.round(ujpf.currentPosition.top)-Math.round(ujpf.targetPosition.top))<3&&
		Math.abs(Math.round(ujpf.currentPosition.left)-Math.round(ujpf.targetPosition.left))<3){
			ujpf.clearPanInterval();
	}
}

ujpf.clearPanInterval = function(){
	window.clearInterval(ujpf.panInterval);
	ujpf.panInterval = null;
	//ujpf.panCallback();
	ujpf.reachedScreen();
}

ujpf.positionScreen = function(pageId, parameters){
	ujpf.wasManuallyDragged = false;
	ujpf.currentPageId = pageId;

    //ujpf.panCallback = callback;
	var width =document.viewport.getWidth();
	var height =document.viewport.getHeight();

	var element = $(pageId);
	var pageDimensions = Element.getDimensions(element);
	ujpf.offsetLeft = (width-pageDimensions.width)/2;
	ujpf.offsetTop = (height-pageDimensions.height)/2;
	
	ujpf.canvas = $('canvas');
	
	if(element==null)
		return;
	
	//var offset = Element.cumulativeOffset(element);
	ujpf.targetPosition = {top: -element.offsetTop+ujpf.offsetTop, left: -element.offsetLeft+ujpf.offsetLeft};
	
	ujpf.canvas.style.top = ujpf.targetPosition.top+"px";
	ujpf.canvas.style.left = ujpf.targetPosition.left+"px";
}

//this positions the window when a browser window is resized
ujpf.onResize = function(){
	if(ujpf.wasManuallyDragged==false) //if manually navigate you dont want to pan to screens on resize
		ujpf.gotoScreen(ujpf.currentPageId);
}

ujpf.reachedScreen = function(){
	
}

ujpf.getURLParameter = function( name )
{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return null;
	  else
		return results[1];
}



ujpf.init = function(){
	var draggable = new Draggable($('canvas'), 0, -2200, 0, -5200);
	draggable.onDragStart = ujpf.onManualPan; 
	

	/*var GaleriePhotos = $('GaleriePhotos');
	GaleriePhotos.cleanWhitespace();
	var images = GaleriePhotos.childNodes;
	for(var i=0; i<images.length; i++){
		new Draggable(images[i], null, null, null, null);
		images[i].onmouseover = function(){
			var element = Element.select(this, ".description");
			if(element!=null&&element[0]!=null){
				element[0].style.display = "block";
			}
		}.bind(images[i]);
		
		images[i].onmousemove = function(event){
			var y = Event.pointerY(event||window.event);
			var x = Event.pointerX(event||window.event);
			var element = Element.select(this, ".description");
			var offsets = Element.cumulativeOffset(this);
			if(element!=null&&element[0]!=null){
				element[0].style.top = (y-offsets.top)+"px";
				element[0].style.left = (x-offsets.left)+"px";
			}
		}.bind(images[i]);
		
		images[i].onmouseout = function(){
			var element = Element.select(this, ".description");
			if(element!=null&&element[0]!=null){
				element[0].style.display = "none";
			}
		}.bind(images[i]);
	}*/
}

ujpf.onManualPan = function(){
	ujpf.wasManuallyDragged = true; //on resize we dont want to pan to screen anymore
	ujpf.clearPanInterval(); //on drag start we want to clear the panning
	ujpf.currentPageId = null;
}

/*
 *	Draggable
 */

var Draggable = Class.create();
Draggable.staticCounter = 0; //this is a counter that increments when an item is dragged. It is added to the Z-Index so that the last dragged item is always on top.

Draggable.prototype = {
	initialize: function(htmlElement, limitYMax, limitYMin, limitXMax, limitXMin){
		this.htmlElement = htmlElement;
		this.limitYMax = limitYMax;
		this.limitYMin = limitYMin;
		this.limitXMax = limitXMax;
		this.limitXMin = limitXMin;
		
		try{
			this.originalZIndex =  parseInt(htmlElement.style.zIndex);
		}catch(e){
			this.originalZIndex = 500;
		}
		
		if(isNaN(this.originalZIndex)){
			this.originalZIndex = 500;
		}
				
		this.boundMouseMainMoveListener = this.mouseMainMoveListen.bind(this);
		this.boundStopObservingMain = this.stopObservingMain.bind(this);
		this.htmlElement.onmousedown = this.startDragMain.bindAsEventListener(this);
	},
	
	startDragMain: function(event){
		this.onDragStart();
		event.cancelBubble = true; //in case of 2 draggables over each other we only want to notify the first one. eg: Trashcan
		Draggable.staticCounter++;
		this.htmlElement.style.zIndex = 1000+Draggable.staticCounter;
		
		//start listening to mouse moves
		Event.observe(document, "mousemove", this.boundMouseMainMoveListener);
		Event.observe(document, "mouseup", this.boundStopObservingMain);
		
		this.initialMouseX = parseInt(Event.pointerX(event));
		this.initialMouseY = parseInt(Event.pointerY(event));
		
		this.initialMainY = this.htmlElement.offsetTop;
		this.initialMainX = this.htmlElement.offsetLeft;
		return false; //image bug fix
	},
	
	onDragStart: function(){
		
	},
	
	/*
	 * This moves the box around after startDragMain started it
	 */
	mouseMainMoveListen: function(event){
		event.cancelBubble = true; //in case of 2 draggables over each other we only want to notify the first one. eg: Trashcan
		var xDelta = Event.pointerX(event)-this.initialMouseX;
		var yDelta = Event.pointerY(event)-this.initialMouseY;
		var top = (parseInt(this.initialMainY)+parseInt(yDelta));
		var left = (parseInt(this.initialMainX)+parseInt(xDelta));
		
		//limit to area
		if(this.limitYMax!=null&&top>this.limitYMax)
			top=this.limitYMax;
			
		if(this.limitYMin!=null&&top<this.limitYMin)
			top=this.limitYMin;
		
		if(this.limitXMax!=null&&left>this.limitXMax)
			left=this.limitXMax;
			
		if(this.limitXMin!=null&&left<this.limitXMin)
			left=this.limitXMin;
			
		this.htmlElement.style.top  = top+"px";
		this.htmlElement.style.left = left+"px";
		return false;
	},
	
	/*
	 * This stops the dragging behaviour
	 */
	stopObservingMain: function(){
		this.htmlElement.style.zIndex = this.originalZIndex+Draggable.staticCounter;
		Event.stopObserving(document, "mousemove", this.boundMouseMainMoveListener);
		Event.stopObserving(document, "mouseup", this.boundStopObservingMain);
	}
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}