
/* Slider */
(function($){
	$.fn.slider = function(options){
		var settings = jQuery.extend({
			speed: 1000,
			slideWidth: 918,
			moveElement: '.slides',
			prevButton: 'a.prev',
			nextButton: 'a.next',
			slides: '.slide'
		}, options);
		
		//each slider
		$(this).each(function(){
			
			var currentSlide = 0,
				slides = $(settings.slides, this),
				movable = $(settings.moveElement, this),
				slidesCount = slides.length;
			
			function showPrevious(){
				(currentSlide == 0)?showSlide(slidesCount - 1):showSlide(currentSlide - 1);
			}
			
			function showNext(){
				(currentSlide == (slidesCount-1))?showSlide(0):showSlide(currentSlide + 1);
			}
			
			function showSlide(nr){
				currentSlide = nr;
				movable.stop(true).animate({'left': (-1 * nr * settings.slideWidth) + 'px'});		
			}
			
			$(settings.prevButton, this).click(function(e){
				showPrevious();
				e.preventDefault();
			});
			$(settings.nextButton, this).click(function(e){
				showNext();									
				e.preventDefault();
			});	
			
			movable.css({'left':'0px'});
			
		});
	};
})(jQuery);

/* Modal window */
(function($){
	$.fn.galleryBox = function(options){
		var settings = jQuery.extend({
			overlay: '#overlay',
			window: '.modalWindow',
			content: '.modalContent',
			setContentHeight: '.modalContent',
			prevButton: 'a.prev',
			nextButton: 'a.next',
			closeButton: 'a.close'
		}, options);	
		var itemList = $(this), visible = false, currentItem = null, currentImg = null;
		
		$(settings.overlay + ',' + settings.closeButton).click(function(){ 
			closeModal(); 
		});
		$(settings.prevButton, $(settings.window)).click(function(e){
			showPrevious();
			e.preventDefault();
		});
		$(settings.nextButton, $(settings.window)).click(function(e){
			showNext();									
			e.preventDefault();
		});	
		
		//each image
		itemList.each(function(i){
			var j = i;
			$(this).click(function(e){
				showModal(j);
				e.preventDefault();
			});			
		});
		
		function initModal(i){
			/* overlay */
			//$('body').css({'overflow-x':'hidden'});
			setOverlaySize();
			$(settings.overlay)
				.css({opacity: 0 })
				.show()	
				.animate( { opacity: 0.9 }, 200 );	
			$(window).resize(setOverlaySize);
			setOverlaySize();
			if(i == 0){
				$(settings.prevButton, $(settings.window)).css({'width':'0px','left':'0px'});	
			} else {
				$(settings.prevButton, $(settings.window)).css({'width':'35px','left':'-45px'});
			}
			if(i == (itemList.length-1)){
				$(settings.nextButton, $(settings.window)).css({'width':'0px','right':'0px'});
			} else {
				$(settings.nextButton, $(settings.window)).css({'width':'35px','right':'-45px'});
			}
			/* window */
			$(settings.window).css({'margin-left': '-100px'});
			$(settings.setContentHeight).css({width: '200px',height:'150px'}).addClass('loading');
			$(settings.window).show();
			visible = true;
		}
		
		function showModal(i){
			if(!visible)
				initModal(i);
			
			$(settings.setContentHeight).addClass('loading');
			if(currentImg != null){
				$(currentImg).fadeOut();
			} else {
				$(settings.window).animate({'margin-left': '-100px'});
				$(settings.setContentHeight).animate({width: '200px', height:'150px'});
			}
			currentItem = i;
			
			var newImg = $('<img src="' + $(itemList.get(currentItem)).attr('href') + '" />');
			currentImg = newImg;
			newImg
			.css({ visibility: 'hidden'})
			.load(function(){
				var image = this;
				$(settings.window).animate({
					'margin-left': '-'+ (($(this).width())/2) + 'px',
					'margin-top': '-'+ (($(this).height())/2) + 'px'
				});
				$(settings.setContentHeight)
					.removeClass('loading')
					.animate({width: ($(this).width()) + 'px',height: ($(this).height()) + 'px'}, function(){
						$(image).css({visibility:'visible',display:'none'}).fadeIn();
						setOverlaySize();
						//$(settings.window).center({ horizontal: false });
					});
			});
			$(settings.content).html(newImg);
						
			if(currentItem > 0){
				$(settings.prevButton, $(settings.window)).animate({'width':'35px','left':'-45px'});
			} else {
				$(settings.prevButton, $(settings.window)).animate({'width':'0px','left':'0px'});		
			}
			if(currentItem < (itemList.length-1)){
				$(settings.nextButton, $(settings.window)).animate({'width':'35px','right':'-45px'});
			} else {
				$(settings.nextButton, $(settings.window)).animate({'width':'0px','right':'0px'});
			}
		}
		
		function showNext(){
			if(itemList.length == currentItem+1)
				return;
			showModal(currentItem+1);
		}
		
		function showPrevious(){
			if(currentItem == 0)
				return;
			showModal(currentItem-1);
		}
	
		function closeModal(){		
			$(settings.overlay + ',' + settings.window).fadeOut();
			$(window).unbind('resize', setOverlaySize);
			$('body').css({'overflow-x':'auto'});
			visible = false;
		}
		
		function setOverlaySize(){	
			var widthHeight = viewport();						
			$(settings.overlay).css({
				width: widthHeight[0],
				height: widthHeight[1]
			});
		}
	
		function viewport() {
			if ($.browser.msie) { 
				// if there are no scrollbars then use window.height
				var d = $(document).height(), w = $(window).height();
			
				return [
					window.innerWidth || 						// ie7+
					document.documentElement.clientWidth || 	// ie6  
					document.body.clientWidth, 					// ie6 quirks mode
					d - w < 20 ? w : d
				];
			} 
			// other well behaving browsers
			return [$(window).width(), $(document).height()];
		}
	};
})(jQuery);

