/**
 * Defines functions used on the Site's Home Page; specifically, the
 * management of the tab-set.
 *
 * To make the tab-set operate, you also need to add "VWEB.HomePage.initTabs"
 * to the set of page initialization functions.
 */
if ( !VWEB )  var VWEB={};
if ( !VWEB.HomePage )  VWEB.HomePage = {};

VWEB.HomePage.curTab = null;
VWEB.HomePage.tabPending = null;
VWEB.HomePage.tabDelay = null;
VWEB.HomePage.delayMillis = 600;
//  these positions/dimensions over-ride the CSS spec for the slide-in div,
//  however... the "top" for the slidein is really dependent on the bottom
//  of the image, which is about 400!
VWEB.HomePage.slideId = "guaranteed";    // was "hotmenu"
VWEB.HomePage.slideLeft = 700;
VWEB.HomePage.slideWidth = 230;
VWEB.HomePage.slideHeight = 306;

VWEB.HomePage.initTabs = function() {
    var self = VWEB.HomePage;
    var list = [ "wwloc", "info", "rsrc", "why" ];
    var curname=list[0], el;
    //  set tab event handlers, noting associated content for each,
    //  and note the name for the "selected" tab (if any)
    for ( i=0; i<list.length; i++ ) {
        el = $( list[i]+"-tab" );
	if ( !el )  continue;
	el.onmouseover = self.overTab;
	el.onmouseout = self.outTab;
	el.onclick = self.activateTab;
	if ( el.hasClassName("selected-tab") )  curname = list[i];
	el.theContent = $( list[i]+"-content" );
    }
    //  note the currently-selected tab
    el = self.curTab = $( curname+"-tab" );
    if ( !el ) return;
    //  make sure that tab and it's content are "selected"
    if ( !el.hasClassName("selected-tab") )  el.addClassName("selected-tab");
    el = el.theContent;
    if ( el  &&  !el.hasClassName("selected-content") ) {
        el.addClassName("selected-content");
    }
    //  also initialize the rolldowns for the "why" words
    VWEB.HomePage.initWhyWords();
}
//  initialization for the "hot courses" slide-in
VWEB.HomePage.initSlidein = function() {
    var self = VWEB.HomePage;
    var el = $(self.slideId);
    if ( !el )  return;
    window.setTimeout( self.startSlidein, 2000 );
    /*  =================================================
     *  Following is the implementation for the "hot courses" menu,
     *  which has been removed temporarily in favor of the "guaranteed
     *  courses" link implemented above
    //  if there's no items in the menu, don't even start the slider,
    //  and then it'll never become visible.
    var n = el.className.indexOf("length-");
    if ( n < 0 )  return;
    n = el.className.substring(n+7) - 0;
    if ( n < 1 )  return;
    //  otherwise, let 'er rip
    window.setTimeout( self.startSlidein, 2000 );
    $("hotlink").onmouseover = self.showHotlist;
    $("hotclose").onclick = self.hideHotlist;
    ========================================================== */
}

//  invoked as handler of mouse-over on a tab
VWEB.HomePage.overTab = function() {
    var self = VWEB.HomePage;
    if ( this == self.tabPending )  return;
    if ( this == self.curTab  )  { self.outTab(); return; }
    self.tabPending = this;
    self.tabDelay = window.setTimeout( self.activateTab, self.delayMillis );
}
//  invoked as handler of mouse-over on a tab, or can be invoked
//  directly just to be sure there's no pending delays.
VWEB.HomePage.outTab = function() {
    var self = VWEB.HomePage;
    if ( self.tabDelay >= 0 )  window.clearTimeout( self.tabDelay );
    self.tabDelay = -1;
    self.tabPending = null;
}
//  can be invoked by a delay or as handler of a tab click, but
//  in the latter case, there better have already been a mouse-over
//  that will set "tabPending" (but that seems like a sure thing!)
VWEB.HomePage.activateTab = function() {
    var self = VWEB.HomePage;
    if ( self.tabPending == self.curTab )  return;
    if ( !self.tabPending  ||  self.tabDelay < 0 )  return;
    self.tabDelay = -1;
    var curtab = self.curTab;
    if ( curtab ) {
        curtab.removeClassName( "selected-tab" );
	if ( curtab.theContent )
			curtab.theContent.removeClassName("selected-content");
    }
    curtab = self.curTab = self.tabPending;
    if ( curtab ) {
        curtab.addClassName( "selected-tab" );
	if ( curtab.theContent )
			curtab.theContent.addClassName("selected-content");
    }
    self.tabPending = null;
}
//  clears pending delayed tab activation
VWEB.HomePage.clearDelay = function() {
    var self = VWEB.HomePage;
    if ( self.tabDelay >= 0 )  self.clearTimeout( self.tabDelay );
    self.tabDelay = -1;
    self.tabPending = null;
}




