/**
 * @author jue
 */

$(document).ready(function(){


	/**
	 * Tooltips
	 */
	$('.tooltip').tooltip({ 
		track: true, 
		delay: 0, 
		showURL: false, 
		showBody: " :: ", 
		fade: 250 
	});
	
	/**
	 * Toggle slideout/in and copying the vote form
	 */
	var voted = false;
	var trackid = 0;
	
	$('#tracklist li div.response').each(function (i) {
		$(this).hide();
	});
	
	/*$('ul#tracklist').hover(function () {
		$('ul#tracklist object').attr('title','Klicken Sie auf diese Noten,<br />um diesen Titel anzuh&ouml;ren.').addClass('tooltip');
		enableTooltip();
	});*/
	
	//,#tracklist li span.title
	$('#tracklist li a.vote,#tracklist li span.title').bind('click',function () {
		$('#tracklist li a.vote').removeClass('active');
		$(this).parent().parent().find('.response').slideUp("slow").html('');
		if ($(this).parent().find('.response').is(':hidden')) {
			$(this).parent().find('a.vote').addClass('active');
			$(this).parent().find('.response').html($('#vote_form').html()).slideDown("slow");
			//trackid = $(this).attr('id').replace(/vote_/g,'');
			trackid = $(this).parent().attr('id');
			//var parentId = $(this).parent().attr('id');
			$(this).parent().find('.response input.hidden_track').val(trackid.replace(/track/g,''));
			$(this).parent().find('.response input.hidden_action').val('vote');
			$(this).parent().find('.response form').addClass('vote');
			$(this).parent().find('.response form').attr('id', 'form_' + trackid);
			
			
			/**
			 * Autom. delete/insert predefined values into inputs
			 */
			$('input.clearvalue').each(function (i) {
				$(this).focus(function () {
					var value = $(this).attr('value');
					var title = $(this).attr('title');
					if(value == title) $(this).attr('value','');
				});
				$(this).blur(function () {
					var value = $(this).attr('value');
					var title = $(this).attr('title');
					if (value == '' && value != title) $(this).attr('value',title);
				});

			});
			
			/**
			 * Fix the double assigning of IDs in tpl and copy
			 */
			$(this).parent().find('.response form.vote input.vote_check_agb').attr('id','vote_check_agb');
			$(this).parent().find('.response form.vote input.vote_check_brochure').attr('id','vote_check_brochure');
			$(this).parent().find('.response form.vote input.vote_check_offer').attr('id','vote_check_offer');
			
			/**
			 * Handles additional validation rules
			 */
			addRuleNotice = false;
			$(this).parent().find('form.vote input.addrule').change(function() {
				if ($(this).attr('checked') === true) {
					addRuleNotice = true;
					$('.response form.vote input[name="street"],.response form.vote input[name="zip"],.response form.vote input[name="city"]').addClass('required');
				} else {
					addRuleNotice = false;
					$('.response form.vote input[name="street"],.response form.vote input[name="zip"],.response form.vote input[name="city"]').removeClass('required');
				}
			});
			
			/**
			 * Saves the user input for later usage
			 */
			/*$(this).parent().find('.response form.vote input[type="text"]').bind('change blur', function(e) {
				$('#form_values span[class="' + $(this).attr('name') + '"]').text($(this).val());
			});*/
			
			/**
			 * Handles the vote form validation(ony clientside) + transmission
			 */
			$('form.vote').submit(function () {
				// Reset all errors
				var error = 0;
				$('form.vote input.error').removeClass('error');
				$('form.vote .error-text').removeClass('error-text');
				$('form.vote span.addRuleNotice').css('display','none');
				
				// Looping through all required fields
				$('form.vote input.required').each(function (i) {
					// Checks if title exists
					if ($(this).attr('title') != '') {
						// If title exists, check if title is equal to value
						if ($(this).val() == $(this).attr('title')) {
							// If title == value, mark as error
							error = 1;
							$(this).addClass('error');
						}
					} else if ($(this).val() == '') {
						// If value is empty, mark as error
						error = 1;
						$(this).addClass('error');
					}
					
					// Check e-mail
					if ($(this).hasClass('email') && !($(this).val().match(/([\w-\.]+)@((?:[\w-\.]+\.)+)([a-zA-Z]{2,4})/g))) {
						error = 1;
						$(this).addClass('error');
					}
					// Check alphanumeric
					else if ($(this).hasClass('alphanum') && !($(this).val().match(/^([a-zA-Z0-9_-]+)$/))) {
						error = 1;
						$(this).addClass('error');
					}
					// Check bool
					else if ($(this).hasClass('bool') && !($(this).is(':checked'))) {
						error = 1;
						$(this).addClass('error');
						// Check if label exists for this input
						if ($(this).attr('id') != '' && $("label[for='" + $(this).attr('id') + "']")) {
							// Color label in error style
							$("label[for='" + $(this).attr('id') + "']").addClass('error-text');
						}
					}
					// Check numeric
					else if ($(this).hasClass('numeric') && !($(this).val().match(/[0-9]/g))) {
						error = 1;
						$(this).addClass('error');
					}
				});
				if (error != 0) {
					// Display notice in error style
					$('form.vote p.notice').addClass('error-text');
					
					// Display extra error for additional validation rules
					if (addRuleNotice === true) $('form.vote span.addRuleNotice').css('display','');
					
					// Disallow form transmission
					return false;
				}
				// Allow form transmission
				 $.ajax({
					type: "POST",
					url: $(this).attr('action'),
					data: $(this).serialize(),
					cache: false,
					async: false,
					success: function(data){
						$('#' + trackid + ' div.response').html(data);
					},
					timeout: function(data){
						$('#' + trackid + ' div.response div.error-response').html('<p><strong>Es ist ein Fehler aufgetreten</strong><br />Leider ist bei der &Uuml;bertragung ein Fehler aufgetreten. Bitte versuchen Sie es erneut.</p>');
						$('#' + trackid + ' div.response div.error-response').show();
					},
					error: function(data){
						$('#' + trackid + ' div.response div.error-response').html('<p><strong>Es ist ein Fehler aufgetreten</strong><br />Leider ist bei der &Uuml;bertragung ein Fehler aufgetreten. Bitte versuchen Sie es erneut.</p>');
						$('#' + trackid + ' div.response div.error-response').show();
					}
				 });
				return false;

			});
			
		} else {
			$(this).parent().find('.response').slideUp("slow", function() {
				$(this).parent().find('.response').html('');
			});
		}
		return false;
	});
	
	$('ul.end li a.vote,ul.end li span.title').unbind('click');


});
