$(document).ready(function(){
	
	$('.keys-controlled li>a').hover(function(){
		$('.keys-controlled li a.hov').removeClass('hov');
		$(this).addClass('hov');
	},function(){
		$(this).removeClass('hov');
	});
	
	if ($('.keys-controlled li').length) {
			
		updateGalleryWidth();
		$(window).resize(function(){updateGalleryWidth()});
		
		$(document).keydown(function(event) {
			if (event.keyCode > 36 && event.keyCode < 41 && $('.keys-controlled li a.hov').length == 0) {
				$('.keys-controlled li a:eq(0)').addClass('hov');
				return false;
			}
			if (event.keyCode == 39) {
				if ($('.keys-controlled li a.hov').parent().next('li').length) {
					$('.keys-controlled li a.hov').parent().next('li').find('a').addClass('hov');
					$('.keys-controlled li a.hov:eq(0)').removeClass('hov');
				} else {
					//$('.keys-controlled li a.hov').removeClass('hov');
					//$('.keys-controlled li a:eq(0)').addClass('hov');
				}
				return false;
			}
			if (event.keyCode == 37) {
				if ($('.keys-controlled li a.hov').parent().prev('li').length) {
					$('.keys-controlled li a.hov').parent().prev('li').find('a').addClass('hov');
					$('.keys-controlled li a.hov:eq(1)').removeClass('hov');
				} else {
					//$('.keys-controlled li a.hov').removeClass('hov');
					//$('.keys-controlled li a:last').addClass('hov');
				}
				return false;
			}	
			if (event.keyCode == 40) {
				if ($('.keys-controlled li a.hov').parent().nextAll('li').length > window.galleryRowCount - 1) {
					$('.keys-controlled li a.hov').parent().nextAll('li:eq(' + (window.galleryRowCount - 1) + ')').find('a').addClass('hov');
					$('.keys-controlled li a.hov:eq(0)').removeClass('hov');
					window.scrollBy(0,$('.keys-controlled li:eq(0)').height());
				} else {
					//$('.keys-controlled li a.hov').removeClass('hov');
					//$('.keys-controlled li a:last').addClass('hov');
				}
				return false;
			}		
			if (event.keyCode == 38) {
				if ($('.keys-controlled li a.hov').parent().prevAll('li').length > window.galleryRowCount - 1) {
					$('.keys-controlled li a.hov').parent().prevAll('li:eq(' + (window.galleryRowCount - 1) + ')').find('a').addClass('hov');
					$('.keys-controlled li a.hov:eq(1)').removeClass('hov');
					window.scrollBy(0, -1 * $('.keys-controlled li:eq(0)').height());
				} else {
					//$('.keys-controlled li a.hov').removeClass('hov');
					//$('.keys-controlled li a:last').addClass('hov');
				}
				return false;
			}
			if (event.keyCode == 13) {
				if ($('.keys-controlled li a.hov').length) {
					document.location.href = $('.keys-controlled li a.hov').attr('href');
					return false;
				}
			}

		});
		
	}
	
	if ($('.main-slideshow').length) {
		$('.items .image img').each(function(){
			$(this).attr('longdesc',$(this).attr('width') + 'x' + $(this).attr('height'));	
		});		
		prts = document.location.href.split('#');
		//console.log('.items .item img[title$="' + prts[1] + '"]');
		if ($('.items .item img[alt$="' + prts[1] + '"]').length == 1) {
			$('.items .item img[alt$="' + prts[1] + '"]').parents('.item:eq(0)').addClass('current');
			$('.item.current.= .image>img').startLoading();
		} else {
			$('.items .item:eq(0)').addClass('current').find('.image img').show().startLoading();
		}

		updateSlideshow();
		$(window).resize(function(){updateSlideshow()});
		
		$('.main-slideshow-left-ar').click(function(){
			if ($('.item.current').prevAll('.item').length) {
				$('.item.current').removeClass('current').prev('.item').addClass('current');
			} else {
				$('.item.current').removeClass('current');
				$('.items .item:last').addClass('current');
			}
			slideshowJump(1);
			return false;
		});
		
		$('.main-slideshow-right-ar').click(function(){
			if ($('.item.current').nextAll('.item').length) {
				$('.item.current').removeClass('current').next('.item').addClass('current');
			} else {
				$('.item.current').removeClass('current');
				$('.items .item:eq(0)').addClass('current');
			}
			slideshowJump(1);
			return false;
		});
		$('.share-vk,.share-twi').each(function(){
			$(this).attr('href',$(this).attr('href') + ' - ' + document.title);
		});
		
		
		$(document).keydown(function(event) {
			if (event.keyCode == 39) {
				$('.main-slideshow-right-ar:visible').click();
			}
			if (event.keyCode == 37) {
				$('.main-slideshow-left-ar:visible').click();
			}	
			if (event.keyCode == 27) {
				document.location.href = '../';
				return false;
			}
		});
		
		$('.main-slideshow .clickable .image img').click(function(){
			if ($('.main-slideshow-right-ar:visible').length) {
				$('.main-slideshow-right-ar').click();	
			} else {
				$('.main-slideshow .item.current').removeClass('current');
				$('.main-slideshow .item:eq(0)').addClass('current');
				slideshowJump(1);
			}
			return false;
		});
		
		$('.bottom').css('z-index','');
		
		$('.top.clickable,.top.clickable *').click(function(){
			document.location.href = $('.close').attr('href');
			//window.alert($('.close').attr('href'));
			return false;
		});
		
		$('.top.clickable').hover(function(){
			$('.close').addClass('hov');
		},function(){
			$('.close').removeClass('hov');
		});
		
	}
	
	if ($('h1 a').length) {
		$(document).keydown(function(event) {
			if (event.keyCode == 27) {
				document.location.href = '../';
				return false;
			}
		});
	}
	
	
	if ($('.slideshow-left-ar').length) {
		$('.slideshow-left-ar').click(function(){
			$('.slideshow .current').removeClass('current').prev('li').addClass('current');
			infoSlideshowJump();
			return false;
		});
		$('.slideshow-right-ar').click(function(){
			$('.slideshow .current').removeClass('current').next('li').addClass('current');
			infoSlideshowJump();
			return false;
		});
		$('.many-pics img').click(function(){
			if ($('.slideshow-right-ar:visible').length) {
				$('.slideshow-right-ar').click();	
			} else {
				$('.slideshow .current').removeClass('current');
				$('.slideshow li:eq(0)').addClass('current');
				infoSlideshowJump();
			}
			
			return false;
		});
	}

	if ($('.text-editor').length) {
		$('.text-editor p img').each(function(){
			$(this).parents('p:eq(0)').addClass('cfix');
		});
		
		$('.text-editor .big-photo').each(function(){
			code = $(this).html() + '<ins>' + $(this).find('img').attr('alt') + '</ins>';
			$(this).html(code);
		});
	}
	
	$('.share-fb-auto').attr('href','http://www.facebook.com/sharer.php?u=' + escape(document.location.href));
	$('.share-vk-auto').attr('href','http://vkontakte.ru/share.php?url=' + escape(document.location.href) + '&title=' + document.title);
	$('.share-twi-auto').attr('href','http://twitter.com/share?url=' + escape(document.location.href) + '&text=' + document.title);

	
});

