function map_init() {
	var map = new Array();
	var s = "abcdefghijklmnopqrstuvwxyz";
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i)] = s.charAt((i+13)%26);
	for (var i = 0 ; i < s.length ; i++)
		map[s.charAt(i).toUpperCase()] = s.charAt((i+13)%26).toUpperCase();
	return map;
}

function decode_link(a,map) {
	var s = "";
	for (var i = 0 ; i < a.length ; i++) {
		var b = a.charAt(i);
		s += (b>='A' && b<='Z' || b>='a' && b<='z' ? map[b] : b);
	}
	return s.replace('=', '@');
}

$(document).ready(function(){

	//empty links
	$('a[href=#]').click(function(){
		return false;
	});
	
	//banners
	bannum = 1;
	$('#some-banner img:not(:first)').hide();
	var ban_name = $('#some-banner a:visible img').attr('name');
	$('#some-banner').everyTime('15s', function() {
		bannum++;
		if (bannum > 3) bannum = 1;
		$(this).children('a').children('img').hide();
		$(this).siblings('#banner-count').children('a').removeClass('current');
		$(this).children('a').children('img[name=ban_'+bannum+']').fadeIn('slow');
		$(this).siblings('#banner-count').children('a[id=ban_'+bannum+']').addClass('current');
	}); 
	$('#banner-count a#' + ban_name).addClass('current');
	
	$('#banner-count a').click(function() {
		$(this).siblings('a').removeClass('current');
		$(this).addClass('current');
		$('#some-banner img').hide();
		$('#some-banner img[name='+$(this).attr('id')+']').fadeIn('slow');
		bannum = $(this).attr('id');
		//bannum = bannum[4];
		bannum = bannum.substr(4,1);
		//alert (bannum);
		return (false);
	});

    /*
	//tooltip
	function simple_tooltip(target_items, name){
            
		$(target_items).each(function(i){
                        var tt = $(this);
			tt.append("<div class='help'><div class='"+name+"' id='"+name+i+"'><span class='triangle'>&nbsp;</span>"+$(this).attr('title')+"</div></div>");
			var my_tooltip = $("#"+name+i);

			tt.removeAttr("title").mouseover(function(){
                            my_tooltip.css({display:'none', left:'100px', top:'-80px'}).fadeIn();
			}).mouseout(function(){
                            my_tooltip.fadeOut();
			});
		});
	}
	simple_tooltip("a.tt","tooltip");
	*/
	
	//tooltip-get
	function simple_tooltip_get(target_items, name){
		$(target_items).each(function(i){
			$(this).after("<div class='help'><div class='"+name+"' id='"+name+i+"'><span class='triangle'>&nbsp;</span>"+$(this).attr('title')+"</div></div>");
			
			var my_tooltip = $("#"+name+i);

			$(this).removeAttr("title").click(function(){			
				my_tooltip.css({display:'none', left:'-200px', top:'5px'}).fadeIn();
			}).mouseout(function(){
				my_tooltip.fadeOut();
			});
		});
	}
	simple_tooltip_get (".ttt","tooltip-get");
	
	//tooltip-user
	function simple_tooltip_get_2(target_items, name){
		$(target_items).each(function(i){
			$(this).parent('label').next('.text').after("<div class='help floatLeft'><div class='"+name+"' id='"+name+i+"'><span class='triangle'>&nbsp;</span>"+$(this).attr('title')+"</div></div>");
			
			var my_tooltip = $("#"+name+i);

			$(this).removeAttr("title").click(function(){			
				my_tooltip.css({display:'none', left:'20px', top:'0px'}).fadeIn();
			}).mouseout(function(){
				my_tooltip.fadeOut();
			});
		});
	}
	simple_tooltip_get_2 (".tt3","tooltip");
	
	//tooltip-user-2
	function simple_tooltip_get_3(target_items, name){
		$(target_items).each(function(i){
			$(this).after("<div class='help floatLeft'><div class='"+name+"' id='"+name+i+"1'><span class='triangle'>&nbsp;</span>"+$(this).attr('title')+"</div></div>");
			
			var my_tooltip = $("#"+name+i+"1");

			$(this).removeAttr("title").click(function(){
				my_tooltip.css({display:'none', left:'160px', top:'0px'}).fadeIn();
			}).mouseout(function(){
				my_tooltip.fadeOut();
			});
		});
	}
	simple_tooltip_get_3 (".tt4","tooltip");
	
	//small-preview
	$('.big-notice .floatLeft').each(function(){
		$(this).find('img:not(:first)').hide();
	})
	var img_num = $('.big-notice .floatLeft img:visible').attr('name');
	$('.big-notice .floatLeft a#' + img_num).addClass('current');
        
	$('.big-notice .floatLeft').each(function(){
		$(this).find('a.btn').click(function(){
                        btn = $(this)
			btn.siblings('a.btn').removeClass('current');
			btn.addClass('current');
			var img_num = btn.attr('id');
			btn.parent().find('img').hide();
			btn.parent().find('img[name=' + img_num + ']').fadeIn();
		});
	});

        // просмотр фотографий в объявлении (big-preview)
	 $('.notice-one .ps-big:not(:first)').hide();
	 $('.notice-one .ps:last').css({margin:'0px'});
	// $('.notice-one .ps').click(function(){
		// var preview = $(this).attr('name');
		// $('.notice-one .ps-big').hide();
		// $('.notice-one .ps-big[name=' + preview +']').fadeIn();
	// });
	
	//notice-info
	$('.notice-links .floatLeft').click(function(){
		$('.notice-info').slideToggle();
	});
	
	$('.ps-big, .ps').click(function(){
		$('.under-gray').show().css({'width': '100%', 'height': $(document).height(), 'opacity': 0.0});
		
		$('.image-preview').show();
		
		if ($.browser.msie) {
			$('.under-gray').css({'width': $(window).width(), 'height': $(document).height(), 'opacity': 0.0});
			$('select').hide();
		}
		
		$('.very-big:not(:first)').hide();
		$('.very-big:first').show();
		var preview_width = $('.image-preview img.very-big:visible').width(); 
		$('.image-preview').css({width:preview_width});
		
		$('.image-preview').centered(1);

                return false;
	});
	
	$('.image-preview .zoom1 a').click(function(){
		var preview_name = $(this).children('img').attr('name');
		$('.very-big').hide();
		$('.very-big[name=' + preview_name + ']').fadeIn().css({display : 'inline'});
		
		var preview_width = $('.image-preview img.very-big:visible').width();
                preview_width = preview_width < 450 ? '450px' : preview_width;
		// $('.image-preview').css({width:preview_width});
                $('.image-preview').css({width:preview_width});
		$('.image-preview').centered(1);
	});
	
	$('.under-gray').click(function(){
		dark_close();		
	});
	
	$('.dark-close').click(function(){
		dark_close();		
	});	
	
	//auction
	$('#notice-get .checkbox').click(function(){ 
		if (this.checked) $('.auction').show(); else $('.auction').hide();
	});
	
});

