var ShortList = {
	// Array for content
	offerImage : {},
	currentImage : {},
	content : [],
	cookieShortListName : "",
	shortListName: "",
	maxLength : 0,
	maxLengthDesc : "",
	minLength : 0,
	minLengthDesc : "",
	alreadyInShortListMessage : "",
	offerIDArray : [],
	cookie_string : "",
	moveFactor : 0.1,
	moveDistanceStop : 20,
	moveDelay : 20,        	
	steps : 20,
	cachePrefix : "",
	
	// Retreiving the cookie content into the content array
	fill : function (cookieContent) {
		if (cookieContent!="") {
			var pairs = cookieContent.split("_|_");
			for (var x=0; x<pairs.length; x++) {
				var singles = pairs[x].split("$@");
				this.content.push([singles[0],singles[1],singles[2]]);
			}
		}
	},
	
	// Extracts the content array, creates html for the shortList, appends it, then resets the cookie content
	display : function () {
		var d = document.getElementById(this.getShortListName());
		d.innerHTML = "";
		
		var cookie_string = "";
		
		for (var x=0; x<this.content.length; x++) {
			cookie_string += this.content[x][0]+"$@"+this.content[x][1]+"$@"+this.content[x][2];
			
			if (x<this.content.length-1) {
				cookie_string += "_|_";
			}
			
			var container = document.createElement("div");
				container.style.position="relative";
				// container.style.border="1px solid #999";
				container.style.padding="5px";
				container.id=x;
			
			// offer image
			var element2 = document.createElement("div");
				element2.innerHTML = '<img src="'+ this.content[x][1] +'" width="96" />';
				// element2.style.border="1px dotted #999";
				container.appendChild(element2);
			
			// offer details
			var element3 = document.createElement("div");
				element3.innerHTML=this.content[x][2];
				// element3.style.border="1px dotted #000";
				container.appendChild(element3);

			var deleteicon = document.createElement("img");
				deleteicon.src = this.getCachePrefix() + "a/i/g/cross.gif";
				deleteicon.style.position="absolute";
				deleteicon.style.top="6px";
				deleteicon.style.right="60px";
				deleteicon.style.cursor="pointer";
				deleteicon.onclick=function(){
					ShortList.remove(this.parentNode.id);
				}
				
			container.appendChild(deleteicon);
			d.appendChild(container);
		}
		
		createCookie(this.getCookieShortListName(),cookie_string);
	
	},
	
	// Adds the offers into the content and therefore the shortlist
	add : function (idToProcess,heading) {
		if(this.content.length > this.getMaxLength()){
			alert(this.getMaxLengthDesc());
		} else {
			var imageSrc = document.getElementById(idToProcess).src;
			
			if(heading=='') {
				heading = document.getElementById("headingId_"+ idToProcess).innerHTML;
				heading = heading.replace(/\n/g,"");
			}
			
			if (!this.contains(idToProcess)) {
				this.doMagic(idToProcess);
				this.content.push([[idToProcess],[imageSrc],[heading]]);
			} else {
				alert(this.getAlreadyInShortListMessage());
			}
		}
			
		return false;
			
	},
		
	// Adds the offers into the content and therefore the shortlist
	/*
	addFromObjectSite : function (idToProcess,heading,imageSrc) {
		if(this.content.length > this.getMaxLength()){
			alert(this.getMaxLengthDesc());
		} else {
			if (!this.contains(idToProcess)){
				if(imageSrc=="carObjectSite") {
					imageSrc = this.getImageSourceFromElement();
				} else {
					if(imageSrc.substring(imageSrc.length-1)=="/") {
						imageSrc = this.getCachePrefix() + "img/icon_job.gif";
					}
				}
				this.content.push([[idToProcess],[imageSrc],[heading]]);
				this.display();
			} else {
				alert(this.getAlreadyInShortListMessage());
			}
		}
			
		return false;
	
	},
	*/
	
	// The implementation of this function can be different in other licensees due to design of the object site
	getImageSourceFromElement : function(){
		var mainImage = document.getElementById('big_img').getElementsByTagName('IMG')[0].src;
		var _image = "";
		if (mainImage.length > 0) {
			if(mainImage.src=="photo_not_available_med.gif") {
				_image = this.getCachePrefix() + "img/photo_not_available.gif";
			} else {
				_image = (!mainImage.match(/.gif/)) ? mainImage.replace(/hoved/,'thumb') : this.getCachePrefix() + "img/photo_not_available.gif";
			}
		} else {
			_image = this.getCachePrefix() + "img/photo_not_available.gif";
		}
		
		return _image;
		
	},
	
	// Function to remove. Works in the same way as add
	remove : function (id) {
		this.content.splice(id,1);
		this.display();
		return false;
	},
	
	// Function for checking if the offer is in the content array
	contains : function (idToProcess) {
		for (var x=0; x<this.content.length; x++) {
			if (this.content[x][0]==idToProcess){
				return true;
			}
		}
		
		return false;
	
	},
	
	// Function that returns a semicolon seperated array with offer IDs
	getArrayOfferIds : function () {
		cookie_string = readCookie(this.getCookieShortListName());
		if (cookie_string!="") {
		  var pairs = cookie_string.split("_|_");
		  for (var x=0; x<pairs.length; x++) {
				var singles = pairs[x].split("$@");
				if(singles[0].substring(0,3) == 'SL-') {
					singles[0] = singles[0].substring(3);
				}
				this.offerIDArray.push([singles[0]]);
		  }
		}

		if(this.offerIDArray.length >0) {
			return this.offerIDArray.join(';');
		}
	},
	
	// Getters and setters for maxlength, maxlengthDesc, cookie and shortList name.
	setCookieShortListName : function (cookieName){
		this.cookieShortListName = cookieName;
	},

	getCookieShortListName : function (){
		return this.cookieShortListName;
	},

	setShortListName : function (shortListName){
		this.shortListName = shortListName;
	},

	getShortListName : function(){
		return this.shortListName;
	},

	setMaxLength : function (maxLength){
		this.maxLength = maxLength;
	},

	getMaxLength : function(){
		return this.maxLength;
	},

	setMaxLengthDesc : function (maxLengthDesc){
		this.maxLengthDesc = maxLengthDesc;
	},

	getMaxLengthDesc : function(){
		return this.maxLengthDesc;
	},
	
	setMinLength : function (minLength){
		this.minLength = minLength;
	},

	getMinLength : function(){
		return this.minLength;
	},

	setMinLengthDesc : function (minLengthDesc){
		this.minLengthDesc = minLengthDesc;
	},

	getMinLengthDesc : function(){
		return this.minLengthDesc;
	},

	setAlreadyInShortListMessage : function(inAlreadyInShortListMessage){
		this.alreadyInShortListMessage = inAlreadyInShortListMessage;
	},

	getAlreadyInShortListMessage : function(){
		return this.alreadyInShortListMessage;
	},

	getCachePrefix : function(){
		return this.cachePrefix;
	},

	setCachePrefix : function(inCachePrefix){
		this.cachePrefix = inCachePrefix;
	},
	
	// Function that sets the semicolon seperated list of offer IDs to the form which is posted
	putOfferIds : function (targetForm, targetField) {
		var offerIds = ShortList.getArrayOfferIds();
		var result = document.forms[targetForm].elements[targetField].value = offerIds;
	},
		
	//
	moveableImage : function (idToProcess) {
		this.move = function() {
			var deltaX = this.posX - this.destX;
			var deltaY = this.posY - this.destY;
			
			var dist = Math.sqrt((deltaX*deltaX) + (deltaY*deltaY));
			var moveX = ShortList.steps * (deltaX / dist);
			var moveY = ShortList.steps * (deltaY / dist);
			
			var tempThis = this;
			
			if (ShortList.steps >= dist) {
				this.posX = this.destX;
				this.posY = this.destY;
				tempThis.remove();
				ShortList.display();
			} else {
				this.posX -= moveX;
				this.posY -= moveY;
				window.setTimeout(function(){tempThis.move();},ShortList.moveDelay,40);
			}
			
			this.newDiv.style.left = this.posX + "px";
			this.newDiv.style.top = this.posY + "px";
			
		}
		
		this.remove = function() {
			document.body.removeChild(this.newDiv);
		}
		
		this.adId = idToProcess;
		
		// Lookup chosen image on adid
		var myImage = document.getElementById(idToProcess);
		var newImage = new Image();
		newImage.src = myImage.src;
		this.destX = getRealPosition(document.getElementById("shortlistbase"),"x");
		this.destY = getRealPosition(document.getElementById("shortlistbase"),"y");

		this.newDiv = document.createElement("div");
		document.body.appendChild(this.newDiv);
		this.newDiv.appendChild(newImage);
		this.posX = getRealPosition(myImage,"x");
		this.posY = getRealPosition(myImage,"y");
		this.newDiv.style.left = this.posX + "px";
		this.newDiv.style.top = this.posY + "px";
		this.newDiv.style.position = "absolute";
	
	},
	
	doMagic : function (idToProcess) {
		this.offerImage = new this.moveableImage(idToProcess);
		this.offerImage.move();
	},
	
	// Fills cookie and displays content on page load.
	init : function() {
		var cookie = readCookie(ShortList.getCookieShortListName());
		ShortList.cookie_string = cookie ? cookie : "";
		ShortList.fill(ShortList.cookie_string);
		ShortList.display();
	}
	
};

