var speedImageArray = ["speed1000.png", "speed2000.png", "speed3000.png", "speed4000.png", "speed5000.png", "speed6000.png", "speed7000.png", "speed8000.png", "speed9000.png", "speed10000.png"];
var pull_down_cat_title = [
	["main",				"Main Page"],
	["whycover",			"Why Cover My Pool"],
	["returnoninvestment",	"Return on Investment"],
	["colorchoice",			"Cover Color Choice"],
	["contractorinfo",		"Contractor Info"],
	["remodel1",			"Remodel Example 1"],
	["remodel2",			"Remodel Example 2"],
	["remodel3",			"Remodel Example 3"],
	["spacovers",			"Spa Covers"],
	["contact",				"Contact Us"],
	["poolcovers",			"Pool Covers"]
];
var current_speed = 4000;

var PictureEvent = {
  
    add: function(obj, etype, fp, cap) {
        cap = cap || false;
        if (obj.addEventListener) obj.addEventListener(etype, fp, cap);
        else if (obj.attachEvent) obj.attachEvent("on" + etype, fp);
    }, 

    remove: function(obj, etype, fp, cap) {
        cap = cap || false;
        if (obj.removeEventListener) obj.removeEventListener(etype, fp, cap);
        else if (obj.detachEvent) obj.detachEvent("on" + etype, fp);
    }, 
    
    DOMit: function(e) { 
        e = e? e: window.event; // e IS passed when using attachEvent though ...
        if (!e.target) e.target = e.srcElement;
        if (!e.preventDefault) e.preventDefault = function () { e.returnValue = false; return false; }
        if (!e.stopPropagation) e.stopPropagation = function () { e.cancelBubble = true; }
        return e;
    },
    
    getTarget: function(e) {
        e = PictureEvent.DOMit(e); var tgt = e.target; 
        if (tgt.nodeType != 1) tgt = tgt.parentNode; // safari...
        return tgt;
    }
    
}

function PictureRotator(rObj) {
    var imgObj = document.getElementById(rObj.id); 
    if ( !imgObj || !rObj.images ) { return; }
    this.id = rObj.id; 
	this.level = 0;
    this.thumb1 = rObj.thumb1; 
    this.thumb2 = rObj.thumb2; 
    this.thumb3 = rObj.thumb3; 
    this.thumb4 = rObj.thumb4; 
    this.thumb5 = rObj.thumb5; 
    this.thumb6 = rObj.thumb6; 
    this.thumb7 = rObj.thumb7; 
    this.thumb8 = rObj.thumb8; 
    this.thumb9 = rObj.thumb9; 
    this.thumb10 = rObj.thumb10;
	this.speed = current_speed;
    this.path = rObj.path || ""; 
	this.thumb_path = rObj.thumb_path || ""; 
	this.image_path = rObj.image_path || ""; 
	this.bMouse = rObj.bMouse;
    this.bTrans = rObj.bTrans && typeof imgObj.filters != 'undefined'; // ie only
    this.actions = rObj.actions; 
	this.captions = rObj.captions; 
	this.captionId = rObj.captionId;
    this.title = rObj.title; 
	this.alt = rObj.alt; 
    this.ctr = 0;
    this.timer = 0; 
	this.imgs = [];
	this.thumbs = [];
	this.selected_thumbs = [];
    this.addImages(rObj.images); 
	this._setupLink(imgObj);
    var index = PictureRotator.col.length; 
	PictureRotator.col[index] = this;
    this.animString = "PictureRotator.col[" + index + "]";
}

PictureRotator.col = []; // hold instances
PictureRotator.resumeDelay = 400; // onmouseout resume rotation after delay
PictureRotator.prototype.on_rotate = function() {}

PictureRotator.getInstanceById = function(id) {
    var i, obj;
    for (i=0; obj = PictureRotator.col[i]; i++) {
        if ( obj.id == id ) { return obj; }
    }
    return null;
}

