$( function() {
	var arabicName = new YabhathTransliterate('txtName');
	arabicName.bindEvents();
	var arabicTitle = new YabhathTransliterate('txtTitle');
	arabicTitle.bindEvents();
	var arabicComment = new YabhathTransliterate('txtBody');
	arabicComment.bindEvents();
	
	function YabhathTransliterate(textElem){
		this.textElement = textElem;
		this.bindEvents = bindEvents;
		this.bindKeyUpEvent = bindKeyUpEvent;
		function bindEvents(){
			bindKeyUpEvent();
		}
		function bindKeyUpEvent(){
			$("#"+textElem).bind('keyup', function(ev){
				var key = getKeyCode(ev);
				if (key == 32) transliterateTextElement(textElem); //space entered
			}); 
		};
		function transliterateTextElement(textElement){
			var currentWord = getWord(textElement);
			currentWord=currentWord.replace(/%/g,"_");
			$.ajax( {
				url : "/yabhath/translateration?sentence="+currentWord+"&"+"ms=" + new Date().getTime(),
				type : 'GET',
				dataType : 'text',
				timeout : 1000,
				error : function() {
					alert('Error loading Comments');
				},
				success : function(text) {
					var splitArray = text.split("^");
					replaceWord(textElement,((splitArray.length>1)?splitArray[1]:splitArray[0]));
				}
			});
		};
		function getWord(textElement){
			cursorPosition = getCursorPos(document.getElementById(textElement));
			strArray = $("#"+textElement).val().substring(0,cursorPosition).split(' ');
			return strArray[strArray.length - 2];
		}
		function replaceWord(textElement,replaceWord){
			var lastIndex = $("#"+textElement).val().substring(0,cursorPosition).lastIndexOf(strArray[strArray.length - 2]);
			$("#"+textElement).val($("#"+textElement).val().substring(0,lastIndex) + $("#"+textElement).val().substring(lastIndex,cursorPosition).replace(strArray[strArray.length - 2],replaceWord));
		}
	};
});

var popupStatus = 0;
var cursorPosition = 0;
var popupType=0;
var strArray = "";
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$( function() {
			$("#backgroundPopup").fadeOut("slow");
			$("#popupContact").fadeOut("slow");
			popupStatus = 0;
		});
	}
}
function showAddComment(popupTypeVal) {
	popupType = popupTypeVal;
	$( function() {
		//centering with css
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popupContact").height();
		var popupWidth = $("#popupContact").width();
		//centering
		var x = self.pageYOffset ? self.pageXOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body ? document.body.scrollLeft : null; 
		var y = self.pageYOffset ? self.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body ? document.body.scrollTop : null;
		$("#popupContact").css({
			"position": "absolute",
			"top": (windowHeight/2-popupHeight/2) + y,
			"left": (windowWidth/2-popupWidth/2)+x
		});
		//only need force for IE6
		
		$("#backgroundPopup").css({
			"height": windowHeight
		});	
		// load popup
		loadPopup();
		setText(popupType);
		return false;
	});
};
function setText(popupType){
	switch (popupType){
		case '0':
			$("div#popupContact > h1").html("Add Comment");
			$("#trSubject").insertAfter("#trEmail");
			$("#trSubject> td > strong").html("Subject");
			$("#btnSend").val("Submit");
			$('<tr id="trType"><td width="1%" height="25"></td><td width="20%" height="25"><font color="#ff6666" size="3"> *</font><strong>Type</strong></td><td height="25"><select id="selectType" style="border-color: Gray; border-width: 1px; border-style: Solid;width: 96%;"><option value="Select">--Select--</option><option value="Feedback">Feedback</option><option value="Question">Question</option><option value="Advertise">Advertise</option></select></td></tr>').remove();
			break;
		case '1':
			$("div#popupContact > h1").html("Contact Us");
			$("#trSubject").insertAfter("#trName");
			$('<tr id="trType"><td width="1%" height="25"></td><td width="20%" height="25"><font color="#ff6666" size="3"> *</font><strong>Type</strong></td><td height="25"><select id="selectType" style="border-color: Gray; border-width: 1px; border-style: Solid;width: 96%;"><option value="Select">--Select--</option><option value="Feedback">Feedback</option><option value="Question">Question</option><option value="Advertise">Advertise</option></select></td></tr>').insertAfter("#tr5");
			$("#trSubject > td > strong").html("Country");
			$("#btnSend").val("Send");
			break;
		case '2':
			$("div#popupContact > h1").html("Feedback");
			$("#trSubject").insertAfter("#trName");
			$("#trSubject> td > strong").html("Country");
			$("#btnSend").val("Send");
			$('<tr id="trType"><td width="1%" height="25"></td><td width="20%" height="25"><font color="#ff6666" size="3"> *</font><strong>Type</strong></td><td height="25"><select id="selectType" style="border-color: Gray; border-width: 1px; border-style: Solid;width: 96%;"><option value="Select">--Select--</option><option value="Feedback">Feedback</option><option value="Question">Question</option><option value="Advertise">Advertise</option></select></td></tr>').remove();
			break;
	}
}
