var currentFX = null;
toggleMenu = function(el)
{
	$('ul', el.parentNode).SlideToggleUp(500);
}

$(document).ready(
	function ()
	{
		if ($.browser.msie) _fixPNG();
		$('#docs').click(
			function() {
				$('ul',this.parentNode).SlideToggleUp(500);
				this.blur();
				return false;
			}
		);
		
		$('.interfacePlugins a').not('.selectNoneSubgroup').not('.selectAllSubgroup').click(addToDownload);
		$('#selectDownload').click(buildDownload);
		$('#selectAll').click(selectAll);
		$('#selectNone').click(selectNone);
		$('a.selectAllSubgroup').click(selectAllSub);
		$('a.selectNoneSubgroup').click(selectNoneSub);
		$('a.goToTop').click(
			function()
			{
				$('#interface_fx').ScrollTo(1000);
				return false;
			}
		);
		$('a.toggleFxContent').click(toggleFxContent);
		a = new String(window.top.location);
		if(a.indexOf('http://interface.eyecon.ro') != 0 && a.indexOf('http://www.eyecon.ro') != 0 && a.indexOf('http://eyecon.ro') != 0)
			window.top.location.href = 'http://interface.eyecon.ro';
	}
);
var selectAllSub = function()
{
	if (building == true)
		return false;
	$('a', this.parentNode.parentNode).not('.selectNoneSubgroup').not('.selectAllSubgroup').addClass('selected').each(
		function()
		{
			selectedElements[$(this).attr('id')] = true;
		}
	);
	this.blur();
	return false;
};
var selectNoneSub = function()
{
	if (building == true)
		return false;
	$('a', this.parentNode.parentNode).not('.selectNoneSubgroup').not('.selectAllSubgroup').removeClass('selected').each(
		function()
		{
			selectedElements[$(this).attr('id')] = false;
		}
	);
	this.blur();
	return false;
};
var selectAll = function()
{
	if (building == true)
		return false;
	$('.interfacePlugins a').not('.selectNoneSubgroup').not('.selectAllSubgroup').addClass('selected').each(
		function()
		{
			selectedElements[$(this).attr('id')] = true;
		}
	);
	this.blur();
	return false;
}
var selectNone = function()
{
	if (building == true)
		return false;
	$('.interfacePlugins a').not('.selectNoneSubgroup').not('.selectAllSubgroup').removeClass('selected').each(
		function()
		{
			selectedElements[$(this).attr('id')] = false;
		}
	);
	this.blur();
	return false;
}
toggleFxContent = function()
{
	if (currentFX && currentFX != this.parentNode.nextSibling)
		$(currentFX).toggle();
	$(this.parentNode.nextSibling).toggle();
	//$(this).ScrollTo(500);
	this.blur();
	currentFX = this.parentNode.nextSibling;
	return false;
}


buildDownload = function()
{
	/*$(this).hide();
	$('#saveAjax').show();*/
	var selected = 0;
	var dataRequest = '';
	for (i in selectedElements) {
		if (selectedElements[i] == true) {
			dataRequest += (dataRequest.length > 0 ? ',' : '') + i;
			selected ++;
		}
	}
	if (selected == 0) {
		$(this).show();
		$('#saveAjax').hide();
		alert('You must select at least one element.');
		return false;
	} else {
		$('#toDownload').val(dataRequest);
		$('#downloadForm').submit();
	}
	
	//building = true;
	/*
	$.ajax(
		{
			url: '/ajax/server.php',
			data: dataRequest,
			type: 'POST',
			complete: function()
			{
				$('#selectDownload').show();
				$('#saveAjax').hide();
				building = false;
			},
			success: function(xmlRes)
			{
				var okies = $('ok', xmlRes);
				var errorsE = $('error', xmlRes);
				if (errorsE.size() > 0) {
					errorsE.each(
						function()
						{
							alert($(this).text());
						}
					);
					return;
				}
				$('#toDownload').val(pack(okies.text(),62, true));
				$('#downloadForm').get(0).submit();
			}
		}
	);*/
	/*for(i = 0; i < selected.length; i++)
	{
		inputEl = document.createElement('input');
		$(inputEl).attr(
			{
				type	: 'hidden',
				name	: 'selected[]',
				value	: selected[i]
			}
		);
		$('#downloadForm').append(inputEl);
	}
	$('#downloadForm').get(0).submit();*/
	
	return false;
}
var building = false;
var selectedElements = {};

var addToDownload = function()
{
	if (building == true)
		return false;
	this.blur();
	var el = $(this);
	var id = el.attr('id');
	if (!selectedElements[id]) {
		selectedElements[id] = true;
		$(this).addClass('selected');
		if ((id == 'idp' || id == 'ist' || id == 'isr') && !selectedElements.idg) {
			$('#idg').addClass('selected');
			selectedElements.idg = true;
		}
		
		if ((id == 'ist' || id == 'isr') && !selectedElements.idp) {
			$('#idp').addClass('selected');
			selectedElements.idp = true;
		}
	} else {
		$(this).removeClass('selected');
		if (id == 'idg') {
			if (selectedElements.idp == true) {
				selectedElements.idp = false;
				$('#idp').removeClass('selected');
			}
			if (selectedElements.ist == true) {
				selectedElements.ist = false;
				$('#ist').removeClass('selected');
			}
			if (selectedElements.isr == true) {
				selectedElements.isr = false;
				$('#isr').removeClass('selected');
			}
		}
		if (id == 'idp') {
			if (selectedElements.ist == true) {
				selectedElements.ist = false;
				$('#ist').removeClass('selected');
			}
			if (selectedElements.isr == true) {
				selectedElements.isr = false;
				$('#isr').removeClass('selected');
			}
		}
		selectedElements[id] = null;
	}
	return false;
}

function _fixPNG() {
	var images = $('img[@src*="png"]'), png;
	images.each(
		function() {
			png = this.src;
			this.src = '/site/spacer.gif';
			this.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + png + "')";
		}
	);
}