﻿///// Implementation of the JSG
if(typeof parkside == "undefined") var parkside = new Object();
if(typeof parkside.JSGallery == "undefined") parkside.JSGallery = new Object();

var g_control = null;
var g_intervalFunction = null;
function scrollControl ( amount, width ) {
	if (g_control == null && g_intervalFunction != null) {
		window.clearInterval( g_intervalFunction );
		intervalFunction = null;
		return;
	}
	if (g_control.style.marginLeft == "")
		g_control.style.marginLeft = 0;
	var intAmount = parseInt(amount, 10);
	var intScroll = parseInt(g_control.style.marginLeft, 10);
	
	/* Boundary Checking */
	if (intScroll + intAmount > 0)
		return;
		
	if (Math.abs(intScroll + intAmount) > width )
		return;
		
	g_control.style.marginLeft = (intScroll + intAmount) + "px";
}
	
// Constructor
parkside.JSGallery = function( scroll_control, disp_control, controlwidth ) {
	this.scroll_control = scroll_control;
	this.disp_control = disp_control;
	var images = this.scroll_control.getElementsByTagName("img");
	for (var i = 0; i < images.length; i++) {
		var preLoad = new Image();
		preLoad.src = images[i].src;
		if (images[i].width != null && images[i].width != "")
			this.width += parseInt(images[i].width, 10); 
	}
	if (controlwidth)
		this.width -= controlwidth;
}

parkside.JSGallery.prototype = {
	  intervalFunction : null
	, scroll_control : null
	, disp_control : null
	, width : null
	
	, startScrolling : function( amount ) {
		g_control = this.scroll_control;
		this.intervalFunction = window.setInterval("scrollControl('" + amount + "', " + this.width + ")", 5);
		g_intervalFunction = this.intervalFunction;
	}
	
	, stopScrolling : function( amount ) {
		if (this.intervalFunction) {
			window.clearInterval( this.intervalFunction );
			this.intervalFunction = null;
			g_intervalFunction = null;
			g_control = null;
		}
	}
	
	, loadImage : function( thumbnail ) {
		if (this.disp_control && this.disp_control.src && thumbnail.src )
			this.disp_control.src = thumbnail.src.replace("img_0x120", "img_472x384");
	}
}
