$(document).ready(function() {
	//$(".font_price_field").numeric({allow:"."});
	
	 $('a[rel*=facebox]').facebox()
	
	$('input.autocomplete').each(function(){
    var input = $(this);
    input.autocomplete(input.attr('autocomplete_url'),{
      matchContains:1,//also match inside of strings when caching
      mustMatch:1,//allow only values from the list
      selectFirst:1,//select the first item on tab/enter
      removeInitialValue:0//when first applying $.autocomplete
    });
  });

});


function update_css_of_sample_box(slug, value) {
	var new_styles = value.split(';');
	for(i=0; i< new_styles.length;i++){
		if ( new_styles[i].split(':')[1]) {
			style = new_styles[i].split(':');		
			style[0] = "'" + style[0].replace(/^\s*/, "").replace(/\s*$/, "") + "'";
			if (!style[1].match(/^(\s)'.*'$/)) { style[1] = "'" + style[1].replace(/^\s*/, "").replace(/\s*$/, "") + "'"; }
			$("#sample-text-" + slug).css(style[0],style[1]);					
		}
	}
}

function update_sample_box_text(value) {
	//var sample_array = $('textarea.sample_text');
	//for(i=1; i< sample_array.length;i++){
		//sample_array[i].innerHTML = value;
	//}
}
	
function update_sample_box_size(value) {
	var sample_array = $('textarea.sample_text');
	var px = "px";
	var new_size = value + px;
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('font-size', new_size);						
	}	
}

function update_sample_box_line_height(value) {
	var sample_array = $('textarea.sample_text');
	var px = "%";
	var new_size = value + px;
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('line-height', new_size);						
	}	
}

function update_sample_box_word_spacing(value) {
	var sample_array = $('textarea.sample_text');
	var px = "px";
	var new_size = value + px;
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('word-spacing', new_size);						
	}	
}

function update_sample_box_letter_spacing(value) {
	var sample_array = $('textarea.sample_text');
	var px = "px";
	var new_size = value + px;
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('letter-spacing', new_size);						
	}	
}

function update_sample_box_background(value) {
	var sample_array = $('textarea.sample_text');
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('background-color', value);						
	}	
}

function update_sample_box_color(value) {
	var sample_array = $('textarea.sample_text');
	for(i=0; i< sample_array.length;i++){
		$(sample_array[i]).css('color', value);						
	}
}

function hide_favorite_links()	{	
	var favorite_array = $('.favorite_link');
	for(i=0; i< favorite_array.length;i++){
		$(favorite_array[i]).hide;						
	}	
}


/* FONT KEYWORD FILTERING */
function show_fonts_tagged(tag) {
	$("#results").slideUp();

	$('.' + tag).toggleClass('selected');	
	
	var filtering = "";
	$('.selected').each(function() {
		var clean_filter = $(this).html().toLowerCase().replace(/ /g, '-');
		filtering = clean_filter + ',' + filtering;
	});
	
	$.get('/styles/' + filtering + '.js', '', function(html) { // perform an AJAX get, the trailing function is what happens on successful get.
	  $("title").html(filtering + " | Kernest.com")
	  $("#results").slideDown();
	  $("#results").html(html); // replace the "results" div with the result of action taken
		// $('#styles').refresh
	});

}