
/**
 * Update the entire image view
 * xmlhttpreq call
 *
 * @author Raymond Julin
 * @access public
 * @return bool false
 * @param int imageId
 * @param int width
 * @param int height
 * @param int productId
 * @param int offset
 * @param int length
 */
function getPage(imageId, width, height, productId, offset, length) {
	if (cp.capable == true) {
		cp.set_response_type('XML');
		cp.call(
			'imageService.php',
			'getImagePage',
			refreshPage,
			imageId,
			width,
			height,
			productId,
			offset,
			length);
	}
	return false;
}


/**
 * Refresh page
 *
 * @author Raymond Julin
 * @access private
 * @return void
 * @param object XMLDocument
 */
function refreshPage(xml) {
	/* Extract array from XMLDocument */
	var images = updateImageSet(xml);
	var offset = xml.getElementsByTagName('offset').item(0).childNodes.item(0).nodeValue;
	var count = xml.getElementsByTagName('count').item(0).childNodes.item(0).nodeValue;
	var length = xml.getElementsByTagName('length').item(0).childNodes.item(0).nodeValue;
	var productId = xml.getElementsByTagName('productId').item(0).childNodes.item(0).nodeValue;
	var image = updateImages(xml.getElementsByTagName('currentImg'));
	var newOffset = NaN;
	/* HTML vars */
	var htmlLinks;
	var imageGroup = '';
	var htmlCurrent;
	var linkNext;
	var linkPrev;
    var linkMagnify;

	/* Figure out if we need to change page */
	var prevOffset = offset;
	var nextOffset = offset;
	if (images[0][0] == image[0] && Number(offset) != 0) {
		prevOffset = Number(offset) - Number(length);
	}
	images.reverse();
	if (images[0][0] == image[0] && (Number(offset) + Number(length)) < Number(count)) {
		nextOffset = Number(offset) + Number(length);
	}
	images.reverse();
	/* Update gallery */
	for (i = 0; i < images.length; i++) {
		imageGroup += '<li>\n<div';
		if (images[i][0] == image[0]) {
			imageGroup += ' class="viewed"';
		}
		imageGroup += 
			'>\n<a href="'+docUrl+'#' + images[i][0] +
			'" onClick="getPage(' + images[i][0] + 
			', ' + width + ', ' + height + ', ' + productId +
			', ' + offset + ', ' + length +
			');">\n<img src="' +
			imageDomain + images[i][1] + 
			'" width="120" height="80" />\n</a>\n</div>\n</li>';
	}
	if (image[5] != 'none') {
		htmlCurrent = '<a href="'+docUrl+'#' + image[5] + 
			'" onClick="getPage(' + image[5] + 
			', ' + width + ', ' + height + ', ' + productId + ', ' + nextOffset +
			', ' + length + 
			');" title="Neste bilde">\n';
		
		htmlCurrent += '<img src="' + imageDomain + image[1] +
			'" width="' + image[2] + '" height="'+image[3]+'" />\n';
		htmlCurrent += '</a>\n';
		linkNext = ' <span class="next"><a href="'+docUrl+'#' + image[5] +
			'" onClick="getPage(' + image[5] +
			', ' + width + ', ' + height + ', ' + productId + ', ' + nextOffset +
			', ' + length +
			');" title="Se neste bilde">' + langNext + ' &raquo;</a></span>';
	}
	else {
		htmlCurrent = '<img src="' + imageDomain + image[1] +
			'" width="' + image[2] + '" height="'+image[3]+'" />\n';
		linkNext = ' <span class="next">'+ langNext + ' &raquo;</span>';
	}

    // Set up magnify if bigger version exists
    if (image[6] != 'none') {
        linkMagnify = '<span class="magnify"><a href="'+
        imageDomain+image[6]+'" title=""><img src="' +
        imageDir+'imageEnlarge.gif" width="29" height="16" /></a>' +
        '</span>';

        linkMagnify = '<span class="magnify"><a href="'+
        imageDomain+image[6]+'" title="">Se st&oslash;rre versjon</a>' +
        '</span>';
    }
    else {
        linkMagnify = '<span class="magnify">&nbsp;</span>';
    }
	
	if (image[4] != 'none') {
		linkPrev = '<span class="prev"><a class="prev" href="'+docUrl+'#' + image[4] +
			'" onClick="getPage(' + image[4] +
			', ' + width + ', ' + height + ', ' + productId + ', ' + prevOffset +
			', ' + length + 
			');" title="Se forrige bilde">&laquo;&nbsp;' + langPrev + '</a></span>';

	}
	else {
		linkPrev = '<span class="prev">&laquo; ' + langPrev + '</span>';
	}

	/* Update links */
	pageLinks = generatePageLinks(
		image[0],
		width,
		height,
		productId,
		offset,
		length,
		count);
	
	/* DEBUG */
	if (debug == true) {
		var html = '\nCURR: ' + htmlCurrent + '\nLINKS: ' + pageLinks;
		html += '\nPrev: ' + linkPrev + '\nNext: ' + linkNext;
		html = '<textarea cols="70" rows="40">' + image + '</textarea>';
		document.getElementById('imageGroup').innerHTML = html;
	}
	else {
		document.getElementById('imageGroup').innerHTML = imageGroup;
		document.getElementById('imageSetLinks').innerHTML = pageLinks;;
		document.getElementById('productImageInner').innerHTML = htmlCurrent;
		document.getElementById('imageLinksUpper').innerHTML = linkPrev + 
            linkMagnify + linkNext + '<div class="clear"></div>';
		document.getElementById('imageLinksLower').innerHTML = linkPrev + linkNext + '<div class="clear"></div>';
	}

}

