// Javascript Document
/*********************************************************************************************************************************
* portageScripts.js
* This file contains the customized code for Portage Health.
* Alter the properties of the optionSlider object to customize the features of the following three components:
*   1) homepage automated carousel, 2) homepage manual slider, 3) virtual tour manual slider
*********************************************************************************************************************************/

/*** BEGIN OPTION OBJECT ***/
function optionSlider() {
	/* PROPERITES */
	this.index = 0;									// index value to determine total number of items to cycle through (used for each of the listed features below)
	
	//Properties for the Homepage main slide carousel
	this.carousel = 'ul.carouselList';				// string value for the class name of the carousel <ul>
	this.carouselSlide = 'slideCarousel';			// string value for the dynamically added class to the <li> in this.carousel
	this.carouselContainer = 'div.carouselSlides';	// string value for the class name of the carousel slide container
	this.intSlideNext = 0;							// int value for the actual current slide
	this.numCarousel = 0;							// int value for the number of slides in the carousel
	this.carouselTicker = 1;						// int value to determine which slide is currently active
	this.currentSlide = '';							// string value for the current slide
	this.destSlide = '';							// string value for the destination slide
	this.carouselTime = 500;						// int value for length of time to fade slides
	this.windowTimer = 5000;						// int value for time between automatic slide transition
	
	// Properties for the Homepage 3 panel slider
	this.slider = 'ul.featuredServicesSlides';		// string value for the class name of the slider <ul>
	this.width = 0;									// int value for width of each slide
	this.num = 0;									// int valuve for the total number of slides
	this.ticker = 1;								// int value to determine if the slider can move left or right
	this.num2Slide = 3;								// int value for number of slides to cycle through -- Change this value depending on how many should cycle (NOTE: each slide must be of equal width for this to function correctly, otherise the width will be set to the width of the largest slide)
	this.location = 0;								// current x value of the location of the slider (This value will change then launch the animation)
	this.destination = 0;							// int value for destination of the slider
	this.slideTime = 750;							// int value for length of time to slide
	this.btnL = 'div.featuredServicesBtnL';			// string value for the left button reference
	this.btnR = 'div.featuredServicesBtnR';			// string value for the right button reference
	this.btnLstatus = 'off';						// default state of the left button
	this.btnRstatus = 'on';							// default state of the right button
	
	// Properties for the Virtual Tours slider
	this.isVirtual = false;							// boolean value to determine which slider to fire up
	this.Vticker = 1;								// int value to track which slide in the Virtual Carousel is active
	this.VcurrentSlide = '';						// string value for the current, active virtual slide
	this.VdestSlide = '';							// string value for the selected virtual slide
	this.Vcarousel = 'ul.virtualContainer';			// string value for the container of the virtual carousel <ul>
	this.Vslide = 'li.vSlide';						// string value of the clall name for each slide in the virtual carousel <li>
	this.Vslider = 'ul.virtualSliderSlides';		// string value for the class name of the virtual slider <ul>
	this.vbtnL = 'div.virtualSliderBtnL';			// string value for the left button reference on the virtual tour page
	this.vbtnR = 'div.virtualSliderBtnR';			// string value for the right button reference on the virtual tour page
	this.vbtnLstatus = 'off';						// default state of the left button
	this.vbtnRstatus = 'off';						// default state of the right button
	
	
	/* METHODS */
	/* Reset the index to 0 */
	this.resetIndex = function() {
		this.index = 0;
	}
	
	/* Set the value for numCarousel (total number of slides in the carousel) to be that of the index */
	this.setNumCarousel = function() {
		this.numCarousel = this.index;
	}
	
	/* Swap the Homepage carousel slides */
	this.swapSlides = function() {
		var swapOut = this.currentSlide.replace('Carousel', '');
		var swapIn = this.destSlide.replace('Carousel', '');
		
		// Validate it isn't empty so that it doesn't throw a JS error.
		if (swapOut != '') {
		    $('div.' + swapOut).fadeOut(this.carouselTime, function() {
			    $('div.' + swapIn).fadeIn(this.carouselTime);
		    });
		}
		
		$('li.' + this.carouselSlide + this.carouselTicker).css('background-position', '0px 0px');
		this.carouselTicker = parseInt(swapIn.replace('slide', ''));
		this.currentSlide = this.destSlide;
		$('li.' + this.carouselSlide + this.carouselTicker).css('background-position', '0px -17px');
	}
	
	/* Set the value for num (total number of slides) to be that of the index */
	this.setNumOfSlides = function() {
		this.num = this.index;
	}
	
	/* Move the Homepage slider */
	this.moveSlider = function() {
		$(this.slider).animate({
			left: this.destination
		}, this.slideTime );
	}
	
	/* Move the Virtual Tours slider */
	this.moveVslider = function() {
		$(this.Vslider).animate({
			left: this.destination
		}, this.slideTime );
	}
	
	/* Swap the Virtual Tours slides */
	this.swapVslides = function() {
		$('.' + this.VcurrentSlide).fadeOut(this.carouselTime);
		$('.' + this.VdestSlide).fadeIn(this.carouselTime);
		this.VcurrentSlide = this.VdestSlide;
	}
	
	/* strictly for debugging purposes */
	this.talk = function() { alert('Hello World!'); }
}
/*** END OPTION OBJECT ***/


