$(function(){

	// Dialog			
	$('#dialog').dialog({
		autoOpen: false,
		width: 488,
		modal: true,
		resizable: false,
		buttons: {
			"Bericht plaatsen": function() { 
				$('form[name=guestbook_form]').submit(); 
			}, 
			"Annuleren": function() { 
				$(this).dialog("close"); 
			} 
		},
		open: function(){
			$.ajax({
			  url: '/includes/captcha.php',
			  success: function(data) {
			    $('#captcha').attr('src','/includes/temp/captcha.png?rand=' + Math.floor(Math.random()*1001))
			  }
			});
		}
	});
	
	$('#login_link').click(function(){
		$('#login').show();
		$(this).hide();
	});
	$('#login .login').click(function(){
		$('#login_link').show();
		$('#login').hide();
	});
	
	$('#login input[name="Password"]').hide();
	$('#login input').focus(function(){
		if($(this).attr('name') == "PasswordText"){
			$(this).hide();
			$('#login input[name="Password"]').show().focus();
		}else{
			$(this).val('');
		}
	});
	
	if($('form[name=contact_form]').html() != null){
		$.ajax({
		  url: '/includes/captcha.php',
		  success: function(data) {
		    $('#captcha').attr('src','/includes/temp/captcha.png?rand=' + Math.floor(Math.random()*1001))
		  }
		});
	}else if($('form[name=register_form]').html() != null){
		$.ajax({
		  url: '/includes/captcha.php',
		  success: function(data) {
		    $('#captcha').attr('src','/includes/temp/captcha.png?rand=' + Math.floor(Math.random()*1001))
		  }
		});
	}
	
	$(".datepicker").datepicker({ dateFormat: 'dd-mm-yy', changeYear: true,yearRange: '1950:2011', dayNamesMin: ['Zo','Ma','Di','Wo','Do','Vr','Za'], monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'], firstDay: 1});
	
	// Dialog Link
	$('#dialog_link').click(function(){
		$('#dialog').dialog('open');
		return false;
	});
	
	 $('#sponsor_slider').cycle({ 
                fx:    'fade', 
                speed:  2000,
                random:  1,
                timeout: 2000 
             });
             
    $('.popular_photo').cycle({ 
                fx:    'fade', 
                speed:  2000,
                random:  1,
                timeout: 2000 
             });
			    
			    
			    
    $('.more_btn').click(function(){
		if($(this).hasClass('less_btn')){
			$(this).parent().find('p.background_information').slideUp();
			$(this).removeClass('less_btn');
			$(this).val('Bekijk de achtergrondinformatie');
		}else{
			$(this).parent().find('p.background_information').slideDown();
			$(this).addClass('less_btn');
			$(this).val('Verberg de achtergrondinformatie');
		}
	});


// We only want these styles applied when javascript is enabled
				$('div.navigation').css({'width' : '200px', 'float' : 'left'});
				$('div.content').css('display', 'block');

				// Initially set opacity on thumbs and add
				// additional styling for hover effect on thumbs
				var onMouseOutOpacity = 0.67;
				$('#thumbs ul.thumbs li').opacityrollover({
					mouseOutOpacity:   onMouseOutOpacity,
					mouseOverOpacity:  1.0,
					fadeSpeed:         'fast',
					exemptionSelector: '.selected'
				});
				

	     if($('#thumbs').html() != null){
	 			// Initialize Advanced Galleriffic Gallery
				var gallery = $('#thumbs').galleriffic({
					delay:                     2500,
					numThumbs:                 8,
					preloadAhead:              10,
					enableTopPager:            true,
					enableBottomPager:         true,
					maxPagesToShow:            4,
					imageContainerSel:         '#slideshow',
					controlsContainerSel:      '#controls',
					captionContainerSel:       '#caption',
					loadingContainerSel:       '#loading',
					renderSSControls:          true,
					renderNavControls:         true,
					playLinkText:              'Start diashow',
					pauseLinkText:             'Pauzeer diashow',
					prevLinkText:              '&lsaquo; Vorige foto',
					nextLinkText:              'Volgende foto &rsaquo;',
					nextPageLinkText:          '&rsaquo;',
					prevPageLinkText:          '&lsaquo;',
					enableHistory:             true,
					autoStart:                 false,
					syncTransitions:           true,
					defaultTransitionDuration: 900,
					onSlideChange:             function(prevIndex, nextIndex) {
						// 'this' refers to the gallery, which is an extension of $('#thumbs')
						this.find('ul.thumbs').children()
							.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
							.eq(nextIndex).fadeTo('fast', 1.0);
					},
					onPageTransitionOut:       function(callback) {
						this.fadeTo('fast', 0.0, callback);
					},
					onPageTransitionIn:        function() {
						this.fadeTo('fast', 1.0);
					}
				});
				
				/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

				// PageLoad function
				// This function is called when:
				// 1. after calling $.historyInit();
				// 2. after calling $.historyLoad();
				// 3. after pushing "Go Back" button of a browser
				function pageload(hash) {
					// alert("pageload: " + hash);
					// hash doesn't contain the first # character.
					var AlbumID = location.href.replace(/#.*/, '').split('/').pop();
					
					if(hash) {
						$.galleriffic.gotoImage(hash);
						
						var hashkey2 = 'photo_'+AlbumID+'_'+(hash-1);
						
					} else {
						gallery.gotoIndex(0);
						var hashkey2 = 'photo_'+AlbumID+'_0';
					}
					
					$.post('/foto/ajaxcounter/', { hashkey: hashkey2 }, function(data) {	});
				}

				// Initialize history plugin.
				// The callback is called at once by present location.hash. 
				$.historyInit(pageload, "advanced.html");

				// set onlick event for buttons using the jQuery 1.3 live method
				$("a[rel='history']").live('click', function(e) {
					
					$.post('/foto/ajaxcounter/', { hashkey: $(this).attr('id') }, function(data) {

					});

					if (e.button != 0) return true;
					
					var hash = this.href;
					hash = hash.replace(/^.*#/, '');

					// moves to a new page. 
					// pageload is called at once. 
					// hash don't contain "#", "?"
					$.historyLoad(hash);

					return false;
				});

				/****************************************************************************************/
		}
		// Einde gallery

	// Moderator
	
	$('#mod_dialog').dialog({
		autoOpen: false,
		width: 788,
		modal: true,
		resizable: false,
		buttons: {
			"Bericht plaatsen": function() { 
				$('form[name=team_form]').submit(); 
			}, 
			"Annuleren": function() { 
				$(this).dialog("close"); 
			} 
		}
	});
	$('#mod_dialog_edit').dialog({
		autoOpen: true,
		width: 788,
		modal: true,
		resizable: false,
		buttons: {
			"Bericht bewerken": function() { 
				$('form[name=team_form]').submit(); 
			}, 
			"Annuleren": function() { 
				window.location.replace(window.location.href.substring(0,window.location.href.indexOf('?')));
			} 
		}
	});
	if($('textarea.editor').html() != undefined){
		$('textarea.editor').ckeditor();
	}
});

