function setFontSize(selectElement) {
	document.applets[0].appletSetFontSize($(selectElement).val());
}

function setFont(baseUrl, selectElement) {
	v  = $(selectElement).val();
	id = $(selectElement).find("option:selected").attr('title');
	family = $("#font_family_select").find("option:selected").html();
	name = $(selectElement).find("option:selected").html();
	$("#record_download_link").attr('href', baseUrl+v);
	$.get(baseUrl+"/search/get_font_price?id="+id, function(data, status, req){
		$("#font_price").html(data);
		//set paypal inputs
		cart = $("#font_cart");
		cart.find(".item_name").val(family+" "+name);
		cart.find(".item_amount").val(data)
	})
	document.applets[0].appletSetFont(v);

}

function setKeyboard(selectElement) {
	document.applets[0].appletSetKeyboard($(selectElement).val());
	  }

function setText(selectElement){
	filename = $(selectElement).val();

	switch(filename){
		case "":
			document.applets[0].appletSetText("");
		break;
		default: 
			//need to do ajax request to get text from file
			$.get(filename, function(data, status, req){
				document.applets[0].appletSetText(data);
			})
			
	}
	
	
}

function setFamily(selectElement, u, baseUrl){
	family_id = $(selectElement).val();
	url = u+"?record_id="+family_id;
	var glyph_select;
	
	//get styles from ajax request
	$.get(url, function(d, status, req){
		data = JSON.parse(d);
		
		//update style list
		//wipe out existing options
		$("#font_select option").remove();
		
		//put in new options
		$.each(data["fonts"], function(key, values) {
			$("#font_select").append($('<option></option>').val(values["url"]).html(values["name"]));			
		});
		
		//choose style in applet
		fs = $("#font_select");//need to reselect b/c html has changed
		setFont(baseUrl, fs);

		//update permalink
		$("#record_permalink").attr('href', data['record_permalink_url']);
		$("#record_permalink").html(data['absolute_record_permalink_url']);
		
		//update get_share_font
		$("#get_share_font").attr('href', data['record_permalink_url']);
		
		//update toolbar edit link, if present
		if($("#record_edit_link").length){
			$("#record_edit_link").attr('href', data["record_edit_url"]);
		}

		//update bookmark link
		$.get(baseUrl+"/search/get_bookmark_link_for_record?id="+data['id'], function(data, status, req){
			$("#record_bookmark_link").replaceWith(data);
		})
		
		//update sample/glyph text values
		glyph_select = $("#glyph_select");
		glyph_options = glyph_select.attr('options');
				
		glyph_options[1].value = data["glyph_text_filename"];//new Option("Glyphs", data["glyph_text_filename"]);
		glyph_options[0].value = data["sample_text_filename"];//new Option("Sample Text",data["sample_text_filename"]);
		glyph_select = $("#glyph_select");
		
		//don't reset text if user is inputting
		if(!glyph_options[2].selected){
			setText(glyph_select);
		}
						
		//set description
		setDescription(data["record_id"], data["record_description"])
	});
}
 
function setImage(selectElement, u){
	val = $(selectElement).val();
	// $("#record_div img").attr('src',  val);
	url = u+"?id="+val;
	$.get(url, function(d, status, req){
		data = JSON.parse(d);
		img = data["image_filename"];
		style = data["style"];//doing this server side to take reuse resizing functionality already written in php
		id = data["record_id"];
		description  = data["record_description"];

		//update image
		$("#record_div img").attr('src',  img);
		$("#record_div img").attr('style', style);

		//update toolbar
		$("#record_toolbar").replaceWith(data["record_toolbar"]);
		
		//update description
		setDescription(id, description);
	})
}

function setDescription(id, description){
	$("#id_data").text(id);
	$("#description_data").html(description);	
}

//this function will takes a select element
//changes the selected value of the dropdown
//to the next option
//and triggers the click event for that option
function chooseItem(selectElement, changeBy){
	s = selectElement;
	i = s.find("option:selected").index();
	opts = s.attr('options');
	if(i+changeBy >= 0){
		next_o = opts[i+changeBy];
		s.val($(next_o).val());		
		s.change();
	}
}



function setBidi() {
  bidi = new String();
  //bidi = document.forms[0].bidi.value;
  bidi = document.getElementById('bidiSelect').value
  document.applets[0].appletSetBidi(bidi);
  return false;
}

function setAlign() {
  alignment = new String();
  alignment = document.getElementById('alignmentSelect').value;
  document.applets[0].appletSetAlign(alignment);
  return false;
}

