// JavaScript Document
$(document).ready(function() {
	initPresskit();
	initPresskitTrigger();
});
$(window).resize(function() {
	$y = ($(window).height() / 2) - ($('.popup_presskit').height() / 2) + $(window).scrollTop();
	//$y = 0;
	$x = ($(window).width() / 2) - ($('.popup_presskit').width() / 2) + $(window).scrollLeft();
	$('.popup_presskit').css({
		'left':$x+'px',
		'top':$y+'px'
	});
	$('#popup_black').css({
		'width':Math.max($(window).width(),$(document).width())+'px',
		'height':Math.max($(window).height(),$(document).height())+'px'
	});
});
$(window).scroll(function() {
	$(window).resize();
});
function initPresskitTrigger() {
	$('.presskitbox-link a').click(presskitTrigger);
}
function presskitTrigger(e) {
	e.preventDefault();
	$pk = $(this).attr('href');
	$.get($pk, {
			type:3173
		}, function(data){
			$('body').append('<div id="popup_black"></div><div class="popup_presskit">' + data + '<div>');
			//$('#content-left').append('<div class="popup_presskit">' + data + '<div>');
			//$('#content-left').append(data);
			$(window).resize();
			$('#popup_black').bind('click', function() {
				$('#popup_black').fadeOut('fast', function() {
					$(this).remove();
				});
				$('.popup_presskit').fadeOut('fast', function() {
					$(this).remove();
				});
			});
			$('#popup_black').fadeTo('fast',0.5, function() {
				$('.popup_presskit').css('display','block');
				//$('.popup_presskit').fadeTo('fast',1);
			});	
			//$('.popup_presskit').fadeTo('fast',1);
			initPresskit();
		});
	
	//$(body).append('');
}
function initPresskit() {
	$presskitIndex = 0;
	$('.presskit-archive-list').after('<a href="#" class="presskit-archive-left">&lt;</a><a href="#" class="presskit-archive-right">&gt;</a>');
	setArchiveArrows();
	$('.presskit-nav ul li a').click(presskitNav);
	$('.presskit-archive-list ul li a').click(presskitNav);
	$('.popup_presskit .presskit .presskit-title').css('position','relative').append('<a href="#">Schließen</a>').find('a').click(function() {
		$('#popup_black').click();
	});
}
function presskitNav(e) {
	e.preventDefault();
	$(this).css('outline','none');
	$href = $(this).attr('href');
	//$isQ = ($hrefpure+'').indexOf('?');
	//$divide = $isQ>=0 ? '&' : '?';
	//$href = $hrefpure + $divide + 'type=3172';
	//$hrefmedia = $hrefpure + $divide + 'type=3174';
	$container = $(this).parents('.presskit').find('.presskit-content');
	$containerd = $(this).parents('.presskit').find('.presskit-footer-item-download .presskit-footer-item-content');
	$('.presskit-nav ul li').removeClass('li-active');
	$('.presskit-archive-list ul li').removeClass('li-active');
	
			$('.presskit-nav ul li').each(function() {
				$a = $(this).find('a');
				$ahref = $a.attr('href');
				if($ahref == $href) {
					$(this).addClass('li-active');
				}
			});
			$('.presskit-archive-list ul li').each(function() {
				$a = $(this).find('a');
				$ahref = $a.attr('href');
				if($ahref == $href) {
					$(this).addClass('li-active');
				}
			});
	
	$container.clearQueue().fadeTo('fast',0,function() {
		$.get($href, {
			type:3172
		}, function(data){
			$container.html(data);
			$container.clearQueue().fadeTo('fast',1, function() {
				$(this).removeAttr('filter');
				$(this).css('opacity','');
			});
		});
	});
	$containerd.clearQueue().css('zoom','1').fadeTo('fast',0,function() {
		$.get($href, {
			type:3174
		}, function(data){
			$containerd.html(data);
			$containerd.clearQueue().fadeTo('fast',1, function() {
				$(this).removeAttr('filter');
				$(this).css('opacity','');
			});
		});
	});
}
function moveArchive($trigger, dir) {
	$('.presskit-archive-left').unbind('click');
	$('.presskit-archive-right').unbind('click');
	$('.presskit-archive-left').click(function(e) {
		e.preventDefault();
	});
	$('.presskit-archive-right').click(function(e) {
		e.preventDefault();
	});
	$list = $trigger.parents('.presskit-archive').find('.presskit-archive-list ul');
	$lis = $list.find('li');
	$f = dir=="+" ? 1 : -1;
	$c = $lis.length;
	$presskitIndex = $presskitIndex + $f;
	$presskitIndex = $presskitIndex>=$c-1 ? $c-1-1 : $presskitIndex;
	$presskitIndex = $presskitIndex<0 ? 0 : $presskitIndex;
	$x = 0 - $presskitIndex*93;
	$list.clearQueue().animate({
			'left':$x+'px'
		}, 'fast', setArchiveArrows);
}
function setArchiveArrows() {
	$('.presskit-archive-left').unbind('click');
	$('.presskit-archive-right').unbind('click');
	$('.presskit-archive-left').click(function(e) {
		e.preventDefault();
		moveArchive($(this),'-');
	});
	$('.presskit-archive-right').click(function(e) {
		e.preventDefault();
		moveArchive($(this),'+');
	});
}
