function load_image(image_src) {
	footer_height = $('#footer').height();
	var marginleft = ($(document).width()-150-20)/2;
	var margintop = ($(document).height()-170-footer_height-20)/2;
	newhtml = '<div style="padding-top: '+margintop+'px; padding-left: '+marginleft+'px"><img src="/i/loader.gif" /></div>';
	$('#back_photo').html(newhtml);
	myImage = new Image();
	myImage.src = image_src;
	myImage.onload = function() {
		resize_image(myImage);
	};
}

function resize_image(image_obj) {
	myImage = image_obj;
	srcwidth = myImage.width;
	srcheight = myImage.height;

	w_width = $(document).width();
	w_height = $(document).height();
	myImgWinWidth = w_width - 150;
	k = srcwidth/myImgWinWidth;
	myImgWinHeight = Math.round(srcheight/k);

	back_photo_height = ($(document).height() - 170) + 'px';
	
	if ((myImgWinHeight + 170 + 36) < $(document).height()) {
		footerHeight = ($(document).height() - 170 - myImgWinHeight) + 'px';
		$('#footer').css('height', footerHeight);
	} else {
		footerHeight = '36px';
		$('#footer').css('height', footerHeight);
	}
	footer_height = $('#footer').height();
	main_part_height = ($(document).height() - 170 - footer_height) + 'px';
	$('#main_part').css('height', main_part_height);
	$('#preview').css('height', main_part_height);
	$('#preview').show();
	$('#back_photo').css('height', back_photo_height);

	newhtml = '<img src="'+myImage.src+'" width="'+myImgWinWidth+'" height="'+myImgWinHeight+'" />';
	$('#back_photo').html(newhtml);
}

function resize_loaded() {
	$('#preview').hide();
	srcwidth = $('#back_photo img').width();
	srcheight = $('#back_photo img').height();
	myImageSrc = $('#back_photo img').attr('src');
	w_width = $(document).width();
	w_height = $(document).height();
	myImgWinWidth = w_width - 150;
	k = srcwidth/myImgWinWidth;
	myImgWinHeight = Math.round(srcheight/k);
	
	back_photo_height = ($('body').height() - 170) + 'px';
	if ((myImgWinHeight + 170 + 36) < $(document).height()) {
		footerHeight = ($(document).height() - 170 - myImgWinHeight) + 'px';
		$('#footer').css('height', footerHeight);
	} else {
		footerHeight = '36px';
		$('#footer').css('height', footerHeight);
	}
	
	footer_height = $('#footer').height();
	main_part_height = ($(document).height() - 170 - footer_height) + 'px';
	$('#main_part').css('height', main_part_height);
	$('#preview').css('height', main_part_height);
	$('#preview').show();
	$('#back_photo').css('height', back_photo_height);
	newhtml = '<img src="'+myImageSrc+'" width="'+myImgWinWidth+'" height="'+myImgWinHeight+'" />';
	$('#back_photo').html(newhtml);
}

function events_cut() {
	//89112111992 фролов
	offset = $('#content').offset();
	footer_height = $('#footer').height();
	content_height = $('#content').height();
	guest_height = $('#guest').height();
	window_content_height = $(document).height() - offset.top  - footer_height - guest_height - ($('#guest').offset().top - content_height - $('#content').offset().top + 20);
	if (window_content_height < content_height && $('#content').children("div.event_block:visible").size() > 0) {
	    while (window_content_height < content_height && $('#content').children("div.event_block:visible").size() > 0) {
			$('#content').children("div.event_block:visible:last").hide();
			content_height = $('#content').height();
			window_content_height = $(document).height() - offset.top  - footer_height - guest_height - ($('#guest').offset().top - content_height - $('#content').offset().top + 20);
		}
	} else if (window_content_height > (content_height + 124)) {
		$('#content').children("div.event_block:hidden:first").show();
		content_height = $('#content').height();
		window_content_height = $(document).height() - offset.top  - footer_height - guest_height - 80;
	}
}

function player_slide() {
	offset = 456;
	all_width = $(document).width();
	margin_right = all_width*0.24;
	player_width = all_width - margin_right - offset;
	$("#player object").attr("width", player_width);
	/* минимальная ширина плеера */
	if(player_width <= 349)
		player_width = 349;
	$("#player embed").attr("width", player_width);
}

