var animationTime = 1000;


//items
var brandy = [11, 10, 12, 13, 1, 2];
var whisky = [8, 16, 17];
var vodka = [7, 15, 5, 6];
var gin   = [9];
var liqueur = [3, 14];

function findCategory(currentId)
{
    if(brandy.indexOf(currentId) != -1) {
        var currentCat = brandy;
        var name = "brandy";
    } else if(whisky.indexOf(currentId) != -1) {
        var currentCat = whisky;
        var name = "whisky";
    } else if(vodka.indexOf(currentId) != -1) {
        var currentCat = vodka;
        var name = "vodka";
    } else if(gin.indexOf(currentId) != -1) {
        var currentCat = gin;
        var name = "gin";
    } else {
        var currentCat = liqueur;
        var name = "liqueur";
    }
    
    return currentCat;
}

$j(document).ready(function(){
	$j("#productNext").click(function() {
		$j("#categoriesAjaxContainer > *").animate( { left:"-150px", opacity: 0}, animationTime, '', loadNextProduct);
	});
	
	$j("#productPrev").click(function() {
		$j("#categoriesAjaxContainer > *").animate( { left:"150px", opacity: 0}, animationTime, '', loadPrevProduct);
	});
	
	//categoryInfo DIV
	targetHeight = $j("#categoriesInfoAjaxContent").innerHeight();
    $j("#categoriesInfoAjaxContentContainer").animate( { height: targetHeight}, 1000);
    //
    
    $j(document).keydown(function (e) {
          if (e.keyCode == 37) {
              $j("#productPrev").trigger('click');
          }
          
          if (e.keyCode == 39) {
              $j("#productNext").trigger('click');
          }
    });
});

function loadNextProduct()
{
	$j("#categoriesAjaxContainer > *").remove();
	
	var currentId = parseInt($F("currentLoadedProductId"));
	categoryArray = findCategory(currentId);
	if(categoryArray.indexOf(currentId) == (categoryArray.size() - 1)) {
	    nextId = categoryArray[0];
	} else {
	    currentIndex = categoryArray.indexOf(currentId);
	    nextId = categoryArray[currentIndex + 1];
	}
	
	changeCurrentLoadedProductId(nextId);
	
	new Ajax.Request
	(
		webroot + language + "/portfolio/products/id/" + nextId + '/ajax/true',
		{
			method: 'get',
			onSuccess: showNextProduct,
			onCreate: function() { $j("#categoriesAjaxLoading").show(); }
		}
	);
}

function showNextProduct(transport) {
	$j("#categoriesAjaxLoading").fadeOut("fast");
	
	$("categoriesAjaxContainer").update(transport.responseText);
	
	$j("#productDetailTable").css("opacity", 0);
	$j("#productDetailTable").css("left", "150px");
	$j("#productDetailTable").show();
	
	$j("#productDetailTable").animate( { left:"0px", opacity: 1}, animationTime);
}

function loadPrevProduct()
{
	$j("#categoriesAjaxContainer > *").remove();
	
	var currentId = parseInt($F("currentLoadedProductId"));
	var categoryArray = findCategory(currentId);
	lastIndex = categoryArray.size() - 1;
	if(categoryArray.indexOf(currentId) == 0) {
	    prevId = categoryArray[lastIndex];
	} else {
	    currentIndex = categoryArray.indexOf(currentId);
	    prevId = categoryArray[currentIndex - 1];
	}
	
	changeCurrentLoadedProductId(prevId);
	
	new Ajax.Request
	(
		webroot + language + "/portfolio/products/id/" + prevId + '/ajax/true',
		{
			method: 'get',
			onSuccess: showPrevProduct,
			onCreate: function() { $j("#categoriesAjaxLoading").show(); }
		}
	);
}

function showPrevProduct(transport) {
	$j("#categoriesAjaxLoading").fadeOut("fast");
	
	$("categoriesAjaxContainer").update(transport.responseText);
	
	$j("#productDetailTable").css("opacity", 0);
	$j("#productDetailTable").css("left", "-150px");
	$j("#productDetailTable").show();
	
	$j("#productDetailTable").animate( { left:"0px", opacity: 1}, animationTime);
}

function changeProductFromMenu(id)
{
    $j("#categoriesAjaxContainer > *").animate( { top:"-150px", opacity: 0}, animationTime, '', function() { loadProductById(id) } );
}

function loadProductById(id)
{
    $j("#categoriesAjaxContainer > *").remove();
	
	changeCurrentLoadedProductId(id);
	
	new Ajax.Request
	(
		webroot + language + "/portfolio/products/id/" + id + '/ajax/true',
		{
			method: 'get',
			onSuccess: showLoadedProductById,
			onCreate: function() { $j("#categoriesAjaxLoading").show(); }
		}
	);
}

function showLoadedProductById(transport)
{
    $j("#categoriesAjaxLoading").fadeOut("fast");
	
	$("categoriesAjaxContainer").update(transport.responseText);
	
	$j("#productDetailTable").css("opacity", 0);
	$j("#productDetailTable").css("top", "150px");
	$j("#productDetailTable").show();
	
	$j("#productDetailTable").animate( { top:"0px", opacity: 1}, animationTime);
}

function changeCurrentLoadedProductId(id)
{
    var currentId = parseInt($F("currentLoadedProductId"));
    $("currentLoadedProductId").value = id;
    
    if(brandy.indexOf(currentId) != -1) {
        var currentCat = brandy;
        var name = "brandy";
    } else if(whisky.indexOf(currentId) != -1) {
        var currentCat = whisky;
        var name = "whisky";
    } else if(vodka.indexOf(currentId) != -1) {
        var currentCat = vodka;
        var name = "vodka";
    } else if(gin.indexOf(currentId) != -1) {
        var currentCat = gin;
        var name = "gin";
    } else {
        var currentCat = liqueur;
        var name = "liqueur";
    }
    
    
    //hide and show next back
    var categoryArray = findCategory(id);
    lastIndex = categoryArray.size() - 1;
    if(categoryArray.indexOf(id) == 0) {
//	    $j("#productPrev").addClass("disabled");
	    $j("#productPrev").fadeOut("normal");
	} else {
//	    $j("#productPrev").removeClass("disabled");
	    $j("#productPrev").fadeIn("normal");
	}
	
	if(categoryArray.indexOf(id) != lastIndex) {
//	   $j("#productNext").removeClass("disabled");
       $j("#productNext").fadeIn("normal");
	} else {
//	   $j("#productNext").addClass("disabled");
       $j("#productNext").fadeOut("normal"); 
	}
    
    if(currentCat.indexOf(id) == -1) {
        new Ajax.Request
        (
            webroot + language + "/portfolio/categories-info/id/" + id + '/ajax/true',
            {
                method: 'get',
                onSuccess: updateCategoryInfo,
                onCreate: function() { $j("#categoriesInfoAjaxLoading").show(); }
            }
        );
    }
}

function updateCategoryInfo(transport)
{
    $j("#categoriesInfoAjaxLoading").fadeOut("fast");
    
    $("categoriesInfoAjaxContent").update(transport.responseText);
    
    $j(document).ready(function() {
        targetHeight = $j("#categoriesInfoAjaxContent").innerHeight();
        $j("#categoriesInfoAjaxContentContainer").animate( { height: targetHeight}, 1000);
    });
}