// Center function

jQuery.fn.center = function () {
    this.css("position","fixed");
    this.css("top", ( $(window).height() - this.outerHeight() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.outerWidth() ) / 2+$(window).scrollLeft() + "px");
    return this;
}



$(document).ready(function(){

	// Padding calculator for subcategories
	var subcategories = $('.subcategories');
	
	if(subcategories.length > 0)
	{
		var page_inner = $('#page .inner');
		var newpadding = ((parseInt(page_inner.css('padding-top')) + subcategories.outerHeight()) / 10) + 2;
		$(page_inner).css('padding-top', newpadding + "em");
	}

	// Tabbox

	var tabs = $('.tabbox .tab');
	var tabcontent = $('.tabbox .tab-content');

	$(tabs).click(function(event){
		event.preventDefault();
		var index = $(tabs).index(this);
		$(tabs).removeClass('tab-active');
		$(tabs[index]).addClass('tab-active');
		$(tabcontent).removeClass('tab-content-active');
		$(tabcontent[index]).addClass('tab-content-active');
	});

	// Galery
	var gallery = $('.gallery');
	var gal_thumbs;
	var page_size = 3;
	var picture_count;
	var total_pages;

	gallery.each(function(index, item) {

		gal_thumbs = $('.entry', this);
		picture_count = $(gal_thumbs).length;

		// scrolling pages

		if(picture_count > page_size)
		{
			// total pages
			total_pages = Math.ceil(picture_count / page_size);

			// gallery wrap width
			$('.wrap', this).css('width',(total_pages * 64.5)+'em');

			// add pagination div
			$(this).prepend('<div class="pages"></div>');

			// create links
			for(i=0;i<total_pages;i++) {
				$('.pages', this).append('<span class="page">&bull;</span>');
			}

			// make first active
			$('.pages span:first', this).addClass('active');

			// pagination buttons click
			var page_links = $('.page', this);

			$(page_links).click(function(event){

				event.preventDefault();

				var gotopage = ($(page_links).index(this)+1);
				var index = $(page_links).index(this);

				$(page_links).removeClass('active');
				$(page_links[index]).addClass('active');

				$(this).parent().parent().find('.wrap').animate({left: (index * (-64.5)+'em')}, 500);

			});
		}

	});


	// Fancybox Links

	$("a[rel=galery]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Bild ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});


	// Remove Modal
	$('#overlay').live('click', function(event){
		event.preventDefault();
		$('#modal').remove();
		$('#overlay').remove();	
	});

	$('.close-modal').live('click', function(event){
		event.preventDefault();
		$('#modal').remove();
		$('#overlay').remove();	
	});

});


function initmodal(event) {

  event.preventDefault();

  $('body').append('<div id="overlay"></div>');
  $('body').append('<div id="modal"></div>');
  $('#overlay').css('opacity','.5')
  $('#overlay').fadeIn();

  $(window).bind({
    scroll: function() {
      // $('#overlay').center();
      // $('#modal').center();
    },
    resize: function() {
      // $('#overlay').center();
      // $('#modal').center();
    }
  });
}

// Center Modal
function centermodal(event) {
  // $('#overlay').center();
  $('#modal').center();
}



