//change language (keyboard layout) function 
function ChangeLang(mode)
{
  if (mode >= 4) { 
	  $('#textarea').attr("dir", "rtl");
	  $('.delete_btn').hide();
	  $('.delete_btn2').show();
  } 
  else {
	  $('#textarea').attr("dir", "ltr");
	  $('.delete_btn').show();
	  $('.delete_btn2').hide();
  }
  for (i=1; i<=47; i++)
	  {
	     $('a[name='+(i)+']').text(chars[mode][i]);
	     $('a[name='+(i)+']').attr('title', chars[mode][i])
	  }
	
}

$(document).ready(function() {

  ChangeLang(4);  //set default language to 0 (english), 4 - hebrew
  $(".form_lang :last").attr("selected", "selected");
  
// click on keyboard button
$('.keyboard_content .key_button').click(function (event) 
{ 
     event.preventDefault(); 
     //here you can also do all sort of things 
     var areaValue = $(this).text();
     if (areaValue == 'Space') areaValue = ' ';
     if (areaValue == 'CTRL' || areaValue == 'Ctrl') areaValue = '';
     if (areaValue == ' Shift' || areaValue == 'Caps ') {
         if ($('#shift_opt').is(':checked')) 
         {
        	 $('#shift_opt').attr('checked', false);
           ChangeLang($(".form_lang").val());
         }
         else
         { 
        	 $('#shift_opt').attr('checked', true);
        	 var shift_lang = $(".form_lang").val();
        	 shift_lang++;
        	 ChangeLang(shift_lang);
         }	 

         areaValue = '';    
     }
     if ($('#shift_opt').is(':checked')) areaValue = areaValue.toUpperCase();
     //insert value at cursor position (little bugged in IE)
     insertAtCaret('textarea', areaValue);
     //insert value to the end of textarea
     //$('#textarea').val($('#textarea').val()+areaValue);
});

// combobox - language select on change
$(".form_lang").change(function() { 
	$('#shift_opt').attr('checked', false);
	ChangeLang($(".form_lang").val()); 

});

//click on backspace button
$('.delete_btn').click(function (event) 
		{ 
	      var areaValue = $('#textarea').val();
	      areaValue = areaValue.substring(0, areaValue.length - 1);
	      $('#textarea').val(areaValue);
		 });

//click on backspace button on hebrew
$('.delete_btn2').click(function (event) 
		{ 
	      var areaValue = $('#textarea').val();
	      areaValue = areaValue.substring(0, areaValue.length - 1);
	      $('#textarea').val(areaValue);
		 });

//click on clear button
$('.keyboard_content .key_clear').click(function (event) { 
	        $('#textarea').val('');
});

//shit checkbox click
$('#shift_opt').click(function (event) { 
	  if ($('#shift_opt').is(':checked')) 
    {
		  var shift_lang = $(".form_lang").val();
	    shift_lang++;
	    ChangeLang(shift_lang);
    }
    else
    { 
    	ChangeLang($(".form_lang").val());      
    }  
});


//action combobox - select actipn and do it:)
$(".post_select").change(function() {

	var action = $(".post_select").val()

	switch (action) {
	
	case 'clipboard' : window.prompt("Copy to clipboard: Ctrl+C, Enter", $('#textarea').val()); break;
	case 'google' : window.open("http://www.google.com/search?q="+$('#textarea').val());  break;	  
	
	}
	//make first element selected
	$(".post_select :first").attr("selected", "selected");
});

//click on copy to clipboard button
$('#clipboard').click(function (event) { 
	    window.prompt("Copy to clipboard: Ctrl+C, Enter", $('#textarea').val()); 
});

//click on copy to clipboard button
$('#google-search').click(function (event) { 
	//window.open("http://www.google.com/search?q="+$('#textarea').val());
	$('#searchform').submit();
});

//click on copy to clipboard button
$('#post-twitter').click(function (event) { 
	window.open("http://twitter.com/share?url="+escape(document.URL)+"&via=heb keyboard&text="+$('#textarea').val(),"twitterwindow",'height=200,width=500');
});


//click on copy to clipboard button
$('#post-facebook').click(function (event) { 
	
//	window.open("http://www.facebook.com/dialog/feed?app_id=228365340539018&link=http://developers.facebook.com/docs/reference/dialogs/&picture=http://fbrell.com/f8.jpg&name=Facebook%20Dialogs&caption=Reference%20Documentation&description=Dialogs%20provide%20a%20simple,%20consistent%20interface%20for%20applications%20to%20interact%20with%20users.&message=Facebook%20Dialogs%20are%20so%20easy!&redirect_uri=http://navigator.co.il","twitterwindow",'height=200,width=500');
	
	FB.init({
	    appId  : '228365340539018',
	    status : true, // check login status
	    cookie : true, // enable cookies to allow the server to access the session
	    xfbml  : true, // parse XFBML
	    channelURL : 'http://WWW.MYDOMAIN.COM/channel.html', // channel.html file
	    oauth  : true // enable OAuth 2.0
	  });
	
	FB.ui(
			  {
			    method: 'feed',
			    attachment: {
			      name: 'JSSDK',
			      caption: 'The Facebook JavaScript SDK',
			      description: (
			        'A small JavaScript library that allows you to harness ' +
			        'the power of Facebook, bringing the user\'s identity, ' +
			        'social graph and distribution power to your site.'
			      ),
			      href: 'http://fbrell.com/'
			    },
			    action_links: [
			      { text: 'fbrell', href: 'http://fbrell.com/' }
			    ]
			  },
			  function(response) {
			    if (response && response.post_id) {
			      //alert('Post was published.');
			    } else {
			      alert('Post was not published.');
			    }
			  }
			);	
});


//tooltip logic
$('#tip a[href]').qtip(
		   {
		        content: "Don't be so naive!",
		        position: {
		            corner: {
		            target: 'topMiddle',
		            tooltip: 'bottomMiddle'
		          }
		        },
		        style: {
		           border: {
		               width: 2,
		               radius: 4
		           },
		           padding: 10, 
		           textAlign: 'center',
		           tip: true, 
		           name: 'light'
		        }
});


});
