// ajax slideshow, daniel sissman, june 2006
// partly based on ajax prototype code from rss javascript ticker object,
// by george at javascriptkit.com/dynamicdrive.com

function createAjaxObj() {
	var httprequest=false;
	if(window.XMLHttpRequest) { // if mozilla, safari etc
		httprequest=new XMLHttpRequest();
		if(httprequest.overrideMimeType) {
			httprequest.overrideMimeType("text/xml");
		}
	} else if (window.ActiveXObject) { // if ie
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			}
		}
	}
	return httprequest;
}

// main slideshow entrypoint function
function ajax_slideshow(xml_file, parent_div_id, div_id, div_width, div_height, div_class, delay, slideshow_link_url, slideshow_link_text, optionalswitch) {
	// this.date = new Date();
	this.xml_file                = xml_file;		// xml file path + "?" + date.getMilliseconds();
	this.slideshow_parent_id     = parent_div_id;   // id of ticker div to display information
	this.slideshow_id            = div_id;			// id of ticker div to display information
	this.slideshow_class         = div_class;		// css class for styling div
	this.slideshow_width         = div_width;
	this.slideshow_height        = div_height;
	this.delay                   = delay;			// delay between image change, in miliseconds
	this.fade_increment_delay    = 30;				// 30
	this.mouse_over_delay        = 200;
	this.logicswitch             = (typeof optionalswitch!="undefined")? optionalswitch : -1;
	this.mouseover_stat          = 0; // bool stores mouseover (on slideshow) status
	this.pointer                 = 0;
	this.pointer_plus            = 1;
	this.fader                   = -10;
	this.slideshow_link_text     = slideshow_link_text;
	this.slideshow_link_url      = slideshow_link_url;
	this.image_cache             = new Array();
	this.ajaxobj                 = createAjaxObj();
	this.image_loaded_counter    = 0;
	this.mouse_over_status       = "";
	this.url_transistion_opacity = 20;
	
	// output main slideshow containers - can be styled using css
		
	var parent_div_element = document.getElementById(this.slideshow_parent_id);
	if(parent_div_element) {
		parent_div_element.innerHTML = "<div id='"+this.slideshow_id+"' class='"+this.slideshow_class+"' style='position:relative;width:"+this.slideshow_width+";height:"+this.slideshow_height+";'><a id='"+this.slideshow_id+"_anchor'><div id='"+this.slideshow_id+"_one' style='position:absolute;top:0;left:0;width:"+this.slideshow_width+";height:"+this.slideshow_height+";background:#ffffff no-repeat;z-index:10;filter:alpha(opacity=100);filter:progid:DXImageTransform.Microsoft.alpha(opacity=100);-moz-opacity:0.999;opacity:0.999;-khtml-opacity:0.999;'></div><div id='"+this.slideshow_id+"_two' style='position:absolute;top:0;left:0;width:"+this.slideshow_width+";height:"+this.slideshow_height+";background:#ffffff no-repeat;z-index:9;filter:alpha(opacity=100);filter:progid:DXImageTransform.Microsoft.alpha(opacity=100);-moz-opacity:0.999;opacity:0.999;-khtml-opacity:0.999;'></div></a></div>";
		
		// call to get content
		this.getAjaxcontent();
	} else {
		// alert("slideshow parent cannot be found!");
	}
}

// function gets xml image data
ajax_slideshow.prototype.getAjaxcontent = function() {
	if(this.ajaxobj) {
		var instanceOfSlideshow = this;
		this.ajaxobj.onreadystatechange = function(){ instanceOfSlideshow.initialize() };
		this.ajaxobj.open("GET",this.xml_file,true);
		this.ajaxobj.send(null);
	}
}

