var SortTypes = {
	NAME     : "name",
	LENGTH   : "length",
	PREMIERE : "premiere",
	RATING   : "rating",
	valueOf  : function(s) {
		for (var t in this)
			if (this[t] == s)
				return s;
		return this.NAME;
	}
}

var FilterTypes = {
	GENRE   : "genre",
	THEATRE : "theatre",
	DATE    : "date",
	TIME    : "time",
	NEW     : "new",
	NONE    : "none",
	NOW		: "now",
	FUTURE	: "future",
	valueOf  : function(s) {
		for (var t in this)
			if (this[t] == s)
				return s;
		return this.NONE;
	}
}

var SortDirections = {
	ASC  : "asc",
	DESC : "desc",
	NONE : "none",
	valueOf  : function(s) {
		return (s == this.DESC) ? this.DESC : this.ASC;
	}
}

var Labels = {
	NAME     : "Välj film",
	LENGTH   : "Längd",
	PREMIERE : "Premiär",
	RATING   : "Åldersgräns",
	NONE     : ""
}

Object.extend(Labels, {
	_LABELS : {
		"name"     : Labels.NAME,
		"length"   : Labels.LENGTH,
		"premiere" : Labels.PREMIERE,
		"rating"   : Labels.RATING
	}
});

Object.extend(Labels, {
	getLabel : function(sortType) {
		return Labels._LABELS[sortType];
	}
});


// ================================================================================================
// FILM LIST
// ------------------------------------------------------------------------------------------------

var FilmLists = new Hash(); // A map of all film lists on the current page (i most cases only one list)

var FilmList = Class.create();

var widthFilmList = "";
var filmSelected = false;
var allShowTimesArr;
var globalFilmObjArr;
var maxShowtimesInRow = 4;

