﻿function PartnerSlideShow( instance, nodeid, delay_display, delay_fade, delay_init, width, ratio, filter ) {
	this.instance = instance;
	this.nodeid = nodeid;
	this.query_id = 256;
	this.query_type = 44;
	this.partners = new Array();
	if (filter) {
		this.filter = filter;
	} else {
		this.filter = "";
	}
	this.pos = 1; // skip start image
	this.delay_init = delay_init;
	this.delay_display = delay_display;
	this.delay_fade = delay_fade;
	this.width = width;
	this.ratio = ratio;
	this.is_fading = false;
	this.Initialize();
}

PartnerSlideShow.prototype.Initialize = function() {
	var _this = this;
	$.ajax({ type: "GET", url: "/__/frontend/handler/document.php", data: "id=" + this.query_id + "&type=" + this.query_type + "&filter=" + this.filter, dataType: "xml", success: function(xml){
		$("partners > partner", xml).each( function() {
			_this.partners.push({partner:$("partner", this).text(), pointer_id:$("pointer_id", this).text(), type:$("type", this).text(), url: $("url", this).text()});
		});
		window.setTimeout(_this.instance + ".DoInitialize()", _this.delay_init);
	}});
}

PartnerSlideShow.prototype.DoInitialize = function() {
	for (i=0; i < this.partners.length; i++) {
		var img = new Image();
		this.SetImageSource( img, this.partners[i].pointer_id, this.partners[i].type );
	}
	window.setInterval( this.instance + ".Forward()", this.delay_display);
}

PartnerSlideShow.prototype.Forward = function() {
	if (this.is_fading) return false;
	this.is_fading = true;
	this.pos = ((this.pos + 1) % this.partners.length);
	// need to wait a litte for ff to render the image - animation would be broken if we didn't
	this.SetVisibility( 'hidden', this.nodeid );
	window.setTimeout(this.instance + ".DoForward()", 0);
}

PartnerSlideShow.prototype.DoForward = function() {
	this.FadeIn(this.delay_fade, this.nodeid);
	var aNode = document.getElementById(this.nodeid + '_url');
	aNode.href = this.partners[this.pos].url;
}

PartnerSlideShow.prototype.SetImageSource = function( img, id, type ) {
	img.src = "/__/frontend/handler/image.php?id=" + id + "&type=" + type + "&ratio=" + this.ratio + "&width=" + this.width + "&fit=1";
}

PartnerSlideShow.prototype.FadeIn = function( millisec, id ) {
	this.SetFrontImage();
    var speed = Math.round(millisec / 100);
	// again - make sure the image is really hidden before starting to fade in...
    var delay = 10;
	this.SetOpacity( 0, id );
	window.setTimeout(this.instance + ".SetVisibility('','" + id + "')", delay);
       if ( speed > 0 ) for(i = 1; i <= 100; i++) {
           window.setTimeout(this.instance + ".SetOpacity(" + i + ",'" + id + "')", ((i * speed) + delay));
       }
       window.setTimeout(this.instance + ".SetBackImage()", (millisec + delay + 10) ); 
}

PartnerSlideShow.prototype.SetBackImage = function() {
	this.SetImageSource( document.getElementById(this.nodeid + '_back'), this.partners[this.pos].pointer_id, this.partners[this.pos].type );
	this.is_fading = false;
}

PartnerSlideShow.prototype.SetFrontImage = function() {
	this.SetImageSource( document.getElementById(this.nodeid + '_front'), this.partners[this.pos].pointer_id, this.partners[this.pos].type );
}

PartnerSlideShow.prototype.SetOpacity = function( opacity, id ) {
	obj = document.getElementById(id + '_front');
	opacity = (opacity == 100) ? 99.999 : opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

PartnerSlideShow.prototype.SetVisibility = function( visibility, id ) {
	obj = document.getElementById(id + '_front');
	obj.style.visibility = visibility;
}
