var barSlide = Array();
var activeBar = 1;  // set first bar active
var optionIdx = 2000;  // admin option reordering z-index

function gE(Id) {
	return document.getElementById(Id);
}

function OkCancel() {
	Msg = (arguments[0]) ? arguments[0] : 'Are you sure?';
	this.blur();
	if (confirm(Msg)) return true;
	return false;
}

function pollVote(order,pollId,optionId,voted) {
	var pollFade = new Fx.Style('dracon_poll_action', 'opacity');
	pollFade.options.transition = Fx.Transitions.Quad.easeOut;
	var pollSlide = new Fx.Slide('dracon_poll_action');
	pollSlide.options.transition = Fx.Transitions.Quad.easeOut;
	var loaderFade = new Fx.Style('dracon_poll_loading', 'opacity');
	loaderFade.options.transition = Fx.Transitions.Quad.easeOut;
	var loaderSlide = new Fx.Slide('dracon_poll_loading');
	loaderSlide.options.transition = Fx.Transitions.Quad.easeOut;

	loaderSlide.hide();
	loaderFade.start(1,0);

	pollFade.start(1,0.1).chain(function(){
		pollSlide.slideOut().chain(function(){
			if (!voted) $('dracon_poll_votes').innerHTML = parseInt($('dracon_poll_votes').innerHTML) + 1;
			$('dracon_poll_action').innerHTML = '<p align="center" style="margin:0">Voted <span style="color:#9999FF">#'+order+'</span></p><br>';
			$('dracon_poll_loading').style.display = 'block';
			loaderSlide.slideIn().chain(function(){
				loaderFade.start(0,1);
			})
			pollSlide.slideIn().chain(function(){
				pollFade.start(0,1).set(0);
				$('dracon_poll_h3').style.color = '#FFF';
			})
		})
	}).chain(function(){
		pollFade.start(1,0);
		pollSlide.slideOut().chain(function(){
			$('dracon_poll_h3').style.color = '#FFCC00';
			new XHR({onSuccess: function(req){ $('dracon_poll_action').innerHTML = req; }}).send(webRoot+'poll.php?results='+pollId+'&optionId='+optionId).chain(function(){
				loaderFade.start(1,0).chain(function(){
					loaderSlide.slideOut().chain(function(){
						$('dracon_poll_loading').style.display = 'none';
					})
					$('dracon_poll_action').className = 'dracon_poll_action_results';
					barArray = countOptions();
					barArray.sort(compareNumbers);
					for (i=1; i < barArray.length; i++) {
						/* Indicate top results */
						if ($('dracon_poll_bar_width['+i+']').innerHTML == barArray[0]) {
							$('dracon_poll_bar_width['+i+']').style.color = '#FFFFFF';
							$('dracon_poll_bar_size['+i+']').className = 'dracon_poll_answers_bar_max';
						}
					}
					pollFade.start(0,1);
					pollSlide.slideIn().chain(function(){
						barSlideEngine(1);
					})
				})
			})
		})
	})
}

function barSlideEngine(barNumber) {
	var allBars = $('dracon_poll_allBars').value;
	if (barNumber > allBars) return;
	barSlide[barNumber] = new Fx.Slide('dracon_poll_bar['+barNumber+']', {mode: 'horizontal', duration: 1000});
	barSlide[barNumber].hide();
	$('dracon_poll_bar['+barNumber+']').style.visibility = 'visible';
	barSlide[barNumber].slideIn();
	barPercentCount(barNumber,$('dracon_poll_bar_width['+barNumber+']').innerHTML,0);
}

function barPercentCount(barNumber,realSize,barCounter) {
	barCounter++;
	$('dracon_poll_bar_width['+barNumber+']').innerHTML = '&nbsp;&nbsp;'+barCounter+'%';
	if (barCounter < realSize) setTimeout('barPercentCount('+barNumber+','+realSize+','+barCounter+')', (100/realSize*13));
	if (barCounter > (realSize/10) && activeBar==barNumber) {
		$('dracon_poll_bar_width['+barNumber+']').innerHTML = '&nbsp;&nbsp;'+realSize+'%';  // 0% fix
		activeBar++;
		barSlideEngine(barNumber+1);
	}
}

function pollShowRes(pollId,optionId) {
	new XHR({onSuccess: function(req){ $('dracon_poll_action').innerHTML = req; }}).send(webRoot+'poll.php?results='+pollId+'&optionId='+optionId).chain(function(){
		$('dracon_poll_action').className = 'dracon_poll_action_results';
		barArray = countOptions();
		barArray.sort(compareNumbers);
		for (i=1; i < barArray.length; i++) {
			$('dracon_poll_bar['+i+']').style.visibility = 'visible';
			/* Indicate top results */
			if ($('dracon_poll_bar_width['+i+']').innerHTML == barArray[0]) {
				$('dracon_poll_bar_width['+i+']').style.color = '#FFFFFF';
				$('dracon_poll_bar_size['+i+']').className = 'dracon_poll_answers_bar_max';
			}
			$('dracon_poll_bar_width['+i+']').innerHTML = '&nbsp;&nbsp;'+$('dracon_poll_bar_width['+i+']').innerHTML+'%';
		}
	})
}

