﻿    
    var begunLoading = false;
    var isEmpty = false;
    var isNowEmpty = false;
    
    function CarouselInit(carousel, carouselInitState)
    {
        carouselSize = carousel.options.size;

        //adjust styles for Main Image Navigation links
        $(".jcarousel-nav a").css({"font-family":"'Trebuchet MS', Arial, Helvetica, sans-serif","font-size":"11pt","color":"#4B79A0","font-weight":"bold","line-height":"15px","height":"20px"});
        
        
        $(".jcarousel-nav a").mouseover(function() {
            $(this).css({"color":"#CCC","text-decoration":"underline"});
        });
        
        $(".jcarousel-nav a").mouseout(function() {
            $(this).css({"color":"#4B79A0","text-decoration":"underline"});
        });
    }
    
    function NowCarouselInit(carousel, carouselInitState)
    {
        nowCarouselSize = carousel.options.size;
    }

    function ItemLoadCB(carousel, carouselState)
    {
    }
    
    
    function BeforeItemLastIn(carousel, elem, idx, carouselState)
    {
       //alert("idx: " + idx + ", size: " + carouselSize);
       
        //alert("prevBeginIndex: " + prevBeginIndex + "idx-1: " + parseInt(idx-1) + "carouselSize: " + carouselSize + "calc: " + parseInt(carouselSize * -1));
        //alert("idx: " + parseInt(idx) + ", isEmpty: " + isEmpty);
        //if ((((parseInt(idx))<=5) && carouselState=='prev') || ((idx)>=carouselSize && carouselState=='next'))
        if ((idx)>=carouselSize && carouselState=='next')
        {
            //alert("loading...isEmpty: " + isEmpty);
            if (!isEmpty)
            {
                var itemArray = new Array($("li.jcarousel-item img[slidertype=main]").size());
                var elemCount = 0;
                
                //gather list of available APIDs
                $("li.jcarousel-item img[slidertype=main]").each(function() {
                    itemArray[elemCount] = $(this).attr("typeid");
                    elemCount += 1;
                });
                elemCount = 0;
                
                var strElements = "";
                var url = "handlers/ts.ashx?action=UpdateImageSlider&TypeIDs[]=" + itemArray + "&type=" + ImageType;
                var doContinue = false;
                   
                $.ajax({
                    type: "GET",
                    url: url,
                    cache: false,
                    success: function(msg){
                        //alert("request" + url + ", response: " + msg);
                        //alert(msg);
                        if (msg != "" && msg != null)
                        {

                            eval('var obj='+msg)
                            //var obj = JSON.parse(msg);

                            for (var item in obj.Group)
                            { 
                                if (obj.Group[item].ImgSrc != null  )
                                {
                                    path = ImagePath.substring(2,ImagePath.length);
                                    //alert(ImageType);
                                    carousel.options.size = carousel.options.size+1;
                                    if (ImageType == "Artists")
                                    {
                                        carousel.add(carousel.options.size,"<a href=\"artists/" + obj.Group[item].TypeIDForURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + "\" width=\"107\" height=\"150\" class=\"artpiece-img-over\" slidertype=\"main\" typeid=\""+ obj.Group[item].TypeID + "\" /></a><a href=\"artists/" + obj.Group[item].TypeIDForURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a><div id=\"artist-display\"><a href=\"artists/" + obj.Group[item].TypeIDForURL  + ".aspx\">" + obj.Group[item].TypeDetail);                                    
                                    }
                                    else if (ImageType == "Organizations")
                                    {
                                        carousel.add(carousel.options.size,"<a href=\"organizations/" + obj.Group[item].TypeIDForURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + "\" width=\"107\" height=\"150\" class=\"artpiece-img-over\" slidertype=\"main\" typeid=\""+ obj.Group[item].TypeID + "\" /></a><a href=\"organizations/" + obj.Group[item].TypeIDForURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a><div id=\"artist-display\"><a href=\"organizations/" + obj.Group[item].TypeIDForURL  + ".aspx\">" + obj.Group[item].TypeDetail);                                    
                                    }
                                    else if (ImageType == "Matter")
                                    {
                                        carousel.add(carousel.options.size,"<a href=\"matter/" + obj.Group[item].APIncludeURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + " $" + obj.Group[item].TypeDetail + "\" width=\"250\" height=\"150\" class=\"artpiece-img-over\" slidertype=\"main\" typeid=\""+ obj.Group[item].TypeID + "\" /><a href=\"matter/" + obj.Group[item].APIncludeURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a><div id=\"artist-display\"><a href=\"" + obj.Group[item].TypeForURL + obj.Group[item].TypeIDForURL + ".aspx\">$" + obj.Group[item].TypeDetail + "</a></div>");
                                    }
                                    else
                                    {
                                        carousel.add(carousel.options.size,"<a href=\"pieces/" + obj.Group[item].APIncludeURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + obj.Group[item].TypeDetail + "\" width=\"250\" height=\"150\" class=\"artpiece-img-over\" slidertype=\"main\" typeid=\""+ obj.Group[item].TypeID + "\" /><a href=\"pieces/" + obj.Group[item].APIncludeURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a><div id=\"artist-display\"><a href=\"" + obj.Group[item].TypeForURL + obj.Group[item].TypeIDForURL + ".aspx\">" + obj.Group[item].TypeDetail + "</a></div>");
                                    }
                                }
                                else
                                {
                                    isEmpty = true;
                                }
                            }
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        //alert(XMLHttpRequest.status);
                    }
                });
            }
        }
    }
    
    function BeforeNowItemLastIn(carousel, elem, idx, carouselState)
    {
        //alert("idx: " + idx + "nowCarouselSize: " + nowCarouselSize);
        if ((((idx)<=1) && carouselState=='prev') || ((idx)==nowCarouselSize && carouselState=='next'))
        {
            //alert("loading...isNowEmpty: " + isNowEmpty);
            if (!isNowEmpty)
            {
                var itemArray = new Array($("li.jcarousel-item img[slidertype=now]").size());
                var elemCount = 0;
                
                //gather list of available APIDs
                $("li.jcarousel-item img[slidertype=now]").each(function() {
                    itemArray[elemCount] = $(this).attr("typeid");
                    elemCount += 1;
                });
                elemCount = 0;
                
                var strElements = "";
                var url = "handlers/ts.ashx?action=UpdateImageSlider&TypeIDs[]=" + itemArray + "&type=" + ImageType + "&now=true";
                var doContinue = false;
                   
                $.ajax({
                    type: "GET",
                    url: url,
                    cache: false,
                    success: function(msg){
                        //alert(msg);
                        
                        if (msg != "" && msg != null)
                        {
                            //alert("test");
                            eval('var obj='+msg)

                            var imgCount = 0;
                            
                            for (var item in obj.Group)
                            { 
                                imgCount += 1;
                            }

                            for (var item in obj.Group)
                            { 
                                if (obj.Group[item].ImgSrc != null  )
                                {
                                    path = ImagePath.substring(2,ImagePath.length);
                        
                                    if (ImageType == "Artists")
                                    {
                                        carousel.add(carousel.options.size+1,"<a href=\"artists/" + obj.Group[item].TypeIDForURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + obj.Group[item].TypeDetail + "\" width=\"150\" height=\"150\" class=\"middle-img\" slidertype=\"now\" typeid=\""+ obj.Group[item].TypeID + "\" /></a><div class=\"middle-nav\"><a href=\"artists/" + obj.Group[item].TypeIDForURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a></div><div class=\"middle-artist\"><a href=\"#\">" + obj.Group[item].TypeDetail + "</a></div>");
                                    }
                                    else
                                    {
                                        carousel.add(carousel.options.size+1,"<a href=\"pieces/" + obj.Group[item].APIncludeURL + ".aspx\"><img src=\"" + path + obj.Group[item].ImgSrc + "\" alt=\"" + obj.Group[item].TypeTitle + obj.Group[item].TypeDetail + "\" width=\"250\" height=\"150\" class=\"middle-img\" slidertype=\"now\" typeid=\""+ obj.Group[item].TypeID + "\" /><div class=\"middle-nav\"><a href=\"pieces/" + obj.Group[item].APIncludeURL + ".aspx\">" + obj.Group[item].TypeTitle + " </a></div><div class=\"middle-artist\"><a href=\"" + obj.Group[item].TypeForURL + obj.Group[item].TypeIDForURL + ".aspx\">" + obj.Group[item].TypeDetail + "</a></div>");
                                    }
                                    carousel.options.size = carousel.options.size+1;
                                    nowCarouselSize = carousel.options.size;
                                }
                                else
                                {
                                    isNowEmpty = true;
                                }
                            }
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.status);
                    }
                });
            }
        }
    }
    
    function AfterItemLastIn(carousel, elem, idx, carouselState)
    {
        //if (ImageType == "Artists")
        //{
        //    alert("hey");
        //    $("li").each(function() {
        //    $(".jcarousel-item]").css({"padding":"27px 10px","float":"left","width":"150px","list-style":"none outside none"});
        //}
    }
    
    function AfterNowItemLastIn(carousel, elem, idx, carouselState)
    {
    }
    
    function PopulateCarousel(carousel, carouselElement, itemIdx, carouselState)
    {
        if (carouselState == 'next')
        {
            if ((itemIdx+carousel.options.scroll) >= carouselSize)
            {
                alert('should load');
                loadedItem = true;
            }
        }
    }
    
    $(".divNowPrev").click(function(e) {
        e.preventDefault();
    });
    
    /*
div.jcarousel-nav a
{
 	font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size: 11pt;
	color: #4B79A0;
	font-weight: bold; 
	line-height: 15px;
	height: 20px;
}

div.jcarousel-nav a:hover {
	color:#CCC;
}
    */
    

