/**
* @package Joomla
* @subpackage com_paxGallery
* @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("PAXimgContainer");
		txtField = document.getElementById("PAXimgTitle");
		descField = document.getElementById("PAXimgDesc");
		sndReq(firstID);
	}
	
	function goNextButton() {
		if (cur == maxPic-1)
			cur = 0;
		else
			cur = cur+1;
		
		sndReq(cur);
	}

	function goPrevButton() {
		if (cur == 0)
			cur = maxPic-1;
		else
			cur = cur -1;
			
		sndReq(cur);
	}
		
	function sndSliderReq(index) {
		try {
			if (typeof index == 'string')
				index = parseInt(index);
				
		} catch (e) {
			alert(e);	
		}
		
		index = index -1;
		sndReq(index);
	}
	
	function sndReq(index) {
		
		// branch for native XMLHttpRequest object
		try {
			if (typeof index == 'string')
				index = parseInt(index);
				
		} 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 = index;
				req.onreadystatechange = processReqChange;
				req.open("GET",reqURL+"&iid="+index , 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]);
			
			
			var width = arr[3];
			var height = arr[4];

			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[0]+'" width="'+width+'" height="'+height+'" border="0" style="margin: 0px; padding: 0px;"></div>';			
			txtField.innerHTML = lname+arr[1];
			descField.innerHTML = ldesc+arr[2];
			
		}
	}
