﻿/*
	v1.2.1, 03.01.2011, dogan

	©2011 Tanyel Dogan, alle Rechte vorbehalten

	03.01.2011, dogan
	Korrektur beim Auslesen HTML codierter Bildquellen

	02.01.2011, dogan
	Zusätzlicher Parameter, mit dem das Ausblenden des Container Hintergrundbildes vor dem ersten Einblenden unterdrückt werden kann

	28.11.2010, dogan
	Fehlerhaftes Ladeverhalten in KHTML Browsern chrome, safari behoben
*/

if(! Cms4d)var Cms4d = {};

if(! Cms4d.StringReplace)
{
	Cms4d.StringReplace = function(t, s1, s2)
	{
		try
		{
			var reg_exp = eval('/' + s1 + '/g');
			t = t.replace(reg_exp, s2);
		}
		catch(e){}
		return(t);
	}
}

if(! Cms4d.Location)
{
	Cms4d.Location = {
		href:document.location.href
		,host:document.location.host
		,hostname:document.location.hostname
		,pathname:document.location.pathname
		,basepath:''
		,path:''
		,pagename:''
		,fname:''
		,fextension:''
		,arps:''
		,port:document.location.port
		,protocol:document.location.protocol
		,search:document.location.search
		,hash:document.location.hash
		,CalcLocation:function()
		{
			var pos1 = 0;

			pos1 = this.href.indexOf(this.host);
			if(pos1 != -1)pos1 += this.host.length;
			this.basepath = this.href.substring(0, pos1) + '/';

			var pn = this.pathname;
			if(pn == '' || pn == '/')
			{
			}
			else
			{
				pos1 = pn.lastIndexOf('/');
				if(pos1 != -1)
				{
					this.path = pn.substring(0, pos1 + 1);
					this.pagename = pn.substring(pos1 + 1);

					if(this.path == '/')
					{
						this.path = '';
					}
					else
					{
						pos1 = this.path.indexOf('/');
						if(pos1 == 0)this.path = this.path.substring(1);
					}
					if(this.pagename == '/')this.pagename = '';
				}

				if(this.pagename != '')
				{
					pos1 = this.pagename.lastIndexOf('.');
					if(pos1 != -1)
					{
						this.fname = this.pagename.substring(0, pos1);
						this.fextension = this.pagename.substring(pos1);
					}
				}

				if(this.path.indexOf('/') != -1)
				{
					var pa = this.path.split('/');
					var s = '';
					for(var i = 0; i < pa.length; i++)
					{
						var p = pa[i];
						if(p != '')s += '../';
					}
					this.arps = s;
				}

			}
		}
		,toString:function()
		{
			var t = '';
			t += 'href: ' + this.href + '\n';
			t += 'host: ' + this.host + '\n';
			t += 'hostname: ' + this.hostname + '\n';
			t += 'pathname: ' + this.pathname + '\n';
			t += 'basepath: ' + this.basepath + '\n';
			t += 'path: ' + this.path + '\n';
			t += 'pagename: ' + this.pagename + '\n';
			t += 'fname: ' + this.fname + '\n';
			t += 'fextension: ' + this.fextension + '\n';
			t += 'arps: ' + this.arps + '\n';
			t += 'protocol: ' + this.protocol + '\n';
			t += 'search: ' + this.search + '\n';
			t += 'hash: ' + this.hash + '\n';
			return(t);
		}
	}
	Cms4d.Location.CalcLocation();
}


var ua = navigator.userAgent.toLowerCase();
if(! Cms4d.UserAgent)Cms4d.UserAgent = ua;
if(! Cms4d.Browser)Cms4d.Browser = 'ff';
if(ua.indexOf('netscape') != -1 && ua.indexOf('firefox') == -1)
{
	Cms4d.Browser = 'ns';
}
else if(ua.indexOf('firefox') != -1)
{
	Cms4d.Brwoser = 'ff';
}
else if(ua.indexOf('msie') != -1)
{
	Cms4d.Browser = 'ie';
}
else if(ua.indexOf('opera') != -1)
{
	Cms4d.Browser = 'opera';
}
else if(ua.indexOf('safari') != -1 && ua.indexOf('chrome') == -1)
{
	Cms4d.Browser = 'safari';
}
else if(ua.indexOf('chrome') != -1)
{
	Cms4d.Browser = 'chrome';
}