VWEB.HomePage.initWhyWords = function() {
    var self = VWEB.HomePage;
    var list = [ "quality", "consistency", "commitment" ];
    for ( i=0; i<list.length; i++ ) {
        el = $( "why-"+list[i] );
	if ( !el )  continue;
	el.onclick = self.ignoreClick;
	el.onmouseover = self.handleWhyWord;
	el.onmouseout = self.handleWhyWord;
    }
}
VWEB.HomePage.ignoreClick = function( evt ) {
    if ( !evt )  evt = window.event;
    this.blur();
    if ( evt.preventDefault ) evt.preventDefault();
    else evt.returnValue = false;
    evt.cancelBubble = true;
}
VWEB.HomePage.handleWhyWord = function() {
    var self = VWEB.HomePage;
    var div = this.rolldownDiv;
    //  if this "word" has never been handled, add state info to it
    if ( !div ) {
	div = this.parentNode.getElementsByTagName("div")[0];
        this.rolldownDiv = $(div);
	div.inAction = 0;		// not rolling now
	div.rolldownState = 0;		// currently closed
	div.rolldownPendingEvent = 0;	// nothing pending
    }
    //  if rolldown is "in action", we need to defer the request until
    //  the current one completes (or Scriptaculous screws things up!)
    if ( div.inAction ) {
	//  if there was already a pending event just undo it
        if ( div.rolldownPendingEvent != 0 ) {
	    div.rolldownPendingEvent = 0;
	    return;
	}
        div.rolldownPendingEvent = 2 - div.rolldownState;
	return;
    }
    //  otherwise, kick off immediate reversal of the curent state
    div.inAction = 1;
    div.rolldownPendingEvent = 0;
    if ( div.rolldownState == 0 ) {
	Effect.SlideDown( div, {duration:0.75, afterFinish:self.doneHandling} );
    } else {
	Effect.SlideUp( div, {duration:0.75, afterFinish:self.doneHandling} );
    }
}
VWEB.HomePage.doneHandling = function( eff ) {
    var self = VWEB.HomePage;
    var div = eff.element;     // for some effects, could be eff[0].element!
    //  set state to what we just finished
    div.rolldownState = 1 - div.rolldownState;
    //  if there's a pending event, do it now
    if ( div.rolldownPendingEvent != 0 ) {
        if ( div.rolldownPendingEvent == 1 ) {		// request to open
	    Effect.SlideDown( div, {duration:0.75, afterFinish:self.doneHandling} );
        } else if ( div.rolldownPendingEvent == 2 ) {	// request to close
	    Effect.SlideUp( div, {duration:0.75, afterFinish:self.doneHandling} );
	}
	div.rolldownPendingEvent = 0;
	return;
    }
    //  otherwise, clear the state
    div.inAction = 0;
}


VWEB.HomePage.startSlidein = function() {
    var hp = VWEB.HomePage;
    var el = $(hp.slideId);
    // height morphing only for "hotmenu":
    // el.style.top=(400-hp.slideHeight)+"px";
    // el.style.height=hp.slideHeight+"px";
    // $("hotlink").style.top = (hp.slideHeight-40)+"px";
    el.style.width = "1px";
    el.style.left = (hp.slideLeft+hp.slideWidth-1)+"px";
    el.style.display="block";
    var style = "width:"+hp.slideWidth+"px; left:"+hp.slideLeft+"px;";
    new Effect.Morph( el, {style:style, duration:3.0} );
}


//  following methods for the "hot course list" will only execute when
//  there's a mouse-over on the slider, so it doesn't hurt to leave
//  them in here even when the hotmenu is not implemented
VWEB.HomePage.hideHotlist = function() {
    var hp = VWEB.HomePage;
    var list = $("hotlist");
    if ( list ) {
        var style="height:1px; top:"+(hp.slideHeight-10)+"px;";
        new Effect.Morph( list, {style:style, duration:0.75,
			         afterFinish:hp.reallyHideHotlist} );
    }
}
VWEB.HomePage.reallyHideHotlist = function() {
    var list = $("hotlist");
    //  this is silliness, but it seems to make the leftover garbage
    //  disappear on IE until I have time to do it right!
    list.style.height = "1px";
    list.style.top = (VWEB.HomePage.slideHeight-10)+"px";
    list.style.display="block";
    var style = "height:1px; top:"+(VWEB.HomePage.slideHeight-10)+"px";
    new Effect.Morph( list, {style:style,duration:0.01,
	                     afterFinish:VWEB.HomePage.zap} );
}
VWEB.HomePage.zap = function() {
    $("hotlist").style.display = "none";
}
VWEB.HomePage.showHotlist = function() {
    var list = $("hotlist");
    if ( list ) {
        list.style.height = "1px";
        list.style.top = (VWEB.HomePage.slideHeight-10)+"px";
        list.style.display="block";
        var style = "height:"+(VWEB.HomePage.slideHeight-10)+"px; top:0px;";
        new Effect.Morph( list, {style:style, duration:1.0} );
    }
}