/*** BEGIN FUNCTIONS TO CONTROL THE FOLLOWING THREE ITEMS: Rotating carousel on homepage, 3 panel slider on homepage, 3 panel slider and viewer on Virtual Tours ***/
/* Function for the automatic timer */
function controlTimer(option) {
		window.setInterval( function() {
			option.intSlideNext = parseInt(option.currentSlide.replace('slideCarousel', ''));
			option.intSlideNext >= option.numCarousel ? option.intSlideNext = 1 + '' : option.intSlideNext = option.intSlideNext + 1 + '';
			option.destSlide = option.carouselSlide + option.intSlideNext;
			option.swapSlides();
		}, option.windowTimer );
}


/* Event handler for the Carousel functionality */
function carouselEvent(event) {
	switch (event.data.type) {
		case 'over':	$(this).find('div').css('visibility', 'visible');
						break;
		case 'out':		$(this).find('div').css('visibility', 'hidden');
						break;
		case 'click':	event.data.option.destSlide = $(this).attr('class');
						if (event.data.option.destSlide != event.data.option.currentSlide) {
							event.data.option.swapSlides();
						}
						break;
		default:	alert('error in carousel switch');
	}
}


/* Function to initialize the carousel */
function initCarousel(option) {
	$(option.carousel + ' > li').each(function() {
		option.index++;
		$(this).addClass(option.carouselSlide + option.index);
		
		// If the slide is the current, active slide, set the background to the correct position
		if (option.index == option.carouselTicker) {
			option.currentSlide = option.carouselSlide + option.index;
			$(this).css('background-position', '0px -17px');
		}
		// If the slide is not current, hide it
		else {
			var remover = $(this).attr('class').replace('Carousel', '');
			$('.' + remover).css('display', 'none');
		}
	});
	option.setNumCarousel();
	
	//if there is only a single slide, remove the dot
	if (option.numCarousel <= 1) {
		$(option.carousel + ' > li').css('display', 'none');
	}
	
	option.resetIndex();
	
	$(option.carousel + ' > li').bind('mouseover', {type: 'over', option: option}, carouselEvent).bind('mouseout', {type: 'out', option: option}, carouselEvent).bind('click', {type: 'click', option: option}, carouselEvent);
	
	if (option.numCarousel > 1) {
		controlTimer(option);
	}
}


/* Function to determine and set option.width */
function setSlideWidth(slide, option) {
	// determine width of each slide (NOTE: it is coded this way so that the <li> does not necessarily need these defined
	var currentSlideWidth = 0;
	if ($(slide).css('margin-left')) { currentSlideWidth += parseInt($(slide).css('margin-left')); };
	if ($(slide).css('margin-right')) { currentSlideWidth += parseInt($(slide).css('margin-right')); };
	if ($(slide).css('padding-left')) { currentSlideWidth += parseInt($(slide).css('padding-left')); };
	if ($(slide).css('padding-right')) { currentSlideWidth += parseInt($(slide).css('padding-right')); };
	if ($(slide).css('border-left')) { currentSlideWidth += parseInt($(slide).css('border-left')); };
	if ($(slide).css('border-right')) { currentSlideWidth += parseInt($(slide).css('border-right')); };
	if ($(slide).width()) { currentSlideWidth += parseInt($(slide).width()); };
			
	// if width of current slide is greater than object.width, set object.width to new value; otherwise, do nothing
	if (option.width < currentSlideWidth) {
		option.width = currentSlideWidth;
	}
}


/* visually turn a button on, off, or give it an active hover state */
function setButtonStatus(status, button) {
	switch(status) {
		case 'off': $(button).css('background-position', '0px -92px').css('cursor', 'default');
					break;
		case 'on':	$(button).css('background-position', '0px 0px').css('cursor', 'pointer');
					break;
		case 'in':	$(button).css('background-position', '0px -46px').css('cursor', 'pointer');
					break;
		default:	alert('error in setButtonStatus');
	}
	
}

