jQuery.ajaxSetup({
  beforeSend: function(xhr) {xhr.setRequestHeader("Accept", "text/javascript");}
});

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
    $.post(this.action, $(this).serialize(), null, "script");
    return false;
  })
  return this;
};


function change_language(){
	var cl= window.location.href;
	var lang = $("#lang_chooser").val();
	var matchStr = /locale=(en|de|sr_Latn|ja|ko|es)/;
	var new_location=cl;
	if(cl.search(matchStr)>-1){
		new_location=cl.replace(matchStr, "locale="+lang);
	}else if(cl.search(/\?/)>-1){
		new_location=cl+"&locale="+lang;
	}else{
		new_location=cl+"?locale="+lang;
	}
	window.location.href=new_location;
}

$(document).ready(function() {
	
  $('#lang_chooser').msDropDown();

	$('#feedback_btn').qtip({
		content: {
			// Set the text to an image HTML string with the correct src URL to the loading image you want to use
			text: '<img class="throbber" src="/home/images/preloader.gif" alt="Loading..." />',
			ajax: {
				url: '/feedback' // Use the rel attribute of each element for the url to load
			},
			title: {
				text: 'Feedback', // Give the tooltip a title using each elements text
				button: true
			}
		},
		position: {
			at: 'top center', // Position the tooltip above the link
			my: 'bottom center',
			viewport: $(window), // Keep the tooltip on-screen at all times
			effect: false // Disable positioning animation
		},
		show: {	
			event: 'click',
			modal: true,
			solo: true // Only show one tooltip at a time
		},
		hide: false,
		style: {
			classes: 'ui-tooltip-wiki ui-tooltip-shadow ui-tooltip-rounded'
		},
		events: {
		      render: function(event, api) {
		         $('#feedback_textarea').focus();
		      }
		},
		api: {
		            onHide: function(){
		               // Pause the vide when hidden
						console.log('hidden player!');
		               var playerAPI = this.elements.content.find('#video-player-flash').get(0);
		               if(playerAPI && playerAPI.pauseVideo) playerAPI.pauseVideo();
		            }
		         }
	});

});
