﻿// JavaScript Document
var goRotateAd1 = true;
var goRotateAd2 = true;
var goRotateAd3 = true;
var goRotateAd4 = true;
var goRotateAd5 = true;
var goRotateAd6 = true;
var goRotateAd7 = true;
var goRotateAd8 = true;
function initListenerAd() {  //here we have the  event listeners for certain items. This makes them active on click for the listed functions

//	var nextList = document.getElementById("nextImage");//attaching click behaviours to the next and previous buttons
//		nextList.style.display='none';
//		nextList.onclick = function() {
//		theId = this.parentNode.parentNode.id;
//		initImageAd('next',theId);
//		return false;
//		}
//	
//	var prevList = document.getElementById("prevImage");//attaching click behaviours to the next and previous buttons
//		prevList.style.display='none';
//		prevList.onclick = function() {
//		theId = this.parentNode.parentNode.id;
//		initImageAd('',theId);
//		return false;
//		}
	    ///alert('in')
	    for(i=1;i<=8;i++)
	    {
	            var add="Add" + i;	            
	            var firstBlock = document.getElementById(add).getElementsByTagName("LI");//making the first list item visible, and starting the rotator script
                if (firstBlock.length > 0)
		        {   		         
		            if(firstBlock.length>1)
		            {
		                for(i1=0;i1<firstBlock.length;i1++)
		                {   
		                    firstBlock[i1].style.display = 'None';
		                }
		            }
		         startstopRotator(i + "",true);
		         if (firstBlock.length > 1) {
	    		    autoRotatorAd(firstBlock[0].parentNode.id);
    	    	    }
		        }
	    }
	    
	    
//	    var firstBlock = document.getElementById("Add1").getElementsByTagName("LI");//making the first list item visible, and starting the rotator script
//	    if (firstBlock.length > 0)
//		{   
//		    firstBlock[0].style.display = 'block';
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    goRotateAd1 = true;
//		    //goRotateAd1=true;
//		    if (firstBlock.length > 1) {		        
//			    autoRotatorAd(firstBlock[0].parentNode.id);
//			    //alert('out');
//		    }
//		}
//		
//	    var firstBlock = document.getElementById("Add2").getElementsByTagName("LI");//making the first list item visible, and starting the rotator script
//		if (firstBlock.length > 0)
//		{   
//		    //alert(firstBlock[0].parentNode.id);
//		    firstBlock[0].style.display = 'block';
//		    //goRotateAd = true;
//		    goRotateAd2 = true; 
//		    
//		    if (firstBlock.length > 1) {
//			    autoRotatorAd(firstBlock[0].parentNode.id);
//		    }
//		    //alert('ose');
//		}
		
}

//var upToAd = 0;
var upToAd1 = 0;
var upToAd2 = 0;
var upToAd3 = 0;
var upToAd4 = 0;
var upToAd5 = 0;
var upToAd6 = 0;
var upToAd7 = 0;
var upToAd8 = 0;
var startAd1 = true;
var startAd2 = true;
var startAd3 = true;
var startAd4 = true;
var startAd5 = true;
var startAd6 = true;
var startAd7 = true;
var startAd8 = true;

function setupToAd(id,value)
{
   //startstopRotator(id,true);
   id=id.replace("Add","");
   
   switch(id)
   {
   case "1":
       upToAd1=value;
       break;
   case "2":           
       upToAd2=value;
       break; 
   case "3":
       upToAd3=value;
       break;
   case "4":           
       upToAd4=value;
       break; 
    case "5":
       upToAd5=value;
       break;
   case "6":           
       upToAd6=value;
       break; 
    case "7":
       upToAd7=value;
       break;
   case "8":           
       upToAd8=value;
       break; 
       
   }
}
function incupToAd(id)
{
   //startstopRotator(id,true);
   id=id.replace("Add","");
   
   switch(id)
   {
   case "1":
       upToAd1=upToAd1 + 1;
       break;
   case "2":           
       upToAd2= upToAd2 + 1;
       break;        
       
   }
}
function getStartup(id)
{
   //alert(id);
   id=id.replace("Add","");
   //alert(id);
   //alert(id); 
   var goRotate;
   switch(id)
   {
   case "1":
       goRotate=upToAd1;
       break;
   case "2":
       goRotate=upToAd2;
       break; 
   case "3":
       goRotate=upToAd3;
       break;
   case "4":
       goRotate=upToAd4;
       break; 
    case "5":
       goRotate=upToAd5;
       break;
   case "6":
       goRotate=upToAd6;
       break; 
    case "7":
       goRotate=upToAd7;
       break;
   case "8":
       goRotate=upToAd8;
       break; 
   }
   //alert(id);
   //alert('go' + goRotate);
   return goRotate;
}
function autoRotatorAd(theId) {	
    
    //alert(theId);
    //alert('t' + theId);
    //alert('h' + getRotator(theId));
    if(getStartAd(theId))
    {   
       initImageStartAd('next',theId);		
	   window.setTimeout("autoRotatorAd('"+theId+"')", 5000);//timeout		
	   
    }
    else
    {          
        if (getRotator(theId))
	        initImageAd('next',theId);		
	    window.setTimeout("autoRotatorAd('"+theId+"')", 5000);//timeout	
	}
}

