/*** 
    Simple jQuery Slideshow Script
    Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/

function slideSwitch() {
    var $active = $('#slideshow img.active');

    if ( $active.length == 0 ) $active = $('#slideshow img:last');

    // use this to pull the images in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow img:first');

    // uncomment the 3 lines below to pull the images in random order
    
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 6000 );
});



$(document).ready(function(){
	$("a#search-submit-btn").click(function() {
		$("#searchform").submit();
	});
});



/* --------------------------------------------------------------------------------------------
	Dialogs/Modals
-------------------------------------------------------------------------------------------- */
/*
$(document).ready(function() {

	$('#search-dialog').dialog({
		autoOpen: false,
		height: 100,
		modal: true
	});

	$('#search-dialog-trigger').click(function(){
		$('#search-dialog').dialog('open');
		return false;
	});
});
*/


/* --------------------------------------------------------------------------------------------
	Smooth Scrolling
-------------------------------------------------------------------------------------------- */

$(document).ready(function() {
	$('.scroller-btn').click(function(){
		var $target = $('body').find(this.hash);
		$.scrollTo( $target , 800 );
		return false;
	});
});


/* --------------------------------------------------------------------------------------------
	Hide Development Bar
-------------------------------------------------------------------------------------------- */

$(document).ready(function(){
	$("a.hide-devbar").css('cursor','pointer').click(function () {
		$("#devbar").hide();
		$("body.logged-in").css('border','none');
	});
});


/* --------------------------------------------------------------------------------------------
	Dialog Ajax
-------------------------------------------------------------------------------------------- */
$(document).ready(function() {
	$('a.ajax').each(function() {
		var $link = $(this);
		var $dialog = $('<div></div>').load($link.attr('href')).dialog({
			autoOpen: false,
			title: $link.attr('title'),
			width: 500,
			height: 300
		});
		$link.click(function() {
			$dialog.dialog('open');
			return false;
		});
	}); 
	
	var options = {
		target: '#output',
		success: function() {
			$('#output').fadeIn('slow');
		} 
		};
		 
		$('#form_email').submit(function() {
		$(this).ajaxSubmit(options);
		return false;
	});
});

/* --------------------------------------------------------------------------------------------
	Dialog iFrame
-------------------------------------------------------------------------------------------- */
$(document).ready(function() {
	$('a.iframe').click(function(e) {
		e.preventDefault();
		var $this = $(this);
		var horizontalPadding = 30;
		var verticalPadding = 30;
        $('<iframe id="externalSite" class="externalSite" src="' + this.href + '" />').dialog({
            title: ($this.attr('title')) ? $this.attr('title') : 'External Site',
            autoOpen: true,
            width: 800,
            height: 500,
            modal: true,
            resizable: true,
			autoResize: true,
            overlay: {
                opacity: 0.5,
                background: "black"
            }
        }).width(800 - horizontalPadding).height(500 - verticalPadding);	        
	});
});

/* --------------------------------------------------------------------------------------------
	Show Hide - With Styles
-------------------------------------------------------------------------------------------- */

/*
function callback(){
	setTimeout(function(){
		$("#applicants-giant-notice:hidden").removeAttr('style').hide().fadeIn();
	}, 1000);
};

$(document).ready(function(){
	// COOKIES
    // Header State
    var hideRandomTags = $.cookie('random-tag-cloud');
	if (hideRandomTags == 'hidden') {
		$("#tag-cloud-random").hide();
	}
	$("#button-toogle").click(function() {
		$("#tag-cloud-random").hide(300);
		$.cookie('random-tag-cloud', 'hidden', { expires: 30 });
	});
});
*/


/* --------------------------------------------------------------------------------------------
	Resize of images in posts
-------------------------------------------------------------------------------------------- */

var h;
jQuery(document).ready(function() {
	$(".post img").each(function() {
		if($(this).width() > 590) {
		h=$(this).height()/$(this).width()*590;
		  $(this).attr({height : h });
		  $(this).attr({width : "590"});
		}
	});
});

/* --------------------------------------------------------------------------------------------
	FANCYBOX MODAL FOR INFO UPDATE FORM
-------------------------------------------------------------------------------------------- */

$(document).ready(function(){
	$("a[rel^='global-modal']").prettyPhoto({
		theme: 'facebook',
		animationSpeed: 'normal', /* fast/slow/normal */
		opacity: 0, /* Value between 0 and 1 */
		showTitle: true /* true/false */
	});
});

/* --------------------------------------------------------------------------------------------
	JS behaviours
-------------------------------------------------------------------------------------------- */

/**
 * Change title attribute in href links on tag cloud to something of our choosing
 *
 * @author R Cardoza.
 **/
jQuery(document).ready(function() {
	var link_attr = "";
	jQuery("#tag-cloud-random a[class^=tag-link]").each(function() {
		link_attr = jQuery(this).attr('title');
		link_attr = link_attr.toString();
		link_attr = link_attr.replace("topic", "post");
		//console.log(link_attr);  //For debugging purposes.
		jQuery(this).attr('title', link_attr);
	});
});

/* --------------------------------------------------------------------------------------------
	SHOW / HIDE
-------------------------------------------------------------------------------------------- */

$(document).ready(function() {

var showText='Show';
var hideText='Hide';

// initialise the visibility check
var is_visible = false;

// append show/hide links to the element directly preceding the element with a class of "toggle"
$('.toggle').prev().append(' (<a href="#" class="toggleLink">'+showText+'</a>)');

// hide all of the elements with a class of 'toggle'
$('.toggle').hide();

// capture clicks on the toggle links
$('a.toggleLink').click(function() {

// switch visibility
is_visible = !is_visible;

// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);

// toggle the display - uncomment the next line for a basic "accordion" style
//$('.toggle').hide();$('a.toggleLink').html(showText);
$(this).parent().next('.toggle').toggle('slow');

// return false so any link destination is not followed
return false;

});
});
