$(document).ready(function() {

	$('#langBox').hide();
	$('#progressbar').hide();
	var pageTitle = document.title;
	var firstRun = true;
	
	$.translate(function(){  //when the Google Language API is loaded

		function translateTo( destLang ){ //this can be declared in the global scope too if you need it somewhere else
			var aSyn = false;
			if (window.location.href.toLowerCase().indexOf('flighttracker') > 0) {
				aSyn = true;
			}
			$('body').translate( 'english', destLang, {   //translate from english to the selected language
				not: '.jq-translate-ui, #langBox',  //by default the generated element has this className
				toggle:true,
				async:aSyn,
				each: function(){
					if (firstRun) {
						transEle(destLang, '#progBarText');
					}
					this.progress("#progressbar"); },
				start: function(){ 
					$("#progressbar").progressbar( "option", "value", "0" );
					$('#progressbar').show();
					transEle(destLang, '#progBarText');
					transPageTitle( destLang );
					transTitles( destLang );
				},
				complete: function(){
					$('#progressbar').hide();
					firstRun = false;
				}
            });
		}
		
		function transPageTitle( destLang ) {
			if (destLang != 'english') {
				$.translate(pageTitle, 'english', destLang, {
					complete: function(translation){ document.title=translation; }
				});
			} else {
				document.title = pageTitle;
			}
		}
		
		function transTitles( destLang ) {
			$('[title != ""]').each(function(i) {
				var $with_title = $(this);
				if ($(this).attr("enTitle") == undefined)
				{
					$with_title.attr("enTitle",$(this).attr("title"))
				}
				var oriText = $(this).attr("enTitle");
				if (destLang == 'english') {
					$with_title.attr("title",oriText);
				} else {
				var trans = $.translate(oriText, 'english', destLang, {
					complete: function(translation){ $with_title.attr("title",translation); }
				});
				}
			});
		}
			
		function tableUI(a, b, c){
			var str='', cs='', cl='', inc=1, img='', propLang='', bstart=1;
			if(c){ cs='<'+c+'><span class="langspan">'; cl='</span></'+c+'>'; }
			$.each( $.translate.getLanguages(true), function(l, lc){
				//make english first
				if (bstart == 1) {
					bstart=0;
					propLang="English";
					str+=('<'+b+'>');
					img = flagImage(propLang);
					str+=(cs+img+propLang+cl);
					inc+=1;
				}
				propLang = l.charAt(0)+l.substring(1).toLowerCase();
				//filter English and unknown
				if ((propLang != "Unknown") && (propLang != "English")) {
					if (inc == 1) {
						str+=('<'+b+'>');
					}
					img = flagImage(propLang);
					str+=(cs+img+propLang+cl);
					if (inc == 4) {
						str+=('</'+b+'>');
						inc=0;
					}
					inc+=1;
				}
			});
			return $('<'+a+' class="jq-translate-ui">'+str+'</'+a+'><div id="GoogTransBrand"></div>');
		}
		
		function flagImage(langName) {
			return '<img class="flagimg" src="'+flagPath(langName)+'" alt="'+langName+'" width="16" height="11" onerror="FlagError(this)" />';
		}
		tableUI('table', 'tr', 'td')
		.appendTo('#langTable')    //insert the element to the page
		.find('span')
		.css('cursor','pointer')
		.click(function(){   //when selecting another language

			$.fn.colorbox.close();
			translateTo( $(this).text() );

			$.cookie('destLang', $(this).text().toLowerCase(), { path: '/' } );
			// set a cookie to remember the selected language
			return false; //prevent default browser action
		})
		
		if ("http:" == document.location.protocol) {
	    	google.language.getBranding('GoogTransBrand');
		}
		
		var destLang = $.cookie('destLang') || 'english'; //get previously translated language
		
		if ( destLang != 'english' ) {  //if it was set then
			translateTo( destLang );
		}

	})
	
	$('#transLink').click(function() {
		return false; //prevent default browser action
	})

	function transEle ( destLang, ele ) {
		$(ele).translate( 'english', destLang, {   //translate from english to the selected language
		toggle:false,
		async:false
		});
	}

	function transCBTitle( destLang ) {
		if (destLang != 'english') {
			var $with_title = $('#cboxTitle');
			var trans = $.translate('Language Translation Options', 'english', destLang, {
			complete: function(translation){ $with_title.html(translation); }
			});
		}
	}

	$('#transLink').colorbox({transition:"none", width:"790px", height:"510px", title:"Language Translation Options", inline:true, href:"#langBox"},
	function(){
		$('#transLink').bind('cbox_cleanup', function(){
			$('#langBox').hide();
			$('#cboxTitle').removeClass('transTitle');
		});
		$('#cboxTitle').removeClass().addClass('transTitle');
		$('#langBox').show();
		var destLang = $.cookie('destLang') || 'english'; //get previously translated language

		transCBTitle(destLang);
		transEle(destLang,'#transHeadText');
	});
	
	function flagPath(fName) {
		var siteUrl = "";
		var imgpath;
		
		//if global variable containing URL to site exists, assign it's value to this local variable
		if (window.translateFlagsURL != undefined && window.translateFlagsURL != ""){
			siteUrl = translateFlagsURL;
		}
		
		if (fName.indexOf('_') > 0) {
			imgpath = siteUrl + '/flags/' + fName.toLowerCase().substring(0,fName.indexOf('_')) + '.gif';
		} else {
			imgpath = siteUrl + '/flags/' + fName.toLowerCase() + '.gif';
		}
		return imgpath;
	}

	function setPageFlag(fName,eleName) {
		var imgpath=flagPath(fName);
		var flagImg=document.getElementById(eleName);
		flagImg.src=imgpath;
	}
	
})

function FlagError(ele) {
	var siteUrl = "";
	
	if (window.translateFlagsURL != undefined && window.translateFlagsURL != ""){
		siteUrl = translateFlagsURL;
	}
	
	ele.src = siteUrl + "/flags/noflag.gif";
}