function stopRotatorAd(i) {

startstopRotator(i,false);
//if(i=='1')
//	goRotateAd = false;
//if(i=='2')
//	goRotateAd1 = false;		
//	
//	alert(goRotateAd);
//	alert(goRotateAd1);
}
function startstopRotator(id,blnType)
{
   //startstopRotator(id,true);
   switch(id)
   {
   case "1":
       goRotateAd1=blnType;
       break;
   case "2":
       goRotateAd2=blnType;
       break; 
    case "3":
       goRotateAd3=blnType;
       break;
   case "4":
       goRotateAd4=blnType;
       break; 
   case "5":
       goRotateAd5=blnType;
       break;
   case "6":
       goRotateAd6=blnType;
       break; 
    case "7":
       goRotateAd7=blnType;
       break;
   case "8":
       goRotateAd8=blnType;
       break; 
   }
}
function setStartAd(id,blnType)
{
   //startstopRotator(id,true);
   id=id.replace("Add","");
   
   switch(id)
   {
   case "1":
       startAd1=blnType;
       break;
   case "2":           
       startAd2=blnType;
       break; 
    case "3":
       startAd3=blnType;
       break;
   case "4":           
       startAd4=blnType;
       break; 
   case "5":
       startAd5=blnType;
       break;
   case "6":           
       startAd6=blnType;
       break;  
    case "7":
       startAd7=blnType;
       break;
   case "8":           
       startAd8=blnType;
       break; 
   }
}
function getStartAd(id)
{
   //alert(id);
   id=id.replace("Add","");
   //alert(id);
   //alert(id); 
   var goRotate;
   switch(id)
   {
   case "1":
       goRotate=startAd1;
       break;
   case "2":
       goRotate=startAd2;
       break; 
    case "3":
       goRotate=startAd3;
       break;
   case "4":
       goRotate=startAd4;
       break; 
    case "5":
       goRotate=startAd5;
       break;
   case "6":
       goRotate=startAd6;
       break; 
    case "7":
       goRotate=startAd7;
       break;
   case "8":
       goRotate=startAd8;
       break; 
   }
   //alert(id);
   //alert('go' + goRotate);
   return goRotate;
}

function getRotator(id)
{
   //alert(id);
   id=id.replace("Add","");
   //alert(id);
   //alert(id); 
   var goRotate;
   switch(id)
   {
   case "1":
       goRotate=goRotateAd1;
       break;
   case "2":
       goRotate=goRotateAd2;
       break; 
   case "3":
       goRotate=goRotateAd3;
       break;
   case "4":
       goRotate=goRotateAd4;
       break; 
   case "5":
       goRotate=goRotateAd5;
       break;
   case "6":
       goRotate=goRotateAd6;
       break; 
   case "7":
       goRotate=goRotateAd7;
       break;
   case "8":
       goRotate=goRotateAd8;
       break; 
   }
   //alert(id);
   //alert('go' + goRotate);
   return goRotate;
}
//function displayNextPrevLinks() {

//	var nextList = document.getElementById("nextImage");//attaching click behaviours to the next and previous buttons
//		nextList.style.display='inline';
//	
//	var prevList = document.getElementById("prevImage");//attaching click behaviours to the next and previous buttons
//		prevList.style.display='inline';
//}



function resumeRotatorAd(i) {		
    startstopRotator(i,true);
}