PictureRotator.prototype.addImages = function(imageArray) { // preloads images
    var i, img, sm_thumb,lg_thumb;
    for (i=0; imageArray[i]; i++) {
        img = new Image();
        img.src = this.path + imageArray[i];
        this.imgs[this.imgs.length] = img;
        sm_thumb = new Image();
        sm_thumb.src = this.thumb_path + imageArray[i];
        this.thumbs[this.thumbs.length] = sm_thumb;
        lg_thumb = new Image();
        lg_thumb.src = this.thumb_path + "selected_" + imageArray[i];
        this.selected_thumbs[this.selected_thumbs.length] = lg_thumb;
    }

	if (this.imgs.length%10 > 0) {
		for (i=this.imgs.length%10; i<10; i++) {
			thumb = new Image();
			thumb.src = this.thumb_path + "blank.jpg";
			this.thumbs[this.thumbs.length] = thumb;
		}
	}
}

PictureRotator.prototype._setupLink = function(imgObj) { 
    if ( imgObj.parentNode && imgObj.parentNode.tagName.toLowerCase() == 'a' ) {
        var parentLink = this.parentLink = imgObj.parentNode;
        if (this.bMouse) {
            PictureEvent.add(parentLink, 'mouseover', PictureRotator.pause);
            PictureEvent.add(parentLink, 'mouseout', PictureRotator.resume);
        }
    }
}

