$(document).ready(function(){
	$('a.iframe').fancybox({
			'width' : '75%',
			'height' : '75%',
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'type' : 'iframe'		
	
	});
	$('img.lightbox').each(function(index) {
		var imgurl =  $(this).attr('src');
		if (imgurl.indexOf('image.php?src=') > 0) {
			imgurl = (imgurl.split('image.php?src='))[1];
			imgurl = (imgurl.split('&'))[0];
		}
		$(this).wrap('<a class="fancybox" href="'+ imgurl +'"></a>');
	});	
	
	$('a.fancybox').each(function(index) {
		$(this).fancybox();	
	});
	
	$('ul#slider').before('<div id="slidernav">') 
	$('ul#slider').cycle({
		fx: 	 'fade',
		timeout: 20000,
		speed:   1000,
		pause:   1,
		pager:  '#slidernav' ,
		prev:   '#slprev', 
    	next:   '#slnext', 
	}); 
	
	
	/* Show/Hide Filter*/
	$("a#filter").click(function(event){
		if ($(this).hasClass('active')) {
			$(this).removeClass('active');
			$("form#filterform").hide('fast');
			
		} else {
			$(this).addClass('active');
			$("form#filterform").show('');
		}		
		event.preventDefault();
	});
	getalliframes();
	
	if ($('#filterform')) {
		$.post('index.php?id=16', $('#filterform').serialize(),
            function(data) {
       			$('#ajaxoutput').html(data);
        	},
        	'html'
    	);
		getalliframes();
	}
	
	$('#filterform').submit(function(e) {
    	$.post('index.php?id=16', $('#filterform').serialize(),
            function(data) {
       		$('#ajaxoutput').html(data);
        	},
        	'html'
        );
		getalliframes();
        e.preventDefault();
    })
	$('#filterform input.reset').click(function(e) {
    	clearform();
		getalliframes();
    });
		
	
	$('#heading span.name strong').click(function(event){
		$('form#filterform input[name|=sorter]').val('pagetitle'); 
		if ($(this).hasClass('asc')) {
			$(this).removeClass('asc');
			$(this).addClass('desc');
			$('form#filterform input[name|=direction]').val('DESC');
		} else if ($(this).hasClass('desc')) {
			$(this).removeClass('desc');
			$(this).addClass('asc');
			$('form#filterform input[name|=direction]').val('ASC');
		}	
		$.post('index.php?id=16', $('#filterform').serialize(),
			function(data) {
				$('#ajaxoutput').html(data);
			},
			'html'
		);
		getalliframes();
		event.preventDefault();	
	});
	
	$('#heading span.loc strong').click(function(event){
		$('form#filterform input[name|=sorter]').val('firma-plz');
		if ($(this).hasClass('asc')) {
			$(this).removeClass('asc');
			$(this).addClass('desc');
			$('form#filterform input[name|=direction]').val('DESC');
		} else if ($(this).hasClass('desc')) {
			$(this).removeClass('desc');
			$(this).addClass('asc');
			$('form#filterform input[name|=direction]').val('ASC');
		}
		$.post('index.php?id=16', $('#filterform').serialize(),
			function(data) {
				$('#ajaxoutput').html(data);
			},
			'html'
		);
		getalliframes();
		event.preventDefault();
	});
	
});

function clearform() {
	var docurl = document.URL;
	var temp = new Array();
	temp = docurl.split('?')
	docurl = temp[0];
	window.location = docurl;
};


function getalliframes() {
	
	setTimeout(function() {
		var pagenum = parseInt( $('form#filterform input[name|=pagenum]').val() );
		$('#ajaxoutput #ditto_pages a.ditto_previous_link').click(function(event){
			if (pagenum > 0) {
				$('form#filterform input[name|=pagenum]').val((pagenum - 1));
				$.post('index.php?id=16', $('#filterform').serialize(),
					function(data) {
						$('#ajaxoutput').html(data);
					},
					'html'
				);
				getalliframes();
			}
			event.preventDefault();
		});
		$('#ajaxoutput #ditto_pages a.ditto_next_link').click(function(event){
			
				$('form#filterform input[name|=pagenum]').val((pagenum + 1));
				$.post('index.php?id=16', $('#filterform').serialize(),
					function(data) {
						$('#ajaxoutput').html(data);
					},
					'html'
				);
				getalliframes();
			
			event.preventDefault();
		});
		$('#ajaxoutput #ditto_pages a.ditto_page').each(function(index) {
			$(this).click(function(event){
					$('form#filterform input[name|=pagenum]').val((parseInt($(this).html())-1));
					$.post('index.php?id=16', $('#filterform').serialize(),
						function(data) {
							$('#ajaxoutput').html(data);
						},
						'html'
					);
					getalliframes();
				
				event.preventDefault();
			});
		});
	
	}, 300 );
	
	setTimeout(function() {
    	
		$('a.iframe').fancybox({
			'width' : '75%',
			'height' : '75%',
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'type' : 'iframe'
		});
		$('a.getcontent').fancybox({
			'autoScale' : true,
			'transitionIn' : 'none',
			'transitionOut' : 'none'
		});
		
		
  	}, 750 );
};

function fancyyframe() {
	$(this).fancybox({
			'width' : '75%',
			'height' : '75%',
			'autoScale' : false,
			'transitionIn' : 'none',
			'transitionOut' : 'none',
			'type' : 'iframe'
	});
}



function printSelection(node){
  var content=node.innerHTML
  var pwin=window.open('','print_content','width=400,height=300');
  pwin.document.open();
  pwin.document.write('<html><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
  setTimeout(function(){pwin.close();},1000);
}

