jQuery(function() {
	jQuery('.mod_callRates .ressourceInfo').hide();
	swfobject.embedSWF(modCallRatesVars.swfPath, "worldMapContainer",
			modCallRatesVars.flashWidth, modCallRatesVars.flashHeight, "9.0.0",
			"expressInstall.swf", {
				options : FWM_options
			}, {
				bgcolor : "#FFFFFF",
				wmode : "transparent"
			});

	jQuery('.worldMapDetailsbuttons input.loadDetails').click(function() {
		jQuery(this).hide();
		var cc = jQuery('div.mod_callRates #worldMapCountries').val();
		getPricesDetails(cc);
	});
	jQuery('.worldMapDetailsbuttons input.closeDetails').click(function() {
		jQuery('.worldMapCountryDetails').fadeOut('fast');
		jQuery(this).hide();
		jQuery('.worldMapDetailsbuttons input.loadDetails').show();
	});
});

var callRatesLastSearch = '';
var latestDetails = '';
var positions = {
	EU : {
		x : 420,
		y : 600,
		z : 9
	},
	NA : {
		x : 1100,
		y : 400,
		z : 4
	},
	SA : {
		x : 1120,
		y : -120,
		z : 6
	},
	AS : {
		x : -100,
		y : 240,
		z : 6
	},
	AF : {
		x : 420,
		y : 60,
		z : 6
	},
	OC : {
		x : -400,
		y : -60,
		z : 6
	},
	0 : {
		x : 480,
		y : 280,
		z : 1
	},
	AN : {
		x : 480,
		y : 280,
		z : 1
	}
};

var FWM_options = {
	draggableMap : false,
	oceanColor : '0xFFFFFF',
	borderColor : '0xFFFFFF',
	selectedColor : '0xB3CDEF',
	showInfoBox : true,
	countryScale : 1.0,
	zoomMax : 20,
	displayList : false,
	statusDefault : {
		lbl : 'Unknown',
		clr : '0xCCCCCC'
	}
};

var FWM_statuses = [ {
	code : 0,
	lbl : 'No Information',
	clr : '0xFFFFFF',
	desc : 'There is no information for this country'
}, {
	code : 1,
	lbl : '',
	clr : '0x99B7DF',
	desc : ''
} ];

function modCallRatesInitialize() {
	jQuery('.worldMapInfo').show();
	jQuery('div.mod_callRates .phoneNumbersCountriesList').hide();
	jQuery('div.mod_callRates #worldMapContinents').change(
			function() {
				var continent = jQuery(this).val();
				document.getElementById('worldMapContainer').setPosition(
						positions[continent]);
			});
	jQuery('div.mod_callRates #worldMapCountries')
			.change(
					function() {
						jQuery('.worldMapDetailsbuttons input.closeDetails')
								.click();
						var country = jQuery(this).val().toUpperCase();
						var htmlCountry = jQuery('div.mod_callRates .country.' + country);
						var continent = htmlCountry.parent().find('.continent')
								.val();
						jQuery('div.mod_callRates #worldMapContinents').val(
								continent).change();
						displayCountryInfo(country);
						document.getElementById('worldMapContainer')
								.setCurrentCountry(country);
					});
}

function displayCountryInfo(country) {
	var htmlCountry = jQuery('div.mod_callRates .country.' + country);
	var continent = htmlCountry.parent().find('.continent').val();
	jQuery('div.mod_callRates #worldMapContinents').val(continent).change();
	jQuery('.worldMapCountryInfo').html(htmlCountry.html());
	jQuery('.worldMapDetailsbuttons .closeDetails').hide();
	jQuery('.worldMapDetailsbuttons .loadDetails').show();
}

function getPricesDetails(cc) {
	var detailsBloc = jQuery('#callRatesInfo .country.' + cc + ' .details');
	var infoBloc = jQuery('.worldMapCountryInfo');
	infoBloc.find('input.loadDetails').hide();
	infoBloc.find('.details').text(modCallRatesVars.texts.loading).addClass(
			'loading');
	var data = {
		cr_action : 'details',
		cr_cc : cc
	};
	jQuery.post(modCallRatesVars.currentUrl, data, function(html) {
		jQuery('.worldMapCountryInfo .details').removeClass('loading');
		infoBloc.find('.details').empty();
		jQuery('.worldMapCountryDetails').html(html).fadeIn('fast');
		jQuery('.worldMapDetailsbuttons .closeDetails').show();
		jQuery('.worldMapDetailsbuttons .loadDetails').hide();
	});
}

/* CALLBACKS FROM FLASH */

function FWM_ready() {
	modCallRatesInitialize();
	document.getElementById('worldMapContainer').setOptions(FWM_options);
	document.getElementById('worldMapContainer').setStatuses(FWM_statuses);
	document.getElementById('worldMapContainer').setCountryList(
			modCallRatesVars.countryList);
	document.getElementById('worldMapContainer').displayZoom(false);
	var continent = jQuery('div.mod_callRates #worldMapContinents').val();
	document.getElementById('worldMapContainer').setPosition(
			positions[continent]);
	document.getElementById('worldMapContainer').displayCountries();
}

function FWM_unknownCountry(obj) {
	// console.log(obj);
}

function FWM_clickOnCountry(obj) {
	if (obj == null)
		jQuery('#worldMapCountries').val('');
	else {
		var country = obj.code.toUpperCase();
		jQuery('#worldMapCountries').val(country).focus();
		displayCountryInfo(country);
	}
}

function FWM_log(message) {
	alert(message);
}

function FWM_callback(name, obj) {
	alert(name);
	switch (name) {
	case 'setCountryList':
		document.getElementById('worldMapContainer').displayCountries();
		break;
	}
}