/* Check the status of the button and the slider position to determine if a button should be turned on or off */
function checkButtonStatus(option) {
	// if the ticker is 1, then the slider is in the initial position, so turn off the left button, and turn on the right button
	if (option.ticker == 1) {
		if (option.isVirtual) {
			setButtonStatus('off', option.vbtnL);
			option.vbtnLstatus = 'off';
		}
		else {
			setButtonStatus('off', option.btnL);
			option.btnLstatus = 'off';
		}
	}
	else if (option.ticker > 1) {
		if (option.isVirtual) {
			setButtonStatus('on', option.vbtnL);
			option.vbtnLstatus = 'on';
		}
		else {
			setButtonStatus('on', option.btnL);
			option.btnLstatus = 'on';
		}
	}
	// if the ticker * the number of slides per pane (in this case, 3) is greater than the total number of slides to sift through, turn on the left button, and turn off the right button
	if ((option.ticker * option.num2Slide) >= option.num) {
		if (option.isVirtual) {
			setButtonStatus('off', option.vbtnR);
			option.vbtnRstatus = 'off';
		}
		else {
			setButtonStatus('off', option.btnR);
			option.btnRstatus = 'off';
		}
	}
	if ( (option.ticker * option.num2Slide) < option.num ) {
		if (option.isVirtual) {
			setButtonStatus('on', option.vbtnR);
			option.vbtnRstatus = 'on';
		}
		else {
			setButtonStatus('on', option.btnR);
			option.btnRstatus = 'on';
		}
	}
}

/* Slide button event handlers */
/* event handler for left button */
function buttonStatusL(event) {
	// based on the mouse event (enter or leave) and the status of the button (if it is on or off), determine how the button should visually behave
	switch (event.data.status) {
		case 'enter':	checkButtonStatus(event.data.option);
						// if the left button is currently on, apply the hover state
						if (event.data.option.isVirtual) {
							if (event.data.option.vbtnLstatus == 'on')
								setButtonStatus('in', event.data.option.vbtnL);
						}
						else {
							if (event.data.option.btnLstatus == 'on')
								setButtonStatus('in', event.data.option.btnL);
						}
						break;
		case 'leave':	checkButtonStatus(event.data.option);
						// if the left button is currently on, apply the default state
						if (event.data.option.isVirtual) {
							if (event.data.option.vbtnLstatus == 'on')
								setButtonStatus('on', event.data.option.vbtnL);
						}
						else {
							if (event.data.option.btnLstatus == 'on')
								setButtonStatus('on', event.data.option.btnL);
						}
						break;
		default:	alert('error in buttonStatusL');
	}
}
/* event handler for right button */
function buttonStatusR(event) {
	// based on the mouse event (enter or leave) and the status of the button (if it is on or off), determine how the button should visually behave
	switch (event.data.status) {
		case 'enter':	checkButtonStatus(event.data.option);
						// if the right button is currently on, apply the hover state
						if (event.data.option.isVirtual) {
							if (event.data.option.vbtnRstatus == 'on')
								setButtonStatus('in', event.data.option.vbtnR);
						}
						else {
							if (event.data.option.btnRstatus == 'on')
								setButtonStatus('in', event.data.option.btnR);
						}
						break;
		case 'leave':	checkButtonStatus(event.data.option);
						// if the right button is currently on, apply the default state
						if (event.data.option.isVirtual) {
							if (event.data.option.vbtnRstatus == 'on')
								setButtonStatus('on', event.data.option.vbtnR);
						}
						else {
							if (event.data.option.btnRstatus == 'on')
								setButtonStatus('on', event.data.option.btnR);
						}
						break;
		default:	alert('error in buttonStatus');
	}
}
/* end Slide button event handlers */

/* Slide Control event handler */
function onClick(event) {
	switch(event.data.direction) {
		case 'left':	if (event.data.option.location <= 0) {
							if (event.data.option.ticker != 1) {
								event.data.option.destination = event.data.option.location + (event.data.option.width * event.data.option.num2Slide);
								event.data.option.location = event.data.option.destination;
								if (event.data.option.isVirtual) {
									event.data.option.moveVslider();
								}
								else {
									event.data.option.moveSlider();
								}
								event.data.option.ticker--;
							}
							checkButtonStatus(event.data.option);
						}
						break;
		
		case 'right':	if (event.data.option.location <= 0) {
							if ((event.data.option.ticker * event.data.option.num2Slide) < event.data.option.num) {
								event.data.option.destination = event.data.option.location - (event.data.option.width * event.data.option.num2Slide);
								event.data.option.location = event.data.option.destination;
								if (event.data.option.isVirtual) {
									event.data.option.moveVslider();
								}
								else {
									event.data.option.moveSlider();
								}
								event.data.option.ticker++;
							}
							checkButtonStatus(event.data.option);
						}
						break;
		default:	alert('error in switch');
	}
}


