$(document).ready(function(){
	$("ul.tabNav a").live("click", function(){
		var curChildIndex = $(this).parent().prevAll().length + 1;
		$(this).parent().parent().children('.active').removeClass('active');
		$(this).parent().addClass('active');
		$(this).parent().parent().next('.tabContainer').children('.active').slideUp('fast',function() {
			$(this).removeClass('active');
			$(this).parent().children('div:nth-child('+curChildIndex+')').slideDown('normal',function() {
				$(this).addClass('active');
			});
		});
		return false;
	});
	
	// Publication Poll popup
	$("a[targetUrl]").docPoll();
	
	// applicationForm
	
	// hide more uploads link on click and display additional upload fields.
	$("#applicationForm fieldset.uploads a").live("click", function(){
		// hide label and link and make span with upload fields visible
		$(this).parent().hide().next().show();
		return false;
	});
	// trigger click on more uploads link on page init (to display upload fields),
	// if additional uploads had been done
	$("#applicationForm fieldset.uploads input[type=hidden][name!=file1]").each(function (i) {
		$("#applicationForm fieldset.uploads a").trigger("click");
		return false;
	});

	init();
});

function init(context) {
	$("form [title]", context).each(function (i) {
		var presetValue = $(this).attr('title');
		if(!this.value && presetValue) {
			$(this).attr('value', presetValue);
		}
	});
	
	$("form [title]", context).focus(function () {
		var presetValue = $(this).attr('title');
		if(this.value == presetValue) {
			$(this).attr('value', "");
		}
		return false;
	});
	
	$("form [title]", context).blur(function () {
		var presetValue = $(this).attr('title');
		if(!this.value && presetValue) {
			$(this).attr('value', presetValue);
		}
		return false;
	});
	
	$("form", context).submit(function () {
		$("[title]", this).each(function (i) {
			var presetValue = $(this).attr('title');
			if(this.value == presetValue) {
				$(this).attr('value', "");
			}
		});
		return true;
	});
	
	$("form[ajaxAction]", context).submit(function () {
	  
	  // NOTE: eForm only works if a post-request has been issued.
	  // jQuery.load() issues post requests if the data-parameter has a key value map
	  // as obtained via serializeArray(). serialize() will result in a get request and won't work!!
	  var values = $("form").serializeArray();
		
	  $(this).parent().load($(this).attr('ajaxAction'), values, function() {
			init(this);
		});
		
		return false;
	});
	
  // contactForm
  
	var cbContainer = $('#contactForm #callbackContainer');
  // show/hide fields according to checkbox-state
  if($('#contactForm #callback').is(':checked')) {
    cbContainer.show();
  }
  else {
    cbContainer.hide();
  }
  // attach eventhandler to show/hide fields
  $('#contactForm #callback', context).click(function () {
    if($('#contactForm #callback').is(':checked')) {
      cbContainer.show();
    }
    else {
      cbContainer.hide();
    }
  });
  
  // Drucken Popup
  $('a.print:not([targetUrl])').popupWindow({
		width: 760,
		height: 600,
		centerScreen: 1,
		scrollbars: 1,
		menubar: 1
	});
}

// DOC POLL

(function($){ 		  
	$.fn.docPoll = function(instanceSettings){
		return this.each(function(){
			$(this).click(function(){
				$.fn.docPoll.defaultSettings = {
					docId: $(this).attr('docId'),
					usageType: $(this).attr('class'),
					targetUrl: $(this).attr('targetUrl')
				};
				
				settings = $.extend({}, $.fn.docPoll.defaultSettings, instanceSettings || {});
				
				$("#modalPopup div").load($(this).attr('pollUrl'), settings, function() {
					$.blockUI({
						message: $('#modalPopup'),
						css: {
							border: 'none',
							cursor: 'default'
						},
						overlayCSS:  {  
							opacity: 0.8 
						}
					});
					init($('#modalPopup'));
				});
			
				return false;
			});
		});	
	};
})(jQuery);