$(document).ready(function() {
	$('#navigation ul li a').each(function (x, item) {
		if ($(item).siblings('ul').length) {
			var link = this;
		
			$(item).click(function() {
				$(this).parent('li').addClass('click');
				navigation.showing[this] = true;
				navigation.over[this] = true;
				return false;
			});
			$(item).mouseover(function () {
				navigation.over[this] = true;
			});
			$(item).mouseout(function () {
				navigation.over[this] = false;
				navigation.hide(this);
			});
			
			$(item).siblings('ul').mouseover(function() {
				navigation.over[link] = true;
			});
			$(item).siblings('ul').mouseout(function() {
				navigation.over[link] = false;
				navigation.hide(link);
			});
		}
	});
	
	$('#footer_elements .element').each(function (x, item) {
		$(item).mouseover(function() {
			$(this).addClass('hover');
		});
		$(item).mouseout(function() {
			$(this).removeClass('hover');
		});
	});
	
	if ($('#content.press .items_wrapper').size()) {
		press.init();
		press.setupImages();
	}
	
	if ($('#content.press_images .items_wrapper').size()) {
		pressImages.init();
		pressImages.setupImages();
	}
});

$(window).load(function() {
	if ($('#content.press .items_wrapper').size()) {
		press.setupImages();
	}
	
	if ($('#content.press_images .items_wrapper').size()) {
		pressImages.setupImages();
	}
});

var navigation = {
	over: {},
	showing: {},
	timeouts: {},
	hide: function(href) {
		if (navigation.timeouts[href]) {
			clearTimeout(navigation.timeouts[href]);
		}
		navigation.timeouts[href] = setTimeout("navigation.tryHide('"+href+"')", 1000);
	},
	tryHide: function(href) {
		if (navigation.showing[href] && !navigation.over[href]) {
			navigation.showing[href] = false;
			
			$('#navigation ul li a').each(function (x, item) {
				if (item == href) {
					$(item).parent('li').removeClass('click');
				}
			});
		}
	}
}

var pressImages = {
	elementCount: 0,
	pageCount: 0,
	currentPage: 1,
	pages: [],
	setupImages: function() {
		$('.items_wrapper .item img').each(function(x, elem) {
			imgHeight = $(elem).height();
			imgWidth = $(elem).width();
			$(elem).css('margin-left', '-'+imgWidth/2+'px');
			$(elem).css('margin-top', '-'+imgHeight/2+'px');
		});
	},
	init: function() {
		var count = 0;
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 14);

			if (!pressImages.pages[page]) {pressImages.pages[page] = [];}
			pressImages.pages[page][x] = elem;
			
			count++;
		});
		
		pressImages.pageCount = Math.ceil(count / 14);

		for (x in pressImages.pages) {
			if (x > 1) {
				pressImages.instantHidePage(x);
			}
		}
		
		$('#next').click(function () {
			pressImages.goNext();
		});
		$('#prev').click(function() {
			pressImages.goPrev();
		});
		
		if (pressImages.pageCount > 1) {
			$('#next').show();
		}
	},
	instantHidePage: function(num) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 14);
			
			if (page == num) {
				$(elem).hide();
			}
		});
	},
	showPage: function(num) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 14);
			
			if (page == num) {
				$(elem).fadeIn('normal');
			}
		});
		pressImages.setupImages();
	},
	hidePage: function(num, callback) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 14);
			
			if (page == num) {
				$(elem).fadeOut('normal', callback);
			}
		});
	},
	hideCurrent: function(callback) {
		pressImages.hidePage(pressImages.currentPage, callback);
	},
	showCurrent: function() {
		pressImages.showPage(pressImages.currentPage);
	},
	goNext: function() {
		if (pressImages.pageCount >= (pressImages.currentPage + 1)) {
			pressImages.hideCurrent(function () {pressImages.showCurrent()});
			pressImages.currentPage++;
			
			$('#prev').show();
			if (pressImages.pageCount >= (pressImages.currentPage + 1)) {
				$('#next').show();
			} else {
				$('#next').hide();
			}
		}
	},
	goPrev: function() {
		if (pressImages.currentPage > 1) {
			pressImages.hideCurrent(function () {pressImages.showCurrent()});
			pressImages.currentPage--;
			
			if (pressImages.currentPage > 1) {
				$('#prev').show();
			} else {
				$('#prev').hide();
			}
			
			if (pressImages.pageCount >= (pressImages.currentPage + 1)) {
				$('#next').show();
			} else {
				$('#next').hide();
			}
		}
	}
}

var press = {
	elementCount: 0,
	pageCount: 0,
	currentPage: 1,
	pages: [],
	setupImages: function() {
		$('.items_wrapper .item .icon img').each(function(x, elem) {
			imgHeight = $(elem).height();
			imgWidth = $(elem).width();
			$(elem).css('margin-left', '-'+imgWidth/2+'px');
			$(elem).css('margin-top', '-'+imgHeight/2+'px');
		});
	
	},
	init: function() {
		
		var count = 0;
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 6);

			if (!press.pages[page]) {press.pages[page] = [];}
			press.pages[page][x] = elem;
			
			count++
		});
		press.pageCount = Math.ceil(count / 6);
		for (x in press.pages) {
			if (x > 1) {
				press.instantHidePage(x);
			}
		}
		
		$('#next').click(function () {
			press.goNext();
		});
		$('#prev').click(function() {
			press.goPrev();
		});
		
		if (press.pageCount > 1) {
			$('#next').show();
		}
	},
	hideAll: function() {
	
	},
	hideCurrent: function(callback) {
		press.hidePage(press.currentPage, callback);
	},
	showCurrent: function() {
		press.showPage(press.currentPage);
		
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 6);
			num = x - (page * 6 - 6);
			
			if (page == press.currentPage) {
				if (num < 2) {
					$(elem).addClass('first');
				}
			}
		});
	},
	showPage: function(num) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 6);
			
			if (page == num) {
				$(elem).fadeIn('normal');
			}
		});
		press.setupImages();
	},
	instantHidePage: function(num) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 6);
			
			if (page == num) {
				$(elem).hide();
			}
		});
	},
	hidePage: function(num, callback) {
		$('.items_wrapper .item').each(function (x, elem) {
			page = Math.ceil((x + 1) / 6);
			
			if (page == num) {
				$(elem).fadeOut('normal', callback);
			}
		});
	},
	goNext: function() {
		if (press.pageCount >= (press.currentPage + 1)) {
			press.hideCurrent(function () {press.showCurrent()});
			press.currentPage++;
			
			$('#prev').show();
			if (press.pageCount >= (press.currentPage + 1)) {
				$('#next').show();
			} else {
				$('#next').hide();
			}
		}
	},
	goPrev: function() {
		if (press.currentPage > 1) {
			press.hideCurrent(function () {press.showCurrent()});
			press.currentPage--;
			
			if (press.currentPage > 1) {
				$('#prev').show();
			} else {
				$('#prev').hide();
			}
			
			if (press.pageCount >= (press.currentPage + 1)) {
				$('#next').show();
			} else {
				$('#next').hide();
			}
		}
	}
}

