﻿ 
    
    var TIMEOUT = 4000;
    var SPEED = 500;
    $(document).ready(function(){
        if($(".produto").length>0){
            $(".controls").css("display","block");
            $(".zoom").not(".inactive").click(function(){
                showLoading();
                var title = $(this).attr("title");
                var target = $(this);
                var src = $(this).find("input.data").val();
                var i = new Image();
                i.src = src;
                $(i).addClass("imgProduto");
                if($(this).is(".loaded"))
                {
                        $(".modalTitle").html(title);
                        $("#zoom").jqmShow();
                        $("#zoomContent").html(i);
                        hideLoading();                    
                }
                else{
                    i.onload = function(){
                        target.addClass("loaded");
                        $(".modalTitle").html(title);
                        $("#zoom").jqmShow();
                        $("#zoomContent").html(i);
                        hideLoading();
                    };
                }
            });
            $("#zoom").jqm();
            
            $("#btnFecharProduto").add("#btnClose").click(function(){
                $("#zoom").jqmHide();
            });
  
            jQuery.each($(".produto"),function(i){
                if($(this).find("img").length<2){
                    $(this).find(".next").css("opacity",.2);
                    $(this).find(".prev").css("opacity",.2);
                }
                if($(this).find("input.data").val()==""){
                    $(this).find(".zoom").css("opacity",.2);
                }
                var delay = 0;
                //var scrollType = "scrollDown";
                if(i%3==0){
                    delay = -(TIMEOUT+SPEED)*2/3;
                }
                if((i-1)%3==0){
                   delay = -(TIMEOUT+SPEED)*1/3;
                }
                //if(i%2==0){
                    //scrollType = "scrollRight";
                //}
                var prevId = "#"+this.id + "Prev";
                var nextId = "#"+this.id + "Next";
                var imgTextId = "#"+this.id + "Text";
                $(this).find(".divImgRotatorProduto").cycle({
                    //fx:     scrollType,
                    prev:   prevId, 
                    next:   nextId,
                    speed: SPEED,
                    fit:    true,
                    pause:  true,
                    timeout: TIMEOUT,
                    delay: delay,
                    cleartype: true,
                    after: ShowText
                });
            });
        }
    });
    
    function ShowText(){
        var alt = $(this).find("img").attr("alt");
        $(this).parent().next(".altSpan").html(alt);
    }
