/*
 * @author	Ozan Vural
 * @version	1.0
 * @date	03.08.2007
 * @copyright	Treda Bilisim Teknolojileri A.S
 */
 
 
if(typeof(Treda) == "undefined") Treda = {}; // if not exists add NameSpace Treda
if(typeof(Treda.Controls) == "undefined") Treda.Controls = {}; // if not exists add NameSpace Treda.Controls

Treda.Controls.PicturePreview = function () {	
	var items = new Array();
	var pictureArea = null;
	var pictureThumbnailContainer = null;
	var selectedIndex = -1;
	var repeatColumns = null;
	var direction = null;
	var repeatColumn = 3;
	var thumbnailSize ;
	var middleSize;
	var bigSize;
	var bigPictureClass;			
	var smallPictureClass;

	return {
		Picture: function (t, m, p) {
			var thumbnail = t;
			var middle = m;
			var picture = p;
			
			this.getThumbnail = function () {
				return thumbnail;
			}
			this.getMiddle = function () {
				return middle;
			}
			this.getPicture = function () {
				return picture;
			}
		},
		Size: function (w, h) {
			var width = w;
			var height = h;
			
			this.getWidth = function () {
				return w;
			}
			this.getHeight = function () {
				return h;
			}
		},
		RepeatDirection : {
				VERTICAL:1, 
				HORIZONTAL:2
		},
		Manager: function () {	
			this.setSelectedIndex = function (newIndex) {
				selectedIndex = newIndex;
			}
			this.setPictureAreaId = function (id) {
				var element = document.getElementById(id);
				if (element == null) {
					alert(id + " not found");
					return;
				}
				pictureArea = element;
			}
			this.setPictureThumbnailContainer = function (id) {
				var element = document.getElementById(id);
				if (element == null) {
					alert(id + " not found ");
					return;
				}
				pictureThumbnailContainer = element;
			}
			this.addPictue = function (p) {
				items.push(p);
			}
			this.generate = function () {
				var thumbnailHtml = new Treda.Text.StringBuilder("<table border='0' cellspacing='1' cellpadding='0'>");
				if (direction == Treda.Controls.PicturePreview.RepeatDirection.VERTICAL) {						
					for (var i = 0; i < items.length; i++) {
						thumbnailHtml.append(Treda.String.format("<tr><td><a href='javascript:Treda.Controls.PicturePreview.setPicture({3});'><img src='{0}' width='{1}' height='{2}' class='{4}' /></a></td></tr>", items[i].getThumbnail(), thumbnailSize.getWidth(), thumbnailSize.getHeight(), i, smallPictureClass));
					}
				}
				else if (direction == Treda.Controls.PicturePreview.RepeatDirection.HORIZONTAL) {	
					for (var i = 0; i < items.length; i++) {
						if (i % repeatColumn  == 0)  thumbnailHtml.append("<tr>");										
						
						thumbnailHtml.append(Treda.String.format("<td><a href='javascript:Treda.Controls.PicturePreview.setPicture({3});'><img src='{0}' width='{1}' height='{2}' class='{4}' /></a></td>", items[i].getThumbnail(), thumbnailSize.getWidth(), thumbnailSize.getHeight(), i, smallPictureClass));
						
						if ((i % repeatColumn == repeatColumn-1) || (i == items.length -1 && i % repeatColumn != repeatColumn-1))  thumbnailHtml.append("</tr>");							
					}
				}				
				thumbnailHtml.append("<table>");
				pictureThumbnailContainer.innerHTML = thumbnailHtml.toString();
				Treda.Controls.PicturePreview.setPicture(0);
			}
			this.setDirection = function (dir) {
				direction = dir;
			}
			this.setRepeatColumn = function (count) {
				repeatColumn = count;
			}
			this.setThumbnailSize = function (s) {
				thumbnailSize = s;	
			}
			this.setMiddleSize = function (s) {
				middleSize = s;
			}
			this.setBigSize = function (s){
				bigSize = s;
			}
			this.setBigPictureClass = function (c) {
			    bigPictureClass = c;
			}
			this.setSmallPictureClass = function (c) {
			    smallPictureClass = c;
			}
		},
		setPicture: function(i) {
			pictureArea.innerHTML = Treda.String.format("<a href='javascript:Treda.Controls.PicturePreview.openPicture({3})'><img src='{0}' width='{1}' height='{2}' class='{4}' /></a>", items[i].getMiddle(),  middleSize.getWidth(), middleSize.getHeight(), i, bigPictureClass);
		},
		openPicture: function (i) {
			var windowObject = window.open("", "picture", "width="+ bigSize.getWidth() + ", height="+ bigSize.getHeight());
			windowObject.document.write("<html><head></head><body>");
			windowObject.document.write(Treda.String.format("<a href='javascript:window.close()'><img border='0' src='{0}' width='{1}' height='{2}' /></a>", items[i].getPicture(),  bigSize.getWidth(), bigSize.getHeight()));
			windowObject.document.write("</body></html>");
			windowObject.document.close();		
		}
	};
}();
