﻿/* JavaScript file */

var liftoffTime= new Date(2010,9-1,28,00,00,00);
var days_val;
var hours_val;
var minutes_val;

var docWidth=995;
var i = new Image();
var iW; var iH;
var pGal = new Image(); var pGalW; var pGalH; var photoA; var dPGal; var textPGal; var Photo = new Array();
var tH =200;
var timertext; var d; var h; var m;

var nM;
var nY;

//var LNGvalue; var lng;
if (navigator.appVersion.indexOf("MSIE 6") != -1){
	//alert (navigator.appVersion);
	var IE6 = '1';
}

$(document).ready(function () {
	$(".noscript").hide();
	document.onkeydown = getNavigationKey;
	$("<form id=\"timerholder\"></form>").appendTo("body");
	$("#timerholder").countdown({until: liftoffTime, 
	layout: '<form id="timerholder">' + 
	'{d<}<input type="hidden" id="timerholder_days" value="{dn}" />{d>}'+
	'{h<}<input type="hidden" id="timerholder_hours" value="{hn}" />{h>}' + 
	'{m<}<input type="hidden" id="timerholder_minutes" value="{mn}" />{m>}'+
	'{s<}<input type="hidden" id="timerholder_seconds" value="{sn}" />{s>}'+
	'</form>'});

		setTimerOrange();
		var dt = window.setInterval(function(){
	        setTimerOrange();
		}, 60000);
	
	$('.main').css({
		height: $(document).height()
	});
	
	if ($("#ajaxMainForm").length>0){
		$.get("/interrogation.xml", function(data){ // форма опроса на главной странице
			CalcAjaxSuccess(data);
		});
	}
	$(i).load(function(){
		iW = i.width;
		iH = i.height;
		showLoadedImg(i);
	});
	$(pGal).load(function(){
		pGalW = pGal.width;
		pGalH = pGal.height
		showPhotoGal();
	});
	
	var nY = $("select[id=newsYear] > option:selected").val();
	var nM = $("select[id=newsMonth] > option:selected").val();
	$('select[id=newsMonth]').change(function(){
		nM = $(this).val();
		window.location="?year="+nY+"&month="+nM;
	});
	$('select[name=year]').change(function(){
		nY = $(this).val();
		window.location="?year="+nY+"&month="+nM;
	});
	/**/
	if (IE6!='1'){
		$(".r_in_flash").show();
		$(".top_flash").show();
		$(".top_in_flash").show();
		$(".left_flash").show();
		$(".top_right_flash").show();
		$(".b_in_flash").show();
		$(".l_in_flash").show();
	} else if ($("div.back_index").length!=0 && IE6=='1') {
		$("div.back_index").css("background","url(/images/back_index.gif) center top no-repeat");
	} else if ($("div.back_inside").length!=0 && $("div.back_inside_2").length!=0 && IE6=='1') {
		$("div.back_inside").css("background","url(/images/back_inside.gif) center top no-repeat");
		$("div.back_inside_2").css("background","url(/images/back_inside_2.gif) center bottom no-repeat");
	}
	
	if ($("#top_right_flash").length && $("#b_right_flash").length){
		// расчет ширины окна без расширяющих флешек на главной странице
		$("#top_right_flash").hide();
		$("#b_right_flash").hide();
		docWidth = $(document).width();
		if (window.navigator.appName == 'Microsoft Internet Explorer' && $(document).height()>$(window).height()){
			docWidth = docWidth -21;
		}
	}
	if ($("#top_right_flash").length ){ // расчет ширины правого верхнего флеша на главной странице
		$("#top_right_flash").css("width","1px").show();
		var pL = $("#top_right_flash").position().left +1159;
		$("#top_right_flash").hide();
		if (docWidth>pL){
			var nWFl = docWidth*1 - pL;
			if (nWFl>300){ nWFl=300; }
			$("#top_right_flash").css("width",nWFl+"px");
			$("#top_right_flash").show();
		}
	}
	if ($("#b_right_flash").length){ // расчет ширины правого нижнего флеша на главной странице
		$("#b_right_flash").css("width","1px").show();
		var pL = $("#b_right_flash").position().left +1063;
		$("#b_right_flash").hide();
		if (docWidth>pL){
			var nWFlb = docWidth*1 - pL;
			if (nWFlb>399){ nWFlb=399; }
			$("#b_right_flash").css("width",nWFlb+"px");
			$("#b_right_flash").show();
		}
	}
	if ($("#r_in_flash").length){ // расчет ширины правого флеша на внутренних страницах
		$("#r_in_flash").hide();
		docWidth = $(document).width();
		if (window.navigator.appName == 'Microsoft Internet Explorer' && $(document).height()>$(window).height()){
			docWidth = docWidth -21;
		}
		$("#r_in_flash").css("width","1px").show();
		var pL = $("#r_in_flash").position().left +1286;
		$("#r_in_flash").hide();
		if (docWidth>pL){
			var nWFlr = docWidth*1 - pL;
			if (nWFlr>186){ nWFlr=186; }
			$("#r_in_flash").css("width",nWFlr+"px");
			$("#r_in_flash").show();
		}
	}
});

