//global object
var ixw = { 'clock': null, 'count': 1 };
var slideDirection = 'lr';
var speed = 5000;
var slideNum=0;
var iebrowser = document.all;
var oldIEbrowser = false;
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/
var slideImages = new Array();
var slideTitles = new Array();
var slideMenus = new Array();

ixw.imgs = [
	'/images/slides/medical.jpg',
	'/images/slides/laboratory.jpg',
	'/images/slides/foodservices.jpg',
	'/images/slides/pharmacy.jpg'
	];

//define images. You can have as many as you want:
slideImages[0] = "/images/slides/medical.jpg";
slideImages[1] = "/images/slides/laboratory.jpg";
slideImages[2] = "/images/slides/foodservices.jpg";
slideImages[3] = "/images/slides/pharmacy.jpg";

slideTitles[0] = "Medical Equipment & Supplies";
slideTitles[1] = "Laboratory";
slideTitles[2] = "Food Services";
slideTitles[3] = "Pharmacy";

//Set corresponding menus for above slides.
var arVersion = navigator.appVersion.split("MSIE");
var version = parseFloat(arVersion[1]);
if (version < 7) {
    oldIEbrowser = true;
    slideMenus[0]="<img src='/images/slides/medical-menu.gif' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Supply/' title='Medical Equipment & Supplies' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(1);' title='Laboratory' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(2);' title='Foodservice' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(3);' title='Pharmacy' /></map>";
    slideMenus[1]="<img src='/images/slides/laboratory-menu.gif' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Laboratory/' title='Laboratory' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(2);' title='Foodservice' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(3);' title='Pharmacy' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /></map>";
    slideMenus[2]="<img src='/images/slides/foodservices-menu.gif' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Foodservice/' title='Foodservice' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(3);' title='Pharmacy' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(1);' title='Laboratory' /></map>";
    slideMenus[3]="<img src='/images/slides/pharmacy-menu.gif' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Pharmacy/' title='Pharmacy' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(1);' title='Laboratory' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(2);' title='Foodservice' /></map>";
}
else {
    oldIEbrowser = false;
    slideMenus[0]="<img src='/images/slides/medical-menu.png' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Supply/' title='Medical Equipment & Supplies' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(1);' title='Laboratory' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(2);' title='Foodservice' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(3);' title='Pharmacy' /></map>";
    slideMenus[1]="<img src='/images/slides/laboratory-menu.png' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Laboratory/' title='Laboratory' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(2);' title='Foodservice' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(3);' title='Pharmacy' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /></map>";
    slideMenus[2]="<img src='/images/slides/foodservices-menu.png' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Foodservice/' title='Foodservice' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(3);' title='Pharmacy' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(1);' title='Laboratory' /></map>";
    slideMenus[3]="<img src='/images/slides/pharmacy-menu.png' border='0' usemap='#HeaderMenuMap' /><map name='HeaderMenuMap' id='HeaderMenuMap'><area shape='rect' coords='11,1,34,199' href='/AboutUs/OperatingCommittees/Pharmacy/' title='Pharmacy' /><area shape='rect' coords='34,1,58,200' href='javascript:crosswipe(0);' title='Medical Equipment & Supplies' /><area shape='rect' coords='58,2,82,205' href='javascript:crosswipe(1);' title='Laboratory' /><area shape='rect' coords='83,1,106,198' href='javascript:crosswipe(2);' title='Foodservice' /></map>";
}

/*****************************************************************************
*****************************************************************************/

$(document).ready(function(){
		if(window.location.pathname=="/AboutUs/OperatingCommittees/Laboratory/") {
			jumpToSlide(1);
		}
		else if(window.location.pathname=="/AboutUs/OperatingCommittees/Foodservice/") {
			jumpToSlide(2);
		}
		else if(window.location.pathname=="/AboutUs/OperatingCommittees/Pharmacy/") {
			jumpToSlide(3);
		}
	
});

function jumpToSlide() {
	slideNum=arguments[0];
	document.getElementById("headerimage").src=slideImages[slideNum];
	document.getElementById("headerimage").title=slideTitles[slideNum];
	updateMenu();
}


//cache the images
ixw.imgsLen = ixw.imgs.length;
ixw.cache = [];
for(var i=0; i<ixw.imgsLen; i++)
{
	ixw.cache[i] = new Image;
	ixw.cache[i].src = ixw.imgs[i];
}

