// Copyrights 2010 CuT Computer

// Init some stuff
$(document).ready(function(){
	// validate the comment form when it is submitted
	$("#makeaapp").validate();
	
	$('#ad-api').attr('action', 'javascript:add();');
	zebra_table();
	
	// Slide Add Form
	$('#slideaddform').click(function() {
		$('#apiadform').slideToggle('slow', function() {});
	});
	
		//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});
	
	// Password Strength
	var bpos = "";
	var perc = 0 ;
	var minperc = 0 ;
	$('#pwd').css( {backgroundPosition: "0 0"} );
	$('#username').keyup(function(){
		$('#result').html(passwordStrength($('#pwd').val(),$('#username').val())) ;
		perc = passwordStrengthPercent($('#pwd').val(),$('#username').val());
	
		bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
		bpos = bpos + perc + "px";
		bpos = bpos + "\" } );";
		bpos=bpos +" $('#colorbar').css( {width: \"" ;
		bpos = bpos + (perc * 2) + "px";
		bpos = bpos + "\" } );";
		eval(bpos);
			$('#percent').html(" " + perc  + "% ");
				  })
	$('#pwd').keyup(function(){
		$('#result').html(passwordStrength($('#pwd').val(),$('#username').val())) ; 
		perc = passwordStrengthPercent($('#pwd').val(),$('#username').val());
		
		bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
		bpos = bpos + perc + "px";
		bpos = bpos + "\" } );";
		bpos=bpos +" $('#colorbar').css( {width: \"" ;
		bpos = bpos + (perc * 2) + "px";
		bpos = bpos + "\" } );";
		eval(bpos);
			$('#percent').html(" " + perc  + "% ");
	})
});
/////// -- Menus -- ///////
// Creating a new function, fadeToggle()
jQuery.fn.fadeToggle = function(speed, easing, callback) { 
    return this.animate({opacity: 'toggle'}, speed, easing, callback); 
};
function jquery_menu_drop() {
    $('#userNav').fadeToggle(400);
    $("#btmenu").toggleClass("open");
}
// Max entry
function limitText(input,wichfield,limitNum) {
	if (input.value.length > limitNum) {
		input.value = input.value.substring(0, limitNum);
	} else {
    	$('#'+wichfield).html(limitNum - input.value.length);
	}
}
// Create new API and add to table
function add() {
	var newapi = $("#new-api").val();
	var newapiname = $("#name-api").val();
	if (newapi == '') {
		feedback('Sie haben keine API eingegeben!', 'fail');
		return;
	}
	if (newapiname == '') {
		feedback('Sie haben keinen Namen eingegeben!', 'fail');
		return;
	}
    if (newapiname.length > '9') {
    	feedback('Der Name darf maximal 9 Zeichen lang sein!', 'fail');
		return;
    }
	add_loading("#submit-api");
	$.getJSON(
		"http://iphonesms.cut-computer.de/beta/ajax.inc.php",
		{mode:'add', api: newapi, name: newapiname},
		function(data){
			if(data.status == 'success') {
				$('#apiDivTable').prepend( data.html ).trigger("update");
				$('.no-api_found').remove();
				zebra_table();
				reset_input();
				increment();
				plural();
			}
			feedback(data.message, data.status);
			end_loading("#submit-api");
			end_disable("#submit-api");
			zebra_table();
		}
	);
}
// Display the edition interface
function edit(id) {
	$("#edit-button-"+id).addClass('loading');
	$('#apiName-id-'+id+' .button').attr("disabled", "disabled").addClass('disabled')
	$.getJSON(
		"http://iphonesms.cut-computer.de/beta/ajax.inc.php",
		{ mode: "edit_display", id: id },
		function(data){
			if (data.status == 1) {
				$("#api-id-"+id).after( data.html );
				$("#edit-api-"+id).focus();
			} else {
				alert('Fehler: '+data.msg);
			}
			end_loading("#edit-button-"+id);
		}
	);
}
// Delete a API
function remove(id) {
	if (!confirm('Moechten Sie wirklich die API loeschen?')) {
		return;
	}
	$("#delete-button-"+id).addClass('loading');
	$.getJSON(
		"http://iphonesms.cut-computer.de/beta/ajax.inc.php",
		{ mode: "delete", id: id },
		function(data){
			if (data.status == 1) {
				$("#id-" + id).fadeOut(function(){$(this).remove();
				zebra_table();
				});
				if (data.end == 1) {
					$('#apiDivTable').prepend('<tr class="no-api_found"><td colspan="8">Sie haben noch keine API&prime;s gespeichert</td></tr>').trigger("update");
				}
				decrement();
				plural();
			} else {
				alert('Der Eintrag konnte nicht entfernt werden: '+data.msg);
			}
		}
	);
}
// Cancel edition of a API
function hide_edit(id) {
	$("#edit-id-"+id).fadeOut(200, function(){
		end_disable('#apiName-id-'+id+' .button');
	});
}
// Save edition of a API
function edit_save(id) {
	add_loading("#edit-close-"+id);
	var newapi = $("#edit-api-"+id).val();
	var newname = $("#edit-name-"+id).val();
	$.getJSON(
		"http://iphonesms.cut-computer.de/beta/ajax.inc.php",
		{mode:'edit_save', id: id, api: newapi, name: newname },
		function(data){
			if(data.status == 'success') {
				$("#api-"+id).html(data.api_new);
				$("#name-"+id).html(data.name_new);
				$("#date-"+id).html(data.date_new);
				$("#edit-id-"+id).fadeOut(200, function(){
					$('#apiDivTable div').trigger("update");
				});
			}
			feedback(data.message, data.status);
			end_disable("#edit-close-"+id);
			end_disable('#apiName-id-'+id+' .button');
			end_loading("#edit-close-"+id);
		}
	);
}
// Begin the spinning animation & disable a button
function add_loading(el) {
	$(el).attr("disabled", "disabled").addClass('disabled').addClass('loading');
}
// End spinning animation
function end_loading(el) {
	$(el).removeClass('loading');
}
// Un-disable an element
function end_disable(el) {
	$(el).removeAttr("disabled").removeClass('disabled');
}
// Prettify table with odd & even rows
function zebra_table() {
	$(".apiBox:even").removeClass('odd').addClass('even');
	$(".apiBox:odd").removeClass('even').addClass('odd');
	$('.apiBox').trigger("update");
}
// Update feedback message
function feedback(msg, type) {
	var span = (type == 'fail') ? '<span class="fail">' : '<span>' ;
	var delay = (type == 'fail') ? 2500 : 1000 ;
	$('#feedback').html(span + msg + '</span>').fadeIn(200,function(){
		$(this).animate({'opacity':1}, delay, function() {
			$(this).fadeOut(800);
		})
	});
}
// Ready to add another API
function reset_input() {
	$('#new-api').val('');
	$('#name-api').val('');
}
// Increment API counter
function increment() {
	$('.increment').each(function(){
		$(this).html( parseInt($(this).html()) + 1);
	});
}
// Decrement API counter
function decrement() {
	$('.increment').each(function(){
		$(this).html( parseInt($(this).html()) - 1 );
	});
}
// Plural counters
function plural() {
	var count = $('.increment').html();
	if (count == '1') {
		$('#pluralentry').html('API');
	} else {
		$('#pluralentry').html('APIs');
	}
}