// Functions for move magic
getRealPosition = function () {
	var pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
	var tmp = arguments[0].offsetParent;
	while(tmp != null) {
		pos += (arguments[1] == 'x') ? tmp.offsetLeft : tmp.offsetTop;
		tmp = tmp.offsetParent;
	}
	
	return pos;
	
}

// Other supporting functions
function removeID(_var,id) {
	var url = unescape(location);
	var finncodes = url.substr(url.indexOf(_var+'=')+(_var.length+1));
	if (finncodes.match('&')) {
		finncodes=finncodes.substr(0,finncodes.indexOf('&'));
	}
	var codearray = finncodes.split(';');
	var codestring = new String();
	for (var x=0; x<codearray.length; x++) {
		if (codearray[x] != id) {
			codestring += codearray[x] + ';';
		}
	}
	window.location = url.replace(finncodes,codestring.substr(0,codestring.length-1));
}

function createCookie(name, value, days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		var expires = "; expires=" + date.toGMTString();
	} else {
		var expires = "";
		document.cookie = name + "=" + value + expires + "; path=/";
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) == ' ') c = c.substring(1, c.length);
		
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}

ShortList.setCachePrefix("http://skixxx.bookableholidays.co.uk/");
ShortList.setCookieShortListName("SunSL");
ShortList.setShortListName("myshortlist");
ShortList.setMaxLength(2);
ShortList.setMaxLengthDesc("Your Short List is limited to a maximum of " + (ShortList.maxLength+1) + " holidays");
ShortList.setMinLength(2);
ShortList.setMinLengthDesc("You must have a least " + (ShortList.minLength) + " offers to compare");
ShortList.setAlreadyInShortListMessage("This holiday is already in 'My Short List'");