Cms4d.SlideShow = {
	ctrlcounter:0
	,Controls:{}
	,fctimerid:null
	,fctimerdelay:50
	,fclcounter:0
	,fclmax:30
	,FindControls:function()
	{
		clearTimeout(this.fctimerid);
		this.fclcounter++;
		if(this.fclcounter > this.fclmax)return;

		if(window.Cms4dSlideShowControlList)
		{
			var clist = window.Cms4dSlideShowControlList;
			for(var i = 0; i < clist.length; i++)
			{
				var c = clist[i];
				var cok = false;
				var ctrltype = c.ctrltype;
				if(typeof(ctrltype) == 'string')
				{
					var ctrlid = c.ctrlid;
					if(typeof(ctrlid) == 'string')
					{
						if(! this.Controls[ctrlid])
						{
							var mycontrol = null;
							if(ctrltype == 'Cms4d.SlideShow.ImageBlender')
							{
								var mycontrol = new Cms4d.SlideShow.ImageBlender(ctrlid, c.autoplay, c.centerimg, c.bgcolor, c.img, c.delete_container_background);
							}

							if(mycontrol != null && mycontrol.ctrlok == true)
							{
								this.Controls[ctrlid] = mycontrol;
								if(mycontrol.autoplay == 1)mycontrol.MoveCursor();
							}
						}
					}
				}
			}
		}
		this.fctimerdelay += 50;
		this.fctimerid = setTimeout('Cms4d.SlideShow.FindControls()', this.fctimerdelay);
	}
};


Cms4d.SlideShow.ImageBlender = function(p_ctrlid, p_autoplay, p_centerimg, p_imgcontainercolor, p_imglist, p_delete_container_background)
{
	this.ctrlid = p_ctrlid;
	this.autoplay = p_autoplay == 0 ? false : true;
	this.centerimg = p_centerimg == 0 ? false : true;
	this.imgcontainercolor = p_imgcontainercolor;
	this.imglist = new Array();
	for(var i = 0; i < p_imglist.length; i++)
	{
		var o = p_imglist[i];
		var myimg = new Cms4d.SlideShow.ImageBlender.prototype.imgo(o.src, o.n, o.href, o.tgt, o.bs, o.wms);
		this.imglist.push(myimg);
	}
	//02.01.2011, dogan
	//Wird dieser Parameter nicht oder mit false übergeben, so wird beim ersten Aufruf von img_OnLoad das ggf. abweichende
	//Hintergrundbild des Containers NICHT zurückgesetzt (s.u.)
	this.delete_container_background = p_delete_container_background == undefined ? false : p_delete_container_background;

	this.ctrlok = false;
	this.imgcontainer_0 = null;
	this.img_0 = null;
	this.imgcontainer_1 = null;
	this.img_1 = null;

	this.timerid = null;
	this.handle_img_OnLoad = false;

	this.firstmove = false;
	this.cursordir = 1;
	this.icursor = -1;
	this.imgtoggle = 1;
	this.act_img_obj = null;
	this.act_img_elm = null;

	this.blendms = 50;
	this.alphacounter = 0;

	if(document.getElementById && this.imglist.length > 0)
	{
		this.container_div = document.getElementById(this.ctrlid+ '_container');
		if(this.container_div != null)
		{
			if(this.centerimg == true)
			{
				this.container_div.style.backgroundPosition = 'center center';
			}

			var src_0 = 'cms4d____slideshow____blank____.gif';
			var t = '';
			for(var i = 0; i < 2; i++)
			{
				var imgcontainer_class2 = i == 0 ? 'imgcontainer_front' : 'imgcontainer_back';
				var img_class2 = i == 0 ? 'img_front' : 'img_back';

				t += '<div id="' + this.ctrlid + '_imgcontainer_' + i + '" class="imgcontainer ' + imgcontainer_class2 + '"';
				t += ' style="display:none';
				if(this.imgcontainercolor != '')t += ';background-color:' + this.imgcontainercolor;
				t += ';">';
					t += '<img src="' + src_0 + '"';
					t += ' id="' + this.ctrlid + '_img_' + i + '"';
					t += ' _cms4did="' + i + '"';
					t += ' class="img_abs ' + img_class2 + '"';
					t += ' style="display:block;"';
					t += ' onload="try{Cms4d.SlideShow.Controls.' + this.ctrlid + '.img_OnLoad(this, event)}catch(e){};"';
					t += ' onclick="Cms4d.SlideShow.Controls.' + this.ctrlid + '.img_OnClick(this, event);"';
					t += '/>\n';
				t += '</div>\n';
			}

			this.container_div.innerHTML += t;
			this.img_0 = document.getElementById(this.ctrlid + '_img_0');
			this.imgcontainer_0 = document.getElementById(this.ctrlid + '_imgcontainer_0');
			this.img_1 = document.getElementById(this.ctrlid + '_img_1');
			this.imgcontainer_1 = document.getElementById(this.ctrlid + '_imgcontainer_1');
			if(this.img_0 != null && this.img_1 != null)
			{
				var cw = this.container_div.offsetWidth;
				var ch = this.container_div.offsetHeight;
				with(this.imgcontainer_0.style)
				{
					width = cw + 'px';
					height = ch + 'px';
				}
				with(this.imgcontainer_1.style)
				{
					width = cw + 'px';
					height = ch + 'px';
				}
				this.ctrlok = true;
			}
		}
	}


	this.Pause = function()
	{
		clearTimeout(this.timerid);
		if(this.ctrlok != true)return;
		if(this.act_img_obj != null)
		{
			this.alphacounter = 100;
			this.SetOpacity(this.act_img_elm.parentNode, this.alphacounter);
			this.act_img_elm.style.visibility = 'visible';
		}
	}

	this.MoveCursor = function(new_dir)
	{
		clearTimeout(this.timerid);
		if(this.ctrlok != true)return;

		if(new_dir != undefined)this.cursordir = new_dir;

		this.icursor += this.cursordir;
		if(this.icursor < 0)this.icursor = this.imglist.length - 1;
		if(this.icursor >= this.imglist.length)this.icursor = 0;
		this.act_img_obj = this.imglist[this.icursor];
		this.old_img_elm = this['img_' + this.imgtoggle];
		this.imgtoggle = this.imgtoggle == 0 ? 1 : 0;
		this.act_img_elm = this['img_' + this.imgtoggle];

		this.handle_img_OnLoad = true;

		this.alphacounter = 0;
		if(new_dir != undefined)this.alphacounter = 100;

		//Wenn CSS background und erstes Bild aus der Liste identisch sind, wird die Deckkraft sofort auf 100% gesetzt.
		//Dadurch entfällt die Einblendphase und das erste Bild steht nur solange, wie seine Wartezeit gesetzt ist.
		//Andernfalls würde durch die Überblendung zweier identischer Bilder das erste gewünschte Bildmotiv für den Benutzer
		//länger sichtbar sein - nämlich Einblendzeit + Wartezeit.
		//I.d.R. sollten CSS background und erstes Bild aus der Liste identisch sein (case sensitiv !)
		if(this.firstmove == false)
		{
			this.firstmove = true;
			var firstsrc = Cms4d.StringReplace(this.imglist[0].src, '&amp;', '&');
			var bg = Cms4d.StringReplace(this.container_div.style.backgroundImage, '&amp;', '&');
			if(bg.indexOf(firstsrc) != -1)
			{
				this.alphacounter = 100;
			}
		}

		//28.11.2010, dogan
		//safari und chrome feuern kein load-Event auf dem einzublendenen img-Element wenn nur 2 Bilder zu wechseln sind.
		//Der Fehler lässt sich beheben, indem zuerst die Quelle leer gesetzt wird und dann zeitverzögert die
		//gewünschte Quelle. Könnte etwas mit dem abweichenden Caching-Verhalten dieser beiden Browser zu tun haben.
		//NICHT für NS7
		if(Cms4d.Browser != 'ns')this.act_img_elm.src = '';
		var func_str = 'Cms4d.SlideShow.Controls.' + this.ctrlid + '.set_act_img_elm_src()'
		this.timerid = setTimeout(func_str, 10);
	}
	this.set_act_img_elm_src = function()
	{
		clearTimeout(this.timerid);
		this.act_img_elm.src = this.act_img_obj.src;
	}

	this.SetOpacity = function(obj, a)
	{
		if(Cms4d.Browser == 'ie')
		{
			obj.style.filter = 'alpha(opacity=' + a + ')';
		}
		else
		{
			var new_op = (a / 100);
			if(new_op == 1 && Cms4d.Browser == 'ns')
			{
				new_op = 0.999999;
			}
			obj.style.opacity = new_op;
		}
	}

	this.img_OnLoad = function(obj, evnt)
	{
		clearTimeout(this.timerid);
		if(this.ctrlok != true || this.handle_img_OnLoad != true)return;
		if(obj.src == '')return;
		var cms4did = parseInt(obj.getAttribute('_cms4did'), 10);

		obj.style.visibility = 'hidden';
		if(obj.style.display != 'block')obj.style.display = 'block';
		if(obj.parentNode.style.display != 'block')obj.parentNode.style.display = 'block';

		//ggf. Hintergrundbild des Containers zurücksetzen, wenn diese Funktion zum ersten Mal aufgerufen wird.
		//Ein Container-Hintergrundbild kann z.B. abweichend festgelegt werden, wenn als erstes Bild über CSS
		//eine Ladegrafik statt dem ersten Bild aus der Liste angezeigt werden soll. Werden CSS background und
		//erstes Bildquelle aus der Liste versehentlich abweichend festgelegt, so kommt es vor dem Einblenden
		//des ersten Bildes aus der Liste zu einem Blitzen, da dann das Zurücksetzen des CSS background
		//kurz sichtbar wird, bevor das erste Bild aus der Liste von alpha=0 langsam eingeblendet wird.
		//Im Normalfall sind Container-Hintergrund und erstes Bild in der Liste identisch. Dann wird das Hintergrundbild
		//NICHT zurückgesetzt, auch wenn delete_container_background==true ist.
		if(this.delete_container_background == true)
		{
			var firstsrc = Cms4d.StringReplace(this.imglist[0].src, '&amp;', '&');
			var bg = Cms4d.StringReplace(this.container_div.style.backgroundImage, '&amp;', '&');
			if(bg != '' && bg.indexOf(firstsrc) == -1)
			{
				this.container_div.style.backgroundImage = '';
			}
		}

		this.SetOpacity(obj.parentNode, 0);

		if(this.centerimg == true)
		{
			var cw = this.container_div.offsetWidth;
			var ch = this.container_div.offsetHeight;
			var iw = obj.width;
			var ih = obj.height;
			if(Cms4d.Browser == 'ie' || Cms4d.Browser == 'opera')
			{
				var x = Math.round(cw / 2) - Math.round(iw / 2);
				var y = Math.round(ch / 2) - Math.round(ih / 2);
			}
			else
			{
				var x = Math.round(cw / 2) - Math.floor(iw / 2);
				var y = Math.round(ch / 2) - Math.floor(ih / 2);
			}
			with(obj.style)
			{
				left = x + 'px';
				top = y + 'px';
			}

		}
		obj.parentNode.style.zIndex = 3;

		var other_imgcontainer = cms4did == 0 ? this.imgcontainer_1 : this.imgcontainer_0;
		other_imgcontainer.style.zIndex = 1;

		var func_str = 'Cms4d.SlideShow.Controls.' + this.ctrlid + '.DoBlend()';
		this.timerid = setTimeout(func_str, 50);
	}

	this.DoBlend = function()
	{
		clearTimeout(this.timerid);
		if(this.ctrlok != true || this.act_img_elm == null)return;

		var bstep = this.act_img_obj.bstep;
		if(bstep <= 0)bstep = 5;
		this.alphacounter += bstep;
		if(this.alphacounter > 100)this.alphacounter = 100;

		this.SetOpacity(this.act_img_elm.parentNode, this.alphacounter);
		this.act_img_elm.style.visibility = 'visible';

		if(this.alphacounter < 100)
		{
			var func_str = 'Cms4d.SlideShow.Controls.' + this.ctrlid + '.DoBlend()';
			this.timerid = setTimeout(func_str, this.blendms);
		}
		else
		{
			this.do_wait();
		}
	}

	this.do_wait = function()
	{
		clearTimeout(this.timerid);
		if(this.ctrlok != true || this.act_img_obj == null)return;
		if(this.autoplay == true)
		{
			var waitms = this.act_img_obj.waitms;
			var func_str = 'Cms4d.SlideShow.Controls.' + this.ctrlid + '.MoveCursor()';
			this.timerid = setTimeout(func_str, waitms);
		}
	}

	this.img_OnClick = function(obj, evnt)
	{
		if(this.act_img_obj == null || this.act_img_obj.href == '')return;
		var href = this.act_img_obj.href;
		var tgt = this.act_img_obj.tgt;
		if(tgt == '_blank')
		{
			window.open(href, '');
		}
		else
		{
			document.location.href = href;
		}
	}


	this.do_status = function(s)
	{
		try
		{
			ausgabe.value = s;
		}
		catch(e){}
	}

	this.toString = function()
	{
		var t = '';
		t += 'ctrlid: ' + this.ctrlid + '\n';
		t += 'autoplay: ' + this.autoplay + '\n';
		t += 'centerimg: ' + this.centerimg + '\n';
		t += 'ctrlok: ' + this.ctrlok + '\n';
		t += 'cursordir: ' + this.cursordir + '\n';
		t += 'icursor: ' + this.icursor + '\n';
		t += 'imgtoggle: ' + this.imgtoggle + '\n';
		t += 'act_img_obj: ' + this.act_img_obj + '\n';
		t += 'alphacounter: ' + this.alphacounter + '\n';
		t += 'imglist:\n';
		for(var i = 0; i < this.imglist.length; i++)
		{
			t += i + ': ' + this.imglist[i].toString() + '\n';
		}
		t += '\n' + this.container_div.innerHTML + '\n';
		return(t);
	}
}

	Cms4d.SlideShow.ImageBlender.prototype.imgo = function(p_src, p_n, p_href, p_tgt, p_bstep, p_waitms)
	{
		this.src = p_src;
		this.n = p_n == undefined ? '' : p_n;
		this.href = p_href == undefined ? '' : p_href;
		this.tgt = p_tgt == undefined ? '' : p_tgt;
		this.bstep = p_bstep == undefined ? 5 : p_bstep;
		if(typeof(bstep) != 'number' || bstep <= 0)bstep = 5;
		this.waitms = p_waitms == undefined ? 1000 : p_waitms;
		this.toString = function()
		{
			var t = '';
			t += 'src: ' + this.src + ' | ';
			t += 'n: ' + this.n + ' | ';
			t += 'href: ' + this.href + ' | ';
			t += 'tgt: ' + this.tgt + ' | ';
			t += 'bstep: ' + this.bstep + ' | ';
			t += 'waitms: ' + this.waitms ;
			return(t);
		}
	}

setTimeout('Cms4d.SlideShow.FindControls()', 0);






