///////////////////////////////////////////////////////////
/// Magic Image Rotation								///
///	v 1.0.1												///
/// Copyright 2007, Loyce Bradley Petrey				///
///	All Rights Reserved.								///
/// http://www.EchoChrist.com/MagicImage				///
/// webmaster@EchoChrist.com							///
///														///
///	This script is free to use as long as this notice 	///
/// remains unchanged and intact.					 	///
///														///
/// This program is free software: you can redistribute ///
///	it and/or modify it under the terms of the GNU 		///
///	General Public License as published by the Free 	///
///	Software Foundation. 								///
///														///
/// This program is distributed in the hope that it 	///
/// will be useful, but WITHOUT ANY WARRANTY; without 	///
///	even the implied warranty of MERCHANTABILITY or 	///
///	FITNESS FOR A PARTICULAR PURPOSE.  					///
/// 													///
/// Be Imitators of God as dear children and walk		///
/// in love as Christ also loved us.					///
///	Ephesians 5:1-2										///
///////////////////////////////////////////////////////////

//precache images
image1 = new Image();
image1.src  = "assets/images/bannerads/bannerad_newburyporttoday.jpg";
image2 = new Image();
image2.src  = "assets/images/bannerads/bannerad_northshore.gif";
image3 = new Image();
image3.src  = "assets/images/bannerads/bannerad_nmc.png";
image4 = new Image();
image4.src  = "assets/images/bannerads/bannerad_wnbpnew.png";
image5 = new Image();
image5.src  = "assets/images/bannerads/bannerad_newburyportdevelopment.png";
image6 = new Image();
image6.src  = "assets/images/bannerads/bannerad_newburyportyachtclub.png";
image7 = new Image();
image7.src  = "assets/images/bannerads/bannerad_ringsislandmarina.png";
image8 = new Image();
image8.src  = "assets/images/bannerads/bannerad_newburyportfivecents.jpg";
image9 = new Image();
image9.src  = "assets/images/bannerads/bannerad_iandolism.gif";
image10 = new Image();
image10.src  = "assets/images/bannerads/bannerad_dunkindonuts.jpg";



var ImageLoad = 
[
['http://www.newburyport-today.com','assets/images/bannerads/bannerad_newburyporttoday.jpg',''],
['http://nshoremag.com/','assets/images/bannerads/bannerad_northshore.gif',''],
['http://www.newburyportmothers.com/','assets/images/bannerads/bannerad_nmc.png',''],
['http://www.wnbp.com/','assets/images/bannerads/bannerad_wnbpnew.png',''],
['http://newburyportdevelopment.com/','assets/images/bannerads/bannerad_newburyportdevelopment.png',''],
['http://www.newburyportyachtclub.com/','assets/images/bannerads/bannerad_newburyportyachtclub.png',''],
['http://www.ringsislandmarina.com/','assets/images/bannerads/bannerad_ringsislandmarina.png',''],
['http://www.newburyportbank.com/','assets/images/bannerads/bannerad_newburyportfivecents.jpg',''],
['http://iandolisupermarketing.com//','assets/images/bannerads/bannerad_iandolism.gif',''],
['http://www.dunkindonuts.com//','assets/images/bannerads/bannerad_dunkindonuts.jpg',''],
];



var ImageCount		= 10;			//  *****  Change this to the total number of images loaded above  ***** 		//	
var ImageDelay		= 5000;			//  *****  Set this to the delay interval desired.  5000 = 5 seconds.			// 
var LinkTarget		= "_blank"		//  *****  Defines where you want linked page to open. _self, _blank, _top, etc	//
var ImageIndex		= 0;			//  DO NOT ALTER	//
var FirstLoad 		= 0;			//  DO NOT ALTER	//
var QuickStartID 	= 0;  			//  DO NOT ALTER	//
var htmlString 		= ""			//  DO NOT ALTER 	//
var randomnumber	= 0;			//  DO NOT ALTER	//
var countBanners	= 0;



//  This function rotates the banner  //
function ImageChange()
{	

	countBanners += 1;
	
	// TOP BANNER AD
	// SJB: weight the "Newburyport 5 cents bank" ad to appear every 5th time, starting with #2
	// SJB: weight the "Dunkin Donuts" ad to appear every 5th time, starting with #4
	if(((countBanners+3)%5)==0) {
		ImageIndex = 7;
	} else {

		if(((countBanners+1)%5)==0) {
			ImageIndex = 9;
		} else {
			ImageIndex = Math.floor(Math.random()*ImageCount);
			
			// SJB: Certain banner ads are relegated only to appear on the bottom of the page, 
			// so check for those and get another if necessary.  Loop until a good one is found.
			var i=0;
			while (i<1) {
				if (ImageIndex == 0 || ImageIndex == 1 || ImageIndex == 2 || ImageIndex == 5) {
					ImageIndex = Math.floor(Math.random()*ImageCount);
				} else {
					i++;
				}
			}
		}
	}
	
	htmlString = '<center>';
	htmlString = htmlString + '<font face = "Verdana" size="2">';		//  Font and Font Size for caption may be changed here	//
	htmlString = htmlString +'<a target="';
	htmlString = htmlString + LinkTarget;
	htmlString = htmlString + '" href="';
	htmlString = htmlString + ImageLoad[ImageIndex][0];
	htmlString = htmlString + '"><img border="0" src="';				//  Image border size may be changed here				//	
	htmlString = htmlString + ImageLoad[ImageIndex][1];
	htmlString = htmlString + '"></a><br>';
	htmlString = htmlString + ImageLoad[ImageIndex][2];
	htmlString = htmlString + '</font>';
	htmlString = htmlString + '</center>';		

	document.getElementById('MagicImage').innerHTML = htmlString; 				

	// BOTTOM BANNER AD
	// SJB: weight the "Newburyport 5 cents bank" ad to appear every 5th time, starting with #4 (opposite of above)
	// SJB: weight the "Dunkin Donuts" ad to appear every 5th time, starting with #2 (opposite of above)
	if(((countBanners+1)%5)==0) {
		ImageIndex = 7;
	} else {

		if(((countBanners+3)%5)==0) {
			ImageIndex = 9;
		} else {
			ImageIndex = Math.floor(Math.random()*ImageCount);
		}
	}

	htmlString = '<center>';
	htmlString = htmlString + '<font face = "Verdana" size="2">';		//  Font and Font Size for caption may be changed here	//
	htmlString = htmlString +'<a target="';
	htmlString = htmlString + LinkTarget;
	htmlString = htmlString + '" href="';
	htmlString = htmlString + ImageLoad[ImageIndex][0];
	htmlString = htmlString + '"><img border="0" src="';				//  Image border size may be changed here				//	
	htmlString = htmlString + ImageLoad[ImageIndex][1];
	htmlString = htmlString + '"></a><br>';
	htmlString = htmlString + ImageLoad[ImageIndex][2];
	htmlString = htmlString + '</font>';
	htmlString = htmlString + '</center>';		

	document.getElementById('MagicImage2').innerHTML = htmlString; 				


	if(FirstLoad == 0)						//  Determines if this is the first time function has run.   // 
	{
	SlowFinish();
	}

	//alert('');
}
//  End Function  //


//  This function ensures first banner is displayted without a delay  //
function  QuickStart()
{
	QuickStartID=setInterval("ImageChange()", 1000);
}
//  End Function  //																		


//  This function sets display rate to user defined speed  //
function SlowFinish()
{
	clearInterval(QuickStartID);
	FirstLoad = 1;
	setInterval("ImageChange()", ImageDelay);	 
}
//  End Function  //

QuickStart();

