var subnavRotateLayers;

initRotation = function() {
    subnavRotateLayers = document.getElementsByClassName("rotateImages");
    if (!subnavRotateLayers || subnavRotateLayers.length < 2) return false;
   
    for (var i=0; i<subnavRotateLayers.length; i++) {
        if (subnavRotateLayers[0].visible()) break;
        var makeLast = subnavRotateLayers.shift();
        subnavRotateLayers.push(makeLast);
    }
    setTimeout(rotateNewsLayers, 7500);
}

rotateNewsLayers = function() {
    var currentNews = subnavRotateLayers.shift();
    var nextNews = subnavRotateLayers[0];

    Effect.Appear(nextNews, {duration:0, 
        afterFinish:function() {
            currentNews.hide();
            nextNews.setStyle({zIndex:0});
            setTimeout(rotateNewsLayers, 7500);
        },
        beforeStart:function() {
            nextNews.setStyle({zIndex:5}).hide();
        }
    });
   
    subnavRotateLayers.push(currentNews);

}

Event.observe(window, 'load', initRotation);

function updateAjaxItineraryCount() {
    var ajaxItineraryCount = $('ajaxItineraryCount');
    if (!ajaxItineraryCount) return false;
    var planNow = $('startPlanningNowPara');
    if (planNow) planNow.hide();
    MainService.getItineraryCountByCode(code, {
        callback:function(str) {
        	ajaxItineraryCount.update(""+str+" Items");
        },
        timeout:6000,
        errorHandler:function(msg) {
        	ajaxItineraryCount.update("? Items");
        }
    });
}

function initAddRemoveActionLink(){
	var itineraryAction = $('itineraryAction');
	if(itineraryAction){
		var key = itineraryAction.getAttribute('key');
		itineraryAction.observe('click', function(e) {
            Event.stop(e);
			if (itineraryAction.hasClassName("museumInItinerary")) {
				MainService.removeFromItinerary(code, key, { 
         		   callback:function(str) {
                		itineraryAction.removeClassName("museumInItinerary");
                		itineraryAction.update("Add to Itinerary");
                		updateAjaxItineraryCount();
            		},
            		timeout:6000,
            		errorHandler:function(msg) {
                		//alert("Ajax call to remove item from itinerary FAILED:\n"+msg);
            		}
        		});
			} else{
				MainService.addToItinerarySimple(code, key, {
					callback:function(str) {
						itineraryAction.addClassName("museumInItinerary");
						itineraryAction.update("Remove from Itinerary");
						updateAjaxItineraryCount();
					},
            		timeout:6000,
            		errorHandler:function(msg) {
            		    //alert("Ajax call to add item from itinerary FAILED:\n"+msg);
            		}
				});
			}
			
		});
	}
}

Event.observe(window, 'load', initAddRemoveActionLink);

