function showOverlay(screen, width, height)
{
	modalDialog = $('#Overlay').modal({

		overlayCss: {
	    backgroundColor: '#000',
	    cursor: 'wait'
	  },
	  containerCss: {
		width: '470px',
	    height: '250px',
	    backgroundColor: '#ffffff',
	    border: '2px solid #808080'
	  },
	  onShow: function(dialog) {

		  // close dailog
          dialog.data.find('.closeDialog').click(function () {
                  $.modal.close();
          });
		}
	});

	return false;
}

$(window).load (function() {
	$('.highlights_box .Products').cycle(highlightScroller);
});

var timerCloseSelectbox;
var objectSelectbox;

$(document).ready( function() {

	$('#viewForm input[name="add_new"]').click(function(e){
		var url = 'index.php?screen=dstore.account.new_memo_box';
		e.preventDefault();
		checkedTNIDs = $('#viewForm input[name="tnids[]"]:checked');
		for(i = 0; i < checkedTNIDs.length; i++)
			url += '&tnids[]='+$(checkedTNIDs[i]).attr('Value');
		openOverlay(url, 500, 400);
	});

	$('.memory_box_menu input[name="edit"]').css('display', 'none');

	$('.memory_box_menu select').change(function(){
		$(this).closest('form').submit();
	});

	$("#memoBoxMenuForm input[name='delete']").click(function(e){
		e.preventDefault();
		submitOverlayForm($("#memoBoxMenuForm"), null, 'delete.x=1')
	});

	$("#memoBoxForm input[name='send']").css('display', 'none');

	$("#memoBoxForm").submit(function(e){
		selectedValue = $('#memoBoxForm select option:selected').attr('value');
		if(selectedValue == 'send'){
			e.preventDefault();
			var dimensions = new Array();
			dimensions.push('500');
			dimensions.push('450');
			submitOverlayForm($("#memoBoxForm"), null, null, dimensions);
		}
	});

	// Overlay links
	$("a[rel^='#overlay']").click(function() {
		var dimensions = getOverlayDimensions($(this).attr('rel'));
		openOverlay($(this).attr('href'), dimensions[0], dimensions[1]);
		return false;
	});

	// Overlay forms
	$("form[rel^='#overlay-formsubmit']").submit(function() {
		submitOverlayForm($(this));
		return false;
	});

	$(".list_type .text_list").click(function(e) {
		e.preventDefault();
		if ( $(".productlist").hasClass("imagelist") ) {
			$(".productlist").removeClass("imagelist").addClass("textlist");
		}

		$.post(
			'index.php?screen=dstore.shop.ajax_productlisttype',
			{
				ClassName:'ProductListType',
	            type: 'textlist'
			}
		);
	});

	$(".list_type .image_list").click(function(e) {
		e.preventDefault();
		if ( $(".productlist").hasClass("textlist") ) {
			$(".productlist").removeClass("textlist").addClass("imagelist");

			$.post(
					'index.php?screen=dstore.shop.ajax_productlisttype',
					{
						ClassName:'ProductListType',
			            type: 'imagelist'
					}
				);
		}
	});

	$("#selectAll").click(function() {
		$(".imagelist_downloads input").attr('checked', 'true');
	});

	$("select[rel^='#Loading']").change( function(event) {
		event.stopImmediatePropagation();

		setGuidedNavigationSelectText($(this));
		var LoaderDiv = '<div class="AjaxLoading"><img src="http://www.pfarrbrief.de/customGates/resources/binaries/universal/icons/ajax_loading.gif" alt="" border="0" /></div>';

		if( $(this).closest('div.answers').next('.delete').length > 0 ) {
			$(this).closest('div.answers').next('.delete').remove();
		}

		$(this).closest('div.answers').after(LoaderDiv);
	});

	$("a[rel^='#Loading']").click( function(event) {
		event.stopImmediatePropagation();
		var LoaderDiv = '<div class="AjaxLoading"><img src="http://www.pfarrbrief.de/customGates/resources/binaries/universal/icons/ajax_loading.gif" alt="" border="0" /></div>';

		$(this).parent().parent().replaceWith(LoaderDiv);
	});

	$("div.DivSelect").each( function() {
		$(this).css('display', 'block');
	});

	$(".memo_select_box .DivSelect").each( function()
	{

		var txt = $(".selectbox .memoBoxName option:selected").text();

		if(txt != "Merkbox auswählen")
			$(this).children('.text').text(txt);
	});

	$("div.DivSelect").mouseover( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
	});

	$("div.DivSelect .background").mouseover( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
	});

	$("div.DivSelect ul").mouseover( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
	});

	$("div.DivSelect li").mouseover( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
	});

	$("div.DivSelect ul").mouseout( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
		timerCloseSelectbox = setInterval('closeCustomSelectBox()', 500);
	});

	$("div.DivSelect").mouseout( function() {
		objectSelectbox = $(this);
		window.clearInterval(timerCloseSelectbox);
		timerCloseSelectbox = setInterval('closeCustomSelectBox()', 500);
	});

	$("div.DivSelect").click( function() {
		// Die Kasse hasChange wird gesetzt falls auf ein li geklickt wurde
		// dann darf die Box nicht wieder auffahren
		if ($(this).children('.background').hasClass('hasChange') === false)
			$(this).children('.background').addClass('showIt');
		else
			$(this).children('.background').removeClass('hasChange');
	});

	$("div.DivSelect li").click( function() {
		id = $(this).attr('id').replace('DivOption_', 'SelectOption_');

		$('#'+id).attr('selected', 'selected');
		if ($('#'+id).closest('form').hasClass('jquerySubmit')) {
			$('#'+id).closest('form.jquerySubmit').submit();
		} else {
			$(this).closest('.DivSelect').children('.text').text($(this).text());
			if ($(this).hasClass('memobox_option'))
			{
				txt = $(this).text();
				merkbox = $("#previous_product").attr("href").match(/merkbox=([0-9A-Za-z]+)/);

				$("#previous_product").attr('href', function(i,a){

					if(merkbox)
						return a.replace( /(merkbox=)[a-z]+/ig, '$1'+txt );
					else
						return a+ "&merkbox=" +txt;
				});

				merkbox = $("#next_product").attr("href").match(/merkbox=([0-9A-Za-z]+)/);

				$("#next_product").attr('href', function(i,a){

					if(merkbox)
						return a.replace( /(merkbox=)[a-z]+/ig, '$1'+txt );
					else
						return a+ "&merkbox=" +txt;
				});

			}
		}
		$(this).closest('.DivSelect').children('.background').removeClass('showIt');
		$(this).closest('.DivSelect').children('.background').addClass('hasChange');
	});

	$("div.selectbox select").each( function() {
		setGuidedNavigationSelectText($(this));

		$(this).change( function() {
			setGuidedNavigationSelectText($(this));
		});
	});

	$('.login_container input.submit').css('margin-top', '-100000px');
	$('.login_container input.text').keyup(function(event){
		if(event.keyCode == 13)
			$(this).closest('form').submit();
		});

	$('.login_box input.text').keyup(function(event){
		if(event.keyCode == 13)
			$(this).closest('form').submit();
		});

	$('.sort_by select').change(function(event){
		$(this).closest('form').submit();
		});


	$('.checkall').click(function () {
		$(this).parents('fieldset:eq(0)').find(':checkbox').attr('checked', this.checked);
		});

	$('#checkbox_institution').change(function() {
		if($(this).attr('checked'))
			$('#field_instituton').css('display', 'block');
		else
			$('#field_instituton').css('display', 'none');
		});


}); // $(document).ready( function()

