(function($){
	$.fn.extend({
		imgPoints: function(options){
        	var defaults = {
        			dest_id: '#point-map',
        			photo_cont: '#point-img',
        			descr_cont: '#point-info',
        			point_sum: '#point-sum'
        		};
             var opts = $.extend(defaults, options);
             var point_dest = $(opts.dest_id).parent().css({position:'relative'});
             var photo_cont = $(opts.photo_cont);
             var descr_cont = $(opts.descr_cont);
             var point_sum = $(opts.point_sum);
             var i = 0;
             return this.each(function(){
            	i = i + 1;
				var self = $(this);
				self.css({textDecoration: 'none'});
				self.attr('id', 'link_'+i);
				var data = eval("("+self.attr('data')+")");				
				var point = $(document.createElement("img"));
				if (i != 1){
					point.attr({src:'/i/city_pass.gif', id:'point_'+i}).addClass('point').css({position: 'absolute', left: data.city_x+'px', top: data.city_y+'px', width: '3px', height: '3px'});
				}else{
					point.attr({src:'/i/city_act.gif', id:'point_'+i}).addClass('point').css({position: 'absolute', left: data.city_x+'px', top: data.city_y+'px', width: '5px', height: '5px'});
				}
				point_dest.append(point);
				$(".city-list a:first").addClass('active');
				var dat = eval("("+$(".city-list a:first").attr('data')+")");
				var photo_img = $(document.createElement("img"));
				photo_img.attr('src', dat.city_image).css({width: '129px', height:'auto'});					
				photo_cont.append(photo_img);
				descr_cont.html(base64_decode(dat.city_descr));
				
				self.mouseover(function(){
					var id = $(this).attr('id').substr(5);
					$(".city-list a").removeClass('active');
					$(this).addClass('active');
					$('.point', point_dest).each(function(){
						$(this).attr('src', '/i/city_pass.gif').css({width: '3px', height: '3px'});
					});

					point_sum.animate({opacity: 0}, 300, function(){
						photo_cont.html('');
						descr_cont.html('');
						$(this).animate({opacity: 1}, 300);
					});								
					
					$("#point_"+id).attr('src', '/i/city_act.gif').css({width: '5px', height: '5px'});
					
					point_sum.animate({opacity: 0}, 300, function(){
						if (data.city_image.length > 0){
							var photo_img = $(document.createElement("img"));
							photo_img.attr('src', data.city_image).css({width: '129px', height:'auto'});					
							photo_cont.append(photo_img);
						}
						descr_cont.html(base64_decode(data.city_descr));
						$(this).animate({opacity: 1}, 300);
					});

				})
				.click(function (){
					//return false;
				});
				
             });
		}
	});
})(jQuery);

function base64_decode (data) {
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, ac = 0, dec = "", tmp_arr = [];

    if (!data) {
        return data;
    }

    data += '';

    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));

        bits = h1<<18 | h2<<12 | h3<<6 | h4;

        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;

        if (h3 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1);
        } else if (h4 == 64) {
            tmp_arr[ac++] = String.fromCharCode(o1, o2);
        } else {
            tmp_arr[ac++] = String.fromCharCode(o1, o2, o3);
        }
    } while (i < data.length);

    dec = tmp_arr.join('');
    dec = utf8_decode(dec);

    return dec;
}
function utf8_decode ( str_data ) {
     var tmp_arr = [], i = 0, ac = 0, c1 = 0, c2 = 0, c3 = 0;
    
    str_data += '';
    
    while ( i < str_data.length ) {
        c1 = str_data.charCodeAt(i);
        if (c1 < 128) {
            tmp_arr[ac++] = String.fromCharCode(c1);
            i++;
        } else if ((c1 > 191) && (c1 < 224)) {
            c2 = str_data.charCodeAt(i+1);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
            i += 2;
        } else {
            c2 = str_data.charCodeAt(i+1);
            c3 = str_data.charCodeAt(i+2);
            tmp_arr[ac++] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }

    return tmp_arr.join('');
}