// calls for xml image data, and parses using dom
ajax_slideshow.prototype.initialize = function() {
	if(this.ajaxobj.readyState == 4) { //if request of file completed
		if(this.ajaxobj.status == 200) { //if request was successful
			var xmldata = this.ajaxobj.responseXML;
			
			if(xmldata.getElementsByTagName("pic").length==0) { //if no <pic> elements found, return error
				document.getElementById(this.slideshow_id).innerHTML="<b>Error</b> fetching remote XML feed!<br/>" + this.ajaxobj.responseText;
				return;
			}
			
			var instanceOfSlideshow = this;
			this.feeditems = xmldata.getElementsByTagName("pic");
			
			//cycle through xml object and store each peice of the item element as an attribute of the element
			for(var i=0; i<this.feeditems.length; i++) {
				this.feeditems[i].setAttribute("img_url",((this.feeditems[i] && this.feeditems[i].getElementsByTagName("link") && this.feeditems[i].getElementsByTagName("link").length > 0 && this.feeditems[i].getElementsByTagName("link")[0] && this.feeditems[i].getElementsByTagName("link")[0].firstChild && this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue)?this.feeditems[i].getElementsByTagName("link")[0].firstChild.nodeValue:""));
				this.feeditems[i].setAttribute("img_src",this.feeditems[i].getElementsByTagName("image")[0].firstChild.nodeValue);
				this.feeditems[i].setAttribute("img_alt",this.feeditems[i].getElementsByTagName("title")[0].firstChild.nodeValue);
			}
			
			if(this.feeditems.length == 1) {
				this.pointer_plus = 0;
			}
			
			document.getElementById(this.slideshow_id).onmouseover = function() {instanceOfSlideshow.mouseover_stat=1;};
			document.getElementById(this.slideshow_id).onmouseout  = function() {instanceOfSlideshow.mouseover_stat=0;};
			
			// cache the images
			if(document.images) {
				for(var i=0;i<this.feeditems.length;i++) {
					this.image_cache[i]        = new Image();
					this.image_cache[i].src    = this.feeditems[i].getAttribute("img_src");
					if(this.image_cache[i] && this.image_cache[i].onload) {
						this.image_cache[i].onload = this.showImage(i);
					} else {
						if(i==0) { // display image 0 whilst the others are loading
							var slideshow_div_one = document.getElementById(this.slideshow_id+"_one");
							if(slideshow_div_one) {
								slideshow_div_one.style.background = "url("+this.feeditems[0].getAttribute("img_src")+")";
								
								if(this.feeditems[0].getAttribute("img_url").length > 0) {
									// (function() {
										// var slideshow_div = document.getElementById(instanceOfSlideshow.slideshow_id);
										// slideshow_div_one.onclick = function() { var _slideshow_url = slideshow_url; window.open(_slideshow_url); }
										var slideshow_a   = document.getElementById(instanceOfSlideshow.slideshow_id+"_anchor");
										var slideshow_url = instanceOfSlideshow.feeditems[0].getAttribute("img_url");
										if(slideshow_a) {
											if(slideshow_url.length > 1) { // prevent properties with no link (represented by '/') from being clickable
												if(slideshow_url.match(/http/)) {
													// slideshow_div.onclick     = function() { var _slideshow_url = slideshow_url; window.open(_slideshow_url); }
													slideshow_a.href   = slideshow_url;
													slideshow_a.target = "_blank";
													slideshow_a.title  = "View property details...";
												} else {
													// slideshow_div.onclick     = function() { var _slideshow_url = slideshow_url; document.location = _slideshow_url; }
													slideshow_a.href   = slideshow_url;
													slideshow_a.target = "";
													slideshow_a.title  = "View property details...";
												}
											// slideshow_div.onmouseover = function() { var _slideshow_url = slideshow_url; window.status = "View property details..."; }
											// slideshow_div.onmouseout  = function() { window.status = ""; }
											} else {
												slideshow_a.href     = "";
												slideshow_a.target   = "";
												slideshow_a.title    = "";
												slideshow_a.disabled = true;
												slideshow_a.onclick  = function() { return false; };
											}
										}
										// slideshow_div_one.onmouseover = function() { window.status = "testing"; }
									// })();
								}
							}
						}
					}
				}
			}
			
			this.rotate();
		} else {
			document.getElementById(this.slideshow_id).innerHTML="<b>XML slideshow data did not load correctly! Status code: "+this.ajaxobj.status+"</b>";
			return;
		}
	} else {
		// document.getElementById(this.slideshow_id).innerHTML="<b>XML slideshow data did not load correctly! State: "+this.ajaxobj.readyState+"</b>";
		return;
		// alert("XML data did not load correctly! State: "+this.ajaxobj.readyState);
	}
}

ajax_slideshow.prototype.showImage = function(id) {
	if(id >= 0) {
		var slideshow_div_one = document.getElementById(this.slideshow_id+"_one");
		slideshow_div_one.style.background = "url("+this.feeditems[id].getAttribute("img_src")+")";
		if(this.feeditems[0].getAttribute("img_url").length > 0) {
			var slideshow_a   = document.getElementById(this.slideshow_id+"_anchor");
			var slideshow_url = this.feeditems[id].getAttribute("img_url");
			if(slideshow_a) {
				if(slideshow_url.length > 1) {
					slideshow_a.href   = slideshow_url;
					if(slideshow_url.match(/http/)) {
						slideshow_a.target = "_blank";
					} else {
						slideshow_a.target = "";
					}
					slideshow_a.title    = "View property details...";
					slideshow_a.disabled = false;
					slideshow_a.onclick  = null;
				} else {
					slideshow_a.href     = "";
					slideshow_a.target   = "";
					slideshow_a.title    = "";
					slideshow_a.disabled = true;
					slideshow_a.onclick  = function() { return false; };
				}
			}
		}
	}
}

