// AddThis
var addthis_config = { 'data_track_clickback': true, 'services_compact': 'email, gmail, facebook, twitter, digg, delicious, print, more' };

$(document).ready(function() {
	if($.support.opacity) $('.button').corner('4px');
	
	// Menu
	$('ul.sf-menu').supersubs({ 
        minWidth:    12, 
        maxWidth:    20, 
        extraWidth:  .5 
        }).superfish({
    		delay:		 250,
    		animation:	 {height:'show'},
    		speed:		 'fast' });
	
	// Search
	$('.txt-search').keypress(function(event) {
		if (event.keyCode == '13') {
			$('.search-form').ajaxSubmit({ success: searchSuccess });
			return false;
		}
	});
	
	$('.btn-search').click(function() {
		$('.search-form').ajaxSubmit({ success: searchSuccess });
	});
	
	$('.btn-advanced').click(function() {
		if ($('.advanced').css('display') == 'none')
		  $('.advanced').slideDown('fast');
		else
		  $('.advanced').slideUp('fast');
	});
	
	// Carousel
	if($('.carousel ul').length > 0) {
		$('.carousel ul').each(function(index) {
			var wrap = $(this).children('li').length > 3 ? 'circular' : null;
			$(this).jcarousel({ auto: 10, wrap: wrap, scroll: 3, buttonNextHTML: null, buttonPrevHTML: null });
		});
		
		jQuery_InitCarousel();
	}
	
	// Newsletter
	$('.subscribe').click(function() {
		$('.form-subscribe').ajaxSubmit({ target: '.newsletter-signup' });
	});
	
	$('.form-subscribe input').keypress(function(event) {
		if(event.keyCode == '13')
		{
			$('.form-subscribe').ajaxSubmit({ target: '.newsletter-signup' });
			return false;
		}
	});
	
	// External
	$('.external, a[rel="external"]').attr('target', '_blank');

	// Links homepage
	if($('.response a').length > 0)
	{
		$('.response a').fancybox({ padding: 0 });
	}
    
    // Categories
    $('.choose-category').click(function() {
    	$('.categories').toggle('fast');
    });
    
    // Favorites
    $('.save-fav').click(function() {
    	$(this).before('Finding toegevoegd aan favorieten.').remove();
    	
    	$.get(ROOT + 'ajax/favorites-add/' + findingID, function(data) { 
    		$('.favorites').load(ROOT + 'ajax/favorites');
    	});
    });
    
    //$('.favorites').load(ROOT + 'ajax/favorites');
    
    $('.delete-fav').click(function() {
    	$('.save-txt').remove();
    	$('.save-url').show();
    	
    	$.get(ROOT + 'ajax/favorites-delete/' + findingID, function(data) {
    		$('.favorites').load(ROOT + 'ajax/favorites');
    	});
    });
    
    // Print
    $('.print').click(function() {
    	var d = new Date();
    	var WindowObject = window.open('', "Print", "width=740,height=600,top=100,left=100,toolbars=no,scrollbars=yes,status=no,resizable=no");
		WindowObject.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
		WindowObject.document.writeln('<html xmlns="http://www.w3.org/1999/xhtml">');
		WindowObject.document.writeln('<head>');
		WindowObject.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />');
		WindowObject.document.writeln('<link type="text/css" rel="stylesheet" href="' + ROOT + 'Style/style.print.css" />');
		WindowObject.document.writeln('</head>');
		WindowObject.document.writeln('<body>');
		WindowObject.document.writeln('<table><tr><td style="width: 20px;"><img src="' + ROOT + 'Style/Images/print-blue.png" alt="Blue" /></td><td style="padding-left: 10px;">http://www.familyfacts.nl/database/finding/' + findingID + '<br />Afgedrukt op ' + d.toLocaleDateString() + '</td><td style="width: 194px;"><img src="' + ROOT + 'Style/Images/familyfacts-logo-print.png" alt="Logo" /></td></tr></table>');
		WindowObject.document.writeln($('.content').html());
		WindowObject.document.writeln('<div style="text-align: center; margin-top: 40px;">Postbus 17199 &middot; 2502 CD Den Haag &middot; www.familyfacts.nl &middot; info@familyfacts.nl &middot; Bank 82.77.08.912 &middot; KvK 50567314</div>');
		WindowObject.document.writeln('</body>');
		WindowObject.document.writeln('</html>');
		WindowObject.document.close();
		WindowObject.focus();
		/*WindowObject.print();
		WindowObject.close();*/
    });
    
    // Donate
    if($('.spec-form').length > 0)
    {	
	    $('.donate input[type="radio"]').click(function() {
	    	if($(this).attr('id') == 'r-0') $('.amount').focus();
	    	else $('.amount').val('');
	    });
	    
	    $('.donate select').change(function() {
	    	if($(this).val() != 0) { $('#p-0').removeAttr('checked').attr('disabled', 'disabled'); $('label[for="p-0"]').addClass('disabled'); }
	    	else { $('#p-0').removeAttr('disabled'); $('label[for="p-0"]').removeClass('disabled'); }
	    });
	    
	    $('.payment input[type="radio"]').click(function() {
	    	// Machtigen
	    	if($(this).val() == 2) {
	    		$('label[for="accountnr"]').removeClass('disabled');
	    		$('#accountnr').removeAttr('disabled');
	    	}
	    	else
	    	{
	    		$('label[for="accountnr"]').addClass('disabled');
	    		$('#accountnr').attr('disabled', 'disabled');
	    	}
	    	
	    	// iDEAL
	    	if($(this).val() == 0)
	    	{
	    		$('.ideal').show();
	    	}
	    	else
	    	  $('.ideal').hide();
	    });
    }
    
    // Images alignment
    $('div.content img').each(function() {
    	if ($(this).css('float') != 'right')
    	  $(this).css('margin-right', '6px');
    	else
    	  $(this).css('margin-left', '6px');
    });
});

// Search
function searchSuccess(responseText, statusText, xhr, form)
{
	//alert('Response: "' + responseText.replace(/ /g, '') + '"');
	window.location = ROOT + 'nl/database/search/' + responseText.replace(/ /g, '');
}

// Carousel buttons
function jQuery_InitCarousel() {
	$('.carousel ul').each(function(i) {
		var carousel = $(this);
		
		if ($(this).children('li').length > 3) {
			$('#btn0-' + i).click(function() { carousel.jcarousel('prev'); });
			$('#btn1-' + i).click(function() { carousel.jcarousel('next'); });
		}
	});
	/*
	$('.b-left').click(function() { carousel.prev(); });
	$('.b-right').click(function() { carousel.next(); });*/
}