//crosswipe setup function
function crosswipe() {

    //if the timer is not already going
	if(ixw.clock == null) {

	    slideNum = arguments[0];
    
	    //copy the image object
		
	    ixw.obj = document.getElementById('headerimage');
	    //get its dimensions
		ixw.size = { 'w' : ixw.obj.width, 'h' : ixw.obj.height };
		
		//copy the image src argument
		ixw.src = slideImages[slideNum];
		
		//change the image alt text if defined
		ixw.obj.title = slideTitles[slideNum];
		
		//if dynamic element creation is supported
		if(typeof document.createElementNS != 'undefined' || typeof document.createElement != 'undefined')
		{
			//create a new image object and append it to body
			//detecting support for namespaced element creation, in case we're in the XML DOM
			ixw.newimg = document.getElementsByTagName('body')[0].appendChild((typeof document.createElementNS != 'undefined') ? document.createElementNS('http://www.w3.org/1999/xhtml', 'img') : document.createElement('img'));
			
			//set positioning classname
			ixw.newimg.className = 'idupe';

			//set src to new image src
			ixw.newimg.src = ixw.src

			//move it to superimpose original image
			ixw.newimg.style.left = ixw.getRealPosition(ixw.obj, 'x') + 'px';
			ixw.newimg.style.top = ixw.getRealPosition(ixw.obj, 'y') + 'px';
			
			//set it to be completely hidden with clip
			ixw.newimg.style.clip = 'rect(0, 0, 0, 0)';

			//show the image 
			ixw.newimg.style.visibility = 'visible';

			//copy and convert fade duration argument 
			ixw.length = parseInt(2, 10) * 1000;

			//create fade resolution argument as 20 steps per transition
			ixw.resolution = parseInt(2, 10) * 20;
			
			//copy slide direction argument
			ixw.dir = slideDirection;
			
			//start the timer
			ixw.clock = setInterval('ixw.crosswipe()', ixw.length/ixw.resolution);
		}
		
		//otherwise if dynamic element creation is not supported
		else
		{
			//just do the image swap
			ixw.obj.src = ixw.src;
        }

        updateMenu();
		
	}
};


//changeSlide timer function
ixw.crosswipe = function() {
    //decrease the counter on a linear scale
    ixw.count -= (1 / ixw.resolution);

    //if the counter has reached the bottom
    if (ixw.count < (1 / ixw.resolution)) {
        //clear the timer
        clearInterval(ixw.clock);
        ixw.clock = null;

        //reset the counter
        ixw.count = 1;

        //set the original image to the src of the new image
        ixw.obj.src = ixw.src;
    }

    //animate the clip of the new image
    //using the width and height properties we saved earlier
    ixw.newimg.style.clip = 'rect('
		+ ((/bt|bltr|brtl/.test(ixw.dir)) ? (ixw.size.h * ixw.count) : (/che|cc/.test(ixw.dir)) ? ((ixw.size.h * ixw.count) / 2) : (0))
		+ 'px, '
		+ ((/lr|tlbr|bltr/.test(ixw.dir)) ? (ixw.size.w - (ixw.size.w * ixw.count)) : (/cve|cc/.test(ixw.dir)) ? (ixw.size.w - ((ixw.size.w * ixw.count) / 2)) : (ixw.size.w))
		+ 'px, '
		+ ((/tb|tlbr|trbl/.test(ixw.dir)) ? (ixw.size.h - (ixw.size.h * ixw.count)) : (/che|cc/.test(ixw.dir)) ? (ixw.size.h - ((ixw.size.h * ixw.count) / 2)) : (ixw.size.h))
		+ 'px, '
		+ ((/lr|tlbr|bltr/.test(ixw.dir)) ? (0) : (/tb|bt|che/.test(ixw.dir)) ? (0) : (/cve|cc/.test(ixw.dir)) ? ((ixw.size.w * ixw.count) / 2) : (ixw.size.w * ixw.count))
		+ 'px)';

    //keep new image in position with original image
    //in case text size changes mid transition or something
    ixw.newimg.style.left = ixw.getRealPosition(ixw.obj, 'x') + 'px';
    ixw.newimg.style.top = ixw.getRealPosition(ixw.obj, 'y') + 'px';

    //if the counter is at the top, which is just after the timer has finished
    if (ixw.count == 1) {
        //remove the duplicate image
        ixw.newimg.parentNode.removeChild(ixw.newimg);
    }
    
};



//get real position method
ixw.getRealPosition = function() {
    this.pos = (arguments[1] == 'x') ? arguments[0].offsetLeft : arguments[0].offsetTop;
    this.tmp = arguments[0].offsetParent;
    while (this.tmp != null) {
        this.pos += (arguments[1] == 'x') ? this.tmp.offsetLeft : this.tmp.offsetTop;
        this.tmp = this.tmp.offsetParent;
    }
    if (iebrowser && arguments[1] == 'x') {
        if (!oldIEbrowser) this.pos = this.pos - 1; //for newer IE browser
    }
    return this.pos;
};


function updateMenu() {
    var div = document.getElementById("slidetoggler");
    div.innerHTML = slideMenus[slideNum];
}

