var img, animate = 1, firstInList = 1, thisPhoto = 1, marginLeftList = 0, totalCount, height;
var opera = Boolean(window["opera"]);
var ie = (navigator.appName.indexOf("Microsoft") != -1) && !opera;

jQuery(document).ready(function () {
	totalCount = jQuery(".photo_item").length;
	img = document.createElement('img');
	img.onload = function () {
		jQuery(".big_photo_img").attr("src", jQuery(".photo_item:first a").attr("href"));
		var photoWidth = jQuery(".big_photo_img").width();
		var photoHeight = jQuery(".big_photo_img").height();
		
		height = (402*photoWidth)/608;
		
		if (photoHeight > height) {
			jQuery(".big_photo_img").attr("height", 402);
		} else {
			jQuery(".big_photo_img").attr("width", 608);
		}
		
		jQuery(".big_photo_img").css("border", "#ffffff 4px solid");
		jQuery("#big_photo_link").attr("href", jQuery(".photo_item:first a").attr("href"));
		jQuery("#big_photo_link").attr("title", jQuery(".photo_item:first a").children("img").attr("alt"));
		
		if (totalCount > 1) {
			jQuery(".big_photo_next").show();
		}
		animate = 0;
	};
	
	jQuery(img).attr('src', jQuery(".photo_item:first a").attr("href"));
	
	jQuery(".photo_item:first a").addClass("sel");
	
	jQuery(".photo_item a").click(function () {
		showPhoto (jQuery(this));
	});
	
	jQuery(".photo_item a").hover(
		function () {
			if (!jQuery(this).hasClass("sel")) {
				jQuery(this).children("img").css("opacity", 0.7);
			}
		},
		function () {
			if (!jQuery(this).hasClass("sel")) {
				jQuery(this).children("img").css("opacity", 0.4);
			}
		}
	);
	
	jQuery(".top_menu").children("li").hover(
		function () {
			jQuery(this).children("ul").show();
		},
		function () {
			jQuery(this).children("ul").hide();
		}
	);
	
	jQuery(".big_photo_prev, .big_photo_next").hover(
		function () {
			jQuery(this).addClass("hover");
		},
		function () {
			jQuery(this).removeClass("hover");
		}
	);
	
	jQuery("#big_photo_link").fancybox({
		'overlayShow'			: false,
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack',
		'imageScale'			: false,
		'centerOnScroll'		: false
	});
});


function showPhoto (clickObj, type) {
	if (animate === 0 && (!clickObj.hasClass("sel"))) {
		animate = 1;
		jQuery(".big_photo_img").animate({opacity: 0.3}, 500, "swing", function() {
			if (type != "list") {
				thisPhoto = jQuery(".photo_item a").index(clickObj) + 1;
				if (thisPhoto - firstInList == 7) {
					marginLeftList = marginLeftList - 142;
					firstInList ++;
				}
				if (thisPhoto - firstInList == -1) {
					marginLeftList = marginLeftList + 142;
					firstInList --;
				}
			}
			jQuery(".photos_list").animate({marginLeft: marginLeftList+"px"}, 500);
			
			jQuery(".photo_item a").removeClass("sel");
			jQuery(".photo_item a").children("img").css("opacity", 0.4);
			clickObj.children("img").css("opacity", 1);
			img = document.createElement('img');
			img.onload = function () {
				jQuery(".big_photo_loader").hide();
				
				var title = trim(clickObj.children("img").attr("alt"));
				var description = trim(clickObj.attr("title"));
				jQuery(".photo_title").html(title);
				jQuery(".description").html(description);
				var filter  = /^\s*$/;
				if (filter.test(title)) {
					jQuery(".photo_title").hide();
				} else {
					jQuery(".photo_title").show();
				}
				if (filter.test(description)) {
					jQuery(".description").hide();
				} else {
					jQuery(".description").show();
				}
				
				jQuery(".big_photo_img").attr("src", clickObj.attr("href"));
				
				var photoWidth = jQuery(".big_photo_img").width();
				var photoHeight = jQuery(".big_photo_img").height();
				height = (402*photoWidth)/608;
				if (photoHeight > height) {
					jQuery(".big_photo_img").removeAttr("width");
					jQuery(".big_photo_img").attr("height", 402);
				} else {
					jQuery(".big_photo_img").removeAttr("height");
					jQuery(".big_photo_img").attr("width", 608);
				}
				
				jQuery(".big_photo_img").animate({opacity: 1}, 500, "swing", function() {
					animate = 0;
				});
				clickObj.addClass("sel");
				clickObj.children("img").css("opacity", 1);
				
				if (clickObj.parent().html() == jQuery(".photo_item:first").html()) {
					jQuery(".big_photo_prev").hide();
				} else {
					jQuery(".big_photo_prev").show();
				}
				if (clickObj.parent().html() == jQuery(".photo_item:last").html()) {
					jQuery(".big_photo_next").hide();
				} else {
					jQuery(".big_photo_next").show();
				}
				
				jQuery("#big_photo_link").attr("href", clickObj.attr("href"));
				jQuery("#big_photo_link").attr("title", clickObj.children("img").attr("alt"));
			};
			jQuery(".big_photo_loader").show();
			jQuery(img).attr('src', clickObj.attr("href"));
		});
	} else if (animate === 0 && type == "list") {
		animate = 1;
		jQuery(".photos_list").animate({marginLeft: marginLeftList+"px"}, 500, "swing", function() {
			animate = 0;
		});
	}
}

function showPrev (photoObj) {
	if (photoObj.html() != jQuery(".photo_item:first").html() && animate === 0) {
		showPhoto (photoObj.prev().children("a"));
	}
}

function showNext (photoObj) {
	if (photoObj.html() != jQuery(".photo_item:last").html() && animate === 0) {
		showPhoto (photoObj.next().children("a"));
	}
}

function moveList (direction) {
	if (animate === 0) {
		var moveCount = 0;
		
		if (direction == "prev") {
			if (firstInList > 7) {
				moveCount = -6;
			} else if (firstInList > 1) {
				moveCount = 1 - firstInList;
			}
		} else {
			if (totalCount - firstInList > 13) {
				moveCount = 6;
			} else if (totalCount - firstInList > 6) {
				moveCount = totalCount - firstInList - 6;
			}
		}
		
		if (moveCount != 0) {
			firstInList = firstInList + moveCount;
			thisPhoto = firstInList;
			marginLeftList = marginLeftList -142*moveCount;
			showPhoto (jQuery('.photo_item:eq('+(thisPhoto-1)+')').children("a"), "list");
		}
	}
}

function trim (string) {
	return string.replace(/^\s*([^\s].*)$/, "$1").replace(/^(.*[^\s])\s*$/, "$1");
}