var newImageId = 0;
var accionUndo = '';
var undoVars = '';
var seleccion = '';
var drags = new Array();
var escala = new Array();
var xo = new Array();
var yo = new Array();
var sliderStart = 0;
var demoSlider = '';
var swdel = 0;

function addImage(image) {
	
	if(document.getElementById('image' + newImageId) != null) {
		sister = 'image' + newImageId;
	}
	else {
		sister = 'subimage' + newImageId;
	}
	
	newImageId++;
	
	pars = 'action=add&newid=' + newImageId + '&src=' + image;
	newObject = new Ajax.Updater(sister, 'toolfunctions.php', { parameters: pars, evalScripts: true, method: 'post', insertion: Insertion.After, 
			onComplete: function() {
				
				var ccoords = findPos(document.getElementById('wallpaper'));
				var cx = ccoords[0];
				var cy = ccoords[1];

				newImage = 'image' + newImageId;
				coords = findPos(document.getElementById(newImage));
				
				x = (cx - coords[0]) + 5 + 'px';
				y = (cy - coords[1]) + 5 + 'px';

				$(newImage).style.position = 'relative';
				$(newImage).style.left = x;
				$(newImage).style.top = y;
				$(newImage).style.visibility = 'visible';
				
			} });
};

function selectElement(obj) {
	if(swdel == 0) {
		limpia_seleccion();
		seleccion = obj.id;
		obj.style.padding = '0';
		obj.style.border = '1px outset Green';
		if(typeof escala[seleccion] != 'undefined') {
			demoSlider.setValue(escala[seleccion]);
		}
		else {
			xo[seleccion] = obj.style.width;
			yo[seleccion] = obj.style.height;
			xo[seleccion] = parseInt(xo[seleccion]);
			yo[seleccion] = parseInt(yo[seleccion]);
			demoSlider.setValue(1);
		}
		margen = 0;
	}
};

function removeElement() {
	if(seleccion != '') {
		obj = document.getElementById(seleccion);
		swdel = 1;
		pars = 'action=remove&oid=' + obj.id +'&width=' + parseInt(obj.style.width) + '&height=' + (parseInt(obj.style.height) + 2) + '&margin=' + parseInt(obj.style.marginBottom);
		thumbs = document.getElementsByClassName('thumb');
		for(i=0; i<thumbs.length; i++) {
			thumbs[i].disabled = true;
		}
		new Effect.Fade(obj, { to: 0.1, afterFinish: function() { 
			menos = new Ajax.Updater(seleccion, 'toolfunctions.php', { parameters: pars, method: 'post', insertion: Insertion.After, 
			onComplete: function() {
				Element.remove(seleccion);
				seleccion = '';
				for(i=0; i<thumbs.length; i++) {
					thumbs[i].disabled = false;
				}
				swdel = 0;
			} })
		} });
	}
};

function limpia_seleccion() {
	//limpia
	if(seleccion != '' && swdel == 0) {
		pasado = document.getElementById(seleccion);
		pasado.style.border = 'none';
		pasado.style.padding = '1px';
		seleccion = '';
	}
};

function setResizeVars() {
	sliderStart = 1;
};

function scaleIt(v) {

	if(seleccion != '') {
		var escalancia = document.getElementById(seleccion);

		floorSize = .26;
		ceilingSize = 1.0;
		v = floorSize + (v * (ceilingSize - floorSize));
		escalancia.style.width = Math.floor((v * xo[seleccion]))+'px';
		escalancia.style.height = Math.floor((v * yo[seleccion]))+'px';
		margen = yo[seleccion] - parseInt(escalancia.style.height);
		escalancia.style.marginBottom = margen + 'px';
	}

};

function undo() {
	if(accionUndo == 'move') {
		vars = undoVars.split("-");
		obj = document.getElementById(vars[0]);
		x1 = vars[1];
		y1 = vars[2];
		
		cPos = findPos(obj);
		x2 = cPos[0];
		y2 = cPos[1];
		
		difX = x1 - x2;
		difY = y1 - y2;
		
		new Effect.Move(obj, { x: difX, y: difY, mode: 'relative' });
		
	}
	
	if(accionUndo == 'resize') {
		
		vars = undoVars.split("-");
		obj = document.getElementById(vars[0]);
		esc = vars[1];
		
		escala[obj.id] = esc;
		selectElement(obj);
		
		demoSlider.setValue(esc);
	}
	
};

function clearAll() {
	var images = document.getElementsByClassName('wallElement');
	for(i = 0; i < images.length; i++) {
		cid = images[i].id;
		if(cid != 'image0') {
			Element.remove(images[i]);
			drags[i].destroy();
			xo[cid] = 0;
			yo[cid] = 0;
			escala[cid] = undefined;
		}
	}
	
	images = document.getElementsByClassName('container');
	for(i = 0; i < images.length; i++) {
		Element.remove(images[i]);
	}
	
	newImageId = 0;
	seleccion = '';
	demoSlider.setValue(1);
}

