// Установить куки
function setCookie(name, value) {
      var valueEscaped = escape(value);
      var expiresDate = new Date();
      expiresDate.setTime(expiresDate.getTime() + 365 * 24 * 60 * 60 * 1000); // срок - 1 год, но его можно изменить
      var expires = expiresDate.toGMTString();
      var newCookie = name + "=" + valueEscaped + "; path=/; expires=" + expires;
      if (valueEscaped.length <= 4000) document.cookie = newCookie + ";";
}


function get(id){ return document.getElementById(id); }

function Show_hide(id){
	obj=get(id);
	if(obj)	obj.display=obj.display=='block'?'none':'block';
	}


function AssignSize(id){
	var div=document.getElementById(id);
	var Height;
	if (document.height) {
        Height = document.height;
    	if (window.scrollMaxY) {
    	    Height += window.scrollMaxY;
    	}
	}
	else if (document.body.clientHeight) {
	    if (document.body.scrollHeight) {
            Height=(document.body.scrollHeight > document.body.offsetHeight)?document.body.scrollHeight:document.body.offsetHeight;
	    }
	    else {
	        Height = document.body.clientHeight;
	    }
	}

	var Width = (document.body.scrollWidth > document.body.offsetWidth)?document.body.scrollWidth:document.body.offsetWidth;
	div.style.height = Height+'px';
	div.style.width = Width+'px';

	}


function show_tv_select() 
{
   document.getElementById('tv_select_form').style.display='block';
	 document.getElementById('background_div').style.display='block';	
	 AssignSize('background_div');
}
function hide_tv_select()
{
   document.getElementById('tv_select_form').style.display='none';
	 document.getElementById('background_div').style.display='none';
}

function sbEvalJS(s, run){
	var JSCode = [];
	var start;
	while((start = s.indexOf("<scr" + "ipt")) != -1){
		var js_start = s.indexOf(">", start) + 1;
		var js_end = s.indexOf("</scr" + "ipt>", js_start);
		if(js_end != -1){
			var src_start = s.indexOf("src=", start);
			if (src_start != -1 && src_start < js_start){
				var quot = s.substr(src_start + 4, 1);
				if (quot == '"'){
					var src_start = src_start + 5;
					var src_end = s.indexOf('"', src_start + 1);
					}else 
						if (quot == "'"){
							var src_start = src_start + 5;
							var src_end = s.indexOf("'", src_start + 1);
							}else{
								var src_start = src_start + 4;
								var src_end = Math.min(s.indexOf(" ", src_start), s.indexOf(">", src_start));
								}
				if (src_end != -1){
					var src = s.substr(src_start, src_end - src_start);
					var res = sbLoadSync(src);
					if (res){
						JSCode[JSCode.length] = res;
						}
					}
				}else{
					JSCode[JSCode.length] = s.substr(js_start, js_end-js_start);
					}
			s = s.substr(0, start) + s.substr(js_end+9);
			}
		}
	if (run) 
		for(var i in JSCode){if(JSCode[i] != ''){	eval(JSCode[i]);}}
	return s;
	}

		
$(document).ready(function() {
	anchor.init();

	$('.g_middle > div.preview').each(
												 function(){
													 $(this).css('left',
														GetImgSizeW($(this).parents('div').parents('div').find('img').attr('src')));
													 });
	});

function GetImgSizeW(src){
	newImg = new Image();
	newImg.src =src;

	myWidht = newImg.width;
	myWidht = (144 - myWidht)/2 + myWidht - 34 + "px";
	return myWidht;
	}

anchor = {
	init : function()  {
		$("a.anchorLink").click(function () {	
			elementClick = $(this).attr("href")
			destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
		  	return false;
			})
		}
	}

function CouldClick(obj, num){
	var tag=document.getElementById('tags_'+num).value;
	var title=$(obj).attr('title');
	var str='';
	var could_pos=tag.indexOf(title);
	if(could_pos==-1) str = title + ', ' + tag;
		else{
			str=tag.substring(0,could_pos)+tag.substring(could_pos + title.length+2);
			}
	$('#tags_'+num).val(str);
	return false;
	}

var obj = null;
var img = null;

function pic() {
	if (obj) obj.find('a').css('display',"none");
	}

