$(window).load(function(){
		
		localStorage['serviceIp']="www.aadhocc.com/demo";
		localStorage['serviceURLHR']="http://"+localStorage['serviceIp']+"/AdminLTE-master/mookadanadmin/";//"http://goldenmist.in/bill/";
		localStorage['basicPATH']="http://"+localStorage['serviceIp']+"/";
		localStorage['homedelivery']='';
		
	//	setTimeout(translation,1000);

});

var selectors = [];
var translation = function(){
	var allConv = '';
	$("translate").each(function (index){
		text = $(this).text().replace('&', '^');
		allConv += text +"!!" 
		selectors[index] = $(this);
	});
	translateThis(allConv, 'en',"ar",selectors);
};
var translateThis = function(text,from,to,selector){
	fromlang  = to;
    var json_url = 'http://www.aadhocc.com/demo/tansator/translate/translator.php?text='+ text +'&to='+to+'&from='+from;	    
	$.post(json_url, function(data) {	
		result = data.translation;
		result = $(result).text();
		result= result.split("!!");
		for (index = 0; index < selectors.length; ++index) {
			/*trans = result[index].replace('^', '&');*/
			if(result[index].index('^') > 0){
				trans = result[index].replace('^', '&');
			}else{
				trans = result[index];
			}
			selectors[index].text(trans); 
		}//$(".content").show('fade',1000);
	},"json");
	translation_buttons();
}

var selectors_button = [];
var translation_buttons = function(){
	var allConvBtn = '';
	$( "button, input[type='button']" ).each(function (index){
		text_val = 	$(this).val();
		console.log(text_val);
		allConvBtn += text_val +"!!" 
		selectors_button[index] = $(this);
	});
	translateThisButton(allConvBtn, 'en',"ar",selectors_button);
};

var translateThisButton = function(text,from,to,selector){
	fromlang  = to;
    var json_url = 'http://www.aadhocc.com/demo/tansator/translate/translator.php?text='+ text +'&to='+to+'&from='+from;	    
	$.post(json_url, function(data) {	
		result_but = data.translation;
		result_but = $(result_but).text();
		result_but= result_but.split("!!");
		for (index = 0; index < selectors_button.length; ++index) {
			/*trans = result_but[index].replace('^', '&');*/
			trans_but = result_but[index];
			console.log('Translated Text is : '+trans_but);
			selectors_button[index].val(trans_but); 
		}//$(".content").show('fade',1000);
	},"json");
}



if(localStorage['language'] == "" || localStorage.getItem('language') === null){
	localStorage['language']	 ="en";
}


if(localStorage['language'] == "en"){
	$('.navbar-nav').append('<li class="dropdown"><a href="#" class="translate-english"><img src="images/english.png" ></a></li>');
}else{
	$('.navbar-nav').append('<li class="dropdown"><a href="#" class="translate-arabic"><img src="images/arabic.png" ></a></li>');
	setTimeout(function(){ translation(); console.log('trans'); },1500);
}
/*
$('.translate-english').live('click',function(){
	localStorage['language'] = "ar";		
	$(this).attr('class','translate-arabic').find('img').attr('src','images/arabic.png');
	setTimeout(translation,1000);
});
$('.translate-arabic').live('click',function(){
	localStorage['language'] = "en";		
	$(this).attr('class','translate-english').find('img').attr('src','images/arabic.png');
	window.location.reload();
});
*/
$(document).on('click','.translate-english',function(){
	localStorage['language'] = "ar";		
	$(this).attr('class','translate-arabic').find('img').attr('src','images/arabic.png');
	setTimeout(translation,100);
});
$(document).on('click','.translate-arabic',function(){
	localStorage['language'] = "en";		
	$(this).attr('class','translate-english').find('img').attr('src','images/arabic.png');
	window.location.reload();
});



