// PICTURISH IMAGE
var PicturishImages = [];
var PicturishHost = "http://picturish.com/";
var PicturishImage;
if (PicturishImage == undefined) {
	PicturishImage = function(attributes,noAjax) {
		if (attributes.id) {
			var self = this;
			this.id = attributes.id;
			//this.thumbURL = "http://localhost:9876/picturish/public_html/th/" + this.id;
			//this.imageURL = "http://localhost:9876/picturish/public_html/v/" + this.id;
			this.thumbURL = PicturishHost+"th/"+this.id;
			this.imageURL = PicturishHost+"v/"+ this.id;
			this.thumbElement = document.createElement("img");
			this.imageElement = document.createElement("img");
			PicturishImages.push(this);

			var attributesLoad = false;
			if(CountKeysInObject(attributes)>1) {
				try { attributesLoad=this.SetAttributes(attributes); } catch(e) {}
			}
			if(!attributesLoad && !noAjax)
				this.GetAttributes(attributes.id, this);
			
		} else if(attributes.thumbURL&&!attributes.imageURL) { //only thumb
			this.thumbURL = attributes.thumbURL;
			this.thumbElement = document.createElement("img");
			PicturishImages.push(this);
		} else if(attributes.thumbURL&&attributes.imageURL) { // both
			this.thumbURL = attributes.thumbURL;
			this.imageURL = attributes.imageURL;
			this.thumbElement = document.createElement("img");		
			this.imageElement = document.createElement("img");
			if (!attributes.width||!attributes.height) {
				var self = this;
				this.LoadImage(function () {
					self.width = self.imageElement.width;
					self.height = self.imageElement.height;
				} );		 
			} else {
				this.width = attributes.width;
				this.height = attributes.height;
			}
			PicturishImages.push(this);
		}
	};
}

PicturishImage.prototype.GetAttributes = function(id, sender) {
	new SimpleAjax(PicturishHost+"pictureinfo.php?p="+id, this.LoadAttributes, sender);
}

PicturishImage.prototype.LoadAttributes = function(ajaxResponse,self) {
	if (ajaxResponse!="ERROR") {
		self.SetAttributes(eval('('+ajaxResponse+')'));
	} else {
		Debug("Picturish Object Ajax Response Error!");
		// Error Handler
	}
};

PicturishImage.prototype.SetAttributes = function(attributes) {
	try {
		if(this.id==attributes.id) {
			this.date = attributes.date;
			this.name = attributes.name;
			this.type = attributes.type;
			this.size = attributes.size;
			this.width = attributes.width;
			this.height = attributes.height;
			if(this.width) this.imageElement.width = this.width;
			if(this.height) this.imageElement.height = this.height;
			return true;
		}
	} catch(e) {
		Debug("Error while setting Picturish Object Attributes!");
		return false;
	}
};

PicturishImage.prototype.LoadImage = function(callback) {
	this.imageElement.onload = function () { if (typeof(callback) == 'function') callback(); };
	this.imageElement.src = this.imageURL;
};

PicturishImage.prototype.LoadThumb = function(callback) {
	var self = this;
	this.thumbElement.onload = function () { if (typeof(callback) == 'function') callback(); };
	if(this.imageURL) {
		this.thumbElement.onclick = function () { ShowBlockImage(self); };
		this.thumbElement.style.cursor = "pointer";
	}
	this.thumbElement.src = this.thumbURL;
	this.thumbElement.style.border = "1px solid #cccccc";
};




PicturishImage.prototype.AppendThumb = function(target,callback,invertAppend) {
	var self = this;
	if (typeof(target)=="string") target = $(target);
	this.thumbContainer = document.createElement("div"); // Container	
	this.thumbContainer.className = "thumbContainer";
	this.thumbContainer.appendChild(this.thumbElement);
	// WARNING: overflow=hidden causes disappearing bug on Chrome
	appendFunction = function() {
		if (invertAppend&&!isIE()) { 
			target.insertBefore(self.thumbContainer, target.childNodes[0]);
		} else {
			target.appendChild(self.thumbContainer);
		}
		OpacityFade(self.thumbContainer, 0, 100);
		if (typeof(callback) == 'function') callback();
	}	
	//ChangeOpacity(this.thumbContainer, 0);
	
	if (this.thumbElement.src=='') { this.LoadThumb(appendFunction); }
	else { appendFunction(); }
};

PicturishImage.prototype.AppendImage = function(target,callback) {
	if (typeof(target)=="string") target = $(target);

	appendFunction = function() { if (typeof(callback) == 'function') callback(); }
	
	this.LoadImage(appendFunction);
	var imageElementClone = this.imageElement.cloneNode(true);
	target.appendChild(imageElementClone);
	return imageElementClone;

	//if (this.imageElement.src=='') { this.LoadImage(appendFunction); }
	//else { appendFunction(); }
};

PicturishImage.prototype.ShowThumbLinks = function () {
	this.thumbContainer.style.width = "95%";
	this.thumbContainer.style.overflow = "hidden";	
	var linksDiv = document.createElement('div');		
	linksDiv.style.cssFloat = "left";
	linksDiv.style.styleFloat = "left";	
	linksDiv.style.marginLeft = "10px";
	linksDiv.style.fontSize = "11px";
	linksDiv.innerHTML = '<table><tr><td align="right">Direct Link:</td><td><input onclick="this.focus(); this.select();" style="border: 1px solid #cccccc; width: 500px; font-size: 11px;"readonly="readonly" value="http://picturish.com/p/'+this.id+'"/></td></tr><tr><td align="right">Webpages:</td><td><input onclick="this.focus(); this.select();" style="border: 1px solid #cccccc; width: 500px; font-size: 11px;"readonly="readonly" value=\'<a href="http://picturish.com/p/'+this.id+'"><img src="http://picturish.com/th/'+this.id+'" /></a>\'/></td></tr><tr><td align="right">Forums:</td><td><input onclick="this.focus(); this.select();" style="border: 1px solid #cccccc; width: 500px; font-size: 11px;"readonly="readonly" value=\'[URL=http://picturish.com/p/'+this.id+'][IMG]http://picturish.com/th/'+this.id+'[/IMG][/URL]\'/></td></tr></table>';
	var imageWrapper = document.createElement('div');
	imageWrapper.style.cssFloat = "left";
	imageWrapper.style.styleFloat = "left";			
	this.thumbContainer.appendChild(imageWrapper);			
	imageWrapper.appendChild(this.thumbElement);
	this.thumbContainer.appendChild(linksDiv);
};

// SIMPLE AJAX
var SimpleAjax = function(url,callbackFunction,sender) {
	// Debug("SimpleAjax on "+url); // RECURSION
	try { this.XMLRequest = new XMLHttpRequest(); }
    catch(e) { try { this.XMLRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
	catch(e) { try { this.XMLRequest = new ActiveXObject('Msxml2.XMLHTTP'); }
	catch(e) { return; } } }
	
	this.XMLRequest.onreadystatechange = function() {
		if (this.readyState == 4) {
			var response;
			if (this.status == 200) { response = this.responseText; }
			else { response = "ERROR"; }
				
			if (typeof(callbackFunction) == 'function') callbackFunction(response,sender);
		}
	}

	this.XMLRequest.open("GET", url);
	this.XMLRequest.send(null);
}