function closeCustomSelectBox() {
	objectSelectbox.closest('.DivSelect').children('.background').removeClass('showIt');
}

highlightScroller = {
    speed:  'fast',
    timeout: 0,
    prev: '#highlights-prev',
    next: '#highlights-next',
    after: function(currSlideElement, nextSlideElement, options, forwardFlag){
    	// Bilder müssen evtl. nachgeladen werden
    	pages = $('.highlights_box .Products').children();
    	images = $(pages[options.currSlide]).children();
		if($(images[0]).find('img').attr('src') == ''){
			for(i = 0; i < images.length; i++){
				bcid = $(images[i]).find('img').attr('id');
				binaryUrl = 'http://www.pfarrbrief.de/customGates/scripts/binary.php?BinaryCodeID='+bcid
					+'&type=list'
				$(images[i]).find('img').attr('src', binaryUrl);
			}
		}

    	// Anzeige der unteren Navi
    	for(i = 0; i < options.slideCount; i++){
			if(options.currSlide != i){
				$('.page_'+i).removeClass('active');
				$('.page_'+i).addClass('inactive');
			}
    	}
		$('.page_'+options.currSlide).addClass('active');
		$('.page_'+options.currSlide).removeClass('inactive');

		//overlay bei mouseover anzeigen
		$('.highlights_box .Products .image').mouseover(function(){

			if($(this).find('img').attr('id') != ''){
				$('.highlights_box .overlay img').attr('src', 'http://www.pfarrbrief.de/customGates/scripts/binary.php?BinaryCodeID='+
						$(this).find('img').attr('id')+'&type=overlay');
				$('.highlights_box .overlay').css('display', 'block');
			}
		});

		$('.highlights_box .Products .image').mouseout(function(){
			$('.highlights_box .overlay').css('display', 'none');
		});

    },
    pagerAnchorBuilder: function(idx, slide) {

    	return '.highlights_box .page_' + idx;
    }
}

function updateMemoboxMenu(memoBoxName){
	urlParams = location.href.split('?');
	if(urlParams.length > 1){
		getParams = urlParams[1].split('&');
		newUrl = urlParams[0]+'?';
		memoboxNameSet = false;
		for(i = 0; i < getParams.length; i++){
			if(!getParams[i].match(/memoBoxName=/)){
				newUrl += getParams[i]+'&';
			}
			else{
				memoboxNameSet = true;
				newUrl += 'memoBoxName='+memoBoxName+'&';
			}
		}
		if(!memoboxNameSet && memoBoxName != '')
			newUrl += 'memoBoxName='+memoBoxName;

	}
	else{
		newUrl = urlParams[0]+'?memoBoxName='+memoBoxName;
	}
	window.location.href = newUrl;
}

function setGuidedNavigationSelectText(selectbox)
{
	if( selectbox.val() == '' )
		return true;

	selectbox.siblings('div.text').text(selectbox.children('option:selected').text());
}

/***********
* Misc     *
***********/
function isPositionFixedSupported()
{
  var isSupported = null;
  if (document.createElement) {
      var el = document.createElement("div");
      if (el && el.style) {
          el.style.width = "1px";
          el.style.height = "1px";
          el.style.position = "fixed";
          el.style.top = "10px";
          var root = document.body;
          if (root && root.appendChild && root.removeChild) {
              root.appendChild(el);
              isSupported = el.offsetTop === 10;
              root.removeChild(el);
          }
          el = null;
      }
  }
  return isSupported;
}