function setInputMethod(){
	var imBtn = document.getElementById('inputmethod');
	var pApplet = document.applets[0];

	if(imBtn && pApplet){

		pApplet.setNaim(imBtn.value);
		pApplet.focus();
		
	}
}

function setOrderAlignInput(){
  //this function changes both the alignment and bidi ordering
  var selector = document.getElementById('orderAlignInputSelect');
  var bidi;		//values = "bidi", "rtl", "ltr"
  var alignment;	//values = "left" or "right"
    
  switch(selector.value){
  	case "leftltrnaim":
  		document.applets[0].setNaim('NAIM')
		document.applets[0].appletSetAlign('left');
		document.applets[0].appletSetBidi('ltr');
		bidi 		= "ltr";
  		alignment 	= "left";
  	break;
  	
  	case "rightrtlnaim":
  		document.applets[0].setNaim('NAIM')
		document.applets[0].appletSetAlign('right');
		document.applets[0].appletSetBidi('rtl');
		bidi 		= "rtl";
  		alignment	= "right";
  	
	break;
  	
	case "leftbidinaim":
  		document.applets[0].setNaim('NAIM')
		document.applets[0].appletSetAlign('left');
		document.applets[0].appletSetBidi('bidi');
		bidi 		= "bidi";
  		alignment 	= "left";
  	break;
	
	case "rightbidinaim":
	    document.applets[0].setNaim('NAIM')
  		document.applets[0].appletSetAlign('right');
		document.applets[0].appletSetBidi('bidi');
		bidi 		= "bidi";
  		alignment 	= "right";
  	break;
	
	case "leftltrshaping":
  		document.applets[0].setNaim('System')
		document.applets[0].appletSetAlign('left');
		document.applets[0].appletSetBidi('ltr');
		bidi 		= "ltr";
  		alignment 	= "left";
  	break;
  	
  	case "rightrtlshaping":
  		document.applets[0].setNaim('System')
		document.applets[0].appletSetAlign('right');
		document.applets[0].appletSetBidi('rtl');
		bidi 		= "rtl";
  		alignment	= "right";
  	
	break;
  	
	case "leftbidishaping":
  		document.applets[0].setNaim('System')
		document.applets[0].appletSetAlign('left');
		document.applets[0].appletSetBidi('bidi');
		bidi 		= "bidi";
  		alignment 	= "left";
  	break;
	
	case "rightbidishaping":
	    document.applets[0].setNaim('System')
  		document.applets[0].appletSetAlign('right');
		document.applets[0].appletSetBidi('bidi');
		bidi 		= "bidi";
  		alignment 	= "right";
  	break;
	
  	default:
  		bidi = "bidi";
  		alignment = "right";
  	break;
  }
  document.applets[0].appletSetBidi(bidi);
  document.applets[0].appletSetAlign(alignment);
}


function toggleSampleUserFields(el) {

 selectElement = $(el);
 selectedVal = selectElement.val();
 //options values are paths
 //but for user input its an empty string
 if(selectedVal == ""){
   $('#alignmentSelect').hide();
   $('#glyph_select').hide();
   $('#keyboard_select').show();
   $('#orderAlignInputSelect').show();
   $('#clear').show();
   $('#close').show();
   $('#try').hide();
  
 }else{
   $('#alignmentSelect').show();
   $('#glyph_select').show();
   $('#keyboard_select').hide();
   $('#orderAlignInputSelect').hide();
   $('#clear').hide();
   $('#close').hide();
   $('#try').show();
   
 }
 
}


function clearText() {
  document.applets[0].clearText();
  return false;
}

function userInputSelect(){
    sel = $("#glyph_select");
    $(sel.find("option")[2]).attr('selected', 'selected');
    setText(sel);
	$('#alignmentSelect').hide();
	$('#glyph_select').hide();
    $('#keyboard_select').show();
    $('#orderAlignInputSelect').show();
    $('#clear').show();
	$('#close').show();
    $('#try').hide();
	
  }
  
  
  function sampleSelect(){
    sel = $("#glyph_select");
    $(sel.find("option")[0]).attr('selected', 'selected');
    setText(sel);
	$('#alignmentSelect').show();
	$('#glyph_select').show();
    $('#keyboard_select').hide();
    $('#orderAlignInputSelect').hide();
    $('#clear').hide();
	$('#close').hide();
    $('#try').show();
	
  }
  
 function rightbidishapingSelect(){
    sel = $("#orderAlignInputSelect");
    $(sel.find("option")[0]).attr('selected', 'selected');
    setText(sel);
 }

function rightAlignSelect(){
	document.applets[0].appletSetAlign('right');
  }

function reloadPage()
  {
  window.location.reload();
  }



