
/**
 * Image src URLs
 */
var imageList = ["/event_gallery/64_picstrip.jpg","/event_gallery/47_picstrip.jpg","/event_gallery/37_picstrip.jpg","/event_gallery/49_picstrip.jpg","/event_gallery/34_picstrip.jpg","/event_gallery/62_picstrip.jpg","/event_gallery/80_picstrip.jpg","/event_gallery/55_picstrip.jpg","/event_gallery/84_picstrip.jpg","/event_gallery/35_picstrip.jpg","/event_gallery/77_picstrip.jpg","/event_gallery/76_picstrip.jpg","/event_gallery/39_picstrip.jpg","/event_gallery/54_picstrip.jpg","/event_gallery/41_picstrip.jpg","/event_gallery/66_picstrip.jpg","/event_gallery/4_picstrip.jpg","/event_gallery/74_picstrip.jpg","/event_gallery/5_picstrip.jpg","/event_gallery/44_picstrip.jpg","/event_gallery/21_picstrip.jpg","/event_gallery/83_picstrip.jpg","/event_gallery/33_picstrip.jpg","/event_gallery/59_picstrip.jpg","/event_gallery/31_picstrip.jpg","/event_gallery/60_picstrip.jpg"];
var imageListTitle = ["Llangollen Fringe Festival","Denbigh  Flint Show","Gwledd Conwy Feast","Eglwysbach Agricultural and Horticultural Show","Conwy Farmers' Market","Llangollen Balloon Festival","The Cambrian Rally","North Wales International Music Festival","Errol Brown","Conwy River Festival","Anglesey Walking Festival","Conwy's Walking Week","Anglesey Show","Anglesey Oyster and Welsh Produce Festival","Anglesey Marathon","Llandudno Celtic Winter Fayre","Abersoch Jazz Festival","Prestatyn Walking Festival","Wakestock 2009","Denbighshire Walking Weekend","Bryn Terfel's Gwyl Y Faenol/Faenol Festival","Christmas Market","Farmers' Market","Mold Food and Drink Festival","North Wales Cricket Festival","Llanrwst Market"];
var imageListLink = ["/events/2807/llangollen-fringe-festival","/events/785/denbigh-flint-show","/events/2361/gwledd-conwy-feast","/events/1373/eglwysbach-agricultural-and-horticultural-show","/events/2699/conwy-farmers-market","/events/1600/llangollen-balloon-festival","/events/3578/the-cambrian-rally","/events/2713/north-wales-international-music-festival","/events/3833/errol-brown","/events/1616/conwy-river-festival","/events/2689/anglesey-walking-festival","/events/1614/conwys-walking-week","/events/1324/anglesey-show","/events/1298/anglesey-oyster-and-welsh-produce-festival","/events/2840/anglesey-marathon","/events/2362/llandudno-celtic-winter-fayre","/events/2685/abersoch-jazz-festival","/events/2005/prestatyn-walking-festival","/events/1353/wakestock-2009","/events/778/denbighshire-walking-weekend","/events/1322/bryn-terfels-gwyl-y-faenol-faenol-festival","/events/3730/christmas-market","/events/1317/farmers-market","/events/2758/mold-food-and-drink-festival","/events/2614/north-wales-cricket-festival","/events/2698/llanrwst-market"];
var imageListRegion = ["North Wales Borderlands","North Wales Borderlands","Snowdonia Mountains and Coast","Snowdonia Mountains and Coast","Snowdonia Mountains and Coast","North Wales Borderlands","Coastal North Wales","North Wales Borderlands","Coastal North Wales","Snowdonia Mountains and Coast","Isle of Anglesey","Snowdonia Mountains and Coast","Isle of Anglesey","Isle of Anglesey","Isle of Anglesey","Coastal North Wales","Snowdonia Mountains and Coast","Coastal North Wales","Snowdonia Mountains and Coast","North Wales Borderlands","Snowdonia Mountains and Coast","North Wales Borderlands","Coastal North Wales","North Wales Borderlands","Coastal North Wales","Snowdonia Mountains and Coast"];
var imageListWidth = [120,120,124,120,107,107,100,111,62,107,120,120,107,120,119,120,107,122,111,120,120,61,107,107,120,107];
var imageListHeight = [80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80];

var lastRan = -1;

var fmtItem = function(imgUrl, url, title, index, region, w , h) {

      var innerHTML = '<a id="dhtml-carousel-a-'+index+'" class="' + region + '" alt="' + title + '" href="' + url + '">';
	  innerHTML = innerHTML + '<img id="dhtml-carousel-img-' + index + '" src="' + imgUrl + '" width="' + w +'" height="' + h +'"/><\/a><p>' + title + '</p>';
	return innerHTML;
};


/**
 * Custom inital load handler. Called when the carousel loads the initial
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadInitHandler
 **/
var loadInitialItems = function(type, args) {

    var start = args[0];
    var last = args[1]; 

    load(this, start, last);    
};