function countOptions() {
	var counter = 0;
	var barArray = new Array();
	while (true) {
		counter++;
		if ($('dracon_poll_bar['+counter+']') == null) break;
		barArray[counter] = $('dracon_poll_bar_width['+counter+']').innerHTML;
	}
	return barArray;
}

function compareNumbers(a, b) {  // for sorting numerical array - descending
	 return b - a;
}


/* ADMIN */
function pollAdminLoading() {
	$('dracon_poll_admin_loading').style.visibility = 'visible';
	$('dracon_poll_add_new').disabled = true;
	$('dracon_poll_save').disabled = true;
}

function pollAdminLogin() {
	$('dracon_poll_admin_loading').style.visibility = 'visible';
	$('dracon_poll_admin_login').disabled = true;
	$('dracon_poll_admin_pass').disabled = true;
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/login.php?login='+$('dracon_poll_admin_login').value+'&pass='+$('dracon_poll_admin_pass').value).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
		if ($('dracon_poll_logged_in').value == 'ok') $('dracon_poll_admin_logout').style.display = 'inline';
		else $('dracon_poll_admin_login').focus();
	})
}

function pollAdminLogout() {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/login.php?logout=x').chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
		$('dracon_poll_admin_logout').style.display = 'none';
	})
}


/* POLLS */
function pollLoadPolls() {
	$('dracon_poll_admin_loading').style.visibility = 'visible';
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?loadPolls').chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollAddNewPoll() {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?addNewPoll').chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollToggleSave() {
	var pollCounter = 0;
	while(true) {
		pollCounter++;
		if (!$('dracon_admin_poll_save['+pollCounter+']')) break;
		if ($('dracon_admin_poll_save['+pollCounter+']').checked == true) $('dracon_admin_poll_save['+pollCounter+']').checked = false
		else $('dracon_admin_poll_save['+pollCounter+']').checked = true;
	}
}

function pollDell(pollId) {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?delPoll='+pollId).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollSavePollData() {
	pollAdminLoading();
	var pollCounter = 0;
	var pollData = '';
	// Build up the data array
	while(true) {
		pollCounter++;
		if (!$('dracon_poll_admin_poll_id['+pollCounter+']')) break;
		if ($('dracon_poll_admin_poll_save['+pollCounter+']').checked) {
			pollData += '&pollId['+pollCounter+']='+$('dracon_poll_admin_poll_id['+pollCounter+']').value+'&title['+pollCounter+']='+encodeURIComponent($('dracon_poll_admin_poll_title['+pollCounter+']').value)+'&width['+pollCounter+']='+$('dracon_poll_admin_poll_width['+pollCounter+']').value+'&bgcolor['+pollCounter+']='+$('dracon_poll_admin_poll_bgcolor['+pollCounter+']').value+'&padding['+pollCounter+']='+$('dracon_poll_admin_poll_padding['+pollCounter+']').value+'&votes['+pollCounter+']='+$('dracon_poll_admin_poll_votes['+pollCounter+']').value+'&status['+pollCounter+']='+$('dracon_poll_admin_poll_status['+pollCounter+']').checked;
		}
	}
	// Send data via POST
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?savePollData=x',pollData).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollLoadOptions(pollId) {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?loadOptions='+pollId).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollToggleSave() {
	var pollCounter = 0;
	while(true) {
		pollCounter++;
		if (!$('dracon_poll_admin_poll_save['+pollCounter+']')) break;
		if ($('dracon_poll_admin_poll_save['+pollCounter+']').checked == true) $('dracon_poll_admin_poll_save['+pollCounter+']').checked = false
		else $('dracon_poll_admin_poll_save['+pollCounter+']').checked = true;
	}
}

/* OPTIONS */
function optionToggleSave() {
	var optionCounter = 0;
	while(true) {
		optionCounter++;
		if (!$('dracon_poll_admin_option_save['+optionCounter+']')) break;
		if ($('dracon_poll_admin_option_save['+optionCounter+']').checked == true) $('dracon_poll_admin_option_save['+optionCounter+']').checked = false
		else $('dracon_poll_admin_option_save['+optionCounter+']').checked = true;
	}
}

function pollAddNewOption(pollId) {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?addNewOption='+pollId).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function optionDell(optionId,pollId) {
	pollAdminLoading();
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?delOption='+optionId+'&pollId='+pollId).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';
	})
}