// Following added in Sept '11, to do the "animated logo picture"

//  adjustable parameters to control the positioning and animation.
MVI = {	cHeight: 292, cWidth: 930, nImg: 12, speed: 30, maxIncrX: .7,
	images: [], order: [ 4,1,7,11,0,8,3,10,9,2,5,6 ], doAnimate: false };



    //  This method initializes the properties of an image passed to it,
    //  including a "random" (but evenly spaced) position in the container,
    //  and a random increment for the horizontal animation, as well as
    //  some styling to make it behave like a link
    //
    MVI.initialPosition = function( img ) {
	//  push this image onto the array
	var i = MVI.images.length;
	MVI.images[i] = img;
	//  set it's properties
	img.maxTop = MVI.cHeight - img.height;
        img.maxLeft = MVI.cWidth - img.width;
        img.style.position = "absolute";
        img.style.display = "block";
	img.style.cursor = "pointer";
	img.onclick = MVI.clickLogo;
	img.onmouseover = MVI.beginLogoHover;
	img.onmouseout = MVI.endLogoHover;
	if ( i == 0 ) {
	    y = 5;
	} else {
	    y = (MVI.cHeight-40)/MVI.nImg * i;
	}
	if ( y > img.maxTop )  y = img.maxTop - 5;
	x = (MVI.cWidth-50)/MVI.nImg * MVI.order[i];
	if ( x < 5 )  x = 5;
	else if ( x > img.maxLeft )  x = img.maxLeft - 5;
	img.posX = x;
	img.style.left = Math.round(x)+"px";
        img.incrX = MVI.maxIncrX*Math.random()
	if ( i % 2 )  img.incrX *= -1;
	img.style.top = Math.round(y)+"px";
    }



    //  This method initializes all the images, and starts the animation
    //
    MVI.initialize = function() {
    // MVI.initialize = function() {
	//  get the images defined in the container, and make sure
	//  there's at least as many as the MVI object expects.
        var container = document.getElementById("picture-container");
        var imgs = container.getElementsByTagName( "img" );
	var nimg = imgs.length;
        // KH.log( "will be using "+MVI.nImg+" of "+nimg+" images" );
	if ( nimg < MVI.nImg )  MVI.nImg = nimg;
        //  mark all the images as unused and hide them
        for ( var i=0; i<nimg; i++ ) {
	    var img = imgs[i];
	    img.isUsed = false;
            img.style.display = "none";
        }
        //  initialize the right number of randomly chosen images
	for ( i=0; i<MVI.nImg; ) {
	    var j = Math.round( Math.random()*nimg );
	    if ( j == nimg )  continue;
	    if ( imgs[j].isUsed )  continue;
	    MVI.initialPosition( imgs[j] );
	    // KH.log( i+": img #"+j+" ("+imgs[j].src+") at "
	    // 		+imgs[j].style.left+","+imgs[j].style.top
	    // 		+"  incr="+imgs[j].incrX );
	    imgs[j].isUsed = true;
	    i++;
	}
	//  fire up the animation, if enabled
        if ( MVI.doAnimate )  setInterval( MVI.updatePosition, MVI.speed );
    }



    //  This method does the "linking" by using the query string
    //  that can be attached to each logo
    //
    MVI.clickLogo = function() {
        var pos = this.src.indexOf( "?" );
	if ( pos < 0 )  return;
	var href = "/vweb/search?q="+this.src.substring( pos+1 );
	try {
	/*
	if ( document.location.protocol.indexOf("http") == -1 ) {
	    alert( "
	} else {
	*/
	    document.location.href = href;
	} catch ( x ) {
	    alert( "Can't go to '"+href+"'" );
	}
    }



    //  These two methods highlight a logo
    //
    MVI.beginLogoHover = function() {
        this.style.backgroundColor = "rgb(255,255,204)";
	this.style.zIndex = 2;
    }
    MVI.endLogoHover = function() {
        this.style.backgroundColor = "transparent";
	this.style.zIndex = 0;
    }



    //  This method animates the logos.
    //
    MVI.updatePosition = function() {
        for ( var i=0; i<MVI.nImg; i++ ) {
	    var img = MVI.images[i];
	    var half = img.width/2;
	    img.posX += img.incrX;
	    if ( img.posX > img.maxLeft+half ) {
	        img.posX = 5 - half;
	        // img.incrX *= -1;
	    } else if ( img.posX < -half ) {
	        img.posX = MVI.cWidth - half;
	        // img.incrX *= -1;
	    }
	    img.style.left = Math.round(img.posX) + "px";
        }
    }



