var noEvaluations = Array();

$(document).ready(function(){
	init();
	$(".evaluations .exists").hide();
	$("#no-javascript").hide();
	
	$("#self-ea").validate();
	
	$("#demovideo .switch").click(function(){
		$("#demovideo .body").slideToggle('fast');
	});

	$("#tradehours .switch").click(function(){
		$("#tradehours .body").slideToggle('fast');
	});
	$("#tradehours .body").css("display","none");
});


function init(){
	var language = $('#LANGUAGE').val();
	
	$(".indicator").unbind().change(function(){
		var value = $(this).val();
		var id = $(this).attr('id');
		var id_encoded = escapeAll(id);

		if(value == ""){
			$('.' + id_encoded + ' > .properties').empty();
			$('.' + id_encoded + ' > .msg_noindicator').show();
		}else{
			$.get("/sites/selfea/form_indicator.php", 
				  {id: id, q: value, lang: language}, function(data){
				
				$('.' + id_encoded + ' > .msg_noindicator').hide();
				$('.' + id_encoded + ' > .properties').html(data);
				
				$(".details").unbind().click(function(){
					var target = $(this).attr("indicator") + "[details]";
					target = escapeAll(target);
					
					$("#" + target).slideToggle('fast');
				});
				
			});
		}
	});
	
	$(".add").unbind().click(function(){
		var eval_type = $(this).attr('eval_type');
		var arg = eval_type.split('_');

		if(noEvaluations[eval_type]){noEvaluations[eval_type]++;}else{noEvaluations[eval_type]=1;}
		
		var target_id = '#' + arg[0] + '_' + arg[1] + 's';
		$.get("/sites/selfea/form_evaluation.php", 
			{target: arg[0], type: arg[1], count: noEvaluations[eval_type], lang: language}, function(data){
			
			$(target_id + ' .no_exists').hide();
			$(target_id + ' > .container').append(data);
			$(target_id + ' > .exists').show();
			init();
		});
		
	});

	$(".minimize").unbind().click(function(){
		var id = $(this).attr("eval_id");
		var id_encoded = escapeAll(id);
		
		$("#" + id_encoded + " .body").slideToggle('fast');
	});

	$(".delete").unbind().click(function(){
		var id = $(this).attr("eval_id");
		var id_encoded = escapeAll(id);
		var eval_type = $(this).attr("eval_type");
		var eval_type_id = '#' + escapeAll(eval_type) + 's';

		$("#" + id_encoded).remove();
		if($(eval_type_id + ' > .container').is(':empty')){
			$(eval_type_id + ' > .exists').hide();
			$(eval_type_id + ' .no_exists').show();
		}
		init();
	});
}

function countEvals(){
	for(var eval_type in noRegEvals){
		var to_be_changed_id = '#no_reg_' + eval_type + 's';
		var count = noRegEvals[eval_type];

		$(to_be_changed_id).html(count);
	}
}

function escapeAll(string){
	string = string.replace(/\[/g, "\\[");
	string = string.replace(/\]/g, "\\]");
	return[string];
}

function in_array (needle, haystack, argStrict) {
    // Checks if the given value exists in the array  
    // 
    // version: 911.718
    // discuss at: http://phpjs.org/functions/in_array
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: vlado houba
    // +   input by: Billy
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: true
    // *     example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
    // *     returns 2: false
    // *     example 3: in_array(1, ['1', '2', '3']);
    // *     returns 3: true
    // *     example 3: in_array(1, ['1', '2', '3'], false);
    // *     returns 3: true
    // *     example 4: in_array(1, ['1', '2', '3'], true);
    // *     returns 4: false
    var key = '', strict = !!argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}