$(function(){
	Cufon.replace('#menu a', { fontFamily: 'Century Gothic', hover: true });
	Cufon.replace('.contentBlock h1', { fontFamily: 'Century Gothic' });
	Cufon.replace('.openTimesColumn address, .openTimesColumn p', { fontFamily: 'Century Gothic' });
	
	$('.slider').slider();
	$('.gallery .image').galleryBox();
	
	/* gallery */
	$('.gallery .image').mouseover(function(){
		$('img', this).stop(true).animate({'opacity':'0.5'});
	}).mouseout(function(){
		$('img', this).stop(true).animate({'opacity':'1'});
	});
	 
	 /* archive dropdown */
	 var archiveMenu = $('.archiveMenu');
	 var archiveMenuLists = $('ul',archiveMenu).length;
	 archiveMenu.css('width', (archiveMenuLists * 120) + 'px');
	 var showArchiveMenu = false;
	 $('.showArchiveMenu').mouseenter(function(){
		showArchiveMenu = true;
		var offset = $(this).offset();
		archiveMenu.css({
			'left': (offset.left + $(this).outerWidth() - (archiveMenuLists * 120)) + 'px',
			'top': (offset.top + 60) + 'px'
		});
		archiveMenu.show();
		$(this).addClass('active');
	 }).mouseleave(function(){
		showArchiveMenu = false;
	 	setTimeout(function(){ if(!showArchiveMenu){ $('.showArchiveMenu').removeClass('active'); archiveMenu.hide(); } },50);
	 });
	 archiveMenu.mouseenter(function(){
	 	showArchiveMenu = true;
	 }).mouseleave(function(){
		 showArchiveMenu = false;
	 	 setTimeout(function(){ if(!showArchiveMenu){ $('.showArchiveMenu').removeClass('active'); archiveMenu.hide(); } },50);
	 });
	 
	/* fill */
	$('table.form input, table.form textarea').each(function(){
		$(this).data('placeholder', $(this).attr('title'))
		if($(this).val() == ''){
			$(this).val($(this).data('placeholder'));			
		}
		
		$(this)
			.blur(function(){
				if($(this).val() == ''){
					$(this).val($(this).data('placeholder'));			
				}	
			})
			.focus(function(){
				if($(this).val() == $(this).data('placeholder')){
					$(this).val('');			
				}
			});
	});
	$('form').submit(function(){
		$('input, textarea').each(function(){
			if($(this).val() == $(this).data('placeholder')){
				$(this).val('');			
			}
		});
	});
});