// JavaScript Document
//7454, 7543, 7693, 7684, 7875, 7980, 8017, 8056
var bgs=new Array(
	'/img/bg/7454.jpg',
	'/img/bg/7543.jpg',
	'/img/bg/7693.jpg',
	'/img/bg/7875.jpg',
	'/img/bg/7980.jpg',
	'/img/bg/8017.jpg',
	'/img/bg/8056.jpg'
);
var o_illus=false;
var curent=false;
//Set fon function
var bg_count=0, bg_interval=false;
function set_bg(){
	//alert('set_bg called');
	$('#sys_pre').css({
		visibility:'hidden',
		display:'block',
		overflow:'hidden',
		height:$(document).height()-10
	});
	var fname=bgs[bg_count];
	$('#sys_pre').append('<img src="'+fname+'" id="bgImg">');
	$('img#bgImg').load(function(){
		$('#sys_pre').html('');
		$('#sys_pre').css({
			visibility:'visible',
			display:'none'
		});
		$('#sys_pre').fadeIn('slow',function(){
			$(document.getElementById('bg_slideshow')).css('background','url('+fname+') no-repeat center center');
		}).fadeOut('slow');
	});	
	bg_count++;
	if(bg_count>(bgs.length-1)) bg_count=0;
}

var now_loading=false;
var current_page=false;
function init_pops(){
	/*$("a.pop").click(function(){
		load_page(''+this);
		return false;
	});*/
	//Galleries
	$('.vergal').fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200,
		'overlayShow'	:	false
	});
	
	$('.flashContent').each(function(){
	  if($(this).hasClass('flashed')) return true;
    var ts=$(this).text(),h='';
    if($(this).width()==240) h='350px';
    $(this).text('Flash FOUND');
    //return;
    $(this).flash({
  		swf:ts,
  		width:'100%',
  		height:h,
  		scale:'showAll',
  		wmode:'transparent'
  	});
  	$(this).addClass('flashed');
  });
}

$(document).ready(function(){
	/*
	//Услуги иллюстрации	
	$('a.pop').mouseenter(function(){
		if(!o_illus) o_illus=$('#illus');
		if(curent!=this){
			
			var tcnt=$(this).siblings('span').html();
			$('#wlc').fadeOut('fast',function(){
				o_illus.fadeOut('fast',function(){
					o_illus.html(tcnt).fadeIn('slow');
				});
			});
			curent=this;	
		}
  });
  */

	//Фоны
	set_bg();
	bg_interval=setInterval(set_bg,'5000');
		
	init_pops();
	if(document.location.hash!='') load_page( document.location.hash.replace('#','') );
	
	
	
	
	
	
	//Video youtube links
	$("a.video").click(function() {
		$.fancybox({
				'padding'		: 0,
				'autoScale'		: false,
				'transitionIn'	: 'none',
				'transitionOut'	: 'none',
				'title'			: this.title,
				'width'			: 680,
				'height'		: 495,
				'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
				'type'			: 'swf',
				'swf'			: {
				  'wmode'		: 'transparent',
					'allowfullscreen'	: 'true'
				}
			});

		return false;
	});
	
});

var reloading={page:'',times:3, timeout:false};
function reload_page(page){
	$('#loading').append('<span>.</span>');
  now_loading=false;
  if(page!=reloading.page){
    reloading.page=page;
		reloading.limes=3;
	}
  if(reloading.times<1) {
    now_loading=false;
    loading_timeout();
    return false;
	}
	//$('#loading').remove();
	load_page(page);
  reloading.times--;
  return true;
}

function loading_timeout(){
	if(!now_loading) return true;
  $('#loading').remove();
	var obj=$(document.getElementById("serv_ills"));
	obj.append('<div class="png" id="timeout"><h2>Произошла заминка.</h2></div>');
	setTimeout( function(){
	  $('#timeout').animate({opacity:0},500,function(){
	    $('#timeout').remove();
		});
	},
	1200);
}

//var timeOutBubble;
function load_page(page){
	if(now_loading) return false;
	if(page==current_page)  return false;
	now_loading=true;
	
	
	if( $('#loading').get().length<1 ){
		var obj=$(document.getElementById("serv_ills"));
		obj.append('<div class="png" id="loading"><img src="/img/loading.gif"/><br></div>');
	}
	
	var req = new JsHttpRequest();
    req.onreadystatechange = function() {
		if (req.readyState == 4) {
			if(req.responseJS) {
				if(req.responseJS.data) {
					var obj=$(document.getElementById("serv_ills"));
					obj.html(req.responseJS.data);
					document.title=(req.responseJS.title);
					var pga=page.replace(document.location.host+'/','').replace('http://','');
					document.location.hash='#'+pga;
					init_pops();
					now_loading=false;
					current_page=page;
					try{
						pageTracker._trackPageview( pga );
					}catch(e){}
				}
			}
		}
		if(req.responseText!='') alert(req.responseText);
	}
	req.open(null, page+'?action=get', true);
	req.send({
		action: 'get',
		f: page 
	});
	
	if(reloading.timeout) clearTimeout(reloading.timeout);
	reloading.timeout=setTimeout('reload_page(\''+page+'\')',3000);
}
