/*
	A simple class for displaying file information and progress
	Note: This is a demonstration only and not part of SWFUpload.
	Note: Some have had problems adapting this class in IE7. It may not be suitable for your application.
*/

// Constructor
// file is a SWFUpload file object
// targetID is the HTML element id attribute that the FileProgress HTML structure will be added to.
// Instantiating a new FileProgress object with an existing file will reuse/update the existing DOM elements


function FP(file, targetID) {
    this.fileProgressID = file.id;
    
    this.opacity = 100;
    this.height  = 0;
    
    this.fileProgressWrapper = document.getElementById('file_progress_' + this.fileProgressID);
	if (!this.fileProgressWrapper) {
        
        this.fileProgressWrapper = document.createElement("div");
		this.fileProgressWrapper.className = "progressWrapper";
		this.fileProgressWrapper.id = 'file_progress_' + this.fileProgressID;
		
		this.fileProgressElement = document.createElement("div");
		this.fileProgressElement.className = "progressContainer blue";
		
		var galleryImage       = document.createElement("img");
		galleryImage.src       = '/images/cupid/web/default/bg_1_3.png';
		galleryImage.id        = 'file_progress_' + file.name;
		galleryImage.className = 'image';
		galleryImage.width     = '95';
		galleryImage.height    = '71';
		
		var progressCancel = document.createElement("a");
		progressCancel.className = "progressCancel";
		progressCancel.href = "#";
		progressCancel.style.visibility = "hidden";
		progressCancel.appendChild(document.createTextNode(" "));
		
		var progressStatus = document.createElement("div");  
		progressStatus.className = "progressBarStatus";
		progressStatus.innerHTML = "&nbsp;";
		
		var progressBar           = document.createElement("div");
		progressBar.className     = "progressBarInProgress";
		progressBar.style.width   = "0%";
		
		var progressBarContainer        = document.createElement("div");
		progressBarContainer.className = "progressBarContainer"
		progressBarContainer.appendChild(progressBar);
		
		this.fileProgressElement.appendChild(galleryImage);
		this.fileProgressElement.appendChild(progressCancel);
		this.fileProgressElement.appendChild(progressStatus);
		this.fileProgressElement.appendChild(progressBarContainer);
		
		this.fileProgressWrapper.appendChild(this.fileProgressElement);
		
		document.getElementById(targetID).appendChild(this.fileProgressWrapper);
    } else {
        this.fileProgressElement = this.fileProgressWrapper.firstChild;
    }
    this.height = this.fileProgressWrapper.offsetHeight;
}
FP.prototype.setStatus = function (status) {
	this.fileProgressElement.childNodes[2].innerHTML = status;
};
// Show/Hide the cancel button
FP.prototype.toggleCancel = function (show, swfUploadInstance) {
	this.fileProgressElement.childNodes[1].style.visibility = show ? "visible" : "hidden";
	if (swfUploadInstance) {
		var fileID = this.fileProgressID;
		this.fileProgressElement.childNodes[1].onclick = function () {
			swfUploadInstance.cancelUpload(fileID);
			return false;
		};
	}
};
FP.prototype.setError = function () {
	this.fileProgressElement.className = "progressContainer red";
	this.fileProgressElement.childNodes[3].firstChild.className = "progressBarError";
	this.fileProgressElement.childNodes[3].firstChild.style.width = "0%";

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 5000);
};
FP.prototype.setProgress = function (percentage) {
	this.fileProgressElement.className = "progressContainer green";
	this.fileProgressElement.childNodes[3].firstChild.className = "progressBarInProgress";
	this.fileProgressElement.childNodes[3].firstChild.style.width = percentage + "%";
};
FP.prototype.setComplete = function () {
	this.fileProgressElement.className = "progressContainer blue";
	this.fileProgressElement.childNodes[3].firstChild.className = "progressBarComplete";
	this.fileProgressElement.childNodes[3].firstChild.style.width = "0%";

    /**
     * DONT WANT THAT FOR NOW
	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 1000);
    */
};
FP.prototype.setUploadFoto = function(file, jpg) {
    var fileArray = jpg.split('.',2);
    var foto = fileArray[0];
    
    this.imageSource = '/getimage/ver/95x71/fk/' + foto;
    this.md5Foto = foto;
};
FP.prototype.setUploadAudio = function(file, json) {
    var data = eval('(' + json + ')');

    if (data.artist) {
        this.artist = data.artist;
    } else {
        this.artist = file.name;
    }
    if (data.title) {
        this.title = data.title;
    } else {
        this.title = '';
    }
    this.ugcId = data.ugc_id;
    this.imageSource = '/images/cupid/web/default/audio_thumb.png';
    this.md5Foto = 'audio_thumb.png';
};
FP.prototype.setUploadDescription = function(file, targetID) {

    // Replace content of file_progress
    this.fileProgressWrapper   = document.getElementById('file_progress_' + this.fileProgressID);
    if (this.fileProgressWrapper) {

        this.fileDescriptionElement           = document.createElement("div");
		this.fileDescriptionElement.className = "progressContainer green";

		var galleryImage       = document.createElement("img");
		galleryImage.src       = this.imageSource;
		galleryImage.id        = 'file_description_' + file.name;
		galleryImage.className = 'image';
		galleryImage.width     = '95';

		var label = document.createElement("label");
		label.innerHTML = 'Image/video description' + ' (File ' +  numFilesUl + '/' + numFilesQu + ')';

		var textarea   = document.createElement("textarea");
		textarea.style.height = '42px';
		textarea.style.width  = '491px';
		textarea.style.minHeight = '28px';
        textarea.className = "input";
        textarea.setAttribute("cols","5");
        textarea.setAttribute("rows","5");
        textarea.setAttribute("name", "description[" + this.md5Foto + "]");

		var descriptionContainer        = document.createElement("div");
		descriptionContainer.className  = "descriptionContainer";
		descriptionContainer.appendChild(label);
		descriptionContainer.appendChild(textarea);

		this.fileDescriptionElement.appendChild(galleryImage);
		this.fileDescriptionElement.appendChild(descriptionContainer);

        //replace
        this.fileProgressWrapper.replaceChild(this.fileDescriptionElement,this.fileProgressElement);
    }
};
FP.prototype.setUploadDescriptionAudio = function(file, targetID) {

    // Replace content of file_progress
    this.fileProgressWrapper   = document.getElementById('file_progress_' + this.fileProgressID);
    if (this.fileProgressWrapper) {

        this.fileDescriptionElement           = document.createElement("div");
		this.fileDescriptionElement.className = "progressContainer green";

		var galleryImage       = document.createElement("img");
		galleryImage.src       = '/images/cupid/web/default/audio_thumb.png';
		galleryImage.id        = 'file_description_' + file.name;
		galleryImage.className = 'image';
		galleryImage.width     = '95';
		galleryImage.height    = '71';

		var label = document.createElement("label");
		label.innerHTML = 'Artist'; //translate

		var input = document.createElement("input");
		input.style.width  = '491px';
        input.className = "input";
        input.setAttribute("type","text");
        input.setAttribute("value",this.artist);
        input.setAttribute("name", "ugc[" + this.ugcId + "][artist]");

		var label2 = document.createElement("label");
		label2.innerHTML = 'Title';

		var input2 = document.createElement("input");
		input2.style.width  = '491px';
        input2.className = "input";
        input2.setAttribute("type","text");
        input2.setAttribute("value",this.title);
        input2.setAttribute("name", "ugc[" + this.ugcId + "][title]");

		var descriptionContainer        = document.createElement("div");
		descriptionContainer.className  = "descriptionContainer";
		descriptionContainer.appendChild(label);
		descriptionContainer.appendChild(input);
		descriptionContainer.appendChild(label2);
		descriptionContainer.appendChild(input2);

		this.fileDescriptionElement.appendChild(galleryImage);
		this.fileDescriptionElement.appendChild(descriptionContainer);

        //replace
        this.fileProgressWrapper.replaceChild(this.fileDescriptionElement,this.fileProgressElement);
    }
};
FP.prototype.setCancelled = function () {
	this.fileProgressElement.className = "progressContainer";
	this.fileProgressElement.childNodes[3].firstChild.className = "progressBarError";
	this.fileProgressElement.childNodes[3].firstChild.style.width = "0%";

	var oSelf = this;
	setTimeout(function () {
		oSelf.disappear();
	}, 2000);
};
// Fades out and clips away the FileProgress box.
FP.prototype.disappear = function () {

	var reduceOpacityBy = 15;
	var reduceHeightBy = 4;
	var rate = 30;	// 15 fps

	if (this.opacity > 0) {
		this.opacity -= reduceOpacityBy;
		if (this.opacity < 0) {
			this.opacity = 0;
		}

		if (this.fileProgressWrapper.filters) {
			try {
				this.fileProgressWrapper.filters.item("DXImageTransform.Microsoft.Alpha").opacity = this.opacity;
			} catch (e) {
				// If it is not set initially, the browser will throw an error.  This will set it if it is not set yet.
				this.fileProgressWrapper.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=" + this.opacity + ")";
			}
		} else {
			this.fileProgressWrapper.style.opacity = this.opacity / 100;
		}
	}

	if (this.height > 0) {
		this.height -= reduceHeightBy;
		if (this.height < 0) {
			this.height = 0;
		}

		this.fileProgressWrapper.style.height = this.height + "px";
	}

	if (this.height > 0 || this.opacity > 0) {
		var oSelf = this;
		setTimeout(function () {
			oSelf.disappear();
		}, rate);
	} else {
		this.fileProgressWrapper.style.display = "none";
	}
};

