// Jon Raasch simple jQuery slideshow
// http://jonraasch.com/blog/a-simple-jquery-slideshow

function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}


/* JACKET SCROLLIN FUNCTIONS */

function shunt(inc, origStart, numresults) { 
// inc can only be +/- 1

// is starter set?
if(undefined===window.starter){
  window.starter = origStart;
  }

if (inc>0) {
	document.getElementById("jkt"+(starter)+"td").style.display = "none";
  	document.getElementById("jkt"+(starter+6)+"td").style.display = "";
       starter=starter+1;
    } else {
	document.getElementById("jkt"+(starter+5)+"td").style.display = "none";
	document.getElementById("jkt"+(starter-1)+"td").style.display = "";
       starter=starter-1;
    }

    if (starter==0) { document.getElementById("shuntleft").innerHTML = " ";
         } else { document.getElementById("shuntleft").innerHTML =
             '<img src="/images/shunt-l.jpg" border="0" title="more this way"  style="cursor: pointer;" onclick="javascript:shunt(-1,'+starter+','+numresults+');">'; }

    if (starter==(numresults-6)) { document.getElementById("shuntright").innerHTML = " ";
         } else { document.getElementById("shuntright").innerHTML = 
             '<img src="/images/shunt-r.jpg" border="0" title="more this way"  style="cursor: pointer;" onclick="javascript:shunt(1,'+starter+','+numresults+');">'; }

return false;
 } // end f(shunt)

function shuffle(numresults) {
   for (i=0;i<=numresults;i++){
     do {
       n1 = String( Math.round((numresults-1)*Math.random()) );
       n2 = String( Math.round((numresults-1)*Math.random()) );
       } while (n1==n2);
       saviourRef = document.getElementById("jkt"+n1+"td").innerHTML ;
	document.getElementById("jkt"+n1+"td").innerHTML = document.getElementById("jkt"+n2+"td").innerHTML;
       document.getElementById("jkt"+n2+"td").innerHTML = saviourRef;
     }
return false;
} // end f(shuffle)

/* DIV toggler */

function toggleDiv(id) {
  var e = document.getElementById(id);
  if(e.style.display == 'none')
  e.style.display = 'block';
  else
  e.style.display = 'none';
}

// INQUIRY FORM functions

function validateInquiry(theForm) {

if (theForm.NAME.value == "") { // must have a name!
  alert("Please enter your name")
  theForm.NAME.focus()
  theForm.NAME.select()
  return false
  }

if ( (theForm.EMAIL.value == "") && (theForm.PHONE.value == "") ) { // must have at least one method of contact
  alert("Please enter your email address, or phone number")
  theForm.EMAIL.focus()
  theForm.EMAIL.select()
  return false
  }

if ( theForm.EMAIL.value!="" ) { // if email is entered check to see it's valid
	if (!validEmail(theForm.EMAIL.value)) {
		alert("You entered an invalid email address. Please try again...")
		theForm.EMAIL.focus()
		theForm.EMAIL.select()
		return false
	}
  }

if ( theForm.DETAILS.value == "" ) { // must have at least one method of contact
  alert("Please enter a message")
  theForm.DETAILS.focus()
  theForm.DETAILS.select()
  return false
  }

theForm.SUBMIT.value = "   Submitting   "
// alert("excellent")
return true
}


/* BANNER STUFF */

// bannerSRC initialisation

function startAds()
{

       var saviourSrc = "";
       var saviourRef = "";
       saviourSrc = document.getElementById("ad1").src;
       saviourRef = document.getElementById("adref1").href;
	document.getElementById("ad1").src = document.getElementById("ad2").src;
	document.getElementById("adref1").href = document.getElementById("adref2").href;
	document.getElementById("ad2").src = document.getElementById("ad3").src;
	document.getElementById("adref2").href = document.getElementById("adref3").href;
	document.getElementById("ad3").src = saviourSrc;
	document.getElementById("adref3").href = saviourRef;
	f00 = setTimeout("startAds()", 3000);	
	// rotateAds();
}

function rotateAds()
{
	calcIndex();
	// alert(_ad1 + ", " + _ad2 + ", " + _ad3);
	
//	while(bannerSRC[_ad1] == null) { _ad1 = Math.floor(Math.random() * bannerSRC.length); }
//	while(bannerSRC[_ad2] == null) { _ad2 = Math.floor(Math.random() * bannerSRC.length); }	
//	while(bannerSRC[_ad3] == null) { _ad3 = Math.floor(Math.random() * bannerSRC.length); }	
		
		setTransition();
		document.getElementById("ad1").src = adRoot + bannerSRC[_ad1];
		document.getElementById("ad2").src = adRoot + bannerSRC[_ad2];
		document.getElementById("ad3").src = adRoot + bannerSRC[_ad3];
		playTransition();

	f00 = setTimeout("rotateAds()", 3000);	
}

function setTransition()
{
   if (document.all)
	{
      document.getElementById("ad1").filters.revealTrans.Transition=Math.floor(Math.random()*24); 
      document.getElementById("ad1").filters.revealTrans.apply();
      document.getElementById("ad2").filters.revealTrans.Transition=Math.floor(Math.random()*24); 
      document.getElementById("ad2").filters.revealTrans.apply();
      document.getElementById("ad3").filters.revealTrans.Transition=Math.floor(Math.random()*24); 
      document.getElementById("ad3").filters.revealTrans.apply();
	}
}


function playTransition()
{
   if (document.all)
	{
		document.getElementById("ad1").filters.revealTrans.play();
		document.getElementById("ad2").filters.revealTrans.play();
   	document.getElementById("ad3").filters.revealTrans.play();
	}
}

function urlJump(whichAd)
{
	var jumpPointer = eval("_" + whichAd.id);
	var jumpURL = adRoot + bannerURL[jumpPointer];
	window.location.href = jumpURL;
}

function displayStatusMsg(whichAd) {
	var adPointer = eval("_" + whichAd.id);	
   window.status = adRoot + bannerURL[adPointer];
   return true;
}

function defaultStatus() { 
   window.status = 'Done. Welcome to Melbourne University Bookshop.';
   return true;
}

function calcIndex()
{
	_ad1 = Math.floor(Math.random() * (bannerSRC.length - 1));
	_ad2 = _ad1 + (1 + Math.floor(Math.random() * 3));
	_ad3 = _ad1 - (1 + Math.floor(Math.random() * 3));
	
	if(_ad2 < 0 ) { _ad2 += bannerSRC.length; }
	if(_ad2 > (bannerSRC.length-1) ) { _ad2 -= bannerSRC.length; }

	if(_ad3 < 0 ) { _ad3 += bannerSRC.length; }
	if(_ad3 > (bannerSRC.length-1) ) { _ad3 -= bannerSRC.length; }
}
