carousel.trigger = function(){}

carousel._updateNavigation = function(navItems, curItem, maxItem) {
	var navArrowLeft = navItems.eq(0);  // .navigation-l
	var navArrowRight = navItems.eq(1); // .navigation-r
	if(curItem == 0) {
		navArrowLeft.addClass("navigation-inactive");
	} else {
		navArrowLeft.removeClass("navigation-inactive");
	}
	if(curItem == maxItem - 1) {
		navArrowRight.addClass("navigation-inactive");
	} else {
		navArrowRight.removeClass("navigation-inactive");
	}
}

carousel._makeCurrent = function(newelem){
	if(typeof(this.indexCurrentFilm) != "undefined") {
		var oldelem = this.filmItems.eq(this.indexCurrentFilm);
		var oldelemheight = parseInt(oldelem.height());
		oldelem.removeClass("current").height(oldelemheight - this.heightDiff);
	}

	var newelemeight = parseInt(newelem.height());
	newelem.addClass("current").height(newelemeight + this.heightDiff);

	this.indexCurrentFilm = this.filmItems.index(newelem);
}

carousel._makeCurrentYear = function(newCurYear) {
	var tmpthis = this;
	if(typeof(tmpthis.indexCurrentYear) != "undefined") {
		var oldcur = tmpthis.yearItems.eq(tmpthis.indexCurrentYear);
		var oldcurwidth = parseInt(oldcur.css("width"));
		oldcur.removeClass("current").css("width", (oldcurwidth - tmpthis.widthDiff));
	}

	var newelem = tmpthis.yearItems.filter("[year="+newCurYear+"]");
	var newelemwidth = parseInt(newelem.css("width"));
	newelem.addClass("current").css("width", (newelemwidth + tmpthis.widthDiff));

	tmpthis.indexCurrentYear = tmpthis.yearItems.index(newelem);
}

// function to scroll film list
carousel.move = function(newelem){
	var tmpthis = this;

	// find position of showing list
	var x = tmpthis.filmItems.index(newelem);
	// normalize it
	if(x < 2) {
		x = 2;
	} else if(x > tmpthis.filmAmount - 3) {
		x = tmpthis.filmAmount - 3;
	}

	// find film offset films to be moved
	var offset = x - tmpthis.currentOffset;
	tmpthis.currentOffset = x;

	// animate offset film to be moved
	tmpthis.filmListOffset -= (offset)*(tmpthis.itemWidth);
	tmpthis.filmList.stop().animate({"left": tmpthis.filmListOffset}, tmpthis.speed, tmpthis.animateMethod)
		.queue(function(){
			if(typeof(tmpthis.redirectLink) != "undefined") {
				tmpthis.redirectLink.trigger("click");
			}
			$(this).dequeue();
		});

	// animate years rolling
	tmpthis.yearListOffset -= (offset)*(tmpthis.itemWidth);
	tmpthis.yearList.stop().animate({"left": tmpthis.yearListOffset}, tmpthis.speed, tmpthis.animateMethod);
}

carousel.moveYear = function(str) {
	this.filmItems.filter("[inyear=" + str + "]").eq(0).trigger("click");
}

carousel._aliveFilmNavigation = function() {
	var tmpthis = this;

	tmpthis.filmNavigation.click(function(){
		var currentfilm = tmpthis.filmItems.eq(tmpthis.indexCurrentFilm);
		var newcurrentfilm = ($(this).hasClass("navigation-r"))?(currentfilm.next("li")):(currentfilm.prev("li"));

		if(newcurrentfilm.length) {
			newcurrentfilm.trigger("click");
		}
		return false;
	});

	tmpthis.filmItems.each(function(){
		var filmItem = $(this);
		$("a", this).click(function(){
			if(!filmItem.hasClass("current")) {
				tmpthis.redirectLink = $(this);
				filmItem.trigger("click");
			} else {
				window.location = $(this).attr("href");
			}
			return false;
		});

	}).click(function(){
		var newYear = $(this).attr("inyear");

		if(tmpthis.currentYear != newYear) {
			tmpthis._makeCurrentYear(newYear);
			tmpthis._updateNavigation(tmpthis.yearNavigation, tmpthis.indexCurrentYear, tmpthis.yearAmount);
		}

		tmpthis._makeCurrent($(this));
		tmpthis.move($(this));
		tmpthis._updateNavigation(tmpthis.filmNavigation, tmpthis.indexCurrentFilm, tmpthis.filmAmount);
	});

	$("#films ul li.currentToBe").removeClass("currentToBe").trigger("click");
}

carousel._aliveYearNavigation = function() {
	var tmpthis = this;

	tmpthis.yearNavigation.click(function(){
		var current = tmpthis.yearItems.eq(tmpthis.indexCurrentYear);
		var newcurrent = $(this).hasClass("navigation-r")?(current.next("li")):(current.prev("li"));

		if(newcurrent) {
			newcurrent.trigger("click");
		}

		return false;
	});

	tmpthis.yearItems.each(function(){
		// pass click to parent
		$(this).children("a").click(function(){
			$(this).parent("li").trigger("click");
			return false;
		});

		$(this).click(function(){
			var year = $(this).attr("year");
			tmpthis._makeCurrentYear(year);
			tmpthis.moveYear(year);
		});
	});
}

carousel._equalizeH = function(str) {
	var arr = $(str);
	var max = 0;
	arr.each(function(){
		var tmp = parseInt($(this).height());
		if(tmp > max) max = tmp;
	}).height(max);

	return arr;
}

carousel.init = function() {
	this.speed = "slow";
	this.animateMethod = "easeOutBack";
	this.redirectLink; // link to be redirected after animating

	this.filmList = $("#films ul");
	this.filmItems = this.filmList.children("li");
	this.filmAmount = this.filmItems.length;
	this.filmListOffset = parseInt(this.filmList.css("left"));
	this.filmNavigation = $(".film-navigation a.navigation");
	this.indexCurrentFilm;

	this.currentOffset = 2;      // older value by default; have to be between {this.spaceToEdge & (length - this.spaceToEdge)}
	this.itemWidth = 133;        // maybe need to be taken from from css
	this.widthDiff = 171 - this.itemWidth; // diff btw wide & narrow film
	this.heightDiff = 45;                  // diff btw wide & narrow film

	this.yearList = $("#years ul");
	this.yearItems = this.yearList.children('li');
	this.yearAmount = this.yearItems.length;
	this.yearListOffset = parseInt(this.yearList.css("left"));
	this.yearNavigation = $("#years a.navigation");
	this.indexCurrentYear;

	this._equalizeH("body.movies-cinema #films ul li .film-info");
	this._equalizeH("body.movies-cinema #films ul li");
	this._aliveFilmNavigation();
	this._aliveYearNavigation();
}

