/**
* @package Joomla
* @subpackage com_PAXXGallery
* @copyright (C) Tobias Floery <tobias.floery@cable.vol.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*/
	
	var req;
	var cur = 0;
	var slideRunning = false;
	var imgField, txtField, descField;
	
	function doSlideShow() {
		if (slideRunning) {
			goNextButton();
			setTimeout("doSlideShow()",timeout);
		}
	}
	
	function toggleSlideshow(on, off) {
		slideRunning = ~slideRunning;
		doSlideShow();
				
		if (slideRunning) {
			document.getElementById("slideLink").innerHTML = off;
		} else {
			document.getElementById("slideLink").innerHTML = on;			
		}
		
	}
	
	
	function init() { 
		imgField = document.getElementById("PAXXimgContainer");
		txtField = document.getElementById("PAXXimgTitle");
		descField = document.getElementById("PAXXimgDesc");
		sndReq(firstID, 0);
	}
	
	function goNextButton() {
		sndReq(cur, +1);
	}

	function goPrevButton() {			
		sndReq(cur, -1);
	}
		
	/*
	function sndSliderReq(index) {
		try {
			if (typeof index == 'string')
				index = parseInt(index);
				
		} catch (e) {
			alert(e);	
		}
		
		index = index -1;
		sndReq(index);
	}*/
	
	function sndSliderReq(id) {
		sndReq(id,0);	
	}
	
	function sndReq(id, dir) {
		
		// branch for native XMLHttpRequest object
		try {
			if (typeof id == 'string')
				id = parseInt(id);
				
		} catch (e) {
			alert(e);	
		}
		
		try {
			if (window.XMLHttpRequest) {
				
				req = new XMLHttpRequest();
				
			// branch for IE/Windows ActiveX version
			} else if (window.ActiveXObject) {	
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			
			if (req) {
				//alert(reqURL);
				cur = id;
				req.onreadystatechange = processReqChange;
				req.open("GET",reqURL+"&iid="+id+"&dir="+dir, true);
				req.send(null);
			} else {
				alert("Error Creating XMLHttpRequest();");
			}
		} catch(e) {
			alert(e);	
		}
	
	}

	function processReqChange() {
		// only if req shows "loaded"
		if (req.readyState == 4) {
			// only if "OK"
			if (req.status == 200) {
				// ...processing statements go here...
				//alert(req.responseText);
				processData(req.responseText);
			} else {
				alert("There was a problem retrieving the data:\n" +
					req.statusText);
			}
		}
	}	
	
	function processData(data) {
		//alert(data);
		if (data.indexOf('|' != -1)) {
			var arr = Array();
			arr = data.split('|');
			//altert(arr[0]);
			
			cur = arr[0];
			
			var width = arr[4];
			var height = arr[5];

			var pw = ctWidth/width;
			var ph = ctHeight/height;
			
			pw = Math.min(pw, ph);
			
			if (pw < 1) {
				width = Math.floor(width * pw);
				height = Math.floor(height * pw);
			}
			
			margintop = Math.floor((ctHeight-height)/2);
			marginleft = Math.floor((ctWidth-width)/2);
			
			

			imgField.innerHTML = '<div style="width: '+width+'px; height: '+height+'px; margin-top: '+margintop+'px;  border: 0px solid red;"><img src="'+imgPath+arr[1]+'" width="'+width+'" height="'+height+'" border="0" style="margin: 0px; padding: 0px;"></div>';			
			if (txtField)
				txtField.innerHTML = lname+arr[2];
			if (descField)
				descField.innerHTML = ldesc+arr[3];
			
		}
	}
