YAHOO.namespace('itcraft');

var percentCut 	= 10;
var DOM 		= YAHOO.util.Dom;
var zoomLink 	= DOM.get("zoomLink");

YAHOO.itcraft.Gallery = function (parentID, isGallery) {
	this.parentID = (!parentID) ? 0 : parentID;
	this.loader();
	
	//YAHOO.util.Event.addListener(window, "load", this.loader);
}

YAHOO.itcraft.Gallery.prototype = {

	loader : function () {
		
		this.wrapperScript 	= 'query_wrapper.php';
		
		this.galleryTable 	= DOM.get("whereAmITable");
		this.imagesLayout	= DOM.get('galleryLayout');

		this.pictureTAG = DOM.get("pictureTAG");

		this.photoID 		= null;
		this.defaultThumb 	= null;
		this.defaultThumb1	= null;
		this.fileIndex 		= null;

		this.wait 			= null;
		this.error 			= null;
		this.errorMsg 		= null;
		
		this.isChangeAvail	= 1;

		this.loadFiles();
		//this.resetLayout();
	},

	loadFiles : function () {
		var callback = {
			success : function (obj) {
			
				//alert(obj.responseText);
				//return;
			
				var response = eval('new Object(' + obj.responseText + ')');
				for(var k in response.assoc){
					Gallery.createGalleryLayout(response.assoc[k], "load");
				}

			},
			failure : function () {alert("ERROR")}
		};
		this.exec({action : 'getGallery', par_id : this.parentID, filling : 0}, callback, this.wrapperScript);
	},
	
	resetLayout : function () {
		while(this.imagesLayout.firstChild){
			this.imagesLayout.removeChild(this.imagesLayout.firstChild);
		}

		this.loadFiles();
	},
	
	changeNoteImage : function (id, src, width, height, file){
	
		if(this.isChangeAvail == 0) return; this.isChangeAvail = 0;

		//alert(file.realWidth);

		this.pictureTAG.src = src;
		this.pictureTAG.width = width;
		this.pictureTAG.height = height;

		//getEl(this.pictureTAG).show(true);
		
		
		this.pictureTAG.onload = function (ev, el) {
			if(!el) var el = this;
			getEl(el.id).show(true);
		};
		
		if(newADD) { newADD.destroyNotes(); }
		newADD = new YAHOO.itcraft.DDApp(id, width, height);
		
		//alert(DOM.get("otherPhotos"));
		
		//DOM.get("otherPhotos").style.height = (height-292)+"px";
		
			zoomLink.onclick = function () {
				//popUp("show_photography.php?id=" + file.thumb.file_id, file.thumb.realWidth, file.thumb.realHeight, 0);
				
				var zoomedWidth = width*2;
				var zoomedHeight = height*2;
				
				if(file){
					if(file.thumb.realWidth < zoomedWidth)
						zoomedWidth = file.thumb.realWidth;
					
					if(file.thumb.realHeight < zoomedHeight)
						zoomedHeight = file.thumb.realHeight;
					
					if(zoomedWidth > window.screen.availWidth || zoomedHeight > window.screen.availHeight){
						zoomedWidth = Math.round(zoomedWidth*(100-percentCut)/100);
						zoomedHeight = Math.round(zoomedHeight*(100-percentCut)/100);
					}
					
					//alert("real: [" + file.thumb.realWidth + "x" + file.thumb.realHeight + "]")
					//alert("zoom: [" + zoomedWidth + "x" + zoomedHeight + "]")
				}
				
				popUp("zoom_pins.php?id=" + id + "&width=" + width + "&height=" + height, zoomedWidth, zoomedHeight, 0);
			};
	},

	createGalleryLayout : function (file, type) {
	
		var newImageDiv = null;
		var newImageDivBig = null;
		var newImage = null;

		if(!file.index) file.index = file.thumb.index;
		
		// THUMB DIV ----------------------------------------------------------- STARTS
		if(type == "load"){
			newImageDiv = document.createElement("DIV");
			newImageDiv.id = "photoDIV" + file.index;
			newImageDiv.setAttribute("photoIndex", file.index);
			newImageDiv.setAttribute("photoID", file.id);
			newImageDiv.className = 'ideaGalleryImage';
			
			if(file.index == 1){
				newImageDiv.setAttribute("noteThumbFileName", file.noteThumb.file_name);
				newImageDiv.setAttribute("noteThumbWidth", file.noteThumb.width);
				newImageDiv.setAttribute("noteThumbHeight", file.noteThumb.height);
			}
		}
		else{
			newImageDiv = DOM.get("photoDIV" + file.index);
			newImageDiv.setAttribute("photoID", file.thumb.file_id);
		}
		//---------------------------------------------------------------------- ENDS

		if(type == "load") (!file.id) ? newImageDiv.setAttribute("default",1) : newImageDiv.setAttribute("default",0);
		
		// THUMB --------------------------------------------------------------- STARTS
		newImage = document.createElement("IMG");
		newImage.id = "photoIMG" + file.index;
		newImage.className = "LineBlue";
		newImage.style.cursor = "pointer";
		newImage.width = file.thumb.width;
		newImage.height = file.thumb.height;
		newImageDiv.width = file.thumb.width;
		newImageDiv.height = file.thumb.height;
		newImageDiv.appendChild(newImage);
		newImage.src = file.thumb.file_name; 
		//---------------------------------------------------------------------- ENDS

		// THUMB BIG --------------------------------------------------------------- STARTS
		newImageBig = document.createElement("IMG");
		newImageBig.id = "photoIMGBig" + file.index;
		newImageBig.className = "LineBlue";
		newImageBig.style.cursor = "pointer";
		newImageBig.setAttribute("photoBigIndex", file.index);
		newImageBig.style.position = 'absolute';
		newImageBig.width = file.thumb1.width;
		newImageBig.height = file.thumb1.height;
		newImageBig.src = file.thumb1.file_name;

		if((type == "load" && file.id) || (file.thumb.file_id && type != "load")){
			newImageBig.onclick = function (ev, el) {
				if(!el) var el = this;
				Gallery.changeNoteImage(file.noteThumb.file_id, 
										file.noteThumb.file_name, 
										file.noteThumb.width, 
										file.noteThumb.height, file);
				el.style.visibility = "hidden";
				Gallery.setPosition();
			};
			if(file.index == 1){
				//killer
				Gallery.changeNoteImage(file.noteThumb.file_id, 
										file.noteThumb.file_name, 
										file.noteThumb.width, 
										file.noteThumb.height, file);
				Gallery.setPosition();
			}
		}

		newImageBig.onmouseout = function (ev, el) {
			if(!el) var el = this;
			getEl(el.id).hide();
		}
		//---------------------------------------------------------------------- ENDS
		
		
		//=========================================== zooming image section ================ STARTS
		newImageDiv.onmouseover = function (ev, el) {
			if(!el) var el = this;
			var imageBigEl = getEl("photoIMGBig" + file.index);

			//var dX = newImageBig.width-el.width;
			//var dY = newImageBig.height-el.height;
			
			imageBigEl.show();
			
			//var newImageDivPos = DOM.getXY(el);
			//imageBigEl.moveTo((newImageDivPos[0]-(dX/2)), (newImageDivPos[1]-(dY/2)));
		};
		//=========================================== zooming image section ================ ENDS

		newImageBig.style.visibility = 'hidden';

		this.imagesLayout.appendChild(newImageDiv);
		this.imagesLayout.appendChild(newImageBig);

		
		var newImageDivPos = DOM.getXY(newImageDiv);
		var dX = newImageBig.width-newImageDiv.width;
		var dY = newImageBig.height-newImageDiv.height;
		DOM.setXY(newImageBig,[(newImageDivPos[0]-(dX/2)),(newImageDivPos[1]-(dY/2))]);

		return newImageDiv.getAttribute("default");
	},
	
	setPosition : function () {
		var imagesBigColl = this.imagesLayout.getElementsByTagName("IMG");
		for(i=0; i<imagesBigColl.length; i++){
			if(imagesBigColl[i].getAttribute("photoBigIndex"))
			{
				var imageBigIndex = imagesBigColl[i].getAttribute("photoBigIndex");
				var imageDiv = DOM.get("photoIMG" + imageBigIndex);
				var imageBig = DOM.get("photoIMGBig" + imageBigIndex);
				
				var ImageDivPos = DOM.getXY(imageDiv);
				
				var dX = imageBig.width-imageDiv.width;
				var dY = imageBig.height-imageDiv.height;

				DOM.setXY(imageBig,[(ImageDivPos[0]-(dX/2)),(ImageDivPos[1]-(dY/2))]);
			}
		}
	},
	
	displaySection : function (id){
	
		var section = DOM.get("prod_" + id);
		var imaGe = DOM.get("prod_" + id + '_img');
		
		if(section.style.display == '' || section.style.display == 'block'){
			section.style.display = 'none';
			imaGe.src = 'images/bullet_whereami_closed.gif';
		}
		else{
			section.style.display = '';
			imaGe.src = 'images/bullet_whereami_opened.gif';
		}
	},
	
	exec : function(data, callback, targetURL){
		var request = "";
		for(i in data){
			if(request != "") request += "&";
			request += i + "=" + data[i];
		}
		if(!targetURL) var targetURL = this.wrapperScript;
		YAHOO.util.Connect.asyncRequest('POST', targetURL, callback, request);
	}
}