PictureRotator.prototype.rotate = function() {
	this.clearTimer(); 
    var imgObj = document.getElementById(this.id);
	var smThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	smThumbObj.src = this.thumbs[10*this.level+this.ctr%10].src;

	if (this.ctr < this.imgs.length-1) this.ctr++;
	else this.ctr = 0;
	var cur_level = (this.ctr-(this.ctr%10)) / 10;
	if (this.level != cur_level)
	{
		this.level = cur_level;
		this.refreshthumbs();
	}
    if (0) { //( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }
	this.showCaption();
	//var el = document.getElementById(this.captionId);
	//el.innerHMTL = this.captions[this.ctr];
	//document.getElementById("cbb").innerHMTL = "test";

	var lgThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	lgThumbObj.src = this.selected_thumbs[10*this.level+this.ctr%10].src;

    this.swapAlt(imgObj); 
	this.prepAction(); 
    this.on_rotate();
    this.timer = setTimeout( this.animString + ".rotate()", this.speed);
}

PictureRotator.prototype.doImageTrans = function(imgObj) {
    imgObj.style.filter = 'blendTrans(duration=1)';
    if (imgObj.filters.blendTrans) imgObj.filters.blendTrans.Apply();
    imgObj.src = this.imgs[this.ctr].src;
    imgObj.filters.blendTrans.Play(); 
}

PictureRotator.prototype.swapAlt = function(imgObj) {
    return;
	if ( !imgObj.setAttribute ) return;
    if ( this.alt && this.alt[this.ctr] ) {
        imgObj.setAttribute('alt', this.alt[this.ctr]);
    }
    if ( this.title && this.title[this.ctr] ) {
        imgObj.setAttribute('title', this.title[this.ctr]);
    }
}

PictureRotator.prototype.prepAction = function() {
    if ( this.actions && this.parentLink && this.actions[this.ctr] ) {
        if ( typeof this.actions[this.ctr] == 'string' ) {
            this.parentLink.href = this.actions[this.ctr];
        } else if ( typeof this.actions[this.ctr] == 'function' ) {
            // to execute function when linked image clicked 
            // passes id used to uniquely identify instance  
            // retrieve it using the PictureRotator.getInstanceById function 
            // so any property of the instance could be obtained for use in the function 
            var id = this.id;
            this.parentLink.href = "javascript: void " + this.actions[this.ctr] + "('" + id + "')";
        } 
    }
}

PictureRotator.prototype.showCaption = function() {
	//var el = document.getElementById(this.captionId);
	//el.innerHMTL = this.captions[this.ctr];
	//alert(el.innerHMTL);
	//el.style.bottom = this.captions[this.ctr][1] + 'px';
//    if ( this.captions && this.captionId ) {
//        var el = document.getElementById( this.captionId );
//        if ( el && this.captions[this.ctr] ) {
//            el.innerHTML = this.captions[this.ctr];
//        }
//    }
}

PictureRotator.prototype.clearTimer = function() {
    clearTimeout( this.timer ); this.timer = null;
}

PictureRotator.prototype.goToPicture = function(img_num) {
    this.clearTimer(); 
	
	//if user clicks on current thumbnail do nothing
    if (this.ctr%10==img_num) { return; }

    var imgObj = document.getElementById(this.id);
	var smThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	smThumbObj.src = this.thumbs[10*this.level+this.ctr%10].src;
	this.ctr = 10*this.level+img_num;
    if (0) { //( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }

	this.showCaption();
	var lgThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	lgThumbObj.src = this.selected_thumbs[10*this.level+this.ctr%10].src;
    this.swapAlt(imgObj); 
	this.prepAction(); 
    this.on_rotate();
}

PictureRotator.prototype.rotateOneForward = function() {
    this.clearTimer(); 
    var imgObj = document.getElementById(this.id);
	var smThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	smThumbObj.src = this.thumbs[10*this.level+this.ctr%10].src;
	if (this.ctr < this.imgs.length-1) this.ctr++;
	else this.ctr = 0;
	var cur_level = (this.ctr-(this.ctr%10)) / 10;
	if (this.level != cur_level)
	{
		this.level = cur_level;
		this.refreshthumbs();
	}

    if (0) { //( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }
	this.showCaption();
	var lgThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	lgThumbObj.src = this.selected_thumbs[10*this.level+this.ctr%10].src;
	this.prepAction(); 
    this.on_rotate();
}

PictureRotator.prototype.rotateOneBack = function() {
    this.clearTimer(); 
    var imgObj = document.getElementById(this.id);
	var smThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	smThumbObj.src = this.thumbs[10*this.level+this.ctr%10].src;
	if (this.ctr > 0) this.ctr--;
	else this.ctr = this.imgs.length-1;
	var cur_level = (this.ctr-(this.ctr%10)) / 10;

	if (this.level != cur_level)
	{
		this.level = cur_level;
		this.refreshthumbs();
	}
    if (0) { //( this.bTrans ) {
        this.doImageTrans(imgObj);
    } else {
        imgObj.src = this.imgs[this.ctr].src;
    }
	this.showCaption();
	var lgThumbObj = eval('document.getElementById(this.thumb'+(1+this.ctr%10)+');');
	lgThumbObj.src = this.selected_thumbs[10*this.level+this.ctr%10].src;
	this.prepAction(); 
    this.on_rotate();
}

PictureRotator.prototype.refreshthumbs = function() {
    var imgObj;
	var i = 0;
	for (i=0; i<10; i++) {
		imgObj = eval('document.getElementById(this.thumb'+(i+1)+');');
		imgObj.src = this.thumbs[10*this.level+i].src;
	}
}

PictureRotator.start = function(delay) {
    var i, obj;
    for (i=0; obj = PictureRotator.col[i]; i++) {
        if ( !obj.isActive ) {
            obj.clearTimer(); obj.isActive = true; obj.isStepping = false; 
            delay = delay || obj.speed;
            obj.timer = setTimeout( obj.animString + ".rotate()", delay);
        }
    }
}

PictureRotator.stop = function() {
    var i, obj;
    for (i=0; obj = PictureRotator.col[i]; i++) {
        obj.clearTimer(); obj.isActive = false; obj.isStepping = false;
    }
}

PictureRotator.pause = function(e) {	
    e = PictureEvent.DOMit(e);
    var id = e.target.id;
    var obj = PictureRotator.getInstanceById(id);
    if (obj) { obj.clearTimer(); }
}

PictureRotator.resume = function(e) {
    e = PictureEvent.DOMit(e);
    var id = e.target.id;
    var obj = PictureRotator.getInstanceById(id);
    if ( obj && obj.isActive ) {
        obj.timer = setTimeout( obj.animString + ".rotate()", PictureRotator.resumeDelay );
    }
}

PictureRotator.setup = function () {
    var rObj, r;
    for (var i=0; arguments[i]; i++) {
        rObj = arguments[i];
        r = new PictureRotator(rObj);
    }
    PictureRotator.start(5000);
}


PictureRotator.faster = function () {
  for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive) {
		  if (obj.speed > 1000) 
      {
        obj.speed = obj.speed - 1000;
		current_speed = current_speed - 1000;
		obj.clearTimer(); 
		obj.timer = setTimeout( obj.animString + ".rotate()", current_speed);
      }
	    var speedObj = document.getElementById("speedpic");
      speedObj.src = "images/" + speedImageArray[10-(obj.speed/1000)];
    }
	}	
}

PictureRotator.slower = function () {
  for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive) {
		  if (obj.speed < 10000) 
      {
        obj.speed = obj.speed + 1000;
		current_speed = current_speed + 1000;
		obj.clearTimer(); 
		obj.timer = setTimeout( obj.animString + ".rotate()", current_speed);
      }
	    var speedObj = document.getElementById("speedpic");
      speedObj.src = "images/" + speedImageArray[10-(obj.speed/1000)];
    }
	}	
}