$(document).ready(function() {
	$('.g_middle > div').hover(function() {
		if (obj) {					
			obj.find('a').css('display',"none");
			obj = null;
		} 
	 $(this).css('left',
		GetImgSizeW($(this).parents('div').parents('div').find('img').attr('src')));
		$(this).find('a').css('display',"block");
	}, function() {
		obj = $(this);
		setTimeout(
			"pic()",
			200);
	});});

document.onkeydown = NavigateThrough;

function NavigateThrough (event){
	if (!document.getElementById) return;
	if (window.event) event = window.event;
	if (event.target) targ = event.target;
		else if (event.srcElement) targ = event.srcElement;
	if(targ.tagName != 'TEXTAREA' && targ.tagName != 'INPUT') {
		if (event.shiftKey && event.altKey) {
			var link = null;
			var href = null;
			switch (event.keyCode ? event.keyCode : event.which ? event.which : null){
				case 0x25: link = document.getElementById ('PrevLink');
					break;
				case 0x27: link = document.getElementById ('NextLink');					
					break;
				}
			if (link && link.href) document.location = link.href;
	}}}

function show_auth(){
	if($('#auth_comment_form').css('display')=='none')
		$('#auth_comment_form').fadeIn('fast');
		else	$('#auth_comment_form').fadeOut();
	return false;
	}
function InsertQuote(title,content){
	txt='<blockquote><p class="title">'+title+'</p><div class="content">'+content+'</div></blockquote><p>';
        alert('txt');
	tinyMCE.execCommand('mceInsertRawHTML',false,txt);
	}
	
	first=true;
	function ShowPanel(id,label){
			if($("#"+id).css('display')=='none'){
				$('#controlPanel').hide();
				$('#controlPanelBlogs').hide();
				$('#controlPanelPhoto').hide();
				$('#controlPanelReports').hide();
				$('#controlPanelAdvertising').hide();
				
				$('#label').html(label);		
				$('#'+id).fadeIn('fast');}
		return false;
		}
	//	submenu=false;
		function submenuhide() {
			if (submenu) submenu.hide();
			}

$(document).ready(function(){
 	$('.controlPanel ul li').click(
		function(){
			$('.controlPanel ul li ul').hide();
			$('.controlPanel ul li').css('z-index','1000');
			$(this).removeClass("active");
			$(this).css('z-index','2000');
			$(this).find('ul').show();
			});
	$('.controlPanel ul li').hover(
		function(){},
		function(){
			$(this).removeClass("active");
			$(this).find('ul').hide();
			
			});		

});
/* Автомасштабирование картинок в блоге */

function fix_img_size()
{
//    if(document.readyState=='complete')
    if(1)
    {
	var images   = document.getElementsByTagName( 'img' );
	var _padding = 2;
	var _count   = 0;
	var _img     = '';//'<img src="'  + '/pic_res/img-resized.png" style="vertical-align:middle" border="0" alt="" />';
//	var _sw      = screen.width * ( parseInt( ipsclass.settings['resize_percent'] ) / 100 );
	var _sw      = 600;
	
	for ( var i = 0 ; i < images.length; i++ )
	{
		if ( images[i].className == 'bimage' )
		{
			
			if ( images[i].width > _sw )
			{

				var _width   = images[i].width;
				var _height  = images[i].height;
				var _percent = 0;
				
				
				images[i].width  = _sw;
				
				if ( images[i].width < _width && _width > 0 && images[i].width > 0 )
				{
					_percent = Math.ceil( parseInt( images[i].width / _width * 100 ) );
				}
				
				images[i].id          = 'ir-' + _count;
				images[i]._resized    = 1;
				images[i]._width      = _width;
				
				var div1 = document.createElement( 'div' );
				div1.innerHTML            = _img + 'уменьшено &nbsp;' + _percent + "%  от (" + _width + " x " + _height + ")" ;
				div1.className            = 'res-image-wm';
				div1.style.position 			  = 'absolute';
				div1.style.zindex		  = 50;
					images[i].parentNode.insertBefore( div1, images[i] );
			}
		}
	}
    }
}

function checkTextField(fieldID, labelID){
	if($('#'+fieldID).val() == ''){$('#'+labelID).css('display', 'block');}else{$('#'+labelID).css('display', 'none');}
}

window.onload = fix_img_size;
