var drinkCarousel;

// Flashkaruselli
var flashcarousel = {
	amount: 6,
	current: 1,
	old: 0,
	moveUp: function() {
		this.calcNewItem("up");
	},
	moveDown: function() {
		this.calcNewItem("down");
	},
	calcNewItem: function(type) {
		var newitem;

		switch(type) {
		case "up":
			if (this.current==this.amount)
				newitem = 1;
			else
				newitem=this.current+1;
			break;
		case "down":
			if (this.current==1)
				newitem = this.amount;
			else
				newitem=this.current-1;
			break;
        default:
            break;
		}

		this.old = this.current;
		this.current = newitem;
		this.changeItem(newitem);
	},
	changeItem: function(newitem) {
		$("carousel_"+this.old).hide();
		$("carousel_"+newitem).show();
	}
};

function Carousel(container, width, direction, initem, tooltip) {
	var carousel = this;
	this.itemWidth = width;
	this.direction = direction;
	this.current = 0;
	this.amount = $$("#"+container+" .itemlist .item").length;
	this.difference = 0;
	this.speed = 0.3;
	this.itemList = $$("#"+container+" .itemlist")[0];
	this.initem = initem;
	this.useTooltip = tooltip;

	var prev = $$("#"+container+" .prevlink")[0];
	var next = $$("#"+container+" .nextlink")[0];

	this.toggleLinks = function() {
		this.difference = this.current * this.itemWidth;

		if(!this.current)
			prev.hide();
		else
			prev.show();

		if(this.current>=Math.round(this.amount/this.initem)-3)
			next.hide();
		else {
			if(this.amount>3)
				next.show();
			else
				next.hide();
		}
	};

	this.moveRight = function() {
		this.current++;
		this.toggleLinks();
		if(this.direction=="x")
			new Effect.Move(this.itemList, { x: 0-this.difference, mode: 'absolute','duration':this.speed});
		else
			new Effect.Move(this.itemList, { y: 0-this.difference, mode: 'absolute','duration':this.speed});
	};

	this.moveLeft =  function() {
		this.current--;
		this.toggleLinks();
		if(this.direction=="x")
			new Effect.Move(this.itemList, { x: 0-this.difference, mode: 'absolute','duration':this.speed});
		else
			new Effect.Move(this.itemList, { y: 0-this.difference, mode: 'absolute','duration':this.speed});
	};

	this.applyTooltip =  function() {
		var items = $$("#"+container+" .itemlist .item");

		for(i=0; i<items.length; i++) {

			// Väliaikainen!
			items[i].alt=i;
			items[i].observe('mouseover', function(e) {

				$("tooltip").innerHTML = this.title;
				$("tooltip").show();
			});

			items[i].observe('mouseout', function(e) {
				$("tooltip").hide();
			});
		}
	};

	this.init = function() {

		carousel.amount = $$("#"+container+" .itemlist .item").length;
		carousel.current = 0;

        if (this.amount) {

    		if(this.direction=="y") {
    			this.itemList.style.height = ""+Math.round(this.amount/this.initem) * this.itemWidth+"px";
    		}
    		else {


    			if(this.amount>3)
    				this.itemList.style.width = ""+Math.ceil(this.amount/this.initem) * 100+"px";
    			else
    				this.itemList.style.width = ""+this.amount * this.itemWidth+"px";
    		}

    		this.itemList.show();
    		this.toggleLinks();
    		if(this.useTooltip)
    			this.applyTooltip();

    		prev.observe('click', function(e) {
    			carousel.moveLeft();
    			Event.stop(e);
    		});
    		next.observe('click', function(e) {
    			carousel.moveRight();
    			Event.stop(e);
    		});

        }
	};
	this.init();

	this.resetcarousel = function() {
		carousel.amount = $$("#"+container+" .itemlist .item").length;
		carousel.current = 0;

		if(this.direction=="y") {
			this.itemList.style.height = ""+Math.round(this.amount/this.initem) * this.itemWidth+"px";
		}
		else {
			if(this.amount>3)
				//this.itemList.style.width = ""+Math.ceil(this.amount/this.initem) * this.itemWidth+"px";
				this.itemList.style.width = ""+Math.ceil(this.amount/this.initem) * 100+"px";
			else
				this.itemList.style.width = ""+this.amount * this.itemWidth+"px";
		}

		this.toggleLinks();
	};
}

function showDrinks() {
	var valintalista = $$("#checkboxlist .product_selected"),
        i,
        drinks;

	if (valintalista.length>0) {
		drinks = $$("#drinkselector li a");
		for (i=0; i<drinks.length; i++) {
			drinks[i].parentNode.className = "hideditem";
		}

		for (i=0; i<drinks.length; i++) {

			for (var k=0; k<valintalista.length; k++) {
				var inrc = drinks[i].className.split(" ");

				for (var j=0; j<inrc.length; j++) {

					if(valintalista[k].parentNode.id==inrc[j]) {
						drinks[i].parentNode.className = "item";
					}
				}
			}

		}
	}

	else {
		drinks = $$("#drinkselector li");
		for (i=0; i<drinks.length; i++) {
			drinks[i].className = "hideditem";
		}
	}

	drinkCarousel.resetcarousel();
	//delete drinkCarousel;
	//drinkCarousel = new Carousel("drinkselector", 70, "x", 1, true);
}