FilmList.prototype = {

	// Public functions ...........................................................................

	initialize: function(id, sortedByName, sortType, sortDirection, filterType, filterValue, filterType2, filterValue2) {
		this.id = id;
		this.sortedByName = sortedByName; // true if sorted using either the label "Välj film" or the option "Bokstavsordning"
		this.sortType = SortTypes.valueOf(sortType);
		this.sortDirection = SortDirections.valueOf(sortDirection);
		this.filterType = FilterTypes.valueOf(filterType);
		this.filterType2 = FilterTypes.valueOf(filterType2);
		this.filterValue = filterValue;
		this.filterValue2 = filterValue2;

		this.films = $A(new Array());
		this.filmsAll = $A(new Array());
		this.filtered = false;
		this.filtered2 = false;
		
		this.numberOfShowMoreButton = 0;
		this.numberOfShowLessButton = 0;
		
		var params2 = "allFilms=allFilms";
		
		new Ajax.Request(
				"/FilmListServlet",
				{
					method: "get",
					parameters: params2,
					onSuccess: (function(res) { this.allFilms(res); }).bind(this),
					onFailure: function(res) { }
				});	
		
		FilmLists[this.id] = this;
	},
	
	allFilms: function(res) {
		this.filmsAll = new Array();
		var filmsNode = new XDOMNode(res.responseXML.firstChild);
		var idx = 0;
		
		//Global array that holds all showtimes
		allShowTimesArr = new Array();
		
		for (var it = filmsNode.iterator("film"); it.hasNext();) {
			
			var filmNode = it.next();			
			var ratingNode = filmNode.getChild("rating");
			var idStr = new String(filmNode.getAttribute("id"));
			var film = new Film(
					filmNode.getAttributeAsInt("id"),
					filmNode.getChild("name").getValue(),
					filmNode.getChild("length").getValue(),
					filmNode.getChild("premiere-date").getValueAsInt(),
					//Listan med föreställningstider ska bara visas när dag och biograf är valda. För att bara visa filmtider när ingen film är vald lägg tiil: && !filmSelected
					(( (this.filterType == "date" && this.filterType2 == "theatre")) ? getShowTimes(filmNode.getChild("showTimes"), idStr, idx) : ""),
					getMoreShowTimesButton(idx),																						
					//filmNode.getChild("img").getValue(),
					ratingNode.getChild("age").getValueAsInt(),
					ratingNode.getChild("description").getValue(),
					filmNode.getChild("shortTimeLeft").getValue());
			film.setParent(this);
			this.filmsAll.push(film);
			idx++;
		}
		
		var filmArrayAll = $A(this.filmsAll);
		
		// Add all films to the select box..
		var filmOptions = $("cycubeQuickSelect").options;
		filmOptions.length = 1;
		filmArrayAll.each(function(film) { filmOptions[filmOptions.length] = new Option(film.name, film.id); });
	},
	
	// Adds a film to the film list.
	addFilm: function(film) {
		film.setParent(this);
		this.films.push(film);
	},

	filter: function(options) {
	
		options = Object.extend(
			{
				forceFilter: false,
				forceSort: false,
				filterType: this.filterType,
				filterValue: this.filterValue,
				filterType2: this.filterType2,
				filterValue2: this.filterValue2,
				sortType: this.sortType,
				sortDirection: this.sortDirection,
				onSuccess: function() { },
				params: {}
			},
			options);
		
		var newFilterType = FilterTypes.valueOf(options.filterType);
		var newFilterType2 = options.filterType2;
		var newFilterValue = options.filterValue;
		var newFilterValue2 = options.filterValue2;		
				
		var doFilter = options.forceFilter || (newFilterType != this.filterType || newFilterValue != this.filterValue || newFilterType2 != this.filterType2 || newFilterValue2 != this.filterValue2);
		
		this.filterType = newFilterType;
		this.filterType2 = newFilterType2;		
		this.filterValue = newFilterValue;
		this.filterValue2 = newFilterValue2;
		this.filtered = this.filterType != FilterTypes.NONE;
		this.filtered2 = this.filterType2 != FilterTypes.NONE;

		var newSortType = SortTypes.valueOf(options.sortType);
		var newSortDirection = SortDirections.valueOf(options.sortDirection);
		var doSort = options.forceSort || (newSortType != this.sortType || newSortDirection != this.sortDirection);

		this.sortType = newSortType;
		this.sortDirection = newSortDirection;
		this.sortedByName = (this.sortType == SortTypes.NAME);
		
		if (newFilterType2 != "none") {
			var dateFilter = ((newFilterType2 == "theatre") ? newFilterValue2 : "")
			this.getDatesForTheater(dateFilter);
		}
		
		var onSortSuccess = function() {};
		if (doSort)
			onSortSuccess = this.doSort.bind(this);		
		
		var onFilterSuccess = function() {};
		if (doFilter) {
			onFilterSuccess = (function(res) { this.updateFromResponse(res); }).bind(this);
		}
						
		var onSuccessCustom = options.onSuccess;
		var onSuccessDefault = this.update.bind(this);
	
		var onSuccess = function(res) {
			onFilterSuccess(res);
			onSortSuccess();
			onSuccessCustom();
			onSuccessDefault();
		}
		
		if (!doFilter) {
			onSuccess();
			return;
		}				
					
		var params = "xml=films"+ (this.filtered ? "&"+ this.filterType +"="+ this.filterValue : "");
		params += (this.filtered2 ? "&"+ this.filterType2 +"="+ this.filterValue2 : "");
		params += ("&type=filmlistCycube");
		
		for (var i in options.params)
			params += "&"+ i +"="+ options.params[i];

		new Ajax.Request(
				"/FilmListServlet",
				{
					method: "get",
					parameters: params,
					onSuccess: onSuccess.bind(this),
					onFailure: function(res) { }
				});	
	},
	
	selectDate: function(filmId, date) {
	
		var url = this.getSelectURL() + filmId;
		var path = url.substring(0, url.indexOf("?"));
		var params = $H(url.parseQuery());
		params.set('date', date);
		if (params.ft == FilterTypes.DATE)
			params.set('fv',date);
		location = path + "?" + params.toQueryString();
	},
	
	//Sätt bredd på filmraderna
	setWidthFilmList: function(width) {
		widthFilmList = width;
	},
	
	//Sätt filmSelected till true när en film är aktivt vald,
	//då ska ej föreställningstider visas på filmraderna
	setFilmSelected: function(value) {
		filmSelected = value;
	},
		
	// "Private" functions ........................................................................

	doSort: function() {
		var sortFunction = function(a, b) {
			if (this.sortType == SortTypes.NAME || this.sortedByName) {
				return a.name.compareTo(b.name);
			} else if (this.sortType == SortTypes.LENGTH) {
				var c = a.length - b.length;
				return (c != 0) ? c : a.name.compareTo(b.name);
			} else if (this.sortType == SortTypes.PREMIERE) {
				var c = a.premiereDate - b.premiereDate;
				return (c != 0) ? c : a.name.compareTo(b.name);
			} else if (this.sortType == SortTypes.RATING) {
				if(a.ratingAge == -1)
					a.ratingAge = 100;
				else if(b.ratingAge == -1)
					b.ratingAge = 100;
				var c = a.ratingAge - b.ratingAge;
				return (c != 0) ? c : a.name.compareTo(b.name);
			} else {
				throw "Invalid sortType: "+ this.sortType;
			}
		};
		this.films = this.films.sort(sortFunction.bind(this));
		if (this.sortDirection == SortDirections.DESC)
			this.films = this.films.reverse();
	},

	sortByName: function() {
		this.sortDirection = (!this.sortedByName || this.sortDirection == SortDirections.DESC) ? SortDirections.ASC : SortDirections.DESC;
		this.sortedByName = true;
		this.doSort();
		this.update();
	},

	sortBySecondColumn: function() {
		this.sortDirection = (this.sortedByName || this.sortDirection == SortDirections.DESC) ? SortDirections.ASC : SortDirections.DESC;
		this.sortedByName = false;
		this.doSort();
		this.update();
	},

	clearFilter: function() {
		var callbackFunction = function() {
			this.doSort();
			this.update();
		};
		this.filter({
			filterType: FilterTypes.NONE,
			filterValue: "",
			onSuccess: callbackFunction.bind(this)});
	},

	updateFromResponse: function(res) {
		this.films = new Array();
		var filmsNode = new XDOMNode(res.responseXML.firstChild);
		var idx = 0;
		
		//Global array that holds all showtimes
		allShowTimesArr = new Array();
		
		for (var it = filmsNode.iterator("film"); it.hasNext();) {
			
			var filmNode = it.next();			
			var ratingNode = filmNode.getChild("rating");
			var idStr = new String(filmNode.getAttribute("id"));
			var film = new Film(
					filmNode.getAttributeAsInt("id"),
					filmNode.getChild("name").getValue(),
					filmNode.getChild("length").getValue(),
					filmNode.getChild("premiere-date").getValueAsInt(),
					//Listan med föreställningstider ska bara visas när dag och biograf är valda. För att bara visa filmtider när ingen film är vald lägg tiil: && !filmSelected
					(( (this.filterType == "date" && this.filterType2 == "theatre")) ? getShowTimes(filmNode.getChild("showTimes"), idStr, idx) : ""),
					getMoreShowTimesButton(idx),																						
					//filmNode.getChild("img").getValue(),
					ratingNode.getChild("age").getValueAsInt(),
					ratingNode.getChild("description").getValue(),
					filmNode.getChild("shortTimeLeft").getValue());
			film.setParent(this);
			this.films.push(film);
			idx++;
		}		
	},
	
	
	//Update the select-list 'sortDay' with new dates from a Ajax.Request
	//
	updateDatesSelect: function(res) {
		
		var selValue = $("sortByDay").options[$("sortByDay").selectedIndex].value;
		$("sortByDay").options.length = 0;
		
		//Loop through the Ajax respons to get the dates
		var datesNode = new XDOMNode(res.responseXML.firstChild);
		$("sortByDay").options[0] = new Option(cycube_filmlistcycube_valjDag, "filter:allFilms:");
		
		var itx = 1;
		for (var it = datesNode.iterator("date"); it.hasNext();) {
			var dateNode = it.next();
			var dateValue = "filter:date:" + dateNode.getAttribute("dateValue");
			var formatDate = dateNode.getChild("formatDate").getValue();
			
			$("sortByDay").options[itx] = new Option(formatDate, dateValue);
			if (selValue == dateValue) {
				$("sortByDay").options[itx].selected = true;
			}
			itx++;			
		}
		
	},

	// Override functions .........................................................................

	write: function(holder) {

		$(holder).update(this.toString());

		this.setMouseOverAndOut($("filmList"));
		this.filter({ forceFilter: true, forceSort: true });
		
	},
	writeMessage: function(holder) {
		var html = '';
		if($A(this.films).length == 0) {
	
		html += '<p class="error">';
		html += '    Du har valt en dag då programmet ej är fastställt på biografen och/eller för den sortering du valt. <br><br>';
		html += '    På onsdagar i varje vecka släpps programmet som gäller från kommande fredag till torsdagen i veckan därpå. Undantaget från detta är vissa premiärer eller specialföreställningar (som tex Barnvagnsbio), till vilka vi släpper biljetterna med längre framförhållning.<br><br>';
		html += '</p>';
		}
		$(holder).update(html);
	},

	//Update the filmlist-rows
	//
	update: function() {
		
		$("filmListHolder").update(this.toString());
		this.setMouseOverAndOut($("filmList"));
		
		var tBody = $("filmListRows");
		var numRows = tBody.rows.length;
		for (var i = 0; i < numRows; i++)
			tBody.deleteRow(0);

		$("sort1st").className = this.sortedByName ? this.sortDirection : SortDirections.NONE;

		var s2nd = $("sort2nd");
		if (this.sortType != SortTypes.NAME) {
			s2nd.update(Labels.getLabel(this.sortType));
			s2nd.className = this.sortedByName ? SortDirections.NONE : this.sortDirection;
			s2nd.show();
		} else {
			s2nd.hide();
		}

		var sortType = this.sortType; // copying to local variable so that the iterator below can access the value
		var rowIndex = 0;
		globalFilmObjArr = new Array();
		$A(this.films).each(function(film) {
			
			var moreShowTimes = false;
			if (film.imgUrl != "") {
				moreShowTimes = true;
			}
			
			//Create the row for a film
			tr = $(document.createElement("tr"));
			if (!moreShowTimes) {
				tr.observe("click", film.select.bindAsEventListener(film));
			}
			tBody.appendChild(tr);

			//Create td for filmname
			td = $(document.createElement("td"));	
			if(!filmSelected) {
			//td.setAttribute("style","width:280px");	
			td.setAttribute("width","280px");	
			}		
			if (moreShowTimes) {
				td.observe("click", film.select.bindAsEventListener(film));
			}
			td.appendChild(document.createTextNode(film.name));
			tr.appendChild(td);
			
			//Create td for showtimes
			td = $(document.createElement("td"));
			if(sortType == "name") {
				if (moreShowTimes) {
					td.observe("click", film.select.bindAsEventListener(film));
				}
				td.setAttribute("id",film.id);									
				td.appendChild(document.createTextNode(film.showTimes));
				tr.appendChild(td);
				
				//Create td for clickable img to see more showtimes
				td = $(document.createElement("td"));
				td.setAttribute("id",film.id+"img");
				if (moreShowTimes) {
					td.innerHTML="<a id='moreShowtimesInfo' href='javascript:listMoreShowtimes("+rowIndex+", "+film.id+");'><img src='" + film.imgUrl + "' onmouseover='Hover.showMarkup(\"moreShowtimesInfo" + filmList.numberOfShowMoreButton + "\", \"Senare föreställningstider.\", event);'/></a>";						
					filmList.numberOfShowMoreButton ++;
					globalFilmObjArr[rowIndex] = film;
				}
				else {
				 var newa=document.createElement("a");
				 newa.type="hidden";
				 td.appendChild(newa);
				}		
			}	
			else {						
				switch (sortType) {
					case SortTypes.LENGTH   : td.update(film.getFormattedLength());       break;
					case SortTypes.PREMIERE : td.update(film.getFormattedPremiereDate()); break;
					case SortTypes.RATING   : td.update(film.ratingDescription);          break;
					default: td.update("&nbsp;");
				}
			}
			tr.appendChild(td);
			rowIndex++;
			
		});
		filmList.writeMessage($("messageNoFilmsDay"));
		// TODO Hide message "Sorterar.."
	},
		
	
	/*
	stax 20080112, Ärende: 41178
	Sets the mouseover/mouseout functionality for each row in 
	the filmlist (film-list.jsp)
	*/
	setMouseOverAndOut: function($filmList) {
	
		var list = $filmList;
		list.observe("mouseover", this.overFilm.bindAsEventListener(this));
		list.observe("mouseout", this.outFilm.bindAsEventListener(this));
		$("sort1st").observe("click", this.sortByName.bindAsEventListener(this));
		$("sort2nd").observe("click", this.sortBySecondColumn.bindAsEventListener(this));
	},

	overFilm: function(event) {
		Try.these(function() { Event.findElement(event, "TR").addClassName("hover"); });
	},
	
	outFilm: function(event) {
		Try.these(function() { Event.findElement(event, "TR").removeClassName("hover"); });
	},
	
	
	//Do a Ajax.Request to BookingServlet to get the dates for a choosen theater
	//
	getDatesForTheater: function (filterValue) {
		
		var onFilterGetDatesSuccess = function() {};
		onFilterGetDatesSuccess = (function(res) { this.updateDatesSelect(res); }).bind(this);
						
		var onGetDatesSuccess = function(res) {
			onFilterGetDatesSuccess(res);
		}		
		
		var params = "cmd=getDates&theaterId="+filterValue;
		
		
		new Ajax.Request(
				"/FilmListServlet",
				{
					method: "get",
					parameters: params,
					onSuccess: onGetDatesSuccess.bind(this),
					onFailure: function(res) { }
				});	
	},
	
	//When a date is selected in the select-list 'sortDay',
	//call the fuction 'filter' to do a Ajax.Request to update the filmlist
	//
	updateFilmlistByDate: function () {

		var selectSortByDay = document.getElementById("sortByDay");
		var filterType = selectSortByDay.options[selectSortByDay.selectedIndex].value;
		var filter = filterType.split(":");
		
		var selectSortBy = document.getElementById("sortBy");
		var filterType2 = selectSortBy.options[selectSortBy.selectedIndex].value;
		var filter2 = filterType2.split(":");
		
		var options = { filterType: filter[1], filterType2: filter2[1], filterValue: filter[2], filterValue2: filter2[2], sortType: SortTypes.NAME, sortDirection: SortDirections.ASC, isDateSelected: true };
		
		var filmList = FilmLists["filmList"];
		filmList.filter(options);
		window.focus();		
	},
		

	getSelectURL: function() {
		var url = "/bio/Booking?cmd=listFilms";
		url += "&sn="+ this.sortedByName;
		url += "&st="+ this.sortType +"&sd="+ this.sortDirection;
		url += "&ft="+ this.filterType +"&fv="+ this.filterValue;
		url += "&ft2="+ this.filterType2 +"&fv2="+ this.filterValue2;
		if (this.filterType == FilterTypes.DATE)
			url += "&date="+ this.filterValue;
		url += "&film=";
		return url;
	}
}


