// -------- trida fotky ---------
function photo(iContainerObj, iPhotoID, iTitle, iComment, iTags, iAlbumNum, iHandler, iSimpleFlag, iLoadPath) {
    var self = this;

	this.parentHandler = iHandler; // kolekce ve ktere je fotografie obsazena
	this.container = null; // div ve kterem se obrazek naleza

	this.photo_ID = iPhotoID;
	this.album_ID = 0;
	this.image = null;
	this.det_image = null;
	this.comment = null;
	this.count = 1;
	this.simpleFlag = iSimpleFlag;
	this.selected = false;
	this.loadPath = iLoadPath;
	this.onphotoload = null;
	
	this.image_loader = null;

	this.noteHandRef = null; // reference na note handler
	this.tags = this.parentHandler.tagParsing ? new tagBuilder( this, null ) : null;

    if (this.parentHandler.printSelect != null) {
    	this.countInput = null;
    	this.alertImg = null;
    	this.flag_resX = 0;
    	this.flag_resY = 0;
	}

	this.title = iTitle;
	this.commentValue = iComment;
	this.albumNum = iAlbumNum;
	this.counter = 0;
	this.visibled = true;

	this.updateContainer = function(ref) {
		this.container = ref;
		this.image     = this.container.childNodes[0];
		this.comment   = this.container.childNodes[1];

        switch (this.simpleFlag) {
            default :
            case 1 : // image
                if (this.container.onclick == null) {
            		this.image.onclick = function() {
                        //var trgEvent = dEvent == null ? event : dEvent;
                        //var selfEl = (trgEvent.srcElement == null) ? trgEvent.target : trgEvent.srcElement;

                        self.call();
                    }
                }

        		this.image.style.cursor = "pointer";
        		this.image.style.border = "2px solid white";
        		this.image.style.marginTop = "3px";

                if (this.simpleFlag != 1) {
            		this.buildDetailImg();
            		this.image.onload = function() { self.refreshBorder(); }
                }
                break;

            case 2 : // preview
                break;
        }
	}

	this.fade = function() {
        this.container.style.filter = "Alpha(opacity : 50)";
    }

    this.visible = function() {
        this.container.style.filter = "";
        this.container.style.display = "block";
        this.visibled = true;

        if (this.parentHandler.handleVisiblity)
            this.parentHandler.addVisible( this );
    }

    this.hideStuff = function() {
		if (this.noteHandRef != null)
			this.noteHandRef.flushNotes();

        this.visibled = false;

        if (this.parentHandler.handleVisiblity)
            this.parentHandler.remVisible( this );
    }

    this.hideForce = function() {
        this.container.style.display = "none";
        this.hideStuff();
    }

	this.hide = function() {
        this.hideStuff();
		this.counter = 100;
		setTimeout("photos.indexes[" + this.photo_ID + "].hideProcess()", 10);
	}

	this.hideProcess = function() {
		this.counter += -10;
		if (this.counter > 0) {
			this.container.style.filter = "Alpha(opacity : " + this.counter + ")";
			setTimeout("photos.indexes[" + this.photo_ID + "].hideProcess()", 10);
		} else this.container.style.display = "none";
	}

	this.setCount = function(iValue) {
        self.count = parseInt(iValue);
        if (self.count < 1) self.count = 1;
        else if (self.count >= 100) self.count = 99;
        this.countInput.value = self.count;
    }

    this.updateSourceForce = function(iSrc, iWidth, iHeight) {
        switch (this.image.tagName.toUpperCase()) {
            case "DIV" :
                this.image.style.background = "url('" + iSrc + "')";
                break;

            case "IMG" :
                this.image.src = iSrc;
                break;
        }

        if (iWidth != null && iHeight != null) {
            this.image.style.width = iWidth + "px";
            this.image.style.height = iHeight + "px";
        }
    }

    this.updateSize = function(iWidth, iHeight) {
        this.image.style.width = iWidth + "px";
        this.image.style.height = iHeight + "px";
        this.updateContainerRatio();
    }

    this.updateRatio = function(befRatio, newRatio, debug) {
        switch (this.image.tagName.toUpperCase()) {
            case "DIV" :
    			if ((befRatio < 1 && newRatio > 1) || (befRatio > 1 && newRatio < 1)) {
    				var tmp = this.image.style.width;
    				this.image.style.width = this.image.style.height;
    				this.image.style.height = tmp;
    			}
                break;

            case "IMG" :
    			if (befRatio < 1 && newRatio > 1) {
                    var tmp = this.image.style.width;
    				this.image.style.width  = this.image.height;
    				this.image.style.height = tmp;
    			}
    			if (befRatio > 1 && newRatio < 1) {
                    var tmp = this.image.style.height;
    				this.image.style.height = this.image.style.width;
    				this.image.style.width  = tmp;
    			}
                break;
        }
    }

    this.updateContainerRatio = function() {
		this.container.style.width = this.image.style.width;
		this.container.style.height = parseInt(this.image.style.height) + (this.container.childNodes.length > 1 ? 20 : 0) + "px";
    }

	this.updateSource = function(befRatio, newRatio) {
        switch (this.image.tagName.toUpperCase()) {
            case "DIV" :
    			this.image.style.background = "url('" + this.image.style.background.substr(4, this.image.style.background.length - 5) + "?" + Math.round(Math.random() * 65535) + "')";
    			break;

    		case "IMG" :
    			this.image.src = this.image.src + "?" + Math.round(Math.random() * 65535);
			    break;
		} // switch
		
		this.updateRatio(befRatio, newRatio);
		this.updateContainerRatio();
	}

	this.updateSourceLoad = function() {
        this.loadPath = "/picture_output.php?phid=" + this.photo_ID + "&sfn=" + photos.big_prev + "&rnd=" + Math.round(Math.random() * 65535);
        this.load_useRender = true;
        this.updateImage();
    }

	this.updateSourceLoadRatio = function(befRatio, newRatio) {
        if (photos.big_prev == "_B") { // prasarnicka
            this.updateSourceForce("/picture_output.php?phid=" + this.photo_ID + "&sfn=" + photos.big_prev + "&rnd=" + Math.round(Math.random() * 65535), null, null);
    		this.updateRatio(befRatio, newRatio, true);

        } else {
            this.loadPath = "/picture_output.php?phid=" + this.photo_ID + "&sfn=" + photos.big_prev + "&rnd=" + Math.round(Math.random() * 65535);
            this.load_useRender = true;
    		this.updateRatio(befRatio, newRatio);
    		this.updateContainerRatio();
            this.updateImage();
        }
    }

	this.load_handle = null;
	this.load_step = 0;
	this.load_toLoad = "";
	this.load_useRender = false;

    this.updateImage = function() {
        if (this.loadPath != null) {
            this.load_toLoad = "";
            this.load_step = 0;

            this.preloadImage();
        }
    }

    this.preloadImage = function() {
        switch (this.load_step) {
            case 0 :
                if (this.load_useRender || this.loadPath.indexOf("picture_output.php") != -1) this.load_toLoad = "/picture_output.php?phid=" + this.photo_ID + "&sfn=" + photos.small_prev + "&rnd=" + Math.round(Math.random() * 65535);
                else this.load_toLoad = this.loadPath.substr(0, this.loadPath.length - 6) + photos.small_prev + this.loadPath.substr(this.loadPath.length - 4, 4);
                break;

            default :
                this.load_toLoad = this.loadPath;
                break;
        }

        if (this.load_handle == null) this.load_handle = document.createElement("IMG");

        this.load_handle.onload = function() { self.finishImage(); }
        this.load_handle.src = this.load_toLoad;

        if (msg != null && this.load_step == 1)
            msg.pushLoader(elementPositionX(this.image) + parseInt(this.image.style.width) / 2 - 30,
                           elementPositionY(this.image) + parseInt(this.image.style.height) / 2 - 30);
    }

    this.finishImage = function() {
        switch (this.load_step) {
            case 0 :
                this.updateSourceForce(this.load_toLoad, null, null);
                this.load_step++;
                this.preloadImage();
                break;

            case 1 :
                this.updateSourceForce(this.load_toLoad, null, null);
                this.load_step++;
                if (msg != null) msg.killLoader();
                if (this.onphotoload != null) this.onphotoload();
                break;

            default :
                break;
        }
    }

    this.dropBorders = function() {
        if (this.alertImg != null) {
            delete this.alertImg;
        }

        if (this.countInput != null) {
            this.comment.innerHTML = "";
            delete this.countInput;
            this.countInput = null;
        }
    }

    this.refreshBorder = function() {
        if (this.alertImg != null || this.det_image != null) {
            elX = elementPositionX(this.image);
            elY = elementPositionY(this.image);

            if (this.alertImg != null) {
                this.alertImg.style.left = (elX + parseInt(this.image.width) - 28) + "px";
                this.alertImg.style.top = (elY + parseInt(this.image.height) - 25) + "px";
            }
            if (this.det_image != null) {
                this.det_image.style.left = (elX + 2) + "px";
                this.det_image.style.top = (elY + parseInt(this.image.height) - 10) + "px";
            }
        }
    }
    
    this.buildBorder = function(minSize, midSize) {
        var elX, elY;

        if (this.countInput == null) {
            elX = elementPositionX(this.image);
            elY = elementPositionY(this.image);

            this.countInput = document.createElement("INPUT");
            this.countInput.style.width = "20px";
            this.countInput.style.border = "1px solid #A5ACB2";
            this.countInput.style.fontSize = "12px";
            this.countInput.style.textAlign = "center";
            this.countInput.value = this.count;

            this.countInput.onchange = function() {
                self.setCount(this.value);

                if (self.parentHandler.printCountChange != null)
                    self.parentHandler.printCountChange(self);
            }

            this.comment.style.paddingTop = "2px";
            this.comment.innerHTML = "počet&nbsp;&nbsp;";
            this.comment.appendChild(this.countInput);
        }

        var bState;

        if (this.flag_resX >= midSize || this.flag_resY >= midSize) bState = 0;
        else if (this.flag_resX >= minSize || this.flag_resY >= minSize) bState = 1;
        else bState = 2;

        switch (bState) {
            case 0 :
                this.container.style.backgroundColor = "#C5E29F";

                if (this.alertImg != null) {
                    delete this.alertImg;
                }
                break;

            case 1 :
            case 2 :
                this.container.style.backgroundColor = bState == 2 ? "#BF1914" : "#E7A800";

                if (this.alertImg == null) {
                    elX = elementPositionX(this.image);
                    elY = elementPositionY(this.image);

                    this.alertImg = document.createElement("IMG");
                    this.alertImg.style.position = "absolute";
                    this.alertImg.style.left = (elX + parseInt(this.image.width) - 28) + "px";
                    this.alertImg.style.top = (elY + parseInt(this.image.height) - 25) + "px";
                    this.alertImg.style.zIndex = "1";
                    this.alertImg.onmouseover = function(trgEvent) {
                        fotolabInfo(trgEvent == null ? event : trgEvent, bState == 2 ? 1 : 0)
                    }

                    //this.alertImg.style.cursor = "pointer";
                    //this.alertImg.onclick = function() { msg.show("<span class='errorText'>Fotografie nemá dostatečné rozlišení pro tisk. Vytisknutá může být nekvalitní.</span>");msg.setButton("pokračovat"); return false; }

                    this.container.appendChild(this.alertImg);
                }

                this.alertImg.src = bState == 2 ? "images/photoprint/alert.gif" : "images/photoprint/sign.gif";
                break;
        }
    }
    
    this.buildDetailImg = function() {
        if (this.det_image == null) {
            elX = elementPositionX(this.image);
            elY = elementPositionY(this.image);

            this.det_image = document.createElement("IMG");
            this.det_image.src = "images/photoprint/prev_icon.gif";
            this.det_image.style.position = "absolute";
            this.det_image.style.left = (elX + 2) + "px";
            this.det_image.style.top = (elY + parseInt(this.image.height) - 10) + "px";
            this.det_image.style.zIndex = "1";
            
            this.det_image.onclick = function() {
                wopen("picture.php?showp=" + self.photo_ID, "resizable");
            }
            
            this.container.appendChild(this.det_image);
        }
    }

	this.call = function() {
		this.selected = dialogs.photoToggle(this);

		if (dialogs.toggleAfterFunc != null)
            dialogs.toggleAfterFunc(this);

        switch (this.simpleFlag) {
            case 1 :
            case 2 :
                break;
                
            default :
                if (this.selected) this.container.style.backgroundColor = "#C5E29F";
                else this.container.style.backgroundColor = "";
                break;
        }

        if (this.parentHandler.printSelect != null) {
            var sel = this.parentHandler.printSelect;

            switch (this.simpleFlag) {
                case 2 :
                case 1 :
                    break;

                default :
                    if (this.selected) {
                        var sizes = photoprint_getMinRes(sel.options[sel.selectedIndex].value);
                        this.buildBorder(sizes[0], sizes[1]);
                    } else this.dropBorders();
                    break;
            }
        }

		return this.selected;
	}

	this.uncheck = function() {
		if (this.selected) return this.call();
		return false;
	}

	this.check = function() {
		if (!this.selected) return this.call();
		return true;
	}
	
	// mapovani
	this.map_xIndex = 0;
	this.map_yIndex = 0;

	// pozice
	this.album_ID = 0;
	this.position = 0;
	this.positioning = false;

	this.assignPhoto = function(iAlbumID, iPosition) { // nastavi fotce pozici
		this.album_ID 	 = iAlbumID;
		this.position 	 = iPosition;
		this.positioning = true;
	}

	this.dragStart = function(dEvent) {
		this.parentHandler.dragStart(this, dEvent.clientX, dEvent.clientY + scY());
	}

	this.dragEnd = function(dEvent) {
		this.parentHandler.dragEnd(dEvent.clientX, dEvent.clientY + scY());
	}
	
	this.dragEnter = function(dEvent) {
		this.parentHandler.dragEnter(this);
	}

	// pri vytvoreni
	this.updateContainer(iContainerObj);
	if (this.parentHandler.tagParsing)
        this.tags.updateTags(iTags);
    this.updateImage();
}