/**
 * Custom load next handler. Called when the carousel loads the next
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadNextHandler
 **/
 var loadNextItems = function(type, args) {    

    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
};

/**
 * Custom load previous handler. Called when the carousel loads the previous
 * set of data items. Specified to the carousel as the configuration
 * parameter: loadPrevHandler
 **/
var loadPrevItems = function(type, args) {
    var start = args[0];
    var last = args[1]; 
    var alreadyCached = args[2];
    
    if(!alreadyCached) {
        load(this, start, last);
    }
};

var load = function(carousel, start, last) {
    for(var i=start;i<=last;i++){
        var liItem = carousel.addItem(i, fmtItem(imageList[i], imageListLink[i], imageListTitle[i], i, imageListRegion[i],imageListWidth[i],imageListHeight[i]));
	  	//Prepare("dhtml-carousel-a-" + i);
/*
        // If you want to add handlers for mouse over, mouse out or other events,
        // here is the pattern
        YAHOO.util.Event.addListener(liItem, "mouseover", function(e) {
        //console.log("mouseover>>"+this.id);
        });
        YAHOO.util.Event.addListener(liItem, "mouseout", function(e) {
            //console.log("mouseout<<"+this.id);
        });
*/
		Prepare(document.getElementById("dhtml-carousel-a-" + i));
    }
}

/**
 * Custom button state handler for enabling/disabling button state. 
 * Called when the carousel has determined that the previous button
 * state should be changed.
 * Specified to the carousel as the configuration
 * parameter: prevButtonStateHandler
 **/
var handlePrevButtonState = function(type, args) {

    var enabling = args[0];
    var leftImage = args[1];
    if(enabling) {
        leftImage.src = "/images/left-enabled.gif";    
    } else {
        leftImage.src = "/images/left-disabled.gif";
    }
    
};

var carousel; // for ease of debugging; globals generally not a good idea

/**
 * You must create the carousel after the page is loaded since it is
 * dependent on an HTML element (in this case 'dhtml-carousel'.) See the
 * HTML code below.
 */
var pageLoad = function() 
{
    carousel = new YAHOO.extension.Carousel("dhtml-carousel", 
        {
            numVisible:        9,
            animationSpeed:   0.5,
            scrollInc:         1,
            navMargin:         40,
            prevElement:     "prev-arrow",
            nextElement:     "next-arrow",
            loadInitHandler:   loadInitialItems,
            loadNextHandler:   loadNextItems,
            loadPrevHandler:   loadPrevItems,
            prevButtonStateHandler:   handlePrevButtonState,
            autoPlay: 3000,
            size:25,
            wrap:true
        }
    );

};
// YAHOO.extension.Carousel.prototype.startAutoPlay = function(interval) {
//         if(this._isValidObj(interval)) {
//             this.autoPlay = interval;
//         }
//         if(this.autoPlayTimer !== null) {
//             return this.autoPlayTimer;
//         }
//         var oThis = this;  
//         var autoScroll = function() { oThis.scrollPrev(); };
//         this.autoPlayTimer = setTimeout( autoScroll, this.autoPlay );
//         return this.autoPlayTimer;
// };
// 
// YAHOO.extension.Carousel.prototype.scrollPrev= function() {
//     this._scrollPrev(null, this);
//     this.autoPlayTimer = null;
//     if(this.autoPlay !== 0) {
//         this.autoPlayTimer = this.startAutoPlay();
//     }
// };
// 
// YAHOO.extension.Carousel.prototype._scrollPrev = function(e, carousel) {
//     if(carousel.scrollPrevAnim.isAnimated()) {
//         return false;
//     }
//     var currStart = carousel.firstVisible;
//     if(carousel.wrap && currStart == 1) {
//         carousel.scrollTo(carousel.size - carousel.numVisible + 1);
//     } else if(e !== null) { // event fired this so disable autoplay
//         carousel.stopAutoPlay();
//         carousel._scrollPrevInc(carousel, carousel.scrollInc, (carousel.getProperty("animationSpeed") !== 0));
//     } else {
//         carousel._scrollPrevInc(carousel, carousel.scrollInc, (carousel.getProperty("animationSpeed") !== 0));
//     }
// };


/**
 * Illustrates stop autoplay
 */
var stopAutoPlay = function(e) {
    //YAHOO.util.Dom.get("status").innerHTML = "Auto Play Stopped!";
    carousel.stopAutoPlay();
};

/**
 * Illustrates start autoplay
 */
var startAutoPlay = function(e) {
    //YAHOO.util.Dom.get("status").innerHTML = "Auto Play Started!";
    carousel.startAutoPlay(2000);
};

YAHOO.util.Event.addListener(window, 'load', pageLoad);
YAHOO.util.Event.addListener("stop-button", 'click', stopAutoPlay);
YAHOO.util.Event.addListener("start-button", 'click', startAutoPlay);