function gebi(obj) { return document.getElementById(obj); }

var PMoveObjects_photo_new = {};
function getPMove_photo_new(id) {
	if (typeof(PMoveObjects_photo_new[id]) == "undefined") {
		PMoveObjects_photo_new[id] = new PMove_photo_new(id);
	}
	return PMoveObjects_photo_new[id];
}

function PMove_photo_new(id) {
	this.id = id;
	this.previewWidth = false;
	this.previewHeight = false;
	this.containerWidth = 0;
	this.containerWidthMin = 300;
	this.previewCount  = 0;
	
	this.marginLeft = false;
	this.scriptURL = "";
	this.xmlEnable = false;
	this.initFlag = 0;
	
	this._container = "cont_";
	this._leftAngle = "left_";
	this._rightAngle = "right_";
	
	this.defaultArray = [];
	this.casheImages = [];
	this.curPos = 0;
	this.isRight = 0;
	this.isLeft = 0;
	
	this.container = null;
	this.leftAngle = null;
	this.rightAngle = null;
	
	// Scroll
	this.initTimeout = 200;
	this.stepTimeout = 200;
	this.stepDirection = "";

	this.init = function(w, h, m, e, u, d) {
		if (
			typeof w == "undefined" ||
			typeof h == "undefined" ||
			typeof m == "undefined" ||
			typeof u == "undefined" ||
			typeof d == "undefined"
		) return;
		this.previewWidth = w;
		this.previewHeight = h;
		this.marginLeft = m;
		this.scriptURL = u;
		this.defaultArray = d;
		this.container = this.gebi(this._container + this.id);
		if (this.container == null) {
			// Error. Do nothing
			return;
		}
		this.leftAngle = this.gebi(this._leftAngle + this.id);
		if (this.leftAngle == null) {
			// Error. Do nothing
			return;
		}
		this.rightAngle = this.gebi(this._rightAngle + this.id);
		if (this.rightAngle == null) {
			// Error. Do nothing
			return;
		}
		if (this.defaultArray.length >= 1) {
			// Mix preview
			this.mixPreview();
			// Draw preview & angles
			this.drawPreview();
			this.drawAngles();
			// Add rezize-event handler
			var id = this.id;
			this.addHandler(
				window,
				"resize",
				function() {
					try {getPMove_photo_new(id).drawPreview();} catch(e) {}
				}
			);
			this.addHandler(
				document, 
				"mouseup", 
				function() {getPMove_photo_new(id).mouseUpHandler()}
			);
			this.addHandler(this.leftAngle, "mousedown", function(evt) {
				evt = evt || window.event;
				if (evt.preventDefault) evt.preventDefault();
			});
			this.addHandler(this.rightAngle, "mousedown", function(evt) {
				evt = evt || window.event;
				if (evt.preventDefault) evt.preventDefault();
			});
			this.addHandler(this.leftAngle, "dragstart", function() {return false;});
			this.addHandler(this.rightAngle, "dragstart", function() {return false;});
			this.initFlag = 1;
		} else {
			// Error. Do nothing
			return;
		}
	}
}