FilmList.prototype.toString = function() {

	var html = '';
	
	html += '<table cellspacing="0" id="filmList" style="'+ widthFilmList +'">';
	html += '    <thead>';
	html += '        <tr>';
	html += '            <td><span id="sort1st" class="'+ SortDirections.ASC +'">'+ Labels.NAME +'</span></td>';
	html += '            <td><span id="sort2nd" class="'+ SortDirections.NONE +'">'+ Labels.NONE +'</span></td>';
	html += '        </tr>';
	html += '    </thead>';
	html += '    <tbody id="filmListRows">';
	html += '    </tbody>';
	html += '</table>';
	
	return html;
}


//If a film has more then 6 showtimes, get the remaining showtimes
//
//idx - number of the filmrow
//id - for the film
//
function listMoreShowtimes(idx, id) {
	
	//Get all the showtimes for the selected film
	var s = allShowTimesArr[idx];
	
	var moreShowTimes = "";
	for (ii=maxShowtimesInRow; ii<s.showTimes.length; ii++) {
		if (ii < s.showTimes.length) {
			moreShowTimes += s.showTimes[ii] + "  ";
		} else {
			moreShowTimes += s.showTimes[ii];
		}
	}
	
	//Create a new td-element for the new showtimes
	var newTd = $(document.createElement("td"));
	var newFilmObj = globalFilmObjArr[idx];
	newTd.observe("click", newFilmObj.select.bindAsEventListener(newFilmObj));
	newTd.setAttribute("id", id);
	newTd.appendChild(document.createTextNode(moreShowTimes));

	//Get the old td-element for the old showtimes
	var oldTd = document.getElementById(id);
	var parentElem = oldTd.parentNode;
	
	//Replace the old showtimes with the new ones
	parentElem.replaceChild(newTd, oldTd);
	
	//Erase the img/"more-button"
	var newTdImg = $(document.createElement("td"));

	//Add the img/"less-button"
	newTdImg.setAttribute("id",id+"img");

	newTdImg.innerHTML="<a id='lessShowtimesInfo' href='javascript:listLessShowtimes("+idx+", "+id+");'><img src='/content/sv/gfx/pil_left.jpg' onmouseover='Hover.showMarkup(\"lessShowtimesInfo" + filmList.numberOfShowLessButton + idx + id +"\", \"Tidigare föreställningstider.\", event);'/></a>";
	filmList.numberOfShowLessButton ++;
	
	var oldTdImg = document.getElementById(id+"img");
	parentElem.replaceChild(newTdImg, oldTdImg);

}

