// Fix Chrome's rendering of the login box
/*
$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
$(function() {
	if($.browser.chrome) {
		$('#login').css("width", "406px");
	}	
})
*/

var flagsVisible = false;
var nameReplaceRegex = /[^\w]/g;
var flags = new Array("af", "al", "aa", "as", "ad", "ao", "ai", "aq", "ag", "ar", "am", "aw", "ac", "au", "at", "az", "bs", "bh", "bd", "bb", "by", "be", "bz", "bj", "bm", "bt", "bo", "ba", "bw", "br", "vg", "bn", "bg", "bf", "bi", "kh", "cm", "ca", "cv", "cs", "cf", "td", "cl", "cn", "co", "cc", "cg", "cd", "ck", "cr", "ci", "hr", "cu", "cy", "cz", "dk", "default", "dj", "dm", "do", "ea", "ec", "eg", "sv", "gq", "er", "ee", "et", "fk", "fo", "fj", "fi", "fr", "default", "gf", "pf", "ga", "gm", "ge", "de", "gh", "gi", "gr", "gl", "gd", "default", "gu", "gt", "gn", "gw", "gy", "ht", "hn", "hk", "hu", "is", "in", "id", "default", "ir", "iq", "ie", "il", "it", "jm", "jp", "jo", "ke", "ki", "kr", "kv", "default", "kw", "kg", "la", "lv", "lb", "ls", "lr", "ly", "li", "lt", "lu", "mo", "mk", "mg", "mw", "my", "mv", "ml", "mt", "mh", "mc", "mr", "mu", "mx", "mq", "md", "mn", "ms", "ma", "mz", "mm", "cq", "na", "nr", "np", "nl", "an", "nd", "nz", "ni", "ne", "ng", "nu", "kp", "no", "om", "pk", "pw", "ps", "ps", "pa", "pg", "py", "pe", "ph", "pl", "pt", "pr", "qa", "re", "ro", "ru", "rw", "sh", "skn", "stl", "sb", "vc", "sm", "st", "sa", "sn", "ser", "sc", "sl", "sg", "sk", "si", "sol", "so", "za", "es", "sri", "sd", "sr", "sz", "se", "ch", "sy", "tw", "tj", "tz", "th", "tg", "tl", "to", "tt", "tn", "tr", "tm", "tc", "tk", "ug", "ua", "ae", "uk", "us", "uy", "vi", "uz", "vu", "va", "ve", "vn", "wf", "wa", "ye", "zm", "zw");
var names = new Array("Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Ascension Island", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia", "Botswana", "Brazil", "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Colombia", "Comoros", "Congo", "Congo Democratic Republic", "Cook Islands", "Costa Rica", "Cote D'Ivoire (Ivory Coast)", "Croatia", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Diego Garcia", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Antilles", "French Guiana", "French Polynesia", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Inmarsat (AOR)", "Iran", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kenya", "Kiribati", "Korea, South / Republic Of", "Kosovo", "Kosovo and Monaco", "Kuwait", "Kyrgyzstan", "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar" ,"Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Island", "Martinique", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "N. Mariana Islands", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue Island", "North Korea", "Norway", "Oman", "Pakistan", "Palau", "Palestine", "Palestinian Authority", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion Islands", "Romania", "Russia", "Rwanda", "Saint Helena", "Saint Kitts & Nevis", "Saint Lucia", "Saint Pierre & Miquelon", "Saint Vincent & Grenadines", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad & Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks & Caicos", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "US Virgin Islands", "Uzbekistan", "Vanuatu", "Vatican City State", "Venezuela", "Vietnam", "Wallis & Fortuna", "Western Samoa", "Yemen", "Zambia", "Zimbabwe");
var id_names = $.map(names, function(s){ return s.replace(nameReplaceRegex,'-');});
var codes = new Array("93", "355", "213", "684", "376", "244", "1264", "672", "1268", "54", "374", "297", "247", "61", "43", "994", "1242", "973", "880", "1246", "375", "32", "501", "229", "1441", "975", "591", "387", "267", "55", "1284", "673", "359", "226", "257", "855", "237", "1", "238", "1345", "236", "235", "56", "86", "57", "269", "242", "243", "682", "506", "225", "385", "53", "357", "420", "45", "246", "253", "1767", "1809", "670", "593", "20", "503", "240", "291", "372", "251", "500", "298", "679", "358", "33", "596", "594", "689", "241", "220", "995", "49", "233", "350", "30", "299", "1473", "590", "1671", "502", "224", "245", "592", "509", "504", "852", "36", "354", "91", "62", "870", "98", "964", "353", "972", "39", "1876", "81", "962", "254", "686", "82", "38649", "377", "965", "996", "856", "371", "961", "266", "231", "218", "423", "370", "352", "853", "389", "261", "265", "60", "960", "223", "356", "692", "596", "222", "230", "52", "691", "373", "976", "1664", "212", "258", "95", "1670", "264", "674", "977", "31", "599", "687", "64", "505", "227", "234", "683", "850", "47", "968", "92", "680", "970", "970", "507", "675", "595", "51", "63", "48", "351", "1787", "974", "262", "40", "7", "250", "290", "1869", "1758", "508", "1784", "378", "239", "966", "221", "381", "248", "232", "65", "421", "386", "677", "252", "27", "34", "94", "249", "597", "268", "46", "41", "963", "886", "992", "255", "66", "228", "690", "676", "1868", "216", "90", "993", "1649", "688", "256", "380", "971", "44", "1", "598", "1340", "998", "678", "379", "58", "84", "681", "685", "967", "260", "263");
var numOfCountries = codes.length;

function init_flag_events() {
	$('#countries-container2').unbind().click(function(e){
		//alert(e.target.id);
		var id = e.target.id;
		if(id == 'country-flag2' || id == 'countries-container2') {
			return false;
		}
		var idxof = id.indexOf('_');
		if (idxof != -1) {
			id = id.substring(0,idxof);
		}
		var idx = $.inArray(id,id_names);
		$("#flag2").css('background-image',("url(images/flags/"+flags[idx]+".png)"));
		// if it doesn't have a +, prepend the country code, otherwise replace
		if($("#number2").val()!='' && $("#number2").val().charAt(0)!='+') {
			$("#number2").val("+"+codes[idx]+$("#number2").val()).focus();
		}
		else {
			$("#number2").val("+"+codes[idx]).focus();
		}
	});

	$("#countries-container2").hide();
	flagsVisible = false;
	
	$("#flag-container2").unbind().hover(function(){
		$(this).attr({
			id: "flag-container2-over"
		});
	}, function(){
		$(this).attr({
			id: "flag-container2"
		});
	}).click(function(){
		$("#countries-container2").toggle();
		flagsVisible = !flagsVisible;
		return false;
	});
	
	// update the flag when they type a number in the #number2field
	$("#number2").unbind().keyup(function(){
		var valueTyped = $(this).val();
		var bSet = false;
		var flag = '';
		
		if(valueTyped.length == 0) {
			// empty string
			bSet = false;
			flag = '';
		}
		else if(valueTyped.substr(0,1) != '+') {
			// US user ommitted country code
			bSet = true;
			flag = 'us';
		}
		else {
			// country code present
			var tstCode;
			var codeIndex = valueTyped.length;
			if (codeIndex > 4) { codeIndex = 4;}
	
			//Check 2,3,4 digit codes
			for (var j=2; j<=codeIndex; j++)
			{
				tstCode = valueTyped.substr(0,j);	
				for(var i=0;i<numOfCountries;i++) {
					if (tstCode == "+"+codes[i]) {
						bSet = true;
						flag = flags[i];
					}
				}
			}
		}
		
		// display the chosen flag next to the number input	
		if (bSet) {
			$("#flag2").css('background-image','url(images/flags/'+flag+'.png)');
		} 
		else {
			$("#flag2").css('background-image','url(images/flags/default.png)');
		}
	}).keyup();
}

$(function() {
	/* Initializing list of countries */
	
	if($("#flag-container2").length==0) {
		$(".flag-container2:first").attr('id','flag-container2');
	}
	
	$("#flag-container2").append("<div id=\"countries-container2\"></div>");
	for(var i=0; i<numOfCountries; i++) {
		var safeName = names[i].replace(nameReplaceRegex, "-");
		$("#countries-container2").append('<a href="#" id="'+safeName+'" class="country-container"><span id="'+safeName+'_name" class="country-name">'+names[i]+'</span><span id="'+safeName+'_code" class="country-code">+'+codes[i]+'</span></a>');
	}
	$("#countries-container2").append("<div class=\"country-flag2\" id=\"country-flag2\"></div>");
	
	$(document).click(function(e) {
		//alert('doc click ' + e.target.id);
		$("#countries-container2").hide();
		flagsVisible = false;
	});
	
	init_flag_events();
	
});