/** (c) 2011 concept computer GmbH **/
var ecomasphotogallery = {
	
	terms:{
		hide : "Bilder ausblenden",
		show : "Bilder einblenden"
	},
	
	memourl :  "",
	hide_derect : false,
	
	changeMethods:["moveleft","moveright","movetop","movebottom","fade"],
	currentChange : "random", // 
	
	// Darstellung
	buttons:{
		size:20,
		color:'#ffffff',
		fill:'transparent',
		scolor:'#ffffff',
		sfill:'#ffffff',
		ofill:'#cccccc',
		otxt:'#999999',
		obgc:'#ffffff'
	},
	
	autoplay:true,
	pause:10,//sek
	images:[],
	_placeholder:null,
	_controls:null,
	_controls_h:null,
	_curimg:0,
	_dplaceholder:null,
	_dcontainer:null,
	_tm:false,
	_moved:false,
	_movedMethods:"",
	_origh : 0,
	_smallh:0,
	
	init:function(settings)
	{
		if (typeof(settings) == "undefined") settings = new Object();
		if (typeof(settings.images) != "undefined") ecomasphotogallery.images = settings.images;
		if (typeof(settings.placeholder) != "undefined") ecomasphotogallery._placeholder = settings.placeholder;
		if (typeof(settings.autoplay) != "undefined") ecomasphotogallery.autoplay = settings.autoplay;
		if (typeof(settings.pause) != "undefined") ecomasphotogallery.pause = settings.pause;
		if (typeof(settings.memourl) != "undefined") ecomasphotogallery.memourl = settings.memourl;
		if (typeof(settings.hide) != "undefined") ecomasphotogallery.hide_derect = settings.hide;
		if (typeof(settings.currentChange) != "undefined") ecomasphotogallery.currentChange = settings.currentChange;
		ecomasphotogallery.addEvent(window,"load",ecomasphotogallery.load);
	},
	
	load:function()
	{
		if (ecomasphotogallery._placeholder == null) return;
		ecomasphotogallery.addCSS();
		ecomasphotogallery.draw();
		ecomasphotogallery.resize();
		ecomasphotogallery.addEvent(window,"resize",ecomasphotogallery.resize);
		if (ecomasphotogallery.autoplay && ecomasphotogallery.images.length > 1) ecomasphotogallery._tm = window.setInterval("ecomasphotogallery.play()",ecomasphotogallery.pause*1000);
		
		if (ecomasphotogallery.hide_derect) ecomasphotogallery.hide();
	},
	
	play:function()
	{
		var nr = ecomasphotogallery._curimg + 1;
		if (nr >= ecomasphotogallery.images.length) nr = 0;
		ecomasphotogallery.showNext(nr);
	},
	
	stop:function()
	{
		if (ecomasphotogallery._tm) { window.clearInterval(ecomasphotogallery._tm);  ecomasphotogallery._tm = false; }
	},
	
	show:function()
	{
		ecomasphotogallery._controls_h.style.display = "none";
		ecomasphotogallery._controls.style.display = "block";
		ecomasphotogallery._placeholder.style.height = ecomasphotogallery._origh.toString()+"px";
		ecomasphotogallery._dplaceholder.style.display = "block";
		if (ecomasphotogallery.autoplay && ecomasphotogallery.images.length > 1) ecomasphotogallery._tm = window.setInterval("ecomasphotogallery.play()",ecomasphotogallery.pause*1000);
		if (ecomasphotogallery.memourl != "")
		{
			var Img = new Image();
			Img.src = ecomasphotogallery.memourl + "0";
		}
		ecomasphotogallery.resize();
	},
	
	hide:function()
	{
		ecomasphotogallery._controls_h.style.display = "block";
		ecomasphotogallery._controls.style.display = "none";
		ecomasphotogallery._placeholder.style.height = ecomasphotogallery._smallh.toString()+"px";
		ecomasphotogallery._dplaceholder.style.display = "none";
		ecomasphotogallery.stop();
		if (ecomasphotogallery.memourl != "")
		{
			var Img = new Image();
			Img.src = ecomasphotogallery.memourl + "1";
		}
		ecomasphotogallery.resize();
	},
	
	addCSS:function()
	{
		var g = ecomasphotogallery.createObject("style","head",{"type":"text/css"});
		var r = Math.round(ecomasphotogallery.buttons.size/4);
		var dh = Math.round( Math.sqrt((ecomasphotogallery.buttons.size-3)*(ecomasphotogallery.buttons.size-3)-(ecomasphotogallery.buttons.size/2)*(ecomasphotogallery.buttons.size/2)));
		var css = '.ecomaspgcontrols { position:absolute; display:block; text-decoration:none;top:0;left:0; }'+"\n"
		+ '.ecomaspgcontrols .ecomaspg_bt { width:'+ecomasphotogallery.buttons.size.toString()+'px; height:'+ecomasphotogallery.buttons.size.toString()+'px; border:1px solid ' + ecomasphotogallery.buttons.color + ';	-webkit-border-radius: '+r.toString()+'px; -moz-border-radius: '+r.toString()+'px; border-radius: '+r.toString()+'px; float:right; margin-left:'+(r*2).toString()+'px; background-color:'+ecomasphotogallery.buttons.fill+'; cursor:pointer; overflow:hidden; font-size:96px;} '+"\n"
		+ '.ecomaspgcontrols .sel { border:1px solid ' + ecomasphotogallery.buttons.scolor + ';	background-color:'+ecomasphotogallery.buttons.sfill+'; } '+"\n"
		+ '.ecomaspgcontrols .min, .ecomaspgcontrolsh .max { float:right; margin-left:'+(r*6).toString()+'px; margin-right:'+ecomasphotogallery.buttons.size.toString()+'px; cursor:pointer;  } '+"\n"
		+ '.ecomaspgcontrols .min { border-color: transparent transparent '+ecomasphotogallery.buttons.sfill+' transparent; border-style: solid; border-width: 0px ' + dh.toString()+'px '+ecomasphotogallery.buttons.size.toString()+'px ' + dh.toString()+'px; width:1px; height:1px; } '+"\n"
		+ '.ecomaspgcontrolsh .max { border-color: '+ecomasphotogallery.buttons.ofill+' transparent transparent transparent; border-style: solid; border-width: '+ecomasphotogallery.buttons.size.toString()+'px ' + dh.toString()+'px 0px ' + dh.toString()+'px; width:1px; height:1px; margin:6px '+ecomasphotogallery.buttons.size.toString()+'px 0 0; } '+"\n"
		+ '.ecomaspgcontrolsh { position:absolute; text-decoration:none; top:0;left:0; background-color:' +ecomasphotogallery.buttons.obgc+'; } '+"\n"
		+ '.ecomaspgcontrolsh .maxtxt { color: '+ecomasphotogallery.buttons.otxt+'; text-decoration:none; font-size:'+ecomasphotogallery.buttons.size.toString()+'px; margin-top:6px; cursor:pointer; float:right; padding-right:'+(r*6).toString()+'px;} '+"\n"
		
		;
		if (typeof(g.styleSheet) != "undefined") g.styleSheet.cssText = css;
		else
		{
			try
			{
				g.innerHTML = css;
			}
			catch(ext)
			{
				try{
					var text = document.createTextNode(css);
					g.appendChild(text);
				}
				catch(ext2){}
			}
		}
	},
	
	/*** ### BEGIN EFFECTS ### ***/
	moveImages:function(nr1,nr2)
	{
		ecomasphotogallery._dplaceholder.style.backgroundRepeat = "no-repeat";
		ecomasphotogallery._placeholder.style.backgroundRepeat = "no-repeat";
		ecomasphotogallery._dplaceholder.style.backgroundImage = "url("+ecomasphotogallery.images[nr2].source+")";
		ecomasphotogallery._placeholder.style.backgroundImage = "url("+ecomasphotogallery.images[nr1].source+")";
		if (nr1 == nr2) return;
		var steps = 100;
		if (!ecomasphotogallery._moved) ecomasphotogallery._curstep = 1;
		else ecomasphotogallery._curstep = ecomasphotogallery._curstep + 1;
		
		if (ecomasphotogallery._curstep == 1)
		{
			ecomasphotogallery._movedMethods = ecomasphotogallery.currentChange;
			if (ecomasphotogallery._movedMethods == "random") ecomasphotogallery._movedMethods = ecomasphotogallery.changeMethods[ecomasphotogallery.random(0,ecomasphotogallery.changeMethods.length-1)];
		}
		
		if (ecomasphotogallery._curstep >= 100)
		{
			ecomasphotogallery._moved = false;
			// "moveleft","moveright","movetop","movebottom","fade"
			
			ecomasphotogallery._dplaceholder.style.backgroundImage = "url("+ecomasphotogallery.images[nr1].source+")";
			ecomasphotogallery._dplaceholder.style.backgroundPosition = "";
			ecomasphotogallery._placeholder.style.backgroundPosition = "";
			ecomasphotogallery._dplaceholder.style.opacity = "1";
			ecomasphotogallery._dplaceholder.style.filter = "";
			if ( ecomasphotogallery.autoplay && ecomasphotogallery.images.length > 1 && !ecomasphotogallery._tm) ecomasphotogallery._tm = window.setInterval("ecomasphotogallery.play()",ecomasphotogallery.pause*1000);
		}
		else
		{
			ecomasphotogallery._moved = true;
			switch(ecomasphotogallery._movedMethods)
			{	
				case "moveleft":
					var w = parseInt(ecomasphotogallery._dplaceholder.style.width);
					var pps = w / steps;
					ecomasphotogallery._dplaceholder.style.backgroundPosition = "-"+(pps*ecomasphotogallery._curstep).toString()+"px 0px";
					ecomasphotogallery._placeholder.style.backgroundPosition = (w-pps*ecomasphotogallery._curstep).toString()+"px 0px";
					break;
				case "moveright":
					var w = parseInt(ecomasphotogallery._dplaceholder.style.width);
					var pps = w / steps;
					ecomasphotogallery._dplaceholder.style.backgroundPosition = (pps*ecomasphotogallery._curstep).toString()+"px 0px";
					ecomasphotogallery._placeholder.style.backgroundPosition = "-"+(w-pps*ecomasphotogallery._curstep).toString()+"px 0px";
					break;
				case "movetop":
					var w = parseInt(ecomasphotogallery._dplaceholder.style.height);
					var pps = w / steps;
					ecomasphotogallery._dplaceholder.style.backgroundPosition = "0px -"+(pps*ecomasphotogallery._curstep).toString()+"px";
					ecomasphotogallery._placeholder.style.backgroundPosition = "0px "+(w-pps*ecomasphotogallery._curstep).toString()+"px";
					break;
				case "movebottom":
					var w = parseInt(ecomasphotogallery._dplaceholder.style.height);
					var pps = w / steps;
					ecomasphotogallery._dplaceholder.style.backgroundPosition = "0px "+(pps*ecomasphotogallery._curstep).toString()+"px";
					ecomasphotogallery._placeholder.style.backgroundPosition = "0px -"+(w-pps*ecomasphotogallery._curstep).toString()+"px";
					break;
				case "fade":
				default:
					ecomasphotogallery._dplaceholder.style.opacity = ((100-ecomasphotogallery._curstep)/100).toString();
					ecomasphotogallery._dplaceholder.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + (100-ecomasphotogallery._curstep).toString() + ")" ;
					break;
			}
			window.setTimeout("ecomasphotogallery.moveImages("+nr1.toString()+","+nr2.toString()+")",1);
		}
	},
	
	/*** ### END EFFECTS ### ***/
	
	showNext:function(nr,ct)
	{
		if (typeof(ecomasphotogallery.images[nr]) == "undefined") return;
		if (ecomasphotogallery._moved) return;
		if (typeof(ct) == "undefined") ct = false;
		if (ct) { ecomasphotogallery.stop(); }
		ecomasphotogallery._placeholder.title = ecomasphotogallery.images[nr].alt;
		ecomasphotogallery._dplaceholder.title = ecomasphotogallery.images[nr].alt;
		ecomasphotogallery.moveImages(nr,ecomasphotogallery._curimg);
		if (ecomasphotogallery.images.length > 1)
		{
			document.getElementById("ecomaspf_bt_"+ecomasphotogallery._curimg.toString()).className = "ecomaspg_bt";
			document.getElementById("ecomaspf_bt_"+nr.toString()).className = "ecomaspg_bt sel";
			ecomasphotogallery._curimg = nr;
		}
	},
	
	draw:function()
	{
		ecomasphotogallery._dcontainer = document.getElementById("ecomaspg_dcontainer");
		if(!ecomasphotogallery._dcontainer)
		{
			ecomasphotogallery._dcontainer = ecomasphotogallery.createObject("DIV","body",{"id":"ecomaspg_dcontainer"});
			ecomasphotogallery._dcontainer.style.cssText = "position:absolute;display:none;top:-500px;";
		}
		ecomasphotogallery._dplaceholder = document.getElementById("ecomaspg_dplaceholder");
		if(!ecomasphotogallery._dplaceholder)
		{
			ecomasphotogallery._dplaceholder = ecomasphotogallery.createObject("DIV","body",{"id":"ecomaspg_dplaceholder"});
			ecomasphotogallery._dplaceholder.style.cssText = "z-index:227;position:absolute;";
		}
		ecomasphotogallery._controls = document.getElementById("ecomaspgcontrols");
		if(!ecomasphotogallery._controls)
		{
			ecomasphotogallery._controls = ecomasphotogallery.createObject("DIV","body",{"id":"ecomaspgcontrols","class":"ecomaspgcontrols"});
			ecomasphotogallery._controls.style.zIndex = "228";
		}
		ecomasphotogallery._controls_h = document.getElementById("ecomaspgcontrolsh");
		if(!ecomasphotogallery._controls_h)
		{
			ecomasphotogallery._controls_h = ecomasphotogallery.createObject("DIV","body",{"id":"ecomaspgcontrolsh","class":"ecomaspgcontrolsh"});
			ecomasphotogallery._controls_h.style.zIndex = "229";
			ecomasphotogallery._controls_h.style.display = "none";
		}
		
		var html = "";
		var dummyImage = new Array();
		// Min-Max Button
		html += '<div id="ecomaspg_min" onclick="ecomasphotogallery.hide();" class="min" title="'+ecomasphotogallery.terms.hide+'"></div>';
		if (ecomasphotogallery.images.length > 1)
		{
			for (var i = ecomasphotogallery.images.length; i > 0; i--)
			{
				html += '<a id="ecomaspf_bt_' + (i-1).toString() + '" class="ecomaspg_bt'+(i == 1 ? ' sel' : '')+'" onclick="ecomasphotogallery.showNext(' + (i-1).toString() + ',true);">&nbsp;</a>';
				ecomasphotogallery._dcontainer.innerHTML += '<img src="'+ecomasphotogallery.images[i-1].source+'" style="width:1px;height:1px;" alt="" />';
			}
		}
		ecomasphotogallery._controls.innerHTML = html;
		ecomasphotogallery._controls_h.innerHTML = '<div id="ecomaspg_max" onclick="ecomasphotogallery.show();" class="max" title="'+ecomasphotogallery.terms.show+'"></div><div class="maxtxt" onclick="ecomasphotogallery.show();">'+ecomasphotogallery.terms.show+'</div>';
		ecomasphotogallery.showNext(0);
	},
	
	resize:function()
	{
		ecomasphotogallery._placeholder.style.display = "";
		var pos = ecomasphotogallery.getElementPosition(ecomasphotogallery._placeholder);
		if(ecomasphotogallery._origh == 0) ecomasphotogallery._origh = pos.height;
		if(ecomasphotogallery._smallh == 0) ecomasphotogallery._smallh = ecomasphotogallery.buttons.size+10;
		ecomasphotogallery._controls.style.width = pos.width.toString()+"px";
		ecomasphotogallery._controls.style.height = (ecomasphotogallery.buttons.size+4).toString()+"px";
		ecomasphotogallery._controls.style.left = pos.x.toString()+"px";
		ecomasphotogallery._controls.style.top = (pos.y+pos.height-ecomasphotogallery.buttons.size-ecomasphotogallery.buttons.size).toString()+"px";
		ecomasphotogallery._controls_h.style.width = pos.width.toString()+"px";
		ecomasphotogallery._controls_h.style.height = (ecomasphotogallery.buttons.size+10).toString()+"px";
		ecomasphotogallery._controls_h.style.left = pos.x.toString()+"px";
		ecomasphotogallery._controls_h.style.top = pos.y.toString()+"px";
		
		ecomasphotogallery._dplaceholder.style.left = pos.x + "px";
		ecomasphotogallery._dplaceholder.style.top = pos.y + "px";
		ecomasphotogallery._dplaceholder.style.width = pos.width + "px";
		ecomasphotogallery._dplaceholder.style.height = pos.height + "px";
	},
	
	
	/** GENERAL FUNCTIONS **/
	addEvent:function(o,n,h)
	{
		if(o.attachEvent) o.attachEvent("on"+n,h);
		else if (o.addEventListener) o.addEventListener(n,h,false);
	},
	removeEvent:function(o,n,h)
	{
		try
		{
			if(o.detachEvent) o.detachEvent("on"+n,h);
			else if (o.removeEventListener) o.removeEventListener(n,h,false);
		}
		catch(ext) {}
	},
	getWidth:function (obj)
	{
		var Width=obj.clientWidth;
		if(obj.offsetWidth && obj.offsetWidth > Width && (Width == 0 || /msie/i.test(navigator.userAgent))) Width=obj.offsetWidth;//für IE
		return Width;
	},
	getHeight:function (obj)
	{
		var Height=obj.clientHeight;
		if(obj.offsetHeight && obj.offsetHeight > Height&& (Height == 0 || /msie/i.test(navigator.userAgent))) Height=obj.offsetHeight;//für IE
		return Height;
	},
	getX:function (obj,relative)
	{
		var x=0;
		if (typeof(relative) == "undefined") relative = false;
		if(typeof(obj)=="object" && document.getElementById)
		{
			x=obj.offsetLeft;
			if(obj.offsetParent && !relative)
			{
				x += ecomasphotogallery.getX(obj.offsetParent);
			}
			return x;
		}
		else return false;
	},
	getY:function(obj,relative)
	{
		var y=0;
		if (typeof(relative) == "undefined") relative = false;
		if(typeof(obj)=="object" && document.getElementById)
		{
			y=obj.offsetTop;
			if(obj.offsetParent && !relative)
			{
				y += ecomasphotogallery.getY(obj.offsetParent);
			}
			return y;
		} else return false;
	},
	getElementPosition:function(obj)
	{
		var x = ecomasphotogallery.getX(obj);
		var y = ecomasphotogallery.getY(obj);
		var width = ecomasphotogallery.getWidth(obj);
		var height = ecomasphotogallery.getHeight(obj);
		return {x:x, y:y, width:width,height:height};
	},
	createObject:function(tag,where,attrib)
	{
		if (typeof(where) == "undefined") where = "body";
		if (typeof(where) == "string")
		{
			if (document.getElementsByTagName) where = document.getElementsByTagName(where)[0];
			else if (document.all) where = document.all(where).parentNode;
		}
		var g = document.createElement(tag);
		
		if (typeof(attrib) != "undefined")
		{
			for(var a in attrib)
			{
				ecomasphotogallery.setAttrib(g,a,attrib[a]);
			}
		}
		
		where.appendChild(g);
		return g;
	},
	random:function(n1,n2)
	{
		if(n1 > n2) return n2;
		if(n1 == n2 ) return(n1);
		return n1+parseInt(Math.random()*(n2-n1+1));
	},
	setAttrib:function(selEl, nm, val)
	{
		if (val == "0%" || val == "%") val = "";
		if(nm == "style") selEl.style.cssText = val;
		else if(nm == "class") selEl.className = val;
		else if (nm == "noWrap") selEl.noWrap = (val == "false" || !val) ? false : true;
		else if (val != null && val != "" && val != -1) selEl.setAttribute(nm,val);
		else selEl.removeAttribute(nm);
	},
	getAttrib:function(selEl, nm, default_value)
	{
		if(typeof(default_value) == "undefined") default_value = "";
		var v = default_value;
		if (selEl.getAttribute)
		{
			if (selEl.getAttribute(nm,0) != null) v = selEl.getAttribute(nm,0);
		}
		else if (selEl.attributes)
		{
			var oAttrColl = selEl.attributes;
			var oAttr = oAttrColl.getNamedItem(nm);
			if(oAttr != null) v = oAttr.value;
		}
		if (nm == "noWrap") v = selEl.noWrap;
		if (nm == "class") v = selEl.className;
		return '' + v;
	}
};
