var carouselTimeout=7000;
var carouselItemWidth=900;
var carouselItemSWidth=100;
var carouselAutoMoveEnabled=true;

$(document).ready(function(){
	// autoslide
	if(!($.browser.msie & ($.browser.version.substr(0,1)<7))){
		window.setTimeout("rotateCarousel();", carouselTimeout);
	}
	// suggest variants near fearch input
	$("input[name='what']").keyup(function(){suggest(this);});
	// check bodies illustr bigsize vari
	$(".bodies img").each(function(){checkBigVari(this)});

	var k=false;
	$(".colorised tr").each(function(i){
		$(".colorised tr:eq("+i+") td").each(function(){
			if(k){
				$(this).addClass('tac');
			}else{k=true;}
		});
		k=false;
	});
	$(".colorised tr:even").each(function(i){$(this).addClass('even');});
});

function suggest(obj){
	var value=$(obj).attr('value');
	$.post("../ajax/search_suggest.php", {'value':value}, function(data){
		$("#suggestBlock").remove();
		$(obj).before(data);
	});
}

function removeSSugg(){$("#suggestBlock").remove();}

function checkBigVari(obj){
	var src=$(obj).attr('src');
	$.post("../ajax/check_big_vari.php", {"src":src}, function(data){
		if(data){
			$(obj).css('cursor', 'pointer');
			$(obj).attr('onClick', 'preview(this);');
		}
	});
}

function preview(obj){
	var src=$(obj).attr('src');
	$.post("../ajax/images_preview.php", {"src":src},
	function(data){
		showMask('images_preview_view.php?src='+data.src, data.width, data.height);
  	}, 'json');
}

function moveCarousel(toTheRight, multiplier){
	var itemWidth=carouselItemWidth*multiplier;
	var carLeft=$(".carousel").css('left');
	var carWidth=$(".carousel").css('width');
	if(carLeft){carLeft=Math.round(carLeft.replace('px', ''));}else{carLeft=0;}
	carWidth=Math.round(carWidth.replace('px', ''));
	var precPos=carLeft/carouselItemWidth;
	if(precPos==Math.round(precPos)){
		if(toTheRight){
			if(carLeft>(0-carWidth+carouselItemWidth)){carLeft-=carouselItemWidth;}
		}else{
			if(carLeft<0){carLeft+=carouselItemWidth;}
		}
		$(".carousel").animate({left: carLeft}, 700, function(){changeKnowMoreUrl();});
		var carISsLeft = Math.round(carLeft/9);
		$("#prevnext #prev .stage").animate({left: (carISsLeft+carouselItemSWidth)}, 700);
		$("#prevnext #next .stage").animate({left: (carISsLeft-carouselItemSWidth)}, 700);
	}
}

function changeKnowMoreUrl(){
	var aidArr=Array(8, 9, 7);
	var carLeft=$(".carousel").css('left');
	if(carLeft){carLeft=Math.round(carLeft.replace('px', ''));}else{carLeft=0;}
	var index= Math.abs(carLeft/carouselItemWidth);
	//$("#knowMoreLnk a").attr('href', '/functions/'+aidArr[index]+'.html');
	$("#knowMoreLnk a").attr('href', '/article/20.html');
}

function rotateCarousel(){
	if(carouselAutoMoveEnabled){
		var carLeft=$(".carousel").css('left');
		var carWidth=$(".carousel").css('width');
		if(carLeft){carLeft=Math.round(carLeft.replace('px', ''));}else{carLeft=0;}
		carWidth=Math.round(carWidth.replace('px', ''));
		var precPos=carLeft/carouselItemWidth;
		if(precPos==Math.round(precPos)){
			if(carLeft==0){direction=1;}
			if(carLeft==(0-(carouselItemWidth*2))){direction=0;}
			moveCarousel(direction, 1);
		}
		window.setTimeout("rotateCarousel();",carouselTimeout);
	}
}


function getClientWDims1(){
	var cd = new Array();
	cd['w'] = window.documentElement.clientWidth;
	cd['h'] = window.documentElement.clientHeight;
return cd;
}

function getClientWDims() {
  var cd = new Array();
	var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
	cd['w'] =myWidth;
	cd['h'] =myHeight;
return cd;
}

function getClientDims(){
	var cd = new Array();
	cd['w'] = document.documentElement.clientWidth;
	cd['h'] = document.documentElement.clientHeight;
return cd;
}


function mousePageXY(e){
      var x = 0, y = 0;
      if (!e) e = window.event;
      if (e.pageX || e.pageY){
        x = e.pageX;
        y = e.pageY;
      }else if (e.clientX || e.clientY){
        x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
        y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
      }
return {"x":x, "y":y};
}

function fixPNG(element){
  if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent)){
    var src;
	if (element.tagName=='IMG'){
	  if (/\.png$/.test(element.src)){
        src = element.src;
        element.src = "../pict/empty.gif";
      }
    }else{
      src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
      if(src){
        src = src[1];
        element.runtimeStyle.backgroundImage="none";
      }
    }
    if(src)element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
  }
}

function showWait(){return '<span class="wait">Секундочку...</span>';}


function setMaskHeight(){
	if($('#mask').css('display')=='block'){
		var cd = getClientWDims();
		$('#mask').css('height', cd['h']);
	}
}

function showMask(page, width, height){
	$('#mask').show();
	$.post("../../ajax/"+page, {"width":width, "height":height},
	function(data){
		$('#mask').html(data);
		setMaskInnerPos(height);
		setMaskHeight();
  	});
}

function setMaskInnerPos(height){
	var clientDims=getClientDims();
	var top=Math.round((clientDims.h-height)/2);
	if(top>120){top-=100;}
	$("#mask .inner").css('top', top+'px');
}

function hideMask(){$('#mask').hide();}

function chObjBg(obj, action){
	var action = Math.round(action);
	switch(action){
		case 0:
			$(obj).css('background-color', '#0066CC');
		break;
		case 1:
			$(obj).css('background-color', '#333');
		break;
	}
}

function sel(obj){$(obj).attr('value', '');}

function showHidden(index){
	var shown=$("del:eq("+index+")").css('display');
	if(shown=='block'){$("del:eq("+index+")").hide('fast');}else{$("del:eq("+index+")").show('fast');}

}

function showTheater(){
	$.post("../ajax/theater.php", function(data){
		if(data){
			if($.browser.msie & ($.browser.version.substr(0,1)<=7)){
				$.post("../ajax/theater_ie.php", function(data){
					$("#head").html(data);
				});
			}else{
				$("#head").html(data);
			}

		}
	});
}