jQuery.fn.voteBox = function(initActivate) {			

	var container = this;
	var votes = 0; // Anzahl der Votes
	var maxVotes = 3;
	
	var votingClosed = false;
	var votingAllVoted = false;
	
	var votedValues = {};
	
	
	var myecho = function(i) {
	//	console.log(i);
	}
	
	// Count Votes
	
	var sendData = function(url) {
//		console.log(maxVotes);
		
		if (votes === maxVotes && !votingClosed) {
			votingClosed = true;
			
			var rating = 'rate';
			
			jQuery.each(votedValues, function(name, value) {
			    rating =  rating + '::' + value;
			});			
			
			var id = $('#q_ref').val();
			var ajaxData = $('#q_ajaxdata').val();
//			q_pid
			var check = $('#q_check').val();
			
			// clicks entfernen
			$('.submit-button').hide();
			$('#q-allreadyVoted').show();
			$('#tx-q-voting .the-hide').show();			
			
			$('#theCloud').load('index.php?eID=tx_multipleratings_ajax',{'ref':id,'rating':rating,'data':ajaxData,'check':check}, function() {
				
									$('#view-tag-clound').trigger('click');
			});
			return true;
			// $.(url, votedValues, function(data){
			// 			return true();
			// 			
			// 		});
		};
		return false;

	}
	
	var addVote = function(item) {
		if (votes < (maxVotes)) {
			votes = votes+1;
			setItemVoted(item);
			setVoteValue(item);
			//Wertsetzen der Übertragenwerden kann
			activateNextItem(item);
			lockPrevItem(item);
			if (votes == (maxVotes)){
				// keine Votes mehr zulassen
				
				$('#q-submit').attr('src',$('#q-submit').attr('src').replace("-off", "-on"));
				closeVoting();
			}
			else {

			}	
		}
		//myecho(votes);
	}
	

	var removeVote = function(item) {

		if(votingAllVoted) {
			openVoting();
			$('#q-submit').attr('src',$('#q-submit').attr('src').replace("-on", "-off"));
		}
		removeVoteValue(item);
		unlockPrevItem(item)
		votes = votes-1;
		//myecho(votes);
	}
	
	// lllll
	
	var setVoteValue = function(item) {
		votedValues[item.id] = item.rel;
		// myecho(votedValues);
	}
	var removeVoteValue = function(item) {
		key = item.id;
		delete votedValues[key];

	}
	
	// Click Items
	
	var setItemActive = function(item) {
		$(item).bind('click' ,function(event) {
				if (!votingAllVoted && !votingClosed) {
					addVote(this);
				}
		}).addClass('active-item').addClass('waiting');
	}
	
	var setItemPassive = function(item) {
		$(item).unbind('click').removeClass('voted-item').removeClass('active-item');
	}
	
	var setItemClosed = function(item) {
		$(item).addClass('closed-item');
	}
	var setItemLocked = function(item) {
		$(item).addClass('locked-item');
	}
	var setItemUnlocked = function(item) {
		$(item).removeClass('locked-item');
	}
	
	var setItemOpen = function(item) {
		$(item).removeClass('closed-item');
	}
	
	var setItemVoted = function(item) {
			$(item).unbind('click').removeClass('active-item').removeClass('waiting').addClass('voted-item').bind('click' ,function() {
				if (! $(this).hasClass('locked-item') && !votingClosed) {
					removeVote(this);
					resetItemToActive(this);
				}
			});
	}
	var resetItemToActive = function(item) {
		$(item).unbind('click').removeClass('voted-item');
		setItemActive(item);
		setFollowingItemPassive(item);
	}
	var setFollowingItemPassive = function(item) {
		var parts = item.id.split('_');
		if(parts[2] < 3) {
			setItemPassive(document.getElementById(parts[0] + '_' + parts[1] + '_' + (parseInt(parts[2]) + 1)));
		}
	}
	var activateNextItem = function(item) {
	 	var parts = item.id.split('_');
		if(parts[2] < 3) {
			setItemActive(document.getElementById(parts[0] + '_' + parts[1] + '_' + (parseInt(parts[2]) + 1)));
		}
	}
	var lockPrevItem = function(item) {
	 	var parts = item.id.split('_');
		if(parts[2] > 1) {
			setItemLocked(document.getElementById(parts[0] + '_' + parts[1] + '_' + (parseInt(parts[2]) - 1)));
		}
	}
	var unlockPrevItem = function(item) {
	 	var parts = item.id.split('_');
		if(parts[2] > 1) {
			setItemUnlocked(document.getElementById(parts[0] + '_' + parts[1] + '_' + (parseInt(parts[2]) - 1)));
		}
	}
	// Voting global
	
	var openVoting = function() {
		votingAllVoted = false;
		container.find('.closed-item').each(function(index){
			setItemOpen(this);
		})
		container.find('.question-list').removeClass('close-list')
		
	}
	
	var closeVoting = function() {
		votingAllVoted = true;
		container.find('.active-item').each(function(index){
			setItemClosed(this);
		})
		container.find('.question-list').addClass('close-list')
	}
	
	
	// row
	
	var initRow = function(row, initActivate) {
		
		$(row).find('a.fake-checkbox').each(function(i){
				
			if (i === 2 && initActivate) {  // wegen float right umgekehrte Riehenfolge im Html -> erste box i=2
				setItemActive(this);
			}
			else {
				setItemPassive(this);
			}		
			
		});
		
	}
	// Box
	
	var initBox = function(initActivate) {
		container.find('.question-list li').each(function(index) {
			initRow(this, initActivate);		
		});
		
		$('#tx-q-voting').submit(function() {
			
			if (sendData(this.action)) {

				//alert('ok');
			}
			else {
				alert('Bitte alle 3 Stimmen abgeben.');	
			}
		  
		  return false;
		});
		
		
		
	}
	

	initBox(initActivate);
	
	
}	


jQuery.fn.tabBox = function() {

	var container = this;
	
	container.find('ol li').each(function(index) {

		if (index == 0) {
			$(this).addClass('active');
			container.find('.tx-q-container-element').eq(index).addClass('current-container');
		}
		container.find('.tx-q-container-element').eq(index).addClass('blind-container');	
	

		$(this).hover( 
			function() {
				$(this).css('cursor','pointer');
			},
			function() {
				$(this).css('cursor','default');
			});
		// Lisk handler
		$(this).click( function() {
			if(index > -1) {
				var myIndex = index;
			}
			container.find('.tx-q-nav .active').removeClass('active');
			$(this).addClass('active');
			container.find('.current-container').removeClass('current-container');
			container.find('.tx-q-container-element').eq(myIndex).addClass('current-container');
			
			
		});
		

	});
	index = -1;	
	
	
}


function tx_multipleratings_submit(id, rating, ajaxData, check) {
	
	$('#tx-multipleratings-display-' + id).css('visibility','hidden');
	$('#tx-multipleratings-wait-' + id).css('visibility','visible');
	$('#tx-multipleratings-' + id).load('index.php?eID=tx_multipleratings_ajax',{'ref':id,'rating':rating,'data':ajaxData,'check':check});

}

