//Ищем вхождение подстроки в строке
function strpos( haystack, needle, offset){    // Find position of first occurrence of a string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
 
    var i = haystack.indexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}


//Штука, которяа позволяет нормально конвертить query строка

var trans=[];
var snart=[];
for(var i=0x410;i<=0x44F;i++)
{
    trans[i]=i-0x350;
    snart[i-0x350] = i;
}
trans[0x401]= 0xA8;
trans[0x451]= 0xB8;
snart[0xA8]    = 0x401;
snart[0xB8]    = 0x451;
window.urlencode = function(str)
{
    var ret=[];
    for(var i=0;i<str.length;i++)
    {
        var n=str.charCodeAt(i);
        if(typeof trans[n]!='undefined')
            n = trans[n];
        if (n <= 0xFF)
            ret.push(n);
    }
    
    return window.escape(String.fromCharCode.apply(null,ret));
}
window.urldecode = function(str)
{
    var ret=[];
    str = unescape(str);
    for(var i=0;i<str.length;i++)
    {
        var n=str.charCodeAt(i);
        if(typeof snart[n]!='undefined')
            n = snart[n];
        ret.push(n);
    }
    
    return String.fromCharCode.apply(null,ret);
}



$(document).ready(
  function() { 
  
 
 //Фоторепортаж 
 //Всем картинкам внутри  #thumbnails делаем onClick
 $('#photo_report_page #thumbnails img').each(function() { this.onclick = function() 
            { 
            //alert($(this).attr('src').replace(/_150px/g,"")); 
            img_url = $(this).attr('src').replace(/_150px/g,"");
            img_title = $(this).attr('alt');
            $('#photo').attr('src', img_url);
            if(img_title=='')img_title="Без описания";
            $('#photodesc').html(img_title);
            }});
		
 		/* var script;
		 if( location.href.indexOf(".html") > 0 ) {
			  script = "<script type='text/javascript'>" +
				  "$('#doc').addClass('_ga1_on_');" +
				  "var _ga1_channel='892';" +
				  "</script>" +
				  "<script type='text/javascript' src='http://files.goodadvert.ru/ga_1.js' charset='utf-8'></script>";
		 } else {
			  script = "<script type='text/javascript'>" +
				  		"var _ga3_channel='891';" +
				  		"</script>" +
				  		"<script type='text/javascript' src='http://files.goodadvert.ru/ga_3.js' charset='utf-8'></script>";
		 }
		 $("#copyrights").after(script);*/
 	addBottomBanners();
  }
  
);

var addBottomBanners = function() {
	var createIframe = function(src, width, height) {
		return $('<iframe scrolling="no" frameborder="0" src="' + src + '" width="' + width + '" height="' + height + '"></iframe>');
	}
	var element = $("#main_banner_bottom");
	if(!element.length) {
		element = $("#vz_right_block");
	}
	element.after(createIframe("/adv/bottom-advert.html", 240, 440));
};

/*  ----------    HEALTH BANNER   ----------  */
switch_tabs_realty_banner = function( name ){
//	var tab = document.getElementById( name );
//	var otherTabName;
//	if( name == "realty" ){
//		otherTabName = "health";
//	} else {
//		otherTabName = "realty";
//	}
//	document.getElementById( otherTabName ).style.display = "";
//	tab.style.display = "none";
//	document.getElementById( "type-" + otherTabName ).style.display = "";
//	document.getElementById( "type-" + name ).style.display = "none";
}

switch_tabs_realty_banner_rand = function(){
//	var rand = Math.round( Math.random() * 100000000000 ) ;
//	if( rand % 2 == 0 ){
//		document.getElementById( "realty" ).style.display = "";
//		document.getElementById( "health" ).style.display = "none";
//		document.getElementById( "type-realty" ).style.display = "";
//		document.getElementById( "type-health" ).style.display = "none";
//	} else {
//		document.getElementById( "health" ).style.display = "";
//		document.getElementById( "realty" ).style.display = "none";
//		document.getElementById( "type-health" ).style.display = "";
//		document.getElementById( "type-realty" ).style.display = "none";
//	}
}


var Url = {
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},
	 
	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},
	 
	// 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;
	}
};

var Realty = {
	init: function() {
		Realty.initAppendOtherApplecationsBlock();
	},
	initAppendOtherApplecationsBlock: function() {
		var title = document.title;
		var link = document.location.href;
		var panel = jQuery(".sys_share");
		Realty.createLinksForAppendOtherApplecations(panel, link, title);
		var inPopup = false;
		panel.find(".append-all-app").hover(function(e) {
			inPopup = true;
			panel.find(".app-popup").show();
			return false;
		}, function() {
			inPopup = false;
		    setTimeout(function() {if(!inPopup) panel.find(".app-popup").hide();}, 1000);
		});
		panel.find(".app-popup").hover(function(e) {
			inPopup = true;
			jQuery(this).show();
			return false;
		}, function() {
			inPopup = false;
			var app = jQuery(this);
		    setTimeout(function() {if(!inPopup) app.hide();}, 1000);
		});
		
		panel.show();
	},
	createLinksForAppendOtherApplecations: function(panel, link, title) {
		var escapeLink = escape(link);
		var escapeTitle = Url.encode(title);
		panel.find(".app-popup").find("a").each(function() {
			var app = jQuery(this);
			if(app.hasClass("blogger"))
			{
				app.attr("href", "http://www.blogger.com/blog_this.pyra?t&a=ADD_SERVICE_FLAG&passive=true&alinsu=0&aplinsu=0&alwf=true&hl=ru&skipvpage=true&rm=false&showra=1&fpui=2&naui=8&u=" + escapeLink + "&n=" + escapeTitle);
			}
			else if(app.hasClass("twitter"))
			{
				app.attr("href", "http://twitter.com/home?status=" + escapeTitle + " " + escapeLink);
			}
			else if(app.hasClass("google"))
			{
				escapeTitleGoogle = Url.encode(Realty.curText(jQuery("#right_td").find(".lead").eq(0).text(), 87));
				app.attr("href", "http://www.google.com/buzz/post?message=" + escapeTitleGoogle + "&url=" +  escapeLink);
			}
			else if(app.hasClass("gmail"))
			{
				app.attr("href", "https://mail.google.com/mail/?view=cm&fs=1&to&ui=2&tf=1&shva=1&su=" + escapeTitle + "&body=" + escapeLink);
			}
			else if(app.hasClass("livejournal"))
			{
				if(!jQuery.browser.msie && typeof blogger != "undefined")
				{
					var form = jQuery("<form style='display:none' accept-charset='UTF-8' action='http://www.livejournal.com/update.bml' method='POST'><input type='hidden' name='subject' value='" + title + "'/><textarea name='event'>" + new Template(blogger.options.add_code_tpl).evaluate(blogger.article_data) + "</textarea></form>");
					app.after(form);
					app.click(function() {
						form.submit();
						return false; 
					});
				}
				app.attr("href", "http://www.livejournal.com/update.bml?subject=" + escapeTitle + "&event=" + escapeLink);
			}
			else if(app.hasClass("liveinternet"))
			{
				app.attr("href", "http://www.liveinternet.ru/journal_post.php?action=n_add&cntitle=" + escapeTitle + "&cnurl=" + escapeLink);
			}
			else if(app.hasClass("facebook"))
			{
				app.attr("href", "http://www.facebook.com/share.php?u=" + escapeLink);
			}
			else if(app.hasClass("myspace"))
			{
				app.attr("href", "https://secure.myspace.com/index.cfm?fuseaction=login.simpleform&featureName=postToV3&dest=" + escapeLink);
			}
			else if(app.hasClass("mailru"))
			{
				app.attr("href", "http://connect.mail.ru/share?share_url=" + escapeLink);
			}
		});
		panel.find(".append .app.big-livejournal").each(function() {
			if(!jQuery.browser.msie && typeof blogger != "undefined")
			{
				var form = jQuery("<form style='display:none' accept-charset='UTF-8' action='http://www.livejournal.com/update.bml' method='POST'><input type='hidden' name='subject' value='" + title + "'/><textarea name='event'>" + new Template(blogger.options.add_code_tpl).evaluate(blogger.article_data) + "</textarea></form>");
				jQuery(this).after(form);
				jQuery(this).click(function() {
					form.submit();
					return false;
				});
				return false;
			}
			jQuery(this).attr( "href", "http://www.livejournal.com/update.bml?subject=" + escapeTitle + "&event=" + escapeLink );
		});
		
		panel.find(".append .app.big-twitter").each(function() {
			jQuery(this).attr( "href", "http://twitter.com/home?status=" + escapeTitle + " " + escapeLink );
			return false;
		} );
		
		panel.find(".append .app.big-vkontakte").click(function() {
			VK.Share.click(0);
			return false;
		} );
		panel.find(".append .app.big-vkontakte").hover(function() {
				jQuery(this).css("background", "url(/images/vkontakte_color.gif) no-repeat 0 0;");
			}, function() {
				jQuery(this).css("background", "url(/images/vkontakte_bw.gif) no-repeat 0 0;");
		});
		panel.find(".app.big-facebook").each(function() {
			jQuery(this).attr("href", "http://www.facebook.com/share.php?u=" + escapeLink);
			return false;
		});
		panel.find(".app.big-russiaru").click(function() {
			RussiaRuAPI.click();
			return false;
		});
		panel.find(".action.print").click(function() {
			location.href = link.replace("html", "print.html");
			return false;
		});
	},
	curText: function(str, count) {
		count -= 3;
		var words = str.split(" ");
		var strShort = "";
		var i = 0;
		for(j = 0; j < words.length; j++)
		{
			var word = words[j];
			if(strShort.length + word.length < count)
			{
				strShort += word;
				i++;
				
				if(i < words.length && strShort.length + words[i].length < count)
				{
					strShort += " ";
				}
				else if(i < words.length && word.match("\\W*"))
				{
					strShort += "...";
					break;
				}
				else
				{
					break;
				}
			}
		}
		return strShort;	
	}
};


jQuery(document).ready( function(){
	Realty.init();
});

document.write( "<script src='/js/adriver.core.2.2.js'></script>" );
