var sTab=false;
var sProccess=false;
function SelectTab(v,isauto){
	if (isauto && sTab) return false;
	if (sProccess) return false;
	if (sTab && !isauto && $(v).attr('href')==$(sTab).attr('href')) return false;	
	stopSlideShow();
	sProccess=true;	
	$(v).addClass('activeMenu');
	cPage=$(v).attr('href').replace(new RegExp("/",'g'),'');
	if (cPage=='') cPage='company';
	$(v).attr('cnt',cPage);
	if (sTab){	
		$('#panel_'+$(sTab).attr('cnt')).animate({"top": "-555px"}, 500);
		$(sTab).removeClass('activeMenu');
		window.setTimeout(function(){sProccess=false;sTab=false;SelectTab(v);},700);
		return false;	
	}
	$('#panel_'+cPage).css("display","block");
	$('#panel_'+cPage).animate({"top": "0"}, 1000);
	if (cPage=='company') 
		$('#companyDivScroller').jScrollPane({showArrows:true,animateTo:true});
	if (cPage=='contacts') 
		$('#contactsTableW').animate({left:'-750px'},200);
	if (cPage=='news') 
		$('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});
		
	sTab=v;
	sProccess=false;
	return false;
}
function centerContacts(){
	$('#contactsTableW').animate({left:'-750'},1000);
	return false;
}

var startScrollGalTick=false;
var startScrollGalTickDelta=30;

function scrollingGal(){
	$('#galPreview').scrollTop($('#galPreview').scrollTop()+startScrollGalTickDelta);
}

function startScrollGal(v,ob){
	if(ob) ob.blur();
	if (startScrollGalTick) return false;
	startScrollGalTickDelta=v;
	startScrollGalTick=window.setInterval(scrollingGal,10);
	return false;
}
function stopScrollGal(){
	window.clearInterval(startScrollGalTick);
	startScrollGalTick=false;
}
function tickScroll(v){	
	if (startScrollGalTick && startScrollGalTickDelta==v) return;
	if (startScrollGalTick && startScrollGalTickDelta!=v) {
		stopScrollGal();
	}
	
	if (v>0)  startScrollGalTickDelta=30; else  startScrollGalTickDelta=-30;
		
	startScrollGal(startScrollGalTickDelta);
	window.setTimeout(stopScrollGal,10);		
}
selectedPortfolio=false;
function selectPortfolio(v){
	if (v===selectedPortfolio)  return false;
	if (selectedPortfolio!==false) $('#p_href'+selectedPortfolio).animate({opacity:0.3},500);
	$('#galPreview').animate({scrollTop:(v-2)*61},700);
	$('#p_href'+v).animate({opacity:1},500);
	selectedPortfolio=v;	
	$('#nameP').html($('#p_name'+v).html());
	
	$('#imageP').attr('src',$('#imageT').attr('src'));
	$('#imageT').css('opacity',0);
	$('#imageP').css('opacity',1);
	$('#imageT').attr('src',$('#p_img'+v).attr('src'));
	$('#imageT').animate({opacity:1},1000);
	$('#imageP').animate({opacity:0},1000);
	return false;
}

function nextPortfolio(){
if (selectedPortfolio===false && !$('#p_href0').length) return false;	
if (selectedPortfolio===false) {
	 selectPortfolio(0);
	 return false;
 }
 c=selectedPortfolio+1;
 if($('#p_href'+c).length) selectPortfolio(c);
 return false;
}

function prevPortfolio(){
if (selectedPortfolio===false && !$('#p_href0').length) return false;	
if (selectedPortfolio===false) {
	 selectPortfolio(0);
	 return false;
 }
 c=selectedPortfolio-1;
 if($('#p_href'+c).length) selectPortfolio(c);
 return false;
}
var sildeShowRunning=false;

function doSlideShow(){
if (selectedPortfolio===false) return;	
 c=selectedPortfolio+1;
 if($('#p_href'+c).length) selectPortfolio(c);
 else selectPortfolio(0);
}

function runSlideShow(){
	$('#playStopPortfolio').css('background-image','url(/site-imgs/LDpause.gif)');
	sildeShowRunning=window.setInterval(doSlideShow,5000);
}

function stopSlideShow(){
	window.clearInterval(sildeShowRunning);
	$('#playStopPortfolio').css('background-image','url(/site-imgs/LDplay.gif)');
	sildeShowRunning=false;
}

function toggleSlideShow(){
	if (!sildeShowRunning) runSlideShow();
		else stopSlideShow();
	return false;
}

var preloadImages= new Array('/site-imgs/LD_panel.png','/site-imgs/LD_bg.jpg', '/site-imgs/LDmap_moscow.jpg', '/site-imgs/LD_headerBg.gif', '/site-imgs/LD_logo.png', '/site-imgs/LDmap_spb.jpg', '/site-imgs/LDgooglemap.jpg');

function siteLoaded(){
			$.preloadImages(preloadImages,function(){window.setTimeout(innerSiteLoaded,1000);});
}

function innerSiteLoaded(){
	$('#areaTd').css('opacoty','0');
	$('#areaTd').css('visibility','visible');
	if(!$.browser.msie) {
			$("#BODY_preload").fadeOut('slow'); 
			$("#areaTd").fadeIn('slow'); 
	}	else {
			$("#BODY_preload").hide();			     			
			$("#areaTd").show(); 
	}
	if (window.location.hash=='#portfolio') {
			sTab=$('#actBtn');
			SelectTab($('#menu a[href="/portfolio/"]'));
	}
	if (window.location.hash=='#contacts') {
			sTab=$('#actBtn');
			SelectTab($('#menu a[href="/contacts/"]'));
	}
	if (window.location.hash=='#news') {
			sTab=$('#actBtn');
			SelectTab($('#menu a[href="/news/"]'));
	}	
	
	if (!$('#actBtn').length) {
		b=$('#menu a'); $(b[0]).attr('id','actBtn');
	}
	window.setTimeout(function(){SelectTab($('#actBtn'),true);},200);
	$('.panel').bind('mousewheel', function(event, delta) {return false;});			
	$('#galPreview').bind('mousewheel', function(event, delta) {tickScroll(0-delta);return false;});			
	$('.photoAlbumLink').css('opacity','0.3');
	nextPortfolio();
	var logo_Swf = new SWFObject("/site-imgs/logo.swf", "", "874", "205", "8"); 
	logo_Swf.addParam("quality", "hight"); 
	logo_Swf.addParam("wmode", "transparent"); 
	logo_Swf.addParam("allowFullScreen","true");
	logo_Swf.addParam("allowScriptAccess","always");
	logo_Swf.addParam("menu","false");
	logo_Swf.write("logoSwf");
	
	//AjaxContentReplace
	lnk=$('#paginator_news').find('a');
	for (i=-1;i<lnk.length;i++){
		$('<a href="/" class="pager" id="pager'+(i+2)+'" onclick="return selectnewsPage(this);"> '+(i+2)+'</a>').appendTo('#paginator_news');
	}
	if (lnk.length) selectnewsPage($('#pager1'));
}
autoPager=true;
function selectnewsPage(ob,a){
		$('#paginator_news').find('a').removeClass('activePager');
		$(ob).toggleClass('activePager');
		$('#newsDivScroller').html('<br>');
		$('#newsDivScroller').css('background','url(/site-imgs/wait.gif) center no-repeat');
		if (!autoPager) $('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});
		i=$(ob).attr('id').replace('pager','');
		$.get('/news/page-'+i+'/', {type:'html', 'var': 'content' }, getAjaxDone);
		return false;
}
function getAjaxDone(res){
	$('#newsDivScroller').css('background','none');
	$('#newsDivScroller').html(res);
	if (!autoPager) $('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});
	$('#newsName').text('Новости'); 
	autoPager=false;
}

var newsTitle=false;
function getAjaxPageDone(res){
	$('#newsDivScroller').css('background','none');
	b=$('<table style="width:100%;"><tr><td style="vertical-align:top;"><a href="/" onclick="return getNewsBack();" style="border:none;"><img src="/site-imgs/LDnewsBack.png" alt=""></a></td><td style="width:100%;vertical-align:top; padding:10px;">'+res+'</td></table>');
	$('#newsDivScroller').html(b);
	if (!newsTitle) $('#newsName').text('Новости'); 
		else {
			$('#newsName').text(newsTitle); 
			newsTitle=false;
		}
	$('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});	
}

var returnContent='';
function showNews(v,adr){
		newsTitle=v;
		returnContent=$('#newsDivScroller').html();
		$('#newsDivScroller').html('<br>');
		$('#newsDivScroller').css('background','url(/site-imgs/wait.gif) center no-repeat');
		$('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});		
		$.get(adr, {type:'html', 'var': 'content' }, getAjaxPageDone);
		return false;
}

function getNewsBack(){
	$('#newsName').text('Новости'); 
	$('#newsDivScroller').html(returnContent);
	$('#newsDivScroller').jScrollPane({showArrows:true,animateTo:true});
	return false;
}
$.preloadImages = function () {
    if (typeof arguments[arguments.length - 1] == 'function') {
        var callback = arguments[arguments.length - 1];
    } else {
        var callback = false;
    }
    if (typeof arguments[0] == 'object') {
        var images = arguments[0];
        var n = images.length;
    } else {
        var images = arguments;
        var n = images.length - 1;
    }
    var not_loaded = n;
    for (var i = 0; i < n; i++) {
        $(new Image()).load(function() {
            if (--not_loaded < 1 && typeof callback == 'function') {
                callback();
            }
        }).attr('src', images[i]);
    }
}