if(jQuery) (function(){
	$.extend($.fn, {
		centered: function(hor) {
		
			if ($.browser.safari){
				var win_scroll_top = document.body.scrollTop;
			} else {
				var win_scroll_top = document.documentElement.scrollTop;
			};

			var pos_top = (($(window).height() - 60 - $(this).height()) /2) + win_scroll_top;
			pos_top = Math.floor(pos_top);
			
			if (hor) {
				var pos_left = (($('.under-gray').width() - $(this).width()) /2);
				pos_left = Math.floor(pos_left);
				if ($.browser.msie) {
					var pos_left = (($('html').width() - $(this).width()) /2);
					pos_left = Math.floor(pos_left);
				}
				$(this).css({top: pos_top + "px", left: pos_left + 'px'});
			} else {
				$(this).css({top: pos_top + 'px'});
			}
			return $(this);
		}
	});
	
})(jQuery);

function dark_close(){
	$('.image-preview').hide();
	$('.under-gray').hide();
	
	if ($.browser.msie) {
		$('select').show();
	};
};
/*
$( function() {

        // проверка формы сейчас отключена, у нас нет формы с id=form
        $('#form').submit(function(){

            $('div.error').remove();
            $('.error').removeClass('error');
            var errors = 0;
            var form_class = $(this).attr('class');
            
            $('.required').each(function()
            {
                if (this.value == '')
                {
                    $(this).addClass('error').fadeIn();
                    errors ++;
                }
                else
                {
                    $(this).removeClass('error').attr({title:''});
                }
            });
            
            $('.required').each(function()
            {
                if (this.value == '' || this.value == 0)
                {
                    $(this).attr({title:'Поле должно быть обязательно заполнено.'});
                }
                else
                {

                    if ($('form[class*="user-form"]').length)
                    {
                        if($('#useremail').attr('value').indexOf("@") < 1) {
                                $('#useremail').addClass('error').attr({title:'Введен неверный e-mail'});
                        }

                        if($('#pass_1').val().length > 12 || $('#pass_1').val().length < 4) {
                                $('#pass_1').addClass('error').attr({title:'Пароль должен быть от 4 до 12 символов.'});
                        }
                        if($('#pass_1').attr('value') != $('#pass_2').attr('value')) {
                                $('#pass_2').addClass('error').attr({title:'Пароли не совпадают.'});
                        }
                    }
                    
//                    if ($('form[class*="notice-form"]').length)
//                    {
//                        if(!($('#userphone').attr('value').length) && !($('#useremail-2').attr('value').length)) {
//                                $('#userphone, #useremail-2').addClass('error').attr({title:'Одно из полей "телефон" или "e-mail" должно быть заполнено обязательно.'});
//                        }
//                        if (!($('#userphone').attr('value').length) && $('#useremail-2').attr('value').length && $('#useremail-2').attr('value').indexOf("@") < 1) {
//                                $('#useremail-2').addClass('error').attr({title:'Введен неверный e-mail'});
//                        }
//                    }
                    
                }
            });

            $('.notice-form input.error, .notice-form textarea.error, .user-form input.error').each(function(){
                    $(this).before("<div class='error floatLeft'><div class='tooltip-error left'><span class='triangle'>&nbsp;</span>"+$(this).attr('title')+"</div></div>");
            });

            $('.user-form .tooltip-error:first').css({left:'20px', top:'0px'}).fadeIn();
            $('.notice-form .tooltip-error.left:first').css({left:'-220px', top:'0px'}).fadeIn();

            return false;
	});
	
	$('input, textarea').focus(function(){
		$('.tooltip-error').fadeOut();
		$(this).removeClass('error');
	});
	
});
*/

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/
 
var Base64 = {
 
	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
 
	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = Base64._utf8_encode(input);
 
		while (i < input.length) {
 
			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);
 
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
 
			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}
 
			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
 
		}
 
		return output;
	},
 
	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;
 
		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
 
		while (i < input.length) {
 
			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));
 
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
 
			output = output + String.fromCharCode(chr1);
 
			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}
 
		}
 
		output = Base64._utf8_decode(output);
 
		return output;
 
	},
 
	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	},
 
	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;
 
		while ( i < utftext.length ) {
 
			c = utftext.charCodeAt(i);
 
			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
 
		}
 
		return string;
	}
 
}