function vtoggle(id, hide) {
	if (!document.getElementById) return false
	if (!hide) hide = false
	var el = document.getElementById(id)
	var vis = el.style.display
	el.style.display = (vis != 'none' || hide) ? 'none' : ''
	el = document.getElementById(id+'arr')
	el.innerHTML = (vis != 'none' || hide) ? '&nbsp;&darr;' : '&nbsp;&uarr;'
	
	return false
}

function show(url, w, h, s, n) {
	if (!n) n = 'vw'
	attr = 'width=' + w + ',height=' + h + ',location=0,menubar=0,resizable=0,scrollbars=' + s + ',status=0,titlebar=0,toolbar=0,hotkeys=0'
	if (parseInt(navigator.appVersion) >= 4) {
		x = (screen.width - w) / 2
		y = (screen.height - h) / 2
		if(x < 0) x = 0
		if(y < 0) y = 0
		attr += ',xposition=' + x + ',left=' + x + ',yposition=' + y + ',top=' + y
	}
	window.open(url, n, attr)
}
function vshow(u) { show(u, 750, 600, 1); return false; }
function rshow(u) { show(u, 400, 250, 0); return false; }

function clickTab(a, otherTab) {
	var span = a.parentNode;
	if (span.className=='active') {
		return true;
	}
	var tab = document.getElementById(otherTab+'Tab');
	span.className = 'active';
	tab.className = '';
	var table = document.getElementById(otherTab+'Table');
	table.style.display = 'none';
	if (otherTab == 'video') {
		table = document.getElementById('photoTable');
	} else {
		table = document.getElementById('videoTable');
	}
	table.style.display = 'block';
	return false;
}


Event.observe(window, 'load', function() {
	setTimeout(load_adverts,100);
} );

var advert = new Array();
load_adverts = function(){
 	if (advert.length) {
 		advert.each(function(s){s(); });
 	}
}

create_iframe = function(div_id,_src,w,h){
	var div = $(div_id);
	if (!div) return;
	var ifr = document.createElement('iframe');
	ifr.frameBorder = 'no';
	ifr.width = w;
	if (h) ifr.height = h;
	ifr.scrolling = 'no';
	ifr.src = _src.toString();
	
    setTimeout(function () { div.appendChild(ifr);  }, 0);
    //div.insert(ifr);
}

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";
	}
}

shufle = function( array ){
	var i = array.length, j, t;
	while( i ){
	  j = Math.floor( ( i-- ) * Math.random() );
	  t = array[i];
	  array[i] = array[j];
	  array[j] = t; 
	}
	return array;
}

insertAppendOtherApplecationsBlock = function(){
	var title = document.title;
	var link = document.location.href;
	
	if( link.indexOf("top") >=0 ){
		link = jQuery(".extra .m3 a" ).eq(2).attr( "href" );
		title = "Взгляд / " + jQuery("#right_td").find("h1").eq(0).text();
	}
	
	var textElement = jQuery(".m3");
	if(document.location.href.indexOf("photoreport") < 0 && document.location.href.indexOf("infographics") < 0
		|| textElement.length == 0 ){
		textElement = jQuery(".text").eq(jQuery(".text").length - 1);
		jQuery("#append-other-applecations").find(".append-other-applecations").css( "margin", "1" );
	}
	textElement.after(jQuery("#append-other-applecations").html());
	jQuery("#append-other-applecations").html( "" );
	createLinksForAppendOtherApplecations( link, title );
	var inPopup = false;
	jQuery(".append-all-app").hover( function( e ){
		inPopup = true;
		jQuery(".app-popup").show();
		return false;
	}, function(){
		inPopup = false;
	    setTimeout(function () { if (!inPopup) jQuery(".app-popup").hide(); }, 1000);
	});
	jQuery(".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);
	});
}

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;
		}
	 
	};
createLinksForAppendOtherApplecations = function( link, title ){
	var escapeLink = escape(link);
	var escapeTitle = Url.encode(title);
	jQuery(".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( shorter( 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 ){
				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 );
		}
	} );
	jQuery(".append .app.big-livejournal").each( function(){
		if( !jQuery.browser.msie ){
			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 );
	} );
	
	jQuery(".append .app.big-twitter").each( function(){
		jQuery(this).attr( "href", "http://twitter.com/home?status=" + escapeTitle + " " + escapeLink );
		return false;
	} );
	
	jQuery(".append .app.big-vkontakte").click( function(){
		VK.Share.click(0);
		return false;
	} );
	jQuery(".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;" );
	});
	jQuery(".app.big-facebook").each( function(){
		jQuery(this).attr( "href", "http://www.facebook.com/share.php?u=" + escapeLink );
		return false;
	} );
	jQuery(".action.print").click( function(){
		location.href = link.replace("html", "print.html");
		return false;
	} );

}
	
shorter = 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("body").ready( function(){
	var td = jQuery("#main table:eq(1) table:eq(1) td:eq(0)");
	td.text("");
	td.prepend("<a href='http://www.vz.ru/information/2010/7/9/417183.html'" +
			"style='display: block; position:relative; right:66px; width: 114px; margin-bottom:6px;'><img src='/images/exchange-news.png'/></a>");
	
});
