var overlay;

$(document).ready(function(){

	// the overlay function for the contactForm
    $('.overlay-catalog-opener').click(function(){
		var _url = $(this).attr('href');
		$.nyroModalManual({
			modal: true,
			closeButton: '<a href="#" class="nyroModalClose" id="closeBut">Close</a>',
			bgColor: '#000000',
			cssOpt: {
				bg: {
					'opacity': '0.20'
				},
				content : {
					'overflow-y': 'auto'
				}
			},
			url : '/layout/set/ajax' + _url,
			endFillContent : function(elts, settings){
				var form = elts.content.find('form');
				$(form).addClass('nyroModal').attr('action', '/layout/set/ajax' + _url + '?mode=ajax');
				// cause of modal is false insert the close-button manually
				$(elts.wrapper).after(settings.closeButton);
			},
			ajax :{
				data: 'mode=ajax'
			},
			endShowContent : function(){
				tooltips();
				$('#additional_data-from_page').val(window.location);
				$('li.error input:first').focus();
			},
			width: 805,
			formIndicator: 'submit_contact_haus_at',
			zIndexStart:5000,
			wrap: {
			  ajax: '<div class="wrapper contact"></div>',
			  form: '<div class="wrapper contact"></div>'
			}
		});
		return false;
	});

	// the overlay function for send_bookmarks
	$('#sendBookmarks').click(function(){
		var _url = $(this).attr('href');
		$.nyroModalManual({
			modal: true,
			closeButton: '<a href="#" class="nyroModalClose" id="closeBut">Close</a>',
			bgColor: '#000000',
			cssOpt: {
				bg: {
					'opacity': '0.20'
				},
				content : {
					'overflow-y': 'auto'
				}
			},
			ajax :{
				data: 'mode=ajax'
			},
			url : '/layout/set/ajax' + _url,
			endFillContent : function(elts, settings){
				var form = elts.content.find('form');
				$(form).addClass('nyroModal').attr('action', '/layout/set/ajax' + _url + '?mode=ajax');
				// cause of modal is false insert the close-button manually
				$(elts.wrapper).after(settings.closeButton);
			},
			endShowContent : function(){
				tooltips();
			},
			width: 450,
			formIndicator: 'submit_send_bookmarks',
			zIndexStart:5000,
			wrap:{
				ajax:'<div class="wrapper sendBookmarks"></div>',
				form: '<div class="wrapper sendBookmarks"></div>'
			}
		});
		return false;
	});

	$('div.search-result table tr').hover(
		function () {
		  $(this).addClass("hover");
		},
		function () {
		  $(this).removeClass("hover");
		}
	);

	$('div.search-result table tr').click(function () {
		if ($(this).find('a:first').attr('href')) {
			window.location = $(this).find('a:first').attr('href');
		}
		return false;
    });

	 $('div.catalog-slide').hover(function () {
		$('div.catalog-slide div.heading span').addClass('active');
		$('div.catalog-slide div.frame').slideDown(400);
	}, function () {
		$('div.catalog-slide div.heading span').removeClass('active');
		$('div.catalog-slide div.frame').slideUp(400);
	});


	function tooltips(){
	// the tooltip function for the forms
		$("form :input[title!='']").tooltip({
			position: "center right",
			// position is now direct at form field
			relative: false,
			opacity: 1,
			effect: "fade",
			offset: [0,10],
			lazy: false,
			tip: '.tooltip'
		});
	}




	// bookmark a page via Ajax
    $('#bookmarkAdd').click(function(){
		var url = $(this).attr('href');
		var docTitle = $(this).attr('name');
		var mailTitle = $(this).attr('title');
        // loader image
        $('div.bookmark-this-page').addClass('loading');
        $.getJSON(url, { title:docTitle, mailTitle:mailTitle}, function(data, textStatus){
            // insert the number of marks
            $('a#bookmarkCount span').text(data.count);

            var toInsert = data.toInsert;
            $('#bookmarkList').append(toInsert);

            // show the bookmarks
            if(!$('.private').is('.active')){
                $('a#bookmarkCount').click();
            }
            if(data.status == 'double'){
                $('#bookmarkList li.double').wait().fadeOut(2000, function(){
                    $(this).remove();
                });
            }
            // remove loader image
            $('.bookmark-this-page').removeClass('loading');

        });
        return false;
    });

    // the function also for the div around the link
    $('.bookmark-this-page').click(function(){
        return $('#bookmarkAdd').click();
    }).hover(function(){
        $(this).css('cursor', 'pointer');
    }, function(){
        $(this).css('cursor', 'default');
    });


    // remove bookmark via Ajax
    $('.bookmarkRemove').live('click', function(){
        var url = $(this).attr('href');
        var parent = $(this).parent();
		// loader image
        $('.bookmark-this-page').addClass('loading');
        $.getJSON(url, function(data, textStatus){
            $('a#bookmarkCount span').text(data.count);
            parent.remove();
            if($('#bookmarkList li').length <  1 && $('.private').is('.active')){
                $('a#bookmarkCount').click();
            }
            // remove loader image
            $('.bookmark-this-page').removeClass('loading');
        });
        return false;
    });




    // bookmarks
	$("li.bookmark a").click(function(event){
		event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var title = document.title;
        var url = location.href;

		if (window.sidebar) { // Mozilla Firefox
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title);
		} else { // for Safari, Konq etc - browsers who do not support bookmarking scripts (that i could find anyway)
			 alert('Please bookmark this page manually.');
		}
        return false;
	});
    $('li.bookmark').click(function(){
        return $("li.bookmark a").click();
    }).hover(function(){
        $(this).css('cursor', 'pointer');
    }, function(){
        $(this).css('cursor', 'default');
    });


    // social bookmarks
	$('.social-bookmarks').hover(function() {
		$('.social-bookmarks-list').stop().animate({'height':25}, 300);
	},	function() {
		$('.social-bookmarks-list').stop().animate({'height':0}, 500);
	});


	// print
	if (jQuery.url.param('mode') == 'print') {
        if (jQuery.url.param('overlay') == 'true') {
            $('div.content').html($('div.overlay').html());
        }
        window.setTimeout('window.print()', 1000);
    }

    // set js-back links visible
    $('div.js-back').css('display', 'block');


    overlay = $('.pdf-opener[rel=#overlay]').overlay({
        expose: '#333333',
        effect: 'apple',
        api: true,
        onBeforeLoad: function() {
            var wrap = this.getContent().find(".contentWrap");
            wrap.load(this.getTrigger().attr("href"));
        },
        onClose: function() {
			var wrap = this.getContent().find(".contentWrap");
			wrap.html('');
        }
    });
});


function closeOverlay() {
	overlay.close();
}