/**
* This file is part of "Thread Download".
*
* "Thread Download" is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* "Thread Download" is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with "Thread Download".  If not, see <http://www.gnu.org/licenses/>.
*/

/**
* Adding popup Menus to the Buttons
*
* @package com.woltlab.community.thread.download
* @author Hawkes
*/


function ThreadDownload(data) {
	this.data = data;
	
	this.init = function() {		
		for (var id in this.data) {
			var link = document.getElementById('threadDownload'+id);
			
			if (link) {
				link.threadDownload = this;
								
				link.name = id;
				link.href = "javascript:void(0);";
				
				link.ondblclick = function() { threadDownload.download(downloadMimeType); };
				
				var icon = false;
				if (link.firstChild && link.firstChild.src) {
					icon = link.firstChild;
				}
				
				// change icon
				//icon.src = icon.src.replace(/postQuote(?=(?:S|M|L|XL)\.png$)/, 'postQuoteOptions');
				
				link.onmousedown = function(e) { this.threadDownload.showMenu(e, this); };
				
				var menuDiv = document.createElement('div');
				menuDiv.id = 'threadDownload' + id + 'Menu';
				menuDiv.className = 'hidden';
				link.parentNode.appendChild(menuDiv);
				popupMenuList.register('threadDownload' + id);
			}
		}
		
		//init Large Buttons
		var largeButtons = getElementsByClass('threadDownloadButton');
		var type = 'Upper';
		var id = type+downloadThreadID;
		for(var i=0; i< largeButtons.length; i++) {
			var element = largeButtons[i];
			element.threadDownload = this;
			
			element.name = id;
			element.id = 'threadDownload' + id;
			element.href = "javascript:void(0);";
				
			element.ondblclick = function() { threadDownload.downloadThread(downloadMimeType); };
				
			var icon = false;
			if (element.firstChild && element.firstChild.src) {
				icon = element.firstChild;
			}
				
			// change icon
			//icon.src = icon.src.replace(/postQuote(?=(?:S|M|L|XL)\.png$)/, 'postQuoteOptions');
			if(type == 'Upper') {
				element.onmousedown = function(e) { this.threadDownload.showThreadMenu(e, this, 'Upper'); };
			} 
			else {
				element.onmousedown = function(e) { this.threadDownload.showThreadMenu(e, this, 'Lower'); };
			}
					
			var menuDiv = document.createElement('div');
			menuDiv.id = 'threadDownload' + id + 'Menu';			
			menuDiv.className = 'hidden';
			element.parentNode.appendChild(menuDiv);
			popupMenuList.register('threadDownload' + id);
			var type = 'Lower';
			id = type+downloadThreadID;
		}

	}
	
	this.showMenu = function(event, element) {
		var id = element.id.replace(/[^\d]+/, '');
		var options = this.getPostDownloadOptions(id);
		this.createMenu(options, element, 'threadDownload' + id + 'Menu');
	}
	this.showThreadMenu = function(event, element, type) {
		var id = element.id.replace(/[^\d]+/, '');
		var options = this.getThreadDownloadOptions(id);
		var id = type+id;
		this.createMenu(options, element, 'threadDownload' + id + 'Menu');
	}
	
	this.getPostDownloadOptions = function(id) {
		var options = new Array();
		var i = 0;
		
		// download text
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.download('+id+', "text/plain");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.text'];
		i++;
		
		// download html
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.download('+id+', "text/html");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.html'];
		i++;
		
		// download pdf
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.download('+id+', "application/pdf");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.pdf'];
		i++;
		
		
		return options;
	}
	
	this.getThreadDownloadOptions = function(id) {
		var options = new Array();
		var i = 0;
		
		// download text
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.downloadThread('+id+', "text/plain");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.text'];
		i++;
		
		// download html
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.downloadThread('+id+', "text/html");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.html'];
		i++;
		
		// download pdf
		options[i] = new Object();
		options[i]['function'] = 'threadDownload.downloadThread('+id+', "application/pdf");';
		options[i]['text'] = language['com.woltlab.community.threaddownload.type.pdf'];
		i++;
		
		
		return options;
	}
	
	this.createMenu = function(options, parentElement, id) {
		var menuDiv = document.getElementById(id);
		
		while (menuDiv.hasChildNodes()) {
			menuDiv.removeChild(menuDiv.firstChild);
		}
		var containmentDiv = document.createElement('div');
		containmentDiv.className = 'downloadMenuButtons';
		menuDiv.appendChild(containmentDiv);
		var menuUL = document.createElement('ul');		
		containmentDiv.appendChild(menuUL);
		
		for (var i = 0; i < options.length; i++) {
			var menuLI = document.createElement('li');						
			menuLI.style.backgroundImage = 'none';
			menuUL.appendChild(menuLI);
			
			var menuA = document.createElement('a');			
			menuA.href = 'javascript:'+options[i]['function'];					
			menuLI.appendChild(menuA);
			menuA.appendChild(document.createTextNode(options[i]['text']));
		}
	}
	
	this.download = function(id, type) {
		window.open('index.php?action=ThreadDownload&postID='+id+'&outputType='+type+SID_ARG_2ND);
	}
	
	this.downloadThread = function(id, type) {
		window.open('index.php?action=ThreadDownload&threadID='+id+'&outputType='+type+SID_ARG_2ND);
	}
	
	this.init();
	
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\\\s)"+searchClass+"(\\\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