/* Function to initialize the slider */
function initSlider(option) {
	// Count number of slides, and set option.width
	$(option.slider + ' > li').each(function() {
		option.index++;
		setSlideWidth($(this), option);
	});
	
	option.setNumOfSlides();
	option.resetIndex();
	
	checkButtonStatus(option);

	$(option.btnL).bind('click', {direction: 'left', option: option}, onClick).bind('mouseenter', {status: 'enter', option: option}, buttonStatusL).bind('mouseleave', {status: 'leave', option: option}, buttonStatusL);
	$(option.btnR).bind('click', {direction: 'right', option: option}, onClick).bind('mouseenter', {status: 'enter', option: option}, buttonStatusR).bind('mouseleave', {status: 'leave', option: option}, buttonStatusR);
}


/* Event Handler for Virtual slide click */
function vSlideClick(event) {
	var intVal = parseInt($(this).attr('class').replace('vSlideControl', ''));
	if (intVal != event.data.option.Vticker) {
		event.data.option.VdestSlide = 'vSlide' + intVal;
		event.data.option.Vticker = intVal;
		event.data.option.swapVslides();
	}
}


/* Function to initialize the Virtual Carousel */
function initVcarousel(option) {
	$(option.Vcarousel + ' > li').each(function() {
		var intVal = parseInt($(this).attr('class').replace('vSlide', ''));
		if (intVal != option.Vticker) {
			$(option.Vslide + intVal).css('display', 'none');
		}
		else {
			option.VcurrentSlide = $(this).attr('class');
		}
	});
	$(option.Vslider + ' > li').each(function() {
		option.index++;
		$(this).addClass('vSlideControl' + option.index);
		$(this).bind('click', {option: option}, vSlideClick);
	});
	option.resetIndex();
}



/* Function for the Virtual Slider */
function initVirtualSlider(option) {
	// Count number of slides, and set option.width
	$(option.Vslider + ' > li').each(function() {
		option.index++;
		setSlideWidth($(this), option);
	});
	
	option.setNumOfSlides();
	option.resetIndex();
	
	checkButtonStatus(option);
	
//	$(option.vbtnL).bind('click', {direction: 'left', option: option}, onClick);
//	$(option.vbtnR).bind('click', {direction: 'right', option: option}, onClick);
	$(option.vbtnL).bind('click', {direction: 'left', option: option}, onClick).bind('mouseenter', {status: 'enter', option: option}, buttonStatusL).bind('mouseleave', {status: 'leave', option: option}, buttonStatusL);;
	$(option.vbtnR).bind('click', {direction: 'right', option: option}, onClick).bind('mouseenter', {status: 'enter', option: option}, buttonStatusR).bind('mouseleave', {status: 'leave', option: option}, buttonStatusR);;
}
/*** END FUNCTIONS TO CONTROL THE FOLLOWING THREE ITEMS: Rotating carousel on homepage, 3 panel slider on homepage, 3 panel slider and viewer on Virtual Tours ***/

/*** BEGIN MAIN MENU FUNCTIONALITY ***/
/* Functions to control Main Menu functionality */
var dropMenu = null;
var timeout = 250;
var closer = 0;
function menuClose() {
	if (dropMenu) {
		dropMenu.css('visibility', 'hidden');
	}
}
function menuTimerCancel() {
	if (closer) {
		window.clearTimeout(closer);
		closer = null;
	}
}
function swapIn(event) {
	menuTimerCancel();
	menuClose();
	dropMenu = $(this).find('div').css('visibility', 'visible');
}
function swapOut(event) {
	closer = window.setTimeout(menuClose, timeout);
}
/*** END MAIN MENU FUNCTIONALITY ***/


/*** Main Functionality ***/
$(document).ready(function() {
	var option = new optionSlider();	// create the option object
	$('ul.mainMenuList > li').bind('mouseover', swapIn).bind('mouseout', swapOut);
	initSlider(option);	// initialize the slider
	initCarousel(option); //initialize the carousel
	if ( $('div.virtualSliderWrapper').find('ul').attr('class') ) {
		option.isVirtual = true;
		initVirtualSlider(option); //initialize the virtual slider
		initVcarousel(option); //initialize the virtual carousel container
	}
});