function setTimerOrange(){
	days_val = $("#timerholder_days").val();
	hours_val = $("#timerholder_hours").val();
	minutes_val = $("#timerholder_minutes").val();
	if (days_val && hours_val && minutes_val) {
      if (timertext!=''){
			toEx = timertext;
		} else if (lng=='en') {
			toEx = 'Before the opening<br/>of EXPO 2010 remains:';
		} else if (lng=='ch') {
			toEx = 'Before the opening<br/>of EXPO 2010 remains:';
		} else {
			toEx = 'До открытия выставки<br/>ЭКСПО-2010 осталось:';
		}
		$("#timer_box").html("<h3>"+toEx+"</h3>"+
		"<div>"+"<b>"+days_val+"</b> "+showDays(days_val)+""+"</div>"+
		"<div>"+"<b>"+hours_val+"</b> "+showHours(hours_val)+"" +"</div>"+
		"<div>"+"<b>"+minutes_val+"</b> "+showMinutes(minutes_val)+"" +"</div>");
	}
}

// pager
function getNavigationKey(e){
	if (!e) var e = window.event;
	var cCode = (e.charCode) ? e.charCode : 0;
	var kCode = (e.keyCode) ? e.keyCode : 0;
	var code = cCode + kCode;
	if (e.ctrlKey) {
		switch (code) {
			case(39) :  // go to next page - Ctrl+(arrow right) // lenux // 23.11.2009
				nextPage();
			break;
			case(37) :  // go to prev page - Ctrl+(arrow left) // lenux // 23.11.2009
				prevPage();
			break;
		}
	}
}
function nextPage(){
	var nextA = $("div.pager > .current").next();
	if ($(nextA).children("a").attr("href")){
		var href = $(nextA).children("a").attr("href");
		GoTo(href);
	}
}
function prevPage(){
	var prevA = $("div.pager > .current").prev();
	if ($(prevA).children("a").attr("href")){
		var href = $(prevA).children("a").attr("href");
		GoTo(href);
	}
}
function GoTo(href){
	window.location=href;
}
// end pager

/* zoom */
$(document).ready(function(){
  $('a.zoom').click(function(){
	$('.poplay_block').remove();

	var src = $(this).attr('href');
	$('body').append('<div class="poplay_block"><img src="'+src+'"><a title="Закрыть"></a></div>');
	
	$('.poplay_block img').load(function(){
		$('.poplay_block').css({top: popy(),left: popx()});
	});	

	$(window).resize(resizePopup);	
	$(document).ready(function(){
	  $('.poplay_block a').click(function(){
		$('.poplay_block').remove();
	  });
	});

	return false;
  });
});