$.fn.startLoading = function(){
	if ($(this).attr('src') == '') {
		$(this).attr('src',$(this).attr('alt'));
		$(this)[0].onload = function(){ slideshowImageLoaded($(this)) };
	}
}

$.fn.getW = function(){

	d = $(this).attr('longdesc').split('x');
	return d[0];
	
}

$.fn.getH = function(){

	d = $(this).attr('longdesc').split('x');
	return d[1];
	
}

$.fn.scale = function(w,h){

	if ($(this).parent().hasClass('no-scale') || $(this).parent().parent().hasClass('no-scale')) {
		dh = $(this).getH();
	} else {
		if ($(this).getW() / $(this).getH() >= w / h) {
			r = $(this).getW() / w;
			$(this).css('width',Math.round(w));
			dh = Math.round($(this).getH() / r)
		} else {
			r = $(this).getH() / h;
			$(this).css('width',Math.round($(this).getW() / r));
			dh = Math.round(h);
		}
		$(this).css('height',dh);
	}
	$(this).css({marginTop:-1 * Math.round(dh / 2)});
	
};

function updateGalleryWidth() {
	
	if ($('.keys-controlled').hasClass('album-list')) {
		w = getWindowWidth() - 144;
		window.galleryRowCount = parseInt(w / 489);
		$('.keys-controlled').css('width',window.galleryRowCount * 489);
	} else {
		w = getWindowWidth() - 144;
		window.galleryRowCount = parseInt(w / 240);
		$('.keys-controlled').css('width',window.galleryRowCount * 240);
	}
	
}

function updateSlideshow() {

	h = $(window).height() - 100;
	w = getWindowWidth();
	if (h < 300) {
		h = 300;
	}
	if (w < 900) {
		w = 900;
	}
	$('.screen,.image').css('height',h);
	$('.image').css('width',w);
	$('.item').css('width',w);
	$('.items .image img').each(function(){$(this).scale(w-100,h)});
	slideshowJump(0);
	
}

function slideshowJump(animation) {
	
	w = getWindowWidth();
	if (w < 900) {
		w = 900;
	}
	if (animation) {
		$('.items').animate({left: -1 * w * $('.item.current').prevAll('.item').length},400);
	} else {
		$('.items').css({left: -1 * w * $('.item.current').prevAll('.item').length});
	}
	prts = $('.item.current .image img').attr('src').split('/');	
	document.location.href = '#' + prts[prts.length - 1];
	if ($('.item.current').prevAll('.item').length == 0) {
		$('.main-slideshow-left-ar').hide()
	} else {
		$('.main-slideshow-left-ar:hidden').fadeIn(100);
	}
	if ($('.item.current').nextAll('.item').length == 0) {
		$('.main-slideshow-right-ar').hide();
	} else {
		$('.main-slideshow-right-ar:hidden').fadeIn(100);
	}
	
}

function infoSlideshowJump() {
	
	$('.screen ul').css({height:$('.slideshow ul li.current img').attr('height')});
	$('.screen').animate({height:$('.slideshow ul li.current img').attr('height')},400);
	$('.slideshow ul').animate({left: -920 * $('.slideshow ul li.current').prevAll('li').length},400);
	if ($('.slideshow ul li.current').nextAll('li').length == 0) {
		$('.slideshow-right-ar').hide();
	} else {
		$('.slideshow-right-ar:hidden').fadeIn(100);
	}
	if ($('.slideshow ul li.current').prevAll('li').length == 0) {
		$('.slideshow-left-ar').hide();
	} else {
		$('.slideshow-left-ar:hidden').fadeIn(100);
	}
	
}

function slideshowImageLoaded($img) {
	$img.hide().css('visibility','visible').fadeIn(200);
	if ($('.main-slideshow').length) {
		$img.parent().parent().next('.item').find('.image img').startLoading();
		$img.parent().parent().prev('.item').find('.image img').startLoading();
	}
}

function  getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		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 if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		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 = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}

function getWindowWidth() {
	
	r = getPageSize();
	return r[2];
	
}