function listLessShowtimes(idx, id) {
	//Get all the showtimes for the selected film
	var s = allShowTimesArr[idx];
	
	var lessShowTimes = "";
	for (ii=0; ii<maxShowtimesInRow; ii++) {
		if (ii < s.showTimes.length) {
			lessShowTimes += s.showTimes[ii] + "  ";
		} else {
			lessShowTimes += s.showTimes[ii];
		}
	}
	
	//Create a new td-element for the new showtimes
	var newTd = $(document.createElement("td"));
	var newFilmObj = globalFilmObjArr[idx];
	newTd.observe("click", newFilmObj.select.bindAsEventListener(newFilmObj));
	newTd.setAttribute("id", id);
	newTd.appendChild(document.createTextNode(lessShowTimes));

	//Get the old td-element for the old showtimes
	var oldTd = document.getElementById(id);
	var parentElem = oldTd.parentNode;
	
	//Replace the old showtimes with the new ones
	parentElem.replaceChild(newTd, oldTd);
	
	//Erase the img/"less-button"
	var newTdImg = $(document.createElement("td"));
	
	newTdImg.setAttribute("id",id+"img");
	newTdImg.innerHTML="<a id='moreShowtimesInfo' href='javascript:listMoreShowtimes("+idx+", "+id+");'><img src='/content/gfx/sv/pil.jpg' onmouseover='Hover.showMarkup(\"moreShowtimesInfo" + filmList.numberOfShowMoreButton + idx + id +"\", \"Senare föreställningstider.\", event);'/></a>";							
	filmList.numberOfShowMoreButton ++;
	
	var oldTdImg = document.getElementById(id+"img");
	parentElem.replaceChild(newTdImg, oldTdImg);

}