function content_resize() {
	typical_height = $(".typical").height();
	guest_height = $(".guest").height();
	if (typical_height < ($(document).height() - 206)) {
	  	$(".typical").height($(document).height() - 206);
	}
	if (guest_height < ($(document).height() - 206)) {
	  	$(".guest").height($(document).height() - 206);
	}
}

/* -------- */
/* LISTENER */
/* -------- */
function add_listener() {
	events_cut();
	player_slide();
	load_image("/main_photo/bg-photo-01.jpg");
	content_resize();
	$(window).resize(function() {
		resize_loaded();
		events_cut();
		player_slide();
		content_resize();
	});
	
	$("#preview a img").hover(function(){
	    if ($(this).css("opacity") != "1") {
			$(this).css("opacity", "0.7");
		}
	}, function(){
		if ($(this).css("opacity") != "1") {
			$(this).css("opacity", "0.3");
		}
	});
	$("#preview a").click(function(){
		//if ($(this).children("img").css("opacity") != "1") {
			newimg = $(this).attr("href");
			$("#preview a img").css("opacity", "0.3");
			$(this).children("img").css("opacity", "1");
			load_image(newimg);
		//}		
		return false;
	});
	
	$('.album_photo a').lightBox();
	
	$(".tour_row").hover(
		function() {
			$(this).addClass("tour_row_hover");
		}, 
		function() {
			if($(this).children(".tour_info").css("display") == "none") {
				$(this).removeClass("tour_row_hover");
			}
		}
	);
	$(".tour_row").click(function(){
		$(this).children(".tour_info").slideDown(400);
		$(this).css("cursor", "default");
		$(this).children(".tour_name").children("h2").children("a").css("border-bottom", "none");
	});
	$(".tour_name a").click(function(){
		$(this).parent().parent().parent().children(".tour_info").slideDown(400);
		$(this).parent().parent().parent().css("cursor", "default");
		$(this).css("border-bottom", "none");
		return false;
	});
	$(".rollup a").click(function(){
		$(this).parent().parent().parent().css("cursor", "pointer");
		$(this).parent().parent().parent().children(".tour_name").children("h2").children("a").css("border-bottom", "1px dashed #ffffff");
		$(this).parent().parent().slideUp(400, function() {
			$(this).parent().removeClass("tour_row_hover");
		});
		return false;
	});
	
	$(".rescent_events a").click(function() {
		if($(".past_events").css("display") == "block") {
			$(".past_events").slideUp();
		} else {
			$(".past_events").slideDown();
		}
		return false;
	});
	
	$("#rider").click(function(){
		var arrPageScroll = getPageScroll();
		var arrPageSizes = getPageSize();
		$("#jquery-overlay").height(arrPageSizes[1]);
		$("#jquery-overlay").width(arrPageSizes[0]);
		$("#jquery-overlay").show();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				/*$('#jquery-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});*/
				
		margin_top = arrPageScroll[1] + (arrPageSizes[3] - 230)/2 + 'px';
		margin_left = ($(document).width() - 680)/2 + 'px';
		$("#rider_block").css("top", margin_top);
		$("#rider_block").css("left", margin_left);
		$("#rider_block").show();
		return false;
	});
	
	$("#jquery-overlay").click(function(){
		$("#jquery-overlay").hide();
		$("#rider_block").hide();
		return false;
	});
	
	$("#close_lnk a").click(function(){
		$("#jquery-overlay").hide();
		$("#rider_block").hide();
		return false;
	});
	
	/* инициализация замены атрибута href в ссылках */
	/* проверка на исходящие и внутренние ссылки */
	$("a.ajax").click(function() {
			var href = $(this).attr("href");
			href = href.replace("#", "");
			$(this).attr("href", "#" + href);
			load_page(href);
			/* подключение AJAX */
	});
	
	//window.scrollTo(0, $("#current_tour").offset().top);
	$("html").animate({scrollTop : $("#current_tour").offset().top},1000);
	//$("html").scrollTop($("#current_tour").offset().top);
}
/* -------- */
/* LISTENER */
/* -------- */

