/*	Autor: Giovanini, Fernando Miguel
	Fecha Creacion: 09.06.2010
	Descripcion: Funcion creadora y manipuladora del visor carrousel
*/

/* $(document).ready(function()
{
	$(".m-721-carrusel").carrousel({
		showbox: ".m-722-visorimagenes";
	});
	$(".m-722-visorimagenes").imagebox();
}); */
//alert('Entra en Carrusel.js');
$.fn.carrousel = function(showSlide) {
    var args = arguments[0] || {};
    var properties = {
        speed: args.speed || 1000,
        showbox: args.showbox || ".m-722-visorimagenes"
    };

    var object = $(this);
    var page = 0;

    $(window).resize(function() {
        _normalize();
    });

    $(".controls", object).children("div").bind('click', function() {
        var dir = $(this).attr("class");		
        _slide1(dir);
    });

    $(".m-721-carrusel a[rel=imagebox]").live("click", function() {
        var id = $(this).parent().parent().index();        
		$(properties.showbox).showBox(id);

        return false;
    });
	
	$(".m-714-listadocongaleria a[rel=imagebox]").live("click", function() {
        var id = $(this).parent().index();        
		$(properties.showbox).showBox(id);

        return false;
    });

	
	
    $(".controls", object).css("display", "block");

    _init();

    function _init() {

        _normalize();
        object.css({
            overflow: "hidden",
            height: parseInt($(".m-721-item-cont .carro", object).outerHeight() - 5) + "px"
        });
    }

    function _normalize() {
        var cant = parseInt($(".m-721-item-cont > .carro", object).size());
        var controlWith = parseInt($(".controls .prev", object).outerWidth());
        var dimensions = parseInt($(object).innerWidth() - (controlWith * 2));

        $(".m-721-item-cont", object).css({
            width: Math.round((dimensions * cant) + (controlWith * 2)) + "px",
            marginLeft: Math.round((dimensions * page) * -1) + "px"
        });

        $(".m-721-item-cont .carro", object).css({
            width: Math.round(dimensions) + "px",
            display: "inline-block",
            Float: "left"
        });
    }

    function _slide1(dir, slide) {
        var dimensions = parseInt($(".m-721-item-cont .carro", object).outerWidth());
        var current = parseInt($(".m-721-item-cont").css("marginLeft"));
        var cant = parseInt($(".m-721-item-cont > .carro").size());
        var msj = '';

        $(".m-721-item-cont .carro", object).css("width", dimensions + 'px');

        if (cant >= 1) {
            var max_width = (Math.round(dimensions * cant)) * 1;

            if (typeof (slide) != 'undefined') {
                $(".m-721-item-cont").css({
                    marginLeft: Math.round((dimensions * (slide - 1)) * -1) + 'px'
                });
            }
            else {
                if (dir == 'next' && (page + 1) < cant && current > (max_width * -1)) {
                    page++;

                    $(".m-721-item-cont").animate({
                        marginLeft: Math.round((dimensions * page) * -1) + 'px'
                    }, properties.speed);
                }
                else if (dir == 'prev' && (page - 1) <= cant && page > 0) {
                    page--;

                    $(".m-721-item-cont").animate({
                        marginLeft: Math.round((dimensions * page) * -1) + 'px'
                    }, properties.speed);
                }
                else {
                    if (dir == 'next' && page <= cant) {
                        $(".m-721-item-cont").animate({
                            marginLeft: '0px'
                        }, properties.speed);
                        page = 0;
                    }
                    else if (page >= 0) {
                        $(".m-721-item-cont").animate({
                            marginLeft: Math.round((dimensions * (cant - 1)) * -1) + 'px'
                        }, properties.speed);
                        page = (cant - 1);
                    }
                }
            }
        }
    };
};

/*
		$(".m-721-item-cont").css({
			overflow: "hidden",
			marginLeft: "0",
			position:"absolute",
			width:"90%"
		});
		$(".m-722-item-cont").css({
			marginLeft: "0",
			overflow: "hidden"
		});
		$(".m-722-visorimagenes .carousel").css({
			height:"475px",
			overflow:"hidden",
			width:"935px"
		});
		$(".m-722-visorimagenes .prev, .m-722-visorimagenes .next").css({
			zIndex: "15"
		});
		$(".m-721-carrusel").css({
			overflow: "visible"
		})		
		
		
				
		*/