function bakeCookies(drink, product, select) {
	vodkas = jar.get(drink);

	if(select=="product_selected" && vodkas) {
		if(vodkas) {
			for(i=0; i<vodkas.length; i++) {
				if(vodkas[i]==product) {
					delete vodkas[i];
					jar.put(drink, vodkas);
				}

			}
		}
	}
	else {
		if(!vodkas)
			jar.put(drink, []);
		vodkas = jar.get(drink);
		vodkas.push(product);

		jar.put(drink, vodkas);
	}
}

window.onload = function() {
    var agecheck = $$("#agecheck"),
        page = $$(".content")[0].id;

    if (agecheck.length) {
        $('age_d').focus();

        new Form.Element.Observer(
            'age_d',
            0.2,
            function(el, value){
                if (value > 3 || value.length == 2)
                    $('age_m').focus();
            }
        );

        new Form.Element.Observer(
            'age_m',
            0.2,
            function(el, value){
                if (value > 1 || value.length == 2)
                    $('age_y').focus();
            }
        );

        return;
    }

	if($('flash_prev') && $('flash_next')) {
        /*
		var playerVersion = swfobject.getFlashPlayerVersion();
		if(playerVersion.major) {
			swfobject.embedSWF("bottles.swf", "bottleContainer", "814", "607", "9.0.0", "expressInstall.swf", {});
		}
		else {
			var bottles = $$(".bottlelist li");
			for(var i=0; i<bottles.length; i++) {
				bottles[i].id = "carousel_"+bottles[i].id;
			}


			$('flash_prev').observe('click', function(e) {
				flashcarousel.moveDown();
				Event.stop(e);
			});
	        $('flash_next').observe('click', function(e) {
				flashcarousel.moveUp();
				Event.stop(e);
			});
		}
        */
	}

	if($("toggle_products_liqueur") && $("toggle_products_liqueur")) {
		$("toggle_products_liqueur").observe('click', function(e) {
			this.hide();
			$("thumb_products_vodka").hide();

			$("thumb_products_liqueur").show();
			$("toggle_products_vodka").show();

			Event.stop(e);
		});

		$("toggle_products_vodka").observe('click', function(e) {
			this.hide();
			$("thumb_products_liqueur").hide();

			$("thumb_products_vodka").show();
			$("toggle_products_liqueur").show();

			Event.stop(e);
		});
	}

	if(page=="drinks" || page=="products") {
		drinkCarousel = new Carousel("drinkselector", 70, "x", 1, true);

		if(page=="drinks") {
			var drink = $("checkboxlist").title;
			var links = $$("#checkboxlist li a");

		    jar = new CookieJar({
				expires:3600,   // seconds
				path: '/'
			});

            if (location.hash) {
                var ingredient = location.hash.substr(1);
				for(i=0; i<links.length; i++) {
					if(links[i].parentNode.id==ingredient)
						links[i].className = "product_selected";
					else
						links[i].className = "";
				}
            } else {
			    vodkas = jar.get(drink);

			    if(vodkas) {
				    for(i=0; i<links.length; i++) {
					    for(j=0; j<vodkas.length; j++) {
						    if(links[i].parentNode.id==vodkas[j]) {
							    links[i].className = "";
							    break;
						    }
						    else
							    links[i].className = "product_selected";
					    }
				    }
			    }
            }

			showDrinks();
		}
	}
	else if (page=="barsevents") {
		var barCarousel = new Carousel("bars", 41, "y", 1);
		var eventCarousel = new Carousel("events", 41, "y", 1);
	}

	var plist = $$('.content ul#checkboxlist li a');
	if (plist.length) {
		for (var i=0; i<plist.length; i++) {
			plist[i].observe('click', function(e) {
				if(this.className != "product_selected") {
					this.className = "product_selected";
				}
				else {
					this.className = "";
				}


				$$('#drinkselector ul')[0].style.left = "0px";
				//drinkCarousel.current = 0;

				bakeCookies(this.parentNode.parentNode.title, this.parentNode.id, this.className);
				showDrinks();



				Event.stop(e);
			});
		}
	}
};

function fadeToBlack (duration) {
    $$('#footer h3,#footer a.txtlink,#footernav,#footernav .fitem').each(function (el) {
        el.removeClassName('inverse');
    });
    $(document.body).setStyle({backgroundColor: '#000000'});
    Cufon.replace('#footer h3.cufoni', {
        fontFamily: 'Helvetica Neue LT Pro',
        hover: true
    });
}

function fadeToWhite (duration) {
    $$('#footer h3,#footer a.txtlink,#footernav,#footernav .fitem').each(function (el) {
        el.addClassName('inverse');
    });
    $(document.body).setStyle({backgroundColor: '#ffffff'});
    Cufon.replace('#footer h3.cufoni', {
        fontFamily: 'Helvetica Neue LT Pro',
        hover: true
    });
}