var stepTimer = {};
PMove_photo_new.prototype = {
	// ----------- Default -----------
	gebi : function(id) {
		return document.getElementById(id);
	},
	newImg : function(path) {
		var image = new Image();
		image.src = path;
		return image;
	},
	addHandler : function(object, event, handler, useCapture) { 
		if (object.addEventListener) { 
			object.addEventListener(event, handler, useCapture ? useCapture : false); 
		} else if (object.attachEvent) { 
			object.attachEvent('on' + event, handler); 
		} 
	},
	screenSize : function() { 
		var w, h;
		w = (window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.offsetWidth)); 
		h = (window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.offsetHeight)); 
		return {w:w, h:h}; 
	},
	// ----------- Funcs -----------
	scrollInit : function(order) {
		if (this.xmlEnable) this.ajaxLoad();
		this.stepDirection = order;
		var id = this.id;
		stepTimer[id] = setInterval("getPMove_photo_new('" + id + "').scrollRepeat()", 500);
	},
	scrollRepeat : function() {
		this.showNext(this.stepDirection);
		var id = this.id;
		if (typeof stepTimer[id] == "undefined" || stepTimer[id] != null) {
			clearInterval(stepTimer[id]);
			stepTimer[id] = null;
			stepTimer[id] = setInterval("getPMove_photo_new('" + id + "').scrollRepeat()", this.stepTimeout);
		}
	},
	mouseUpHandler : function() {
		var id = this.id;
		if (typeof stepTimer[id] == "undefined" || stepTimer[id] == null) return;
		clearInterval(stepTimer[id]);
		stepTimer[id] = null;
		this.showNext(this.stepDirection);
	},
	mixPreview : function() {
		if (this.defaultArray.length > 1) {
			var randKey = 0;
			var bufer = [];
			for (i = 0; i <= 3; i++) {
				randKey = parseInt(Math.random() * this.defaultArray.length);
				if (typeof this.defaultArray[i] == "undefined") continue;
				bufer = this.defaultArray[i];
				this.defaultArray[i] = this.defaultArray[randKey];
				this.defaultArray[randKey] = bufer;
			}
		}
	},
	drawPreview : function() {
		this.containerWidthDetect();
		var html = [];
		try {
			html[html.length] = "<table class='w0 h0'><tr>";
			var j;
			for (i = 0; i < this.previewCount; i++) {
				j = (this.curPos + i) % this.defaultArray.length;
				this.casheImages[j] = this.newImg(this.defaultArray[j][1]);

				if (!(document.all && !window.opera && parseInt(/msie\s([^;]+)/i.exec(navigator.appVersion)[1]) <= 6))
					{var watermark_src = '';}
				else
					{var watermark_src = '';}

				if (this.id == "video")
				{
					var preview_unit = '';
				}
				else
				{
					var preview_unit = '<td class="s bc1"><a href="' + this.defaultArray[j][0] + '" rel="lightbox[fn]" title="'+this.defaultArray[j][2]+' "><img id="preview_' + this.id + i + '_Id" src="' + this.defaultArray[j][1] + '"' + (this.previewWidth ? ' width="' + this.previewWidth + '"' : '') + (this.previewHeight ? ' height="' + this.previewHeight + '"' : '') + ' /></a></td><td width=7 class=bc1></td>';
				}
				html[html.length] = preview_unit;
			}
			html[html.length] = "</tr></table>";
			html = html.join("");
		} catch (e) {
			// Error. Do nothing
			return;
		}
		this.container.innerHTML = html;
	},
	showPreview : function() {
		var j;
		for (i = 0; i < this.previewCount; i++)
		{
			j = (this.curPos + i) % this.defaultArray.length;
			this.gebi("preview_" + this.id + i + "_Id").src = this.defaultArray[j][1];

			if (this.id == "video") {gebi("preview_" + this.id + i + "_Id").nextSibling.firstChild.href = this.defaultArray[j][0];}
			else {this.gebi("preview_" + this.id + i + "_Id").parentNode.href = this.defaultArray[j][0];}

		}
	},
	containerWidthDetect : function() {
		this.containerWidth = this.screenSize().w * 0.9 * 0.26;
		this.containerWidth = this.containerWidth < this.containerWidthMin ? this.containerWidthMin : this.containerWidth;
		this.previewCount = 2;
	},
	showNext : function(side) {
		if (side == "right") this.curPos++;
		else this.curPos--;
		if (this.curPos < 0) this.curPos = this.defaultArray.length - 1;
		if (this.curPos > this.defaultArray.length - 1) this.curPos = 0;
		this.showPreview();
		this.drawAngles();
		if (side == "left" && !this.isLeft) {
			clearInterval(stepTimer[this.id]);
			stepTimer[this.id] = null;
		}
		return false;
	},
	drawAngles : function() {
		this.define();
		var i = a = img = 0, node, nodes = {};
		for (i = 0; i < this.leftAngle.childNodes.length; i++) {
			node = this.leftAngle.childNodes[i];
			if (node.nodeType != 1) continue;
			nodes[node.nodeName.toLowerCase()] = node;
		}
		if (typeof nodes["a"] != "undefined" && typeof nodes["img"] != "undefined") {
			nodes["a"].style.display = this.isLeft ? "" : "none";
			nodes["img"].style.display = this.isLeft ? "none" : "";
			if (!this.initFlag) nodes["img"].style.backgroundColor = "";
		}
		for (i = 0; i < this.rightAngle.childNodes.length; i++) {
			node = this.rightAngle.childNodes[i];
			if (node.nodeType != 1) continue;
			nodes[node.nodeName.toLowerCase()] = node;
		}
		if (typeof nodes["a"] != "undefined" && typeof nodes["img"] != "undefined") {
			nodes["a"].style.display = this.isRight ? "" : "none";
			nodes["img"].style.display = this.isRight ? "none" : "";
			if (!this.initFlag) nodes["img"].style.backgroundColor = "";
		}
		//this.loadPreview();
	},
	define : function() {
		/*
		//this.isLeft = this.curPos ? true : false;
		this.isLeft = true;
		this.isRight = true;
		*/
		this.isLeft = this.curPos ? true : false;
		this.isRight = true;
	},
	loadPreview : function() {
		if (this.casheImages.length != this.defaultArray.length) {
			for (i = 0; i < this.previewCount * 2; i++) {
				j = (this.curPos + i) % this.defaultArray.length;
				if (typeof this.casheImages[j] == "undefined") this.casheImages[j] = this.newImg(this.defaultArray[j][1]);
			}
		}
	},
	ajaxLoad : function() {
		var _this = this;
		this.scriptURL = 'http://' + window.location.hostname + this.scriptURL;
		ajax_call_static(
			this.scriptURL,
			function(arr) {
				var item = new Array();
				if (arr.length) {
					for (var i = 0; i < arr.length; i++) {
						item = [
							arr[i]["url"],
							arr[i]["prevurl"]
						];
						_this.defaultArray[_this.defaultArray.length] = item;
					}
				}
			}
		);
		this.xmlEnable = false;
	}
}