$(document).ready(function() { 
    //mover personaliztion
    $ektron(".EktronPersonalizationWrapper").prependTo("#secondaryLinks");
    //end move personalization
});

/***Hide all map elements ***/
$(document).ready(function() {
$ektron('#__SearchPane').hide();
$ektron('#dvSearch').hide();
$ektron('#__findtitle').hide();
$ektron('#__ChkControl').hide();
$ektron('#dvDirection').hide();
$ektron('#dvCheck').hide();
$ektron('#dvFind').hide();
$ektron('#__SearchTxtResultPane').hide();
$ektron("#dvSearch").css("display", "none");
});

//** Print Function ***/
//function printCafeMenu() {
//    var w = window.open('', '', 'width=500,height=500');
//    w.document.open("text/html");
//    w.document.write(document.getElementById('printArea').innerHTML);
//    w.document.close();
//    w.print();
//}

//** Print Function ***/
function printpage() {
    window.print();
}

//** Search Function ***/
function searchKeypress() {
    $ektron(".searchBox").keypress(function(e) {
        var evt = e ? e : window.event;
        var code = evt.keyCode || evt.which;
        if (code == 13) {
            var text = $ektron(".searchBox").val();
            $ektron("#container .searchButton").trigger("click");
            e.preventDefault();
        //  alert("/Search.aspx?searchtext=" + text);
        //    window.top.location.replace("http://www.google.com");
        }
    });
}

/* all additional functionality */
$(document).ready(function () {
    // attach keypress event to search box - jessie 6/21/2011
    // searchKeypress();
    // if there is only 1 secondary link block, then center the existing one
    if ($('div.portageSecondaryLinks > div').size() <= 1) {
        $('div.portageSecondaryLinks').find('div.portageSecondaryLink1').css('float', 'none').css('margin', '0px auto 0px auto');
    }

    /*begin menu script for positioning*/
    $(".topLevelMenuItem").hover(function () {
        var positionOfLiParent = $(this).position();
        //alert("the position is " + positionOfLiParent.left);
        var ulLength = $(this).find("ul").length;
        //alert("there are " + ulLength + " ul tags");
        if ((positionOfLiParent.left) > 600 && (ulLength == 2)) {
            $(this).addClass("lastMenuItem");
        }
        if ((positionOfLiParent.left) > 600 && (ulLength < 2)) {
            $(this).children("div").addClass("oneColumnPortageMenu");
        }
        if (ulLength < 2) {
            $(this).children("div").addClass("oneColumnPortageMenu");
        }
    });
    /*end menu script for positioning*/

    function styleUlTags() {
        var pageBuilder = $(".PBViewing").length; //check to see if we are using a pagebuilder template
        if (pageBuilder < 1) {
            $(".bodyContent").addClass("portageFormatListTags");
        }
    }

    styleUlTags();

});



$(document).ready(function() {
    $('li.topLevelMenuItem').find('div').each(function() {
        var numMenu = 0;
        var numCol = 0;
        var col1 = 0;
        var col2 = 0;

        //set the width of a column to 210 if there's only two menu blocks in it
        $(this).find('ul.listLevel_2').each(function() {
            $(this).find('ul').each(function() {
                numMenu++;
            });
            if ((numMenu == 2) && ($(this).width() == 420)) {
                $(this).width(210);
            }
            numCol++;
        });

        //determine which column is longer, and set the borders appropriately
        col1 = $(this).find('ul.listLevel_2:first').height();
        col2 = $(this).find('ul.listLevel_2:last').height();

        if (col1 >= col2) {
            if (numCol > 1) {
                $(this).find('ul.listLevel_2:last').css('border-right', 'none');
            }
            else {
                $(this).find('ul.listLevel_2:first').css('border-right', 'none');
            }
        }
        else {
            $(this).find('ul.listLevel_2:first').css('border-right', 'none');
            $(this).find('ul.listLevel_2:last').css('border-right', 'none').css('border-left', '1px solid #d0cec9');
        }
        col1 = 0;
        col2 = 0;
        numCol = 0;
        numMenu = 0;
    });
	
	$('div.homepageClearDiv').parent().parent().css('padding', '0px');
	if ( $('div.interactiveBlock-1').html() ) {
		$('div.secondaryBlockContent').find('p').each(function() {
			if ($(this).text() == '') {
				$(this).remove();
			}
			else if ($(this).text().length > 150) {
				//alert( $(this).text().length );
				var subString = $(this).text().substring(0, 150);
				subString = subString + '...';
				$(this).text(subString);
			}
		});
	}
});                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