$(document).ready(function(){
	add_listener();
	
	$("a.ajax_once").click(function() {
		if($(this).attr("href").indexOf("http://") >= 0) {
			return true;
		} else {
			var href = $(this).attr("href");
			href = href.replace("#", "");
			$(this).attr("href", "#" + href);
			load_page(href);
			/* подключение AJAX */
		}
	});
	
	var anchor = location.hash;
	if(anchor.length > 0) {
		window.scrollTo(0,0);
		var href = anchor.substr(1, anchor.length - 1);
		load_page(href);
	}
	
	$.get("/gettitle.php", {"url": page_url}, function(data) {
		$(document).attr('title', data);
	});
});

function remove_old_listener() {
	$("a, div, h1, h2, img").unbind();
}

function load_page(page_url) {
	window.scrollTo(0,0);
	footer_height = $('#footer').height();
	var marginleft = ($(document).width()-150-20)/2;
	var margintop = ($(document).height()-170-footer_height-20)/2;
	newhtml = '<div style="padding-top: '+margintop+'px; padding-left: '+marginleft+'px"><img src="/i/loader.gif" /></div>';
	/* необходимо сделать ресайз окна */
	$('div.page').html(newhtml);
	$.get("/gettitle.php", {"url": page_url}, function(data) {
		$(document).attr('title', data);
	});
	$.get(page_url, {},
	function(data){
		if(page_url != "/start/" && page_url != "/") {
			$(".footer_id").removeClass("footer");
			$(".footer_id").addClass("footer_page");
			$(".footer_id").removeAttr("id")
		} else {
			$(".footer_id").removeClass("footer_page");
			$(".footer_id").addClass("footer");
			$(".footer_id").attr("id", "footer")
		}
		$('div.page').html(data);
		remove_old_listener();
		add_listener();
	});	
}

		/**
		 / THIRD FUNCTION
		 * getPageSize() by quirksmode.com
		 *
		 * @return Array Return an array with page width, height and window width, height
		 */
		function getPageSize() {
			var xScroll, yScroll;
			if (window.innerHeight && window.scrollMaxY) {	
				xScroll = window.innerWidth + window.scrollMaxX;
				yScroll = window.innerHeight + window.scrollMaxY;
			} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
				xScroll = document.body.scrollWidth;
				yScroll = document.body.scrollHeight;
			} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
				xScroll = document.body.offsetWidth;
				yScroll = document.body.offsetHeight;
			}
			var windowWidth, windowHeight;
			if (self.innerHeight) {	// all except Explorer
				if(document.documentElement.clientWidth){
					windowWidth = document.documentElement.clientWidth; 
				} else {
					windowWidth = self.innerWidth;
				}
				windowHeight = self.innerHeight;
			} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
				windowWidth = document.documentElement.clientWidth;
				windowHeight = document.documentElement.clientHeight;
			} else if (document.body) { // other Explorers
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}	
			// for small pages with total height less then height of the viewport
			if(yScroll < windowHeight){
				pageHeight = windowHeight;
			} else { 
				pageHeight = yScroll;
			}
			// for small pages with total width less then width of the viewport
			if(xScroll < windowWidth){	
				pageWidth = xScroll;		
			} else {
				pageWidth = windowWidth;
			}
			arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
			return arrayPageSize;
		};
		/**
		 / THIRD FUNCTION
		 * getPageScroll() by quirksmode.com
		 *
		 * @return Array Return an array with x,y page scroll values.
		 */
		function getPageScroll() {
			var xScroll, yScroll;
			if (self.pageYOffset) {
				yScroll = self.pageYOffset;
				xScroll = self.pageXOffset;
			} else if (document.documentElement && document.documentElement.scrollTop) {	 // Explorer 6 Strict
				yScroll = document.documentElement.scrollTop;
				xScroll = document.documentElement.scrollLeft;
			} else if (document.body) {// all other Explorers
				yScroll = document.body.scrollTop;
				xScroll = document.body.scrollLeft;	
			}
			arrayPageScroll = new Array(xScroll,yScroll);
			return arrayPageScroll;
		};