/**
 * Generate page links for pagination of gallery
 *
 * @author Raymond Julin
 * @access public
 * @return string Html for links
 * @param int productId
 * @param int offset
 * @param int length
 * @param int count Number of results total
 */
function generatePageLinks(imageId,width,height,productId,offset,length,count) {
	var linkPrev = '';
	var linkNext = '';

	/* Explisit typecast on offset to ensure its not a string as it normaly would be! */
	offset = new Number(offset);

	/* Create prev link if needed */
	if (offset != 0) {
		prev = offset - length;
		linkPrev = 'getPage(' + imageId + ', ' + width + ', ' + height + ', ' + 
			productId + ', ' + prev + ', ' + length + ');';
			
		linkPrev = '<a href="'+docUrl+'#'+imageId+'" onClick="' + linkPrev + '" title="' + langNext + '" class="prev">&laquo; ' + langPrev + '</a>';
	}

	/* Create next link if needed */
	if ((Number(offset) + Number(length)) < Number(count)) {
		next = Number(offset) + Number(length);
		linkNext = 'getPage(' + imageId + ', ' + width + ', ' + height + ', ' + 
			productId + ', ' + next + ', ' + length + ');';
		linkNext = '<a href="'+docUrl+'#'+imageId+'" onClick="' + linkNext + '" title="' + langNext + '" class="next">' + langNext + ' &raquo;</a>';
	}
	return linkPrev + linkNext;
}



/**
 * Parse the incoming data over to a multidimensional array
 * 
 * @author Raymond Julin
 * @access private
 * @return array Array of images
 * @param object XMLObject
 */
function updateImageSet(data) {
	var tree = data.getElementsByTagName('image');
	var images = new Array();
	var image;
	/*
		Assign image id and url to a multidimensional array:
		array images (
			array image (
				0 => id
				1 => url
				2 => width
				3 => height
			)
		);
	*/
	
	for (i=0; i < tree.length; i++) {
		image = new Array();
		image.push(tree.item(i).getElementsByTagName('id').item(0).childNodes.item(0).nodeValue);
		image.push(tree.item(i).getElementsByTagName('url').item(0).childNodes.item(0).nodeValue);
		image.push(tree.item(i).getElementsByTagName('width').item(0).childNodes.item(0).nodeValue);
		image.push(tree.item(i).getElementsByTagName('height').item(0).childNodes.item(0).nodeValue);
		images.push(image);
	}
	/* Update state */
	return images;
}

/**
 * Parse the incoming data over to a multidimensional array
 * 
 * @author Raymond Julin
 * @access private
 * @return array Array of images
 * @param object XMLObject
 */
function updateImages(tree) {
	var image = new Array();
	/*
		Assign image id and url to a multidimensional array:
		array images (
			array image (
				0 => id
				1 => url
				2 => width
				3 => height
				4 => prevId
				5 => nextId
                6 => max (fullsize version)
			)
		);
	*/
	image.push(tree.item(0).getElementsByTagName('id').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('url').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('width').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('height').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('prev').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('next').item(0).childNodes.item(0).nodeValue);
	image.push(tree.item(0).getElementsByTagName('max').item(0).childNodes.item(0).nodeValue);

	return image;
}