//Print max 4 showtimes in a row, if a film has more showtimes
//display a clickable "more-button"
//
function getMoreShowTimesButton(idx) {
	
	var s = allShowTimesArr[idx];
	
	if (s != null) {
		if (s.showTimes.length > maxShowtimesInRow) {
			return "/content/gfx/sv/pil.jpg";
		} else {
			return "";
		}
	}
	return "";
}


//Get the showtimes for a film. Display max 6 showtimes in a row.
//All showtimes are saved in the global array 'allShowTimesArr'.
//
function getShowTimes(times, filmId, idx) {
	var itx = times.iterator("showTime"+filmId);
	var showTimes = "";
	var showTimesArr = new Array();
	var i = 0;

	for (itx; itx.hasNext();) {
		var showTimeTmp = itx.next();
		
		//Show max 6 showtimes in the row
		if (i < maxShowtimesInRow-1) {
			showTimes += showTimeTmp.getValue() + "  ";
		}
		if (i == maxShowtimesInRow-1) {
			showTimes += showTimeTmp.getValue();
		}
		showTimesArr[i] = showTimeTmp.getValue();
		i++;
	}
	allShowTimesArr[idx] = new ShowTime(showTimesArr);
	return showTimes;
}

function ShowTime(timeArr) {
	this.showTimes = timeArr;
}