//function mouseOutEventHandler(e) {	 	       
//     e = e || window.event;
//     var target = e.srcElement || e.target;
//     if (target != this) return;
//     var related = e.relatedTarget || e.toElement;
//     while (related != this && related.nodeName != 'BODY')
//          related = related.parentNode;
//     if (related == this) return;
//   
//     // the rest of the function
//	 
//	goRotateAd = true;	
//	theId = this.parentNode.id;	
//	autoRotatorAd(theId);
//	return false; 
//}

function initImageAd(next, theId) {
    
	if (document.getElementById(theId).getElementsByTagName("LI")) {//checking to see if what we are looking for exists
	    ii = document.getElementById(theId).getElementsByTagName("LI");// and the getting it
//	    alert(ii);
        var up=getStartup(theId);  
              
        //alert(theId);
        //alert(up);
		currentOne = ii[up].id;
		if (next) { 
		    up+=1
			//upToAd += 1;
			if (up > ii.length -1) {
			up = 0;
			}
			nextOne = ii[up];
		}
		else {
			up -= 1;
			if (up < 0) {
				up = ii.length - 1;
			}
			nextOne = ii[up];
			
		}
		setupToAd(theId,up)
		fadeOutAd(currentOne, 100);//the hard work of actually running the functions to change opacity
		setOpacityAd(nextOne, 0);
		//alert(nextOne.id);
		nextOne.style.display = 'block';//all LIs start out as display:none, but once they have their opacity set to 0 they can stay display:block
		imageId = nextOne.id;
		fadeInAd(imageId, 0);
	}
	//displayNextPrevLinks();
}

function initImageStartAd(next, theId) {
    
    if (document.getElementById(theId).getElementsByTagName("LI")) {//checking to see if what we are looking for exists
	    ii = document.getElementById(theId).getElementsByTagName("LI");// and the getting it
		var up=getStartup(theId)
		//alert(up);	
	
	    //alert(theId);
        
        		
		currentOne = ii[up].id;		
		//alert(ii[up].id);
		if (next) { 
		    //alert(up);
		//alert('in');
			up += 1;
			if (up > ii.length -1) {
			up = 0;
			//startAd = false;
			setStartAd(theId,false);
			}
			nextOne = ii[up];
			
			
			
		}
		else {
			up -= 1;
			if (up < 0) {
				up = ii.length - 1;
			}
			nextOne = ii[up];
			
			
			
		}
		
		setupToAd(theId,up);
		fadeOutAd(currentOne, 100);//the hard work of actually running the functions to change opacity
		
		
		
		setOpacityAd(nextOne, 0);
		
		//alert('stop');
		//alert(nextOne.id);
		nextOne.style.display = 'block';//all LIs start out as display:none, but once they have their opacity set to 0 they can stay display:block
		
		
		imageId = nextOne.id;
		fadeInAd(imageId, 0);
	}
	//displayNextPrevLinks();
}

function setOpacityAd(obj, opacity) {//setting the opacity using various browser styles as seen on http://clagnut.com/sandbox/imagefades/
  opacity = (opacity == 100)?99.999:opacity;
  obj.style.filter = "alpha(opacity:"+opacity+")";
  obj.style.KHTMLOpacity = opacity/100;
  obj.style.MozOpacity = opacity/100;
  obj.style.opacity = opacity/100;
}

//Function which actually steps through fading in / out the object the as seen on http://clagnut.com/sandbox/imagefades/
function fadeOutAd(objId,opacity) 
{
	obj = document.getElementById(objId);
	if (opacity >= 0) {
		setOpacityAd(obj, opacity);
		opacity -= 20;
		window.setTimeout("fadeOutAd('"+objId+"',"+opacity+")", 100);
	}
	else 
	{   
		obj.style.display = 'none'
 	}
}

function fadeInAd(objId,opacity) {//Function which actually steps through fading in / out the object the as seen on http://clagnut.com/sandbox/imagefades/
    obj = document.getElementById(objId);
	if (opacity <= 100) {
      setOpacityAd(obj, opacity);
      opacity += 20;
      window.setTimeout("fadeInAd('"+objId+"',"+opacity+")", 100);
    }
  }

function addLoadEventAd(funcAd) { //Simon Willison's exciting new version of the addLoadEvent function. Allows us to attach multiplefunctions to the onload event. http://simon.incutio.com/archive/2004/05/26/addLoadEvent
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = funcAd;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      funcAd();
    }
  }
}

//add functions to the addLoadEvent;

addLoadEventAd(initListenerAd);