PictureRotator.setRotationSpeed = function (new_speed) {
  for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive) {
			obj.speed = new_speed;
			current_speed = new_speed;
			obj.clearTimer(); 
			obj.timer = setTimeout( obj.animString + ".rotate()", current_speed);
			var speedObj = document.getElementById("speedpic");
			speedObj.src = "images/" + speedImageArray[10-(obj.speed/1000)];
		}
	}	
}

PictureRotator.forwardOne = function () {
    for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive &&(!obj.isStepping)) {
			obj.clearTimer(); obj.isActive = false; obj.isStepping = true;
			obj.timer = setTimeout( obj.animString + ".rotateOneForward()", 10);
		}
		else {
			obj.timer = setTimeout( obj.animString + ".rotateOneForward()", 10);
		}
	}	
}

PictureRotator.selectpic = function (img_num) {
    for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive) {
			obj.clearTimer(); obj.isActive = false; obj.isStepping = false;
		}
		obj.timer = setTimeout( obj.animString + ".goToPicture(" + img_num + ")", 10);
	}	
}

PictureRotator.backOne = function () {
    for (i=0; obj = PictureRotator.col[i]; i++) {
		if (obj.isActive &&(!obj.isStepping)) {
			obj.clearTimer(); obj.isActive = false; obj.isStepping = true;
			obj.timer = setTimeout( obj.animString + ".rotateOneBack()", 10);
		}
		else {
			obj.timer = setTimeout( obj.animString + ".rotateOneBack()", 10);
		}
	}	
}
/*
PictureRotator.PrintPictureRotator = function() {

	//for (j=0; obj = PictureRotator.col[j]; j++) 
	//{	
		//if (obj.isActive) 
		//{
	alert("in PrintPictureRotator obj");
			document.write('<div id=\"mainbody\">');
			document.write('<div id=\"SelectBox\" style=\"position:absolute;height:5px;width:5px;\"></div>');
			document.write('<table width=1024 cellspacing=0 cellpadding=0>');
			document.write('<tr>');
			document.write('<td width=540 align=right>');

			document.write('<TABLE class=\"phototable\" width=510 cellspacing=0 cellpadding=0>');
			document.write('<TR>');
			document.write('<TD align=left valign=top colspan=10 width=500 title=\"Click for Hi-Resolution Photo\"><a href=\"#\" onMouseOver=\"PictureRotator.pause()\" onMouseOut=\"PictureRotator.resume()\"><img id=\"'+this.id+'\" src=\"'+this.path+this.images[0]+'\" alt=\"\" width=500 border=0></a></TD>');
			document.write('</TR>');
			document.write('<TR>');
			for (i=0;i<10; i++) {
				if (i<this.imgs.length)
					document.write('<TD class=\"thumbimage\" align=center valign=center width=50 height=50><a href=\"#\" onclick=\"javascript:PictureRotator.selectpic('+i+')\" title=\"Select This Picture\"><img src=\"'+this.thumb_path+this.images['+i+']+'\" border=0 id=\"'+this.id+'t'+(i+1)+'\"></a></TD>');
				else
					document.write('<TD class=\"thumbimage\" align=center valign=center width=50 height=50><a href=\"#\" onclick=\"javascript:PictureRotator.selectpic('+i+')\" title=\"Select This Picture\"><img src=\"'+this.thumb_path+'blank.jpg\" border=0 id=\"'+this.id+'t'+(i+1)+'\"></a></TD>');
			}
			document.write('</TR>');
			document.write('<TR>');
			document.write('<TD align=left colspan=5>');
			document.write('&nbsp;<a href=\"#\" onclick=\"javascript:PictureRotator.start(1)\" title=\"Play\"><img src=\"images/arrow_right_play24.png\" alt=\"Play\" border=0></a>&nbsp;');
			document.write('<a href=\"#\" onclick=\"javascript:PictureRotator.stop()\" title=\"Pause\"><img src=\"images/arrow_pause24.png\" alt=\"Pause\" border=0></a>&nbsp;');
			document.write('<a href=\"#\" onclick=\"javascript:PictureRotator.backOne()\" title=\"Back One Picture\"><img src=\"images/arrow_left_ff24.png\" alt=\"Page Back\" border=0></a>&nbsp;');
			document.write('<a href=\"#\" onclick=\"javascript:PictureRotator.forwardOne()\" title=\"Forward One Picture\"><img src=\"images/arrow_right_ff24.png\" alt=\"Page Forward\" border=0></a> &nbsp;  ');
			document.write('<a href=\"#\" onclick=\"javascript:PictureRotator.slower()\" title=\"Slow Down Photo Rotation Speed\"><img src=\"images/speedminus.png\" border=0 title=\"Slow Down Photo Rotation Speed\"></a>');
			document.write('<img src=\"images/'+speedImageArray[10-(this.speed/1000)]+'\" border=0 id=\"speedpic\" USEMAP=\"#rotation_map\">');
			document.write('<a href=\"#\" onclick=\"javascript:PictureRotator.faster()\" title=\"Speed Up Photo Rotation Speed\"><img src=\"images/speedplus.png\" border=0 title=\"Speed Up Photo Rotation Speed\"></a>');
			document.write('</TD>');
			document.write('<TD align=center colspan=5>');
			document.write('<form>');
			document.write('<select name=\"file\" size=\"1\"');
			document.write('onchange=\"loadPage(this.form.elements[0])\"');
			document.write('target=\"_parent._top\"');
			document.write('onmouseclick=\"this.focus()\"');
			document.write('style=\"background-color:#ffffff\">');
			var val_sel = ["VALUE","VALUE","VALUE","VALUE","VALUE","VALUE","VALUE","VALUE","VALUE","VALUE","VALUE"];
			val_sel[this.index-1] = "SELECTED";
			for (i=0;i<pull_down_cat_title.length; i++) {
				document.write('<OPTION '+val_sel[i]+'=\"index.php?cat='+pull_down_cat_title[i][0]+'\"><b>'+pull_down_cat_title[i][1]+'</b></OPTION>');
			}
			document.write('</select>');
			document.write('</form>');
			document.write('</TD>');
			document.write('</TR>');
			document.write('</TABLE>');

			document.write('</TD>');
			document.write('<td width=20> <!--SPACER--> </td>');
			document.write('<td width=400 valign=top align=left>');
		}
	//}
//}
function PrintBusinessLogos() {
	document.write('</td>');
	document.write('<td width=64 align=center valign=top>');
	document.write(' &nbsp; <BR>');
	document.write('<img src=\"images/ul_logo_60.PNG\" width=40><br>');
	document.write(' &nbsp; <BR>');
	document.write('<a href=\"http://www.santabarbara.bbb.org/BusinessReport.aspx?CompanyID=11000032\" target=_blank title=\"View our A+ rating with the BBB!!\"><img src=\"images/bbb_transparent.gif\" width=40 border=0></a><br>');
	document.write(' &nbsp; <BR>');
	document.write('<img src=\"images/ASTM_logo_transparent.gif\" width=40>');
	document.write('</td>');
	document.write('</TR>');
	document.write('</TABLE>');
	document.write('</div> <!--mainbody-->');
}
*/