function duplicate() {
	if(seleccion != '') {
		src = $(seleccion).src;
		addImage(src);
	}
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
};

function changeBackground(bgsrc) {
	$('image0').style.backgroundImage = 'url(\'' + bgsrc + '\')';
};

function compileData() {
	var images = document.getElementsByClassName('wallElement');
	allow = 1;
	swSubmit = false;
	allow = 0;
	
	if( parseInt($('screenwidth').value) > 1920 || parseInt($('screenheight').value) > 1200) {
		alert('Max resolution is 1920 x 1200');
		allow = 0;
	}
	else { allow = 1; }
	
	if(allow == 1) {
		
		verifs = document.getElementsByClassName('verif');
		for(i=0;i<verifs.length;i++) {
			Element.remove(verifs[i]);
		}
		
		for(i = 0; i < images.length; i++) {
		
			src = '';
		
			inID = images[i].id;
		
			if(typeof images[i].src != 'undefined') {
				src = images[i].src;
			}
			else {
				src = images[i].style.backgroundImage;
				src = src.replace( /"/g, '' );
			}
			
			//INICIA
			
			var BrowserDetect = {
				init: function () {
					this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
					this.version = this.searchVersion(navigator.userAgent)
						|| this.searchVersion(navigator.appVersion)
						|| "an unknown version";
					this.OS = this.searchString(this.dataOS) || "an unknown OS";
				},
				searchString: function (data) {
					for (var i=0;i<data.length;i++)	{
						var dataString = data[i].string;
						var dataProp = data[i].prop;
						this.versionSearchString = data[i].versionSearch || data[i].identity;
						if (dataString) {
							if (dataString.indexOf(data[i].subString) != -1)
								return data[i].identity;
						}
						else if (dataProp)
							return data[i].identity;
					}
				},
				searchVersion: function (dataString) {
					var index = dataString.indexOf(this.versionSearchString);
					if (index == -1) return;
					return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
				},
				dataBrowser: [
					{ 	string: navigator.userAgent,
						subString: "OmniWeb",
						versionSearch: "OmniWeb/",
						identity: "OmniWeb"
					},
					{
						string: navigator.vendor,
						subString: "Apple",
						identity: "Safari"
					},
					{
						prop: window.opera,
						identity: "Opera"
					},
					{
						string: navigator.vendor,
						subString: "iCab",
						identity: "iCab"
					},
					{
						string: navigator.vendor,
						subString: "KDE",
						identity: "Konqueror"
					},
					{
						string: navigator.userAgent,
						subString: "Firefox",
						identity: "Firefox"
					},
					{
						string: navigator.vendor,
						subString: "Camino",
						identity: "Camino"
					},
					{		// for newer Netscapes (6+)
						string: navigator.userAgent,
						subString: "Netscape",
						identity: "Netscape"
					},
					{
						string: navigator.userAgent,
						subString: "MSIE",
						identity: "Explorer",
						versionSearch: "MSIE"
					},
					{
						string: navigator.userAgent,
						subString: "Gecko",
						identity: "Mozilla",
						versionSearch: "rv"
					},
					{ 		// for older Netscapes (4-)
						string: navigator.userAgent,
						subString: "Mozilla",
						identity: "Netscape",
						versionSearch: "Mozilla"
					}
				],
				dataOS : [
					{
						string: navigator.platform,
						subString: "Win",
						identity: "Windows"
					},
					{
						string: navigator.platform,
						subString: "Mac",
						identity: "Mac"
					},
					{
						string: navigator.platform,
						subString: "Linux",
						identity: "Linux"
					}
				]

			};
			BrowserDetect.init();
			
			//alert(BrowserDetect.browser + ' ' + BrowserDetect.version);
			
			//TERMINA
			
			if(BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7 && images[i].id != 'image0') {
				src = images[i].style.filter;
			}
			
			//if(images[i].style.filter != undefined && images[i].id != 'image0' && src == '') { src = images[i].style.filter; }
		
			wcoords = findPos($('wallpaper'));
			wx = wcoords[0];
			wy = wcoords[1];
		
			icoords = findPos(images[i]);
			ix = icoords[0] - wx;
			iy = icoords[1] - wy;
		
			iw = parseInt(images[i].style.width);
			ih = parseInt(images[i].style.height);
		
			pars = 'action=compile&id=' + inID +'&src=' + src + '&x=' + ix + '&y=' + iy + '&w=' + iw + '&h=' + ih;
			inputea = new Ajax.Updater('imgdata', 'toolfunctions.php', { parameters: pars, method: 'post', insertion: Insertion.After, 
			onComplete: function() {

				verifs = document.getElementsByClassName('verif');
				
				if(verifs.length == images.length) {
					
					document.walldata.submit();
				
				}
			} });
		}
	}
	
};