/* add random image */

// id of image container
var imc='marketing';

// array of all background images
var im=new Array();
// the image and link that are written into the
// page are added to the list by the script. don't
// add them here.
// the format is
// im[1]=new Array();
// im[1].image=['path/to/img','alt text of image']
// im[1].link=['link/location','title of link'];
im[0]=new Array();
im[0].image=['img/bm_7x7.jpg','7x7 Readers vote Burger Meister BEST BURGER 2008!']
im[0].link=['http://www.7x7sf.com/eat_drink/restaurants/4836631.html','7x7.com'];

im[1]=new Array();
im[1].image=['img/bm_sfbg.jpg','SF Bay Guardian Readers vote Burger Meister BEST BURGER 2008!']
im[1].link=['http://www.sfbg.com/bob/2008/food.php','www.sfbg.com'];

im[2]=new Array();
im[2].image=['img/bm_sfweekly_2008.jpg','SF Weekly Readers vote Burger Meister BEST BURGER 2008!']
im[2].link=['http://www.sfweekly.com/bestof/2008/award/readers-poll-winners-1034012/'];

im[3]=new Array();
im[3].image=['img/bm_sfweekly.jpg','SF Weekly Readers vote Burger Meister BEST BURGER 2007!']
im[3].link=['http://www.sfweekly.com/bestOf/index/2007'];

im[4]=new Array();
im[4].image=['img/bm_nationalgeographic.jpg','National Geographic - The 10 Best of Everything']
im[4].link=['pdf/Press/NationalGeographic.pdf'];

im[5]=new Array();
im[5].image=['img/bm_foodnetwork.jpg','Fast Food With Character! -Food Network']
im[5].link=['pdf/Awards/2007_Food_Network_Awards.pdf'];

im[6]=new Array();
im[6].image=['img/bm_michelin.jpg','Michelin Guide Recommended']
im[6].link=['pdf/Awards/2007_Michelin_Guide.pdf'];

// animation: to change the image at a time interval
//  enter a value here in miliseconds
//  enter 0 to only change it on load
var imgChgTime = 3000;

// name of cookie
var ckname = 'rndimg';
// length of time for cookie to last, months
var ckage = 3;
// cookie field delimiter
var ckdelim = ':';
// preload image
var imprld = new Image;

/* begin script */
var imn=D(imc);
var img,ima;
if (imn) {
	// add image and link on page to array
	var last=im.length;
	im[last]=new Array();
	ima=imn.getElementsByTagName('a');
	im[last].link=new Array();
	if (ima.length) {
		img = ima[0].getElementsByTagName('img');
		im[last].link[0]=ima[0].href;
		im[last].link[1]=ima[0].title;
	}else{
		img = imn.getElementsByTagName('img');
	}
	im[last].image=new Array();
	if (img.length) {
		im[last].image[0]=img[0].src;
		im[last].image[1]=img[0].alt;
	}
	
	// try to set cookie to only serve each image once.
	var ck=new Array();
	if(document.cookie != ''){
		var ckl=document.cookie.split('; ');
		for (var i=0;i<ckl.length;i++) {
			var temp=ckl[i].split('=');
			if (temp[0]==ckname) ck=temp[1].split(ckdelim);
		}
	}
	// this is the next image
	var imnx=urimg(ck,im);
	// add next image
	ck[ck.length]=imnx;
	// update cookie
	document.cookie = niCkie(ck);
	// write into page
	if (imnx!=last) {
		// image, alt
		if (img.length && im[imnx].image.length) {
			img[0].src=im[imnx].image[0];
			img[0].alt=im[imnx].image[1];
		}
		// href, title
		if (ima.length && im[imnx].link.length) {
			ima[0].href=im[imnx].link[0];
			ima[0].title=im[imnx].link[1];
		}
	}
	if (imgChgTime>0) {
		// animate the image
		// get next image
		imnx=urimg(ck,im);
		// preload image
		if (img.length && im[imnx].image.length) {
			imprld.src=im[imnx].image[0];
		}
		// call timeout
		imprld.timeoutID=setTimeout("chgImg("+imnx+")",imgChgTime);
	}
}

function chgImg(imgNo){
// update image and re-set timeout
	if (imprld && imprld.complete) {
		// only run on complete image
		// image, alt
		if (img.length && im[imgNo].image.length) {
			img[0].src=imprld.src; // preloaded
			img[0].alt=im[imgNo].image[1];
		}
		// href, title
		if (ima.length && im[imgNo].link.length) {
			ima[0].href=im[imgNo].link[0];
			ima[0].title=im[imgNo].link[1];
		}
		// add image
		ck[ck.length]=imgNo;
		// get next image
		imgNo=urimg(ck,im);
		// preload image
		if (img.length && im[imgNo].image.length) {
			imprld.src=im[imgNo].image[0];
		}
	}
	// call timeout
	imprld.timeoutID = setTimeout("chgImg("+imgNo+")",imgChgTime);
}

function niCkie(ck){
	// create cookie string
	var expireDate = new Date();
	// expires in ckage
	expireDate.setMonth(expireDate.getMonth()+ckage);
	return ckname+"="+ck.join(ckdelim)+"; expires="+expireDate.toGMTString();
}

function urimg(ck,im) {
// pick unseen random image from im() given seen images ck()
	// check if seen all images
	if (ck.length >= im.length) {
		// hold last image displayed so it is not repeated immediately
		var temphold=ck[ck.length-1];
		ck.length=0;
		ck[0]=temphold;
	}
	// create array of images not looked at
	var notSeen=new Array();
	for (var i=0;i<im.length;i++){
		var seen=false;
		for (var j=0;(j<ck.length)&& !seen;j++)
			seen=(i==parseInt(ck[j]))
		if (!seen) notSeen[notSeen.length]=i;
	}
	return notSeen[parseInt(Math.random()*notSeen.length)];
}

function D(id){
	var d=null;
	if (document.getElementById) d=document.getElementById(id);
	else if (document.all) d=document.all[id];
	return d;
}
