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

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:23,
            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);