var mapImgPath = 'http://chileabroad.gov.cl/wp-content/themes/imag/mapas';
var msie6 = (navigator.userAgent.match(/MSIE 6/) != null) ? true : false;
		
function fixImgAlpha(e){ // para $('img', '#mapa').each() en MSIE6
		var iso = $(e).parents('li').attr('id');
		if ($(e).hasClass('hover')) {
			var thisHover = $(e).attr('src');
			$(e).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+thisHover+')')
		}
		if ($(e).hasClass('inactive')) $(e).css('filter', 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+mapImgPath+'/flags/'+iso+'.png")');
		$(e).attr('src', mapImgPath+'/blank.gif');
		$('#'+iso+'-link').css({
			'background-image': 'none',
			'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+mapImgPath+'/flags/'+iso+'.png", sizingMethod="crop")'
		});
};

function remoteHover(e) {
	var country = e.id;
	if ( country.match('-link') != null ) {
			var country = country.split('-link');
			var country = country[0];
		};
	
	var thisCountry = $('#'+country);
	
	$(e).hover(
		function(){ // e:hover
				thisCountry.css({
					'background-image': 'url('+mapImgPath+'/maps/'+country+'.png)'
				});
				$('img.inactive', '#'+country).css('display', 'none');
				$('img.hover', '#'+country).css('display', 'inline');
		},
		function(){ // e:normal
			thisCountry.css({
				'background-image': 'none'
			});
			$('img.hover', '#'+country).css('display', 'none');
			$('img.inactive', '#'+country).css('display', 'inline');
		}
	);
};

function mapHoverIE6(e) {
	var country = e.id;
	if ( country.match('-link') != null ) {
			var country = country.split('-link');
			var country = country[0];
	};
	
	var thisCountry = $('#'+country+' a');
	var thisHover = $('img.hover', '#'+country).attr('src');
	
	$(e).hover(
		function(){ // e:hover
				thisCountry.css({
					'background-image': 'none',
					'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+mapImgPath+'/maps/'+country+'.png", sizing="crop")'
				});
				$('img.inactive', '#'+country).css('display', 'none');
				$('img.hover', '#'+country).css({
					'display': 'inline',
					'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+thisHover+'", sizing="crop")'
				}).attr('src', mapImgPath+'/blank.gif');
		},
		function(){ // e:normal
			thisCountry.css({
				'background-image': 'none',
				'filter': 'none'
			});
			$('img.hover', '#'+country).css('display', 'none');
			$('img.inactive', '#'+country).attr('src', mapImgPath+'/blank.gif').css({
				'display': 'inline',
				'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+mapImgPath+'/flags/'+country+'.png", sizing="crop")'
			});
			}
	);
};

function countryInfo(e){
	var iso = e.id;
	if ( iso.match('-link') != null ) {
			var iso = iso.split('-link');
			var iso = iso[0];	
	};
	if ( $('#'+iso+'-info').hasClass('show') ) return false; else {
		$('div.cont>p, div.cont>h2, #selectCountry', '#mapBox').slideUp();
		$('div.detInfo.show', '#mapBox').slideUp().removeClass('show');
		$('#'+iso+'-info').addClass('show').show();	
		$('a', '#selectCountry').each( function(e){ remoteHover(this) } );
		$('a', '#mapa').each( function(e){ remoteHover(this) } );
	}
};

function changeTab(e){
	if ( $(e).parents('li').hasClass('acti') ) return false;
	else {
		var id_box = $(e).parents('div').attr("id");
		$('.selectedOffice li.officeTab, .selectOffice li','#'+id_box).toggleClass('acti');
		return false;
	}
};

function hideCountry(e){
	$(e).parents('div.detInfo').hide();
	$('div.cont>p, div.cont>h2, #selectCountry', '#mapBox').slideDown();
};

function changeMap(e){
	var newMap = $(e).attr('href').split('?');
		newMap = newMap[1];
	$("#TB_ajaxContent").append('<div id="loading_thickbox">Loading...</div>');
	$.get('/', newMap, function(data){
		$("#loading_thickbox").remove();
		$('#mapBox').remove();
		$('#TB_ajaxContent').append(data);
			$('#selectCountry a, #mapa li', '#mapBox').click( function(e) { countryInfo(this) } );
			$('.officeTab a', '#selCouWrap').click( function(e) { changeTab(this) });
			$('div.back a', '#selCouWrap').click( function(e){ hideCountry(this) } );
			$('#contSelector a').click( function(e){ changeMap(this); return false; });
			if (msie6) {
				$('img.inactive', '#mapa').each( function(e){ fixImgAlpha(this) } );
				$('a', '#selectCountry').each( function(e){ mapHoverIE6(this) } );
				$('li', '#mapa').each( function(e){ mapHoverIE6(this) } );
			} else {
				$('a', '#selectCountry').each( function(e){ remoteHover(this) } );
				$('li', '#mapa').each( function(e){ remoteHover(this) } );								
			}
		return false;
		});
};

jQuery.extend({
	id : function(id) {
		var id = id.split("#");
		return document.getElementById(id[1])?true:false;
	}
});