function resizePopup() {
 	$('.poplay_block').css({left: popx()});
}
function popx() {
	var imgw = $('.poplay_block img').attr('width'); 
	return ($(window).width() / 2) - (imgw / 2)
}
function popy() { return $(window).scrollTop() + 100 }
/* zoom end */

function photoGal(im,a){
	dPGal = Photo[im][1];
	textPGal = Photo[im][2];
	im = Photo[im][0];
	photoA = a;
	if (pGal.src == im){
		showPhotoGal();
	} else {
		pGal.src=im;
	}
}
function showPhotoGal(){
	$("#MainPhotoGal").attr({
		"src":pGal.src,
		"width":pGalW,
		"height":pGalH
	});
	/*
	$("#MainPhotoGal").animate({opacity: "0.1"},500).attr({
		"src":pGal.src,
		"width":pGalW,
		"height":pGalH
	}).animate({opacity: "1"},500);
	*/
	$("#PhotoDate").html(dPGal);
	$("#PhotoDescription").html(textPGal);
	$(photoA).parents("tr").children("td").each(function(){
		$(this).children("a").each(function(){
			$(this).removeClass("activ_ph");
		})
	});
	$(photoA).addClass("activ_ph");
}

function CalcAjaxSuccess(data){ // форма опроса на главной странице
	$("#ajaxMainForm").append(data);
	var options = {
		target: "#ajaxMainForm",
		url: "/interrogation.xml",
		success: function(){
			CalcAjaxSuccess();
		}
	};
	$("#interrogation").submit(function(){
		$("#interrogation").ajaxSubmit(options);
		return false;
	})
}

function showDays(n) {
	sn = n.toString();
	var r1=0, r2=0;
	var t='дней';
	var l = sn.length;
	r1 = new Number((sn.length < 2)? 0 : sn.substr(l-2,1));
	r2 = new Number(sn.substr(l-1,1));
	if (d!=''){
		t=d;
	} else if (r1 != 1) {
		if (r2 > 1 && r2 < 5) {
			t='дня';
		} else if (r2 > 4 && r2 <= 9 || r2 == 0) {
			t='дней';
		} else if (r2 == 1) {
			t='день';
		}
	} else {
		t='дней';
	}
	//if (lng=='en') { t='days'; } else if (lng=='ch') { t='?'; }
	return t;
}
function showHours(n) {
	sn = n.toString();
	var r1=0, r2=0;
	var t='часов';
	var l = sn.length;
	r1 = new Number((sn.length < 2)? 0 : sn.substr(l-2,1));
	r2 = new Number(sn.substr(l-1,1));
	if (h!=''){
		t=h;
	} else if (r1 != 1) {
		if (r2 > 1 && r2 < 5) {
			t='часа';
		} else if (r2 > 4 && r2 <= 9 || r2 == 0) {
			t='часов';
		} else if (r2 == 1) {
			t='час';
		}
	} else {
		t='часов';
	}
	//if (lng=='en') { t='hours'; } else if (lng=='ch') { t='??'; }
	return t;
}

function showMinutes(n) {
	sn = n.toString();
	var r1=0, r2=0;
	var t='минут';
	var l = sn.length;
	r1 = new Number((sn.length < 2)? 0 : sn.substr(l-2,1));
	r2 = new Number(sn.substr(l-1,1));
	if (m!=''){
		t=m;
	} else if (r1 != 1) {
		if (r2 > 1 && r2 < 5) {
			t='минуты';
		} else if (r2 > 4 && r2 <= 9 || r2 == 0) {
			t='минут';
		} else if (r2 == 1) {
			t='минута';
		}
	} else {
		t='минут';
	}
	//if (lng=='en') { t='minutes'; } else if (lng=='ch') { t='??'; }
	return t;
}

$(window).resize(function(){
	$('.main').css({
		height: $(document).height()
	}); 
});

$(document).ready(function(){
	if($.browser.opera && (parseInt(jQuery.browser.version) == 9)){
		$('#r_in_flash').remove();
	}
});