var numberOfThumbnails=9;
var thumbnailsPerPanel=4;
var thumbnailWidth=70;
var thumbnailIndex=1;
var thumbnailInProgress=false;

var textIndex=0;
var successInterval;
var mainAnimation=true;
var highlightedButton='btn1';
$(document).ready(function() {
    var selector;
	// hide all 5 of the main panels
	$('#topPanels > div').hide();
	// show default panel (normally 1st panel)
	$('#topPanels > div').filter(':eq(0)').show();
	// zero the opacity for all the timeline overlay images
	$('.tiOverlay').css({opacity:0.0});
	// hide all but text that accompanies the success photos, except the 1st
	$('#textShow div').filter(':not(:first)').css({opacity:0.0});
	$('#textShow').show();
	// mouseover the main images suspends the animation
	$('#successStories').hover(
	    function() {
	        mainAnimation=false;
	    },
	    function() {
	        mainAnimation=true;
	    }
	);
	// 4 of 5 main buttons are slightly transparent
	highlightedButton='btn1';
	$('#btn2').css({opacity:0.5});
	$('#btn3').css({opacity:0.5});
	$('#btn4').css({opacity:0.5});
	$('#btn5').css({opacity:0.5});
	// set up mouseovers for the 5 main buttons
	$('.button').hover(
		function() {
			// slide image up on mouseover
			$("#" + this.id).css('z-index', 99);
			$("#" + this.id).animate({top: "-=30px"}, 400);
		},
		function() {
			// slide image back down on mouseout
			$("#" + this.id).animate({top: "+=30px"}, 400);
		}
	);
	// handle click on 1 of the 5 main panels
	$('.button').click(function() {
        $('#'+highlightedButton).css({opacity:0.5});
        $('#'+this.id).css({opacity:1.0});
        highlightedButton=this.id;	    
		$('#topPanels > div').hide();
		var p=parseInt(this.id.substring(3,4));
		p=p-1;
		$('#topPanels > div:eq('+p+')').show();
	});
	$('.tiOverlay').hover(
		function() {
			$('.tiNarrative').hide();
			var p=parseInt(this.id.substring(9),10);
			var x=''+p;
			if (p<10) x='0'+x;
			x='#tiNarrative0'+x;
			$(x).show();
		},
		function() {
			$('.tiNarrative').hide();
		}
	);
	// handle the click on the left arrow
	$('#thumbnailLeft').click(function() {
	    if (thumbnailInProgress)
	        return;
	    var left;
	    var i;
	    var k=increment(thumbnailIndex,thumbnailsPerPanel);
        var x='';
	    left=0-thumbnailWidth*(thumbnailsPerPanel+1);
	    for (i=5;i<=numberOfThumbnails;i++) {
	        $('#tg'+k).css({left:left});
	        k=increment(k,1);
	        left+=thumbnailWidth;
	    }
        for (i=1;i<=9;i++) {
            x=x+i+' : '+$('#tg'+i).css('left')+'\n';
        }
        thumbnailIndex=decrement(thumbnailIndex,thumbnailsPerPanel);
        $('.tg').animate({left: '+=280'}, {duration : 1000});
        thumbnailInProgress=true;
        setTimeout("thumbnailInProgress=false",1100);
	});
	// handle the click on the right arrow
	$('#thumbnailRight').click(function() {
	    if (thumbnailInProgress)
	        return;
	    var left;
	    var i;
	    var k=increment(thumbnailIndex,thumbnailsPerPanel);
        var x='';
	    left=thumbnailWidth*thumbnailsPerPanel;
	    for (i=5;i<=numberOfThumbnails;i++) {
	        $('#tg'+k).css({left:left});
	        k=increment(k,1);
	        left+=thumbnailWidth;
	    }
        for (i=1;i<=9;i++) {
            x=x+i+' : '+$('#tg'+i).css('left')+'\n';
        }
        thumbnailIndex=increment(thumbnailIndex,thumbnailsPerPanel);
        $('.tg').animate({left: '-=280'}, {duration : 1000});
        thumbnailInProgress=true;
        setTimeout("thumbnailInProgress=false",1100);
	});
	// set up the thumbnail image functionality
	$('.tg').click(function() {
		clearInterval(successInterval);
		setSuccessImage(this.id.substring(2));
	});
	// product accordion
    lastBlock = $("div.beLFL #a2");
    maxWidth = 390;
    minWidth = 50;	

    $("div.beLFL ul li a").hover(function(){
        $(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400 });
    	$(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
	    lastBlock = this;
    });
});	
$(function() {
	successInterval=setInterval("rotateImages()", 5000);
});
function increment(index,increment) {
    index=index+increment;
    if (index>numberOfThumbnails) {
        index=index % numberOfThumbnails;
    }
    return index;
}   
function decrement(index,increment) {
    index=index-increment;
    if (index<1) {
        index=index + numberOfThumbnails;
    }
    return index;
}   
function rotateImages() {
    if (mainAnimation==true) {
        var selector;
	    var oCurPhoto = $('#photoShow div.current');
	    var oNxtPhoto = oCurPhoto.next();
	    if (oNxtPhoto.length == 0)
		    oNxtPhoto = $('#photoShow div:first');
	    oCurPhoto.removeClass('current').addClass('previous');
	    oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({opacity:1.0},1000,
		    function() {
			    oCurPhoto.removeClass('previous');
		    });
    		
	    selector='#textShow div:eq('+textIndex+')';
	    var oCurText = $(selector);
	    textIndex+=1;
	    if (textIndex>8)
	        textIndex=0;
	    var oNxtText = $('#textShow div:eq('+textIndex+')');
	    if (oNxtText.length == 0)
		    oNxtText = $('#textShow div:first');
	    oCurText.css({ opacity: 1.0 }).animate({opacity:0.0},100);
	    oNxtText.css({ opacity: 0.0 }).animate({opacity:1.0},1000);
	}
}
function setSuccessImage(index) {
	// specifically target one particular success story
	var oCurPhoto = $('#photoShow div.current');
	var oNxtPhoto = $('#photoShow div:eq('+(index-1)+')');
	oCurPhoto.removeClass('current').addClass('previous');
	oNxtPhoto.css({ opacity: 0.0 }).addClass('current').animate({opacity:1.0},1000,
		function() {
			oCurPhoto.removeClass('previous');
		});
	var oCurText = $('#textShow div:eq('+textIndex+')');
	var oNxtText = $('#textShow div:eq('+(index-1)+')');
	oCurText.css({ opacity: 1.0 }).animate({opacity:0.0},100);
	oNxtText.css({ opacity: 0.0 }).animate({opacity:1.0},1000);
	textIndex=index-1;
}
function Handle_ss_read_more() {
	// handle click on success story "read more"
	location='successstories.aspx?id='+SSTT[textIndex];
}
var win = null;
function p11pop(theURL,winName,features,w,h,centered) { //v2.0
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	if (centered == 'yes') {
	realFeatures = (features+',top='+TopPosition+',left='+LeftPosition+',width='+w+',height='+h);
	}
	else {
	realFeatures = (features+',width='+w+',height='+h+'left=0,top=0');
	}
	win = window.open(theURL,winName,realFeatures);
}
function worksShow(index) {
    var x,img;
    $('#w1').hide();
    $('#w2').hide();
    $('#w3').hide();
    $('#w4').hide();
    $('#w'+index).show();
    //if (index==1) x='images/works/MedicalSupervision.gif';
    //if (index==2) x='images/works/LifestyleFocus.gif';
    //if (index==3) x='images/works/NurseCoaching.gif';
    //if (index==4) x='images/works/NutritionEducation.gif';
    //img=document.getElementById('worksDetails');
    //img.src=x;
}    
function playVideo(vid)
{
    var player = document.getElementById("videoPlayer");
    player.src = "video/" + vid;
    player.style.visibility = "visible";
    player.style.display = "block";
}