function pollSaveOptionData(pollId) {
	pollAdminLoading();
	var optionCounter = 0;
	var optionData = '';
	// Build up the data array
	while(true) {
		optionCounter++;
		if (!$('dracon_poll_admin_option_id['+optionCounter+']')) break;
		if ($('dracon_poll_admin_option_save['+optionCounter+']').checked) {
			optionData += '&optionId['+optionCounter+']='+$('dracon_poll_admin_option_id['+optionCounter+']').value+'&title['+optionCounter+']='+encodeURIComponent($('dracon_poll_admin_option_title['+optionCounter+']').value)+'&bullet['+optionCounter+']='+$('dracon_poll_admin_option_bullet['+optionCounter+']').value+'&order['+optionCounter+']='+$('dracon_poll_admin_option_order['+optionCounter+']').value+'&status['+optionCounter+']='+$('dracon_poll_admin_option_status['+optionCounter+']').checked;
		}
	}
	// Send data via POST
	new XHR({onSuccess: function(req){ $('dracon_poll_admin_area').innerHTML = req; }}).send(webRoot+'admin/admin.php?saveOptionData='+pollId,optionData).chain(function(){
		$('dracon_poll_admin_loading').style.visibility = 'hidden';

	})
}


function pollOptionReOrder(orderId) {
	//$orderObj = $('order_div['+orderId+']').getCoordinates();
	$('dracon_poll_order_div['+orderId+']').setStyle('opacity',.5);
	$('dracon_poll_order_div['+orderId+']').setStyle('z-index',optionIdx++);
	var optionCounter = 0;
	while(true) {
		optionCounter++;
		if (!$('dracon_poll_order_div['+optionCounter+']')) break;
		var dropBox = $('dracon_poll_admin_option_order['+optionCounter+']');
		if (optionCounter != orderId) {
			new Drag.Move('dracon_poll_order_div['+orderId+']', { 'droppables': dropBox });

			dropBox.addEvent('over', function(){
				this.className = 'dracon_poll_admin_input dracon_poll_options_order_2';
				this.setStyle('left', 10);
			});
			dropBox.addEvent('leave', function(){
				this.className = 'dracon_poll_admin_input dracon_poll_text_center';
			});
			dropBox.addEvent('drop', function(){
				this.setStyle('background-color', '#face8f');
				this.setStyle('border', '1px solid black');
			});

		}
	}
}

function pollOptionReOrderBack(orderId) {
	var box = $('dracon_poll_order_div['+orderId+']');
	var pollFade = new Fx.Style(box, 'opacity');
	pollFade.options.transition = Fx.Transitions.Quad.easeOut;
	pollFade.start(1,0).chain(function(){

		$('dracon_poll_order_div['+orderId+']').style.top = 0;
		$('dracon_poll_order_div['+orderId+']').style.left = 0;
		$('dracon_poll_order_div['+orderId+']').setStyle('opacity',1);

	});

}

function pollInstall(page) {
	this.blur();
	var postData = '';
	switch (page) {
		case 0: file = 'install.php'; break;
		case 1: file = 'files_check.php'; break;
		case 2: file = 'config_set.php'; break;
		case 3: file = 'finish.php'; break;
		case 10: file = 'readme.php'; break;
		case 11: file = 'changelog.php'; break;
		case 12: file = 'license.php'; break;
	}
	// Config file
	if (page == 3) {
    /* create an array, careful with checkbox */
		postData += 'poll_admin_login='+$('poll_admin_login').value+'&poll_admin_pass='+$('poll_admin_pass').value+'&poll_voted_time='+$('poll_voted_time').value+'&poll_voted_ani='+$('poll_voted_ani').checked+'&poll_web_root='+$('poll_web_root').value+'&poll_data='+$('poll_data').value+'&poll_db_host='+$('poll_db_host').value+'&poll_db_user='+$('poll_db_user').value+'&poll_db_pass='+$('poll_db_pass').value+'&poll_db_name='+$('poll_db_name').value+'&poll_db_prefix='+$('poll_db_prefix').value;
	}
	var instFade = new Fx.Style('install_div', 'opacity', {duration: 500, transition: Fx.Transitions.Quad.easeOut});
	var instLoader = new Fx.Style('install_loading', 'opacity', {duration: 500, transition: Fx.Transitions.Quad.easeOut});
	instFade.start(1,0).chain(function(){
		$('install_loading').setStyle('display', 'inline');
		new XHR({onSuccess: function(req){ $('install_div').innerHTML = req; }}).send('include/'+file,postData).chain(function(){
			$('install_loading').setStyle('display', 'none');
			instFade.start(0,1);
		})
	})
}

// Planned features
function resizePoll(Id) {
	$(Id).style.border = '1px dotted white';
	$(Id).makeResizable();
}
