//---------------------------------------------------------------------------------------------------------------------------
//document.onselectstart = new Function ("return false");
//document.oncontextmenu = new Function("return false");
//---------------------------------------------------------------------------------------------------------------------------
function SetCookie1(jName, jValue, jPath, jDomain, jSecure, jDayExpired){
	var jDate = new Date();
	jDate.setTime(jDate.getTime() + jDayExpired * 24 * 60 * 60 * 1);
	document.cookie = jName + "=" + escape(jValue) + ";expires=" + jDate.toGMTString() + ";path=" + jPath;
}	
//---------------------------------------------------------------------------------------------------------------------------
// функции визуального редактора

var jFieldEdit = "";
var jPageEdit = "";

function OpenWindow(jUrl, jNameWin, jHeight, jWidth, jStringView){

	jLeftPosition = (screen.width) ? (screen.width-jWidth)/2 : 0;
	jTopPosition = (screen.height) ? (screen.height-jHeight)/2 : 0;
	
	aStringView = jStringView.split(";");
	window.open(jUrl, jNameWin, 'scrollbars='+aStringView[0]+', status='+aStringView[1]+',resizable='+aStringView[2]+',titlebar='+aStringView[3]+',menubar='+aStringView[4]+',width=' + jWidth + ',height=' + jHeight + ',top=' + jTopPosition + ',left=' + jLeftPosition);
}

function onEditField(jField){
	jPageEdit = false;
	jFieldEdit = ((document.getElementById(jField.id + "Name")) ? document.getElementById(jField.id + "Name").innerHTML : false);
	OpenWindow('/visual_edit/index.php', jField.name, 470, 700, '0;0;1;0;0');
}

function onEditPage(jBody){
	jFieldEdit = false;
	jPageEdit = ((document.getElementById(jBody + "Name")) ? document.getElementById(jBody + "Name").innerHTML : false);
	OpenWindow('/visual_edit/index.php', jBody, 470, 700, '0;0;1;0;0');
}
//---------------------------------------------------------------------------------------------------------------------------

upath = "/";
udomian = "";
var expdate = new Date();
expdate.setTime(expdate.getTime() + 365 * 24 * 60 * 60 * 1);

function OpenCloseLeftMenu(jLevelMenu){
	if(document.getElementById('menu' + jLevelMenu + "_Left")){
		var Obj = document.getElementById('menu' + jLevelMenu + "_Left");
		if(Obj && Obj.style.display == 'none'){
			Obj.style.display = 'block';
			SetCookie('menu' + jLevelMenu + "_Left", "1", expdate, upath, udomian, 0); 
		}else{
			Obj.style.display = 'none';
			SetCookie('menu' + jLevelMenu + "_Left", "0", expdate, upath, udomian, 0); 
		}
	}
	return false;
}

function SetCookie (name, value, expires, path, domain, secure){
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	document.cookie = name + "=" + escape(value) +";expires="+expires.toGMTString()+";path="+path;
}

function AddBasket(jID, jForm, jThis){
	if(jID){
		jForm.fCount.value = parseInt(jForm.fCount.value);
		document.getElementById(jID).style.color = 'red';
		document.getElementById(jID).style.fontWeight = 'bold';
		jThis.style.display = 'none';
		document.getElementById('jDisplayMain').style.display = 'none';
		document.getElementById('jDisplayBasket').style.display = 'block';
		alert('Товар добавлен в корзину.');
	}
}

function ShowImagePopup2(img,alt) {
	
	// nothing.
	
	return false;
}

var blank_pic = '/pics/null.gif';

$(document).ready(function(){
	
	
		$('.smallTeaserR a, .smallTeaser a, .teaserLeft a, .teaserRight a').click(function(){
			
			showPic(this);
			return false;
		});
		
});

function showPic(pic) {
	
		if ($('#shadow').length==0) {
			
			appendShadow();
			
		}
		
		
		
		if($.browser.msie && $.browser.version == '6.0' && $('.spawn').length == 0) {
			$('body').prepend('<img onfocus="hideBox()" class="spawn" src="'+blank_pic+'" />');
			$('body').scrollTop(0);
			$('body').addClass('selectHidden');
		}
		
		var height = Math.max($(document).height(), $('body').height()) + 38;
			
		$('#shadow').css('min-height', height+'px').show();
		$('#shadow').css('height', height+'px').show();
		
		$('#shadow .pic').html('<img src="'+blank_pic+'" width="200" height="200" alt="" />');

		var mtop = -$('#shadow .pic img').attr('height')/2;

		$('#shadow .flyer .flyerTable').css('margin-top',mtop+'px');	
		
		$('#shadow .flyer').show();
		
		var img = new Image();
		
		$(img).load(function(){
			var src = this.src;
			var w = this.width;
			var h = this.height;
			
			// Check window sizes...
			var height2 = Math.min($(document).height(), $('body').height()) - 30;
			var width2 = $('body').width()-60;
			
			var picWidth = w;
			var blockWidth = w + 20;
			var picHeight = h;
			var blockHeight = h + 45;
			
			//alert(blockHeight);
			
			var indentWidth = blockWidth - picWidth;
			var indentHeight = blockHeight - picHeight;
			
			// ADJUST!
			if (blockHeight>height2 || blockWidth>width2) {
					// by height
				   newHeight1 = height2 - indentHeight;
				   newWidth1 = newHeight1*picWidth/picHeight;
				///}
				
				//if (blockWidth>width2) {
					// by width
					newWidth2 = width2 - indentWidth;
				  	newHeight2 = newWidth2*picHeight/picWidth;
				
				
				if (newWidth1>newWidth2) {
					w = newWidth2;
					h = newHeight2;
				} else {
					w = newWidth1;
					h = newHeight1;
				}
			}
			// size check finish;

			var mtop = parseInt($('#shadow .flyer .flyerTable').css('margin-top'));
			//alert(mtop);
			mtop = - (h/2 + 25);
			//alert(mtop);
			$('#shadow .flyer .flyerTable').animate({marginTop:mtop},200,'linear');
			
			$('#shadow .flyer .flyerTable td.content').animate(
				{height:h, width:w},200,'linear',function(){
				
					$('#shadow .pic img').attr('width',w).attr('height',h).attr('src',src);
					$('#shadow .close').show();
		
				
			}).attr('title',$('img',$(pic)).attr('alt'));			
			
			//$(this).appendTo($('#shadow .pic'));
			
			
			
			
		}).attr('src',$(pic).attr('href'));
		
		//adjustFlyerSize();
		
		
		
		$(document).bind('keyup',escCheck);
	
		//document.documentElement.scrollTop = 170;
		//document.scrollTop = 170;
		
		//alert('balls');
		return false;
		
	
}

function escCheck(evt) {
	
	if (evt.keyCode==27) 
		hideBox();
	 
}

function hideBox() {
	
	$('#shadow').remove();	
	
	if($.browser.msie && $.browser.version == '6.0') {
		$('body .spawn').remove();
		$('body').removeClass('selectHidden');
	}
	/*
	if (savedScroll!=undefined) {
		document.documentElement.scrollTop = savedScroll;
	}*/
	
}


function appendShadow() {
	
	$('body').append('<div id="shadow"><div class="flyer"><table class="flyerTable"><tr><td class="content"><div class="pic"></div><div class="close" title="Закрыть" onclick="hideBox()"><!--//--></div></td></tr></table></div></div>');
	
}