// ================================================================================================
// FILM
// ------------------------------------------------------------------------------------------------

var Film = Class.create();

Film.prototype = {

	initialize: function(id, name, length, premiereDate, showTimes, imgUrl, ratingAge, ratingDescription, shortTimeLeft) {
		this.id = id;
		this.name = name;
		this.length = length;
		this.premiereDate = premiereDate;
		this.showTimes = showTimes;
		this.imgUrl = imgUrl;
		this.ratingAge = ratingAge;
		this.ratingDescription = ratingDescription;
		this._months = new Array("jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"); 
		this._parent = null;
		this.shortTimeLeft = shortTimeLeft;
	},
	
	setParent: function(parent) {
		this._parent = parent;
	},
	
	select: function(event) {
		if (this._parent)
// lägger till URL:n filmnamnet med "-" ist för " "
		var strReplaceAll = this.name;
		var intIndexOfMatch6 = strReplaceAll.indexOf( "-" );
		while (intIndexOfMatch6 != -1){
			strReplaceAll = strReplaceAll.replace( "-", " " );
			intIndexOfMatch6 = strReplaceAll.indexOf( "-" );
		}		
		var intIndexOfMatch4 = strReplaceAll.indexOf( "/" );
		while (intIndexOfMatch4 != -1){
			strReplaceAll = strReplaceAll.replace( "/", "" );
			intIndexOfMatch4 = strReplaceAll.indexOf( "/" );
		}	
		var intIndexOfMatch1 = strReplaceAll.indexOf( "(" );
		while (intIndexOfMatch1 != -1){
			strReplaceAll = strReplaceAll.replace( "(", "" );
			intIndexOfMatch1 = strReplaceAll.indexOf( "(" );
		}	
		var intIndexOfMatch2 = strReplaceAll.indexOf( ")" );
		while (intIndexOfMatch2 != -1){
			strReplaceAll = strReplaceAll.replace( ")", "" );
			intIndexOfMatch2 = strReplaceAll.indexOf( ")" );
		}	
		var intIndexOfMatch3 = strReplaceAll.indexOf( "." );
		while (intIndexOfMatch3 != -1){
			strReplaceAll = strReplaceAll.replace( ".", "" );
			intIndexOfMatch3 = strReplaceAll.indexOf( "." );
		}	
		var intIndexOfMatch5 = strReplaceAll.indexOf( "," );
		while (intIndexOfMatch5 != -1){
			strReplaceAll = strReplaceAll.replace( ",", "" );
			intIndexOfMatch5 = strReplaceAll.indexOf( "," );
		}	
		var intIndexOfMatch7 = strReplaceAll.indexOf( "&" );
		while (intIndexOfMatch7 != -1){
			strReplaceAll = strReplaceAll.replace( "&", " " );
			intIndexOfMatch7 = strReplaceAll.indexOf( "&" );
		}	
		var intIndexOfMatch8 = strReplaceAll.indexOf( "å" );
		while (intIndexOfMatch8 != -1){
			strReplaceAll = strReplaceAll.replace( "å", "aa" );
			intIndexOfMatch8 = strReplaceAll.indexOf( "å" );
		}
		var intIndexOfMatch9 = strReplaceAll.indexOf( "ä" );
		while (intIndexOfMatch9 != -1){
			strReplaceAll = strReplaceAll.replace( "ä", "ae" );
			intIndexOfMatch9 = strReplaceAll.indexOf( "ä" );
		}
		var intIndexOfMatch10 = strReplaceAll.indexOf( "ö" );
		while (intIndexOfMatch10 != -1){
			strReplaceAll = strReplaceAll.replace( "ö", "oe" );
			intIndexOfMatch10 = strReplaceAll.indexOf( "ö" );
		}
		var intIndexOfMatch11 = strReplaceAll.indexOf( "Å" );
		while (intIndexOfMatch11 != -1){
			strReplaceAll = strReplaceAll.replace( "Å", "Aa" );
			intIndexOfMatch11 = strReplaceAll.indexOf( "Å" );
		}
		var intIndexOfMatch12 = strReplaceAll.indexOf( "Ä" );
		while (intIndexOfMatch12 != -1){
			strReplaceAll = strReplaceAll.replace( "Ä", "Ae" );
			intIndexOfMatch12 = strReplaceAll.indexOf( "Ä" );
		}
		var intIndexOfMatch13 = strReplaceAll.indexOf( "Ö" );
		while (intIndexOfMatch13 != -1){
			strReplaceAll = strReplaceAll.replace( "Ö", "Oe" );
			intIndexOfMatch13 = strReplaceAll.indexOf( "Ö" );
		}
		var intIndexOfMatch14 = strReplaceAll.indexOf("!" );
		while (intIndexOfMatch14 != -1){
			strReplaceAll = strReplaceAll.replace( "!", "" );
			intIndexOfMatch14 = strReplaceAll.indexOf( "!" );
		}
		var intIndexOfMatch15 = strReplaceAll.indexOf("&" );
		while (intIndexOfMatch15 != -1){
			strReplaceAll = strReplaceAll.replace( "&", " " );
			intIndexOfMatch15 = strReplaceAll.indexOf( "&" );
		}

		var intIndexOfMatch16 = strReplaceAll.indexOf(":" );
		while (intIndexOfMatch16 != -1){
			strReplaceAll = strReplaceAll.replace( ":", "" );
			intIndexOfMatch16 = strReplaceAll.indexOf( ":" );
		}

		var intIndexOfMatch17 = strReplaceAll.indexOf("%" );
		while (intIndexOfMatch17 != -1){
			strReplaceAll = strReplaceAll.replace( "%", "" );
			intIndexOfMatch17 = strReplaceAll.indexOf( "%" );
		}

		var intIndexOfMatch = strReplaceAll.indexOf( " " );
		while (intIndexOfMatch != -1){
			strReplaceAll = strReplaceAll.replace( " ", "-" );
			intIndexOfMatch = strReplaceAll.indexOf( " " );
		}	
		location = this._parent.getSelectURL() + this.id + "/" + strReplaceAll;
	//	location = this._parent.getSelectURL() + this.id;
	},
	
	getFormattedLength: function() {
		return Math.floor(this.length / 60) +"h "+ (this.length % 60) +"m";
	},
	
	getFormattedPremiereDate: function() {
		var s = ""+ this.premiereDate;
		var month = parseInt(s.substring(4, 6), 10) - 1;
		var date = parseInt(s.substring(6, 8), 10);
		var year = s.substring(2,4);
		return date +" "+ this._months[month] + " - " + year;
	}
}