Event.onReady(function() {
	//$("cycubeSortForm").reset();
	$("cycubeSortBy").observe("change", sortFilmList.bind($("cycubeSortBy")));
	$("cycubeQuickSelect").observe("change", selectFilm.bind($("cycubeQuickSelect")));
	
	var filmList = new CycubeFilmList("cycubeFilmList", false, sortType, sortDirection, filterType, filterValue);
	filmList.write("cycubeFilmList");
});

function sortByCommingMovies() {
var filmList = FilmLists["cycubeFilmList"];
	var sortType = "filter:future:true";
	Cycube.Cookie.create("sortTypeWhole", sortType);
	var options = {
		filterType: FilterTypes.NONE,
		filterValue: "",
		sortType: SortTypes.NAME,
		sortDirection: SortDirections.ASC,
		onSuccess: function() { filmList.page = 0; }
	};

	if (sortType.startsWith("filter")) {
		var filter = sortType.split(":");
		options = Object.extend(options, { filterType: filter[1], filterValue: filter[2] });
		if (filter[1] == "future")
			options = Object.extend(options, { sortType: SortTypes.PREMIERE });
	} else {
		options = Object.extend(options, { filterType: FilterTypes.NONE, filterValue: "", sortType: sortType });
	}
	//create cookies
	Cycube.Cookie.create("sortDirection", options.sortDirection);
	Cycube.Cookie.create("filterType", options.filterType);
	Cycube.Cookie.create("filterValue", options.filterValue);
	Cycube.Cookie.create("sortType", options.sortType);
	filmList.filter(options);
	alterselected();
}

function sortByNewMovies() {
var filmList = FilmLists["cycubeFilmList"];
	var sortType = "filter:new:true";
	Cycube.Cookie.create("sortTypeWhole", sortType);
	var options = {
		filterType: FilterTypes.NONE,
		filterValue: "",
		sortType: SortTypes.NAME,
		sortDirection: SortDirections.ASC,
		onSuccess: function() { filmList.page = 0; }
	};

	if (sortType.startsWith("filter")) {
		var filter = sortType.split(":");
		options = Object.extend(options, { filterType: filter[1], filterValue: filter[2] });
	} else {
		options = Object.extend(options, { filterType: FilterTypes.NONE, filterValue: "", sortType: sortType });
	}
	//create cookies
	Cycube.Cookie.create("sortDirection", options.sortDirection);
	Cycube.Cookie.create("filterType", options.filterType);
	Cycube.Cookie.create("filterValue", options.filterValue);
	Cycube.Cookie.create("sortType", options.sortType);
	filmList.filter(options);
	alterselected();
}

function sortFilmList() {
	var filmList = FilmLists["cycubeFilmList"];
	var sortType = this.value;
	//create cookie to be used for setting the menu item (in cycubeSortForm in file cycube-film-list.jsp) to the one 
	//that was selected the last time.
	Cycube.Cookie.create("sortTypeWhole", sortType);
	var options = {
		filterType: FilterTypes.NONE,
		filterValue: "",
		sortType: SortTypes.NAME,
		sortDirection: SortDirections.ASC,
		onSuccess: function() { filmList.page = 0; }
	};

	if (sortType.startsWith("filter")) {
		var filter = sortType.split(":");
		options = Object.extend(options, { filterType: filter[1], filterValue: filter[2] });
		if (filter[1] == "future")
			options = Object.extend(options, { sortType: SortTypes.PREMIERE });
	} else {
		options = Object.extend(options, { filterType: FilterTypes.NONE, filterValue: "", sortType: sortType });
	}
	//create cookies
	Cycube.Cookie.create("sortDirection", options.sortDirection);
	Cycube.Cookie.create("filterType", options.filterType);
	Cycube.Cookie.create("filterValue", options.filterValue);
	Cycube.Cookie.create("sortType", options.sortType);
	filmList.filter(options);
	
	this.blur();
}

function replaceChar (inStr, patternChar, repChar){
	var intIndexOfMatch = inStr.indexOf( patternChar );
	while (intIndexOfMatch != -1){
		inStr = inStr.replace(patternChar, repChar);
		intIndexOfMatch = inStr.indexOf(patternChar);
	}
	return inStr;
}
function selectFilm() {
// lägger till URL:n filmnamnet med "-" ist för " "
	var strReplaceAll = this.options[this.selectedIndex].text;

	strReplaceAll = replaceChar (strReplaceAll, "-", " ");
	strReplaceAll = replaceChar (strReplaceAll, "(", "");
	strReplaceAll = replaceChar (strReplaceAll, ")", "");
	strReplaceAll = replaceChar (strReplaceAll, ".", "");
	strReplaceAll = replaceChar (strReplaceAll, "!", "");
	strReplaceAll = replaceChar (strReplaceAll, "\"", "");
	strReplaceAll = replaceChar (strReplaceAll, "\'", "");
	strReplaceAll = replaceChar (strReplaceAll, ",", " ");
	strReplaceAll = replaceChar (strReplaceAll, ";", "");
	strReplaceAll = replaceChar (strReplaceAll, ":", "");
	strReplaceAll = replaceChar (strReplaceAll, "/", "");
	strReplaceAll = replaceChar (strReplaceAll, "?", "");
	strReplaceAll = replaceChar (strReplaceAll, "_", "");
	strReplaceAll = replaceChar (strReplaceAll, "&", "");
	strReplaceAll = replaceChar (strReplaceAll, "å", "aa");
	strReplaceAll = replaceChar (strReplaceAll, "Å", "Aa");
	strReplaceAll = replaceChar (strReplaceAll, "ä", "ae");
	strReplaceAll = replaceChar (strReplaceAll, "Ä", "Ae");
	strReplaceAll = replaceChar (strReplaceAll, "ö", "oe");
	strReplaceAll = replaceChar (strReplaceAll, "Ö", "Oe");
	strReplaceAll = replaceChar (strReplaceAll, "%", "");
	strReplaceAll = replaceChar (strReplaceAll, "ü", "u");
	strReplaceAll = replaceChar (strReplaceAll, "Ü", "U");
	strReplaceAll = replaceChar (strReplaceAll, " ", "-");

	location = "/filmer/film/" + this.value + "/" + strReplaceAll;
//	location = "/filmer/film?filmid="+ this.value; 
	strReplaceAll = "";
}