// rotate function, fades through images
ajax_slideshow.prototype.rotate = function() {
	var status_div = document.getElementById("slideshow_status");
	
	var instanceOfSlideshow = this;
	var slideshow_div_one = document.getElementById(this.slideshow_id+"_one");
	var slideshow_div_two = document.getElementById(this.slideshow_id+"_two");
	var slideshow_a       = document.getElementById(this.slideshow_id+"_anchor");
	
	if(this.mouseover_stat == 1) { // if mouse is currently over slideshow, pause
		setTimeout(function() { instanceOfSlideshow.rotate() }, this.mouse_over_delay);
	} else {
		if(instanceOfSlideshow.fader < 0) {			
			instanceOfSlideshow.fader = 110;
			if(slideshow_div_one) {
				var opa = ((instanceOfSlideshow.fader/100) > 0.999) ? 0.999 : (instanceOfSlideshow.fader / 100);
				this.set_opacity(slideshow_div_one,opa);
			}
			
			if(slideshow_div_one && slideshow_div_two) {
				if(this.image_cache[this.pointer].src) {
					slideshow_div_one.style.background = "url("+this.image_cache[this.pointer].src+")";
					slideshow_div_two.style.background = "url("+this.image_cache[this.pointer_plus].src+")";
					
					if(this.feeditems[this.pointer] && this.feeditems[this.pointer].getAttribute("img_url") && this.feeditems[this.pointer].getAttribute("img_url").length > 1) {
						slideshow_a.href = this.feeditems[this.pointer].getAttribute("img_url");
						if(this.feeditems[this.pointer].getAttribute("img_url").match(/http/)) {
							slideshow_a.target = "_blank";
						} else {
							slideshow_a.target = "";
						}
						slideshow_a.title    = "View property details...";
						slideshow_a.disabled = false;
						slideshow_a.onclick  = null;
					} else {
						slideshow_a.href     = "";
						slideshow_a.target   = "";
						slideshow_a.title    = "";
						slideshow_a.disabled = true;
						slideshow_a.onclick  = function() { return false; };
					}
				}
			} else {
				// layers not found!
			}
			
			this.pointer      = (this.pointer<this.feeditems.length-1)? this.pointer+1 : 0;
			this.pointer_plus = (this.pointer_plus<this.feeditems.length-1)? this.pointer_plus+1 : 0;
			
			setTimeout(function() {instanceOfSlideshow.rotate() }, this.delay);
		} else {
			instanceOfSlideshow.fader -= 5;
			if(instanceOfSlideshow.fader <= 100) {
				if(slideshow_div_two) {
					var opa = ((instanceOfSlideshow.fader/100) > 0.999) ? 0.999 : (instanceOfSlideshow.fader / 100);
					this.set_opacity(slideshow_div_one,opa);
				}
			}
			
			// as the top layer fades into the one below, we need to change the anchor url otherwise the user will start to see
			// the bottom layer property image and still be taken to the details page of top layer property
			if(instanceOfSlideshow.fader <= instanceOfSlideshow.url_transistion_opacity) {
				if(this.feeditems[this.pointer] && this.feeditems[this.pointer].getAttribute("img_url") && this.feeditems[this.pointer].getAttribute("img_url").length > 1) {
					slideshow_a.href = this.feeditems[this.pointer].getAttribute("img_url");
					if(this.feeditems[this.pointer].getAttribute("img_url").match(/http/)) {
						slideshow_a.target = "_blank";
					} else {
						slideshow_a.target = "";
					}
					slideshow_a.title    = "View property details...";
					slideshow_a.disabled = false;
					slideshow_a.onclick  = null;
				} else {
					slideshow_a.href     = "";
					slideshow_a.target   = "";
					slideshow_a.title    = "";
					slideshow_a.disabled = true;
					slideshow_a.onclick  = function() { return false; };
				}
			}
			
			// cause initial entrance to rotate loop, after displaying image for 30 millisecs
			setTimeout(function(){ instanceOfSlideshow.rotate() }, this.fade_increment_delay);
		}
	}
}

ajax_slideshow.prototype.set_opacity = function(object_id,opacity) {
	// var bv = navigator.userAgent; if(bv && !bv.match(/Firefox\/2/)) // allow opacity fading if not using firefox 2, for some reason there is a flickering image bug in this version
	{
		if(object_id && object_id.style) {
			opacity = ((opacity < 0)?0:((opacity>=1)?0.999:opacity));
			
			object_id.style.KHTMLOpacity = opacity;
			object_id.style.MozOpacity   = opacity;
			object_id.style.opacity      = opacity;
			
			if(object_id.style.filter) {
				object_id.style.filter = "alpha(opacity:" + opacity + ")";
			}
			
			if(object_id.filters && object_id.filters.alpha) {
				object_id.filters.alpha.opacity = (opacity * 100);
			}
			
		}
	}
}