/*
Bookmark Script
*/
$("a.bookmark").click(
	function(){
		var bookmarkurl = $(this).attr('href');
		var bookmarktitle = $(this).attr('title');
		if (document.all)
		window.external.AddFavorite(bookmarkurl,bookmarktitle);
		else if (window.sidebar)		alert('This feature only works for Internet Explorer, use Control+D to bookmark This website');
		else if( window.opera && window.print )		alert('This feature only works for Internet Explorer, use Control+T to bookmark This website');
		
		return false; // Do not follow link
		}	);/*Colorbox CallThis is for all inline contentthe link needs to be done like this...<a href="#blah" class="colorbox">blah</a>and the hidden div like this...<div class="details"><div id="blah" class="colorbox"></div></div>*/
$("a.colorbox").click(
	function(){
			var href = "";
			href = $(this).attr("href");
			$(".colorbox").colorbox({width:"860px", inline:true, href:href});
			}
	);	
	
	
$("a.colorboxphoto").colorbox();

$("a.colorboxiframe").colorbox({width:"80%", height:"80%", iframe:true});

	
/* Slideshow */

/* this should be in head */
$(document).ready(function() {		
	
	//Execute the slideShow, set 4 seconds for each images
	slideShow(5000);

});

/* Slideshow function */
/*
*
*	Add class "slideshow" to a ul
*	Add class "show" to first li *Optional
*	Add image titles in <h3></h3>
*	Add descriptions in <p></p>
*	For Cufon support uncomment the two cufon.replace(); functions
*
*/

/* Slideshow function */
/*
*
*	Add class "slideshow" to a ul
* 	
*
*/

function slideShow(speed) {

	//Append a LI to the UL list for displaying caption
	$('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-background"></div><div class="slideshow-caption-container"><h3></h3><div class="text"></div></div></li>');

	//Set the opacity of all images to 0
	$('ul.slideshow li').css({opacity: 0.0});

	//Get the first image and display it (set it to full opacity)
	$('ul.slideshow li:first').css({opacity: 1});

	//Get the caption of the first image from appropriate attribute and display it
	$('#slideshow-caption h3').html($('ul.slideshow li:first').find('h3').html());
	$('#slideshow-caption .text').html($('ul.slideshow li:first').find('p').html());

	//Optional Cufon Support
	//Cufon.replace('#slideshow-caption h3');

	//Display the caption
	$('#slideshow-caption').css({opacity: 1, top:0});

	//Call the gallery function to run the slideshow
	var timer = setInterval('gallery()',speed);

	//pause the slideshow on mouse over
	$('ul.slideshow').hover(
		function () {
			clearInterval(timer);	
		},
		function () {
			timer = setInterval('gallery()',speed);			
		}
	);
	
}

function gallery() {

	//if no IMGs have the show class, grab the first image
	var current = ($('ul.slideshow li.show') ?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));

	//Get next image, if it reached the end of the slideshow, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
		
	//Get next image caption
	var title = next.find('h3').html();	
	var desc = next.find('p').html();
	
	//Set the fade in effect for the next image, show class has higher z-index
	next.css({opacity: 0}).addClass('show').animate({opacity: 1.0}, 1000);
	
	//Update the caption to perform funky animated transition
	$('#slideshow-caption').animate({ 
		opacity:0,
		left:'-=405px'
		}, 400, function(){
			$('#slideshow-caption h3').html(title);
			//Optional Cufon Support
			//Cufon.replace('#slideshow-caption h3');
			$('#slideshow-caption .text').html(desc);
			$('#slideshow-caption').css({'left':'-405px'});
			$('#slideshow-caption').animate({
				opacity: 1,
				left: ['+=405px','swing']}, 400
			);
	});

	//Hide the current image
	current.animate({opacity: 0}, 1000).removeClass('show');

}


	
/*
Slickboxes
markup like...

<ul class="slick-links">
	<li><a href="#blah">blah</a>
		<ul>
			<li>blah info</li>
		</ul>
	</li>
</ul>

*/

$(function () {
	
	var slickboxes = $('.slick-links > li > ul');
	slickboxes.hide();
	
	$('.slick-links li h3').click(function() {
		var current = $(this).siblings('ul');
		slickboxes.filter('.showing').not(current).toggle(400).toggleClass('showing');
		current.toggle(200).toggleClass('showing');
		return false;
	});
});


/*
*
*	Replace submit input
*	Looks for a submit button and changes it to button sprite
*
*/
$(document).ready( function(){

	//Function to add each attribute of submit to the <a>
	jQuery.fn.replaceWith = function(replacement) {
	  return this.each(function(){
		element = $(this);
	  $(this)
		  .after(replacement).next()
		  .attr('class', element.attr('class'))
		  .attr('title',element.attr('title'))
		 .html(element.html())
		  .prev().remove();
	  });
	};

	//Replace submit button with <a><span></span></a>
	$("form .submit").replaceWith("<a></a>");
	$("form .submit").html("<span>Send</span>");
	$("form .submit").addClass("tab button");
	
	//Bind submit event to <a>
	$("a.submit").click( function(){
		$(this).parents("form").submit();
	});

	// TODO: Validate on submit();
	// TODO: Tidy function
	
});




/*
Tabs Script

<div class="tabs">
	<ul class="nav">
		<li><a href="#panel_id"></li>
		<li><a href="#panel2_id"></li>
	</ul>
	<div id="panel_id">
		<p>Content</p>
	</div>
	<div id="panel2_id">
		<p>Content</p>
	</div>
</div>

NOTE: Make sure this isn't clashing with the nav tab... Change these to class="menu" or something

*/

$(function () {
    var tabContainers = $('.tabs > div');
    
    $('div.tabs .nav a').click(function () {
        tabContainers.hide().filter(this.hash).show();
        
        $('div.tabs .nav a').removeClass('selected');
        $(this).addClass('selected');
        
        return false;
    }).filter(':first').click();
});
