//Tab Area Functions

var navTabClass = 'TabNav';
var navTabSelectedClass = 'selectedTab';
var defaultTabId = 'defaultTab';

var preventDefaultEventonTabSelect = 1; // true==1 stop the url from being updated.


function showTab(e){
    var target, id, liElm, aElm, targetId; 
    elm = $(e);   
    // alert(elm.tagName);
    if (elm.tagName    == 'A' ) {   // in case the anchore link eack clicked
        liElm = elm.parentNode;
        aElm = elm ;
    }  else {   
        liElm = elm;
        aElm = elm.firstDescendant();   
    }
    target = aElm.readAttribute('href').strip();
    liElm.siblings().invoke('removeClassName', navTabSelectedClass); //de-select all tabs
    liElm.addClassName(navTabSelectedClass); // make our new tab selected
    if (target.startsWith('#'))  { //only if the anchor link is within the current document
        targetId = target.substring(1);
        var targetElm = $(targetId);  
        targetElm.siblings().invoke('hide');
        targetElm.show();           
    }
}

function showTabEvent(Event) {
    var elm = Event.element(); 
    showTab(elm);    
    if (preventDefaultEventonTabSelect) Event.preventDefault(); 
}

function markEvent(event) { // for debuging events
    var node = Event.element(event); // the node that was clicked on
    node.style.color = "green";
}

// this sets up the showTabEvent on the click events, after the dom has been loaded
document.observe('dom:loaded', function () {
    $$('.'+navTabClass+' li').each ( function (e)   {       
        e.observe('click',showTabEvent);   
        //      e.observe('click',markEvent);          
    } );
    if ($(defaultTabId) == null) { // if the default Tab Id is NOT used in the pages markup
        // set the default tab based on the first li of the list with the TabNav Class
        showTab($$('.'+navTabClass).first().firstDescendant());  
    }  else { // set the default tab based on the Id
        showTab($(defaultTabId));
    }    
})


function setNewStyleSheet (ss ) { 
    $$('link[rel="stylesheet"]').invoke('remove');
 
        $$('link[rel="stylesheet"]').invoke('remove');
        var attrs = {
            type 	: "text/css",
            href 	: ss,
            rel	:"stylesheet"
        };
 
        var sslink = new Element('link', attrs);
        $$('head').invoke('insert',sslink);

}

//Slideshow Functions


// set the starting image.
var i = 0;			

// The array of div names which will hold the images.
var image_slide = new Array('image-1', 'image-2', 'image-3');

// The number of images in the array.
var NumOfImages = image_slide.length;



	
// The time to wait before moving to the next image. Set to 4 seconds by default.
var wait = 4000;

// The Fade Function
function SwapImage(x,y) {
	$(image_slide[y]).fade({duration: 0});
	$(image_slide[x]).appear({ duration: 0 });
}

// the onload event handler that starts the fading.
function StartSlideShow() {
	play = setInterval('Play()',wait);
}

function Play() {
	var imageShow, imageHide;

	imageShow = i+1;
	imageHide = i;
	
	if (imageShow == NumOfImages) {
		SwapImage(0,imageHide);	
		i = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		i++;
	}
	
	var textIn = i+1 + ' of ' + NumOfImages;
}

function GoNext() {
	clearInterval(play);
	
	var imageShow, imageHide;

	imageShow = i+1;
	imageHide = i;
	
	if (imageShow == NumOfImages) {
		SwapImage(0,imageHide);	
		i = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		i++;
	}
}

function GoPrevious() {
	clearInterval(play);

	var imageShow, imageHide;
				
	imageShow = i-1;
	imageHide = i;
	
	if (i == 0) {
		SwapImage(NumOfImages-1,imageHide);	
		i = NumOfImages-1;		
		
		//alert(NumOfImages-1 + ' and ' + imageHide + ' i=' + i)
					
	} else {
		SwapImage(imageShow,imageHide);			
		i--;
		
		//alert(imageShow + ' and ' + imageHide)
	}
}

