// Slider
$(window).load(function() {
	$('#slides').nivoSlider({
		effect:'random',
    	boxCols: 10,
    	boxRows: 5,
    	
    	animSpeed:500,
        pauseTime:5000,
        
        startSlide:0,
        
        directionNav:false,
        directionNavHide:true,
        
    	controlNav:true,
    	controlNavThumbs:true,
    	controlNavThumbsFromRel:true,
    	controlNavThumbsSearch: '.jpg',
        controlNavThumbsReplace: '',
        
        keyboardNav:true,
        
        pauseOnHover:true,
        
        manualAdvance:false,
        
        captionOpacity:0.7
	});
	
});

$(document).ready(function(){

// Admin bar fading
(function() {

//settings
var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
var topbarME = function() { $('#wp-admin-bar').fadeTo(fadeSpeed,1); }, topbarML = function() { $('#wp-admin-bar').fadeTo(fadeSpeed,fadeTo); };
var inside = false;

//do
$(window).scroll(function() {
position = $(window).scrollTop();
if(position > topDistance && !inside) {

//add events
topbarML();
$('#wp-admin-bar').bind('mouseenter',topbarME);
$('#wp-admin-bar').bind('mouseleave',topbarML);
inside = true;
}

else if (position < topDistance){
topbarME();
$('#wp-admin-bar').unbind('mouseenter',topbarME);
$('#wp-admin-bar').unbind('mouseleave',topbarML);
inside = false;
}
});
})();

// Equalize content and sidebar heights
$.fn.equalizeHeights = function(){
  return this.height( Math.max.apply(this, $(this).map(function(i,e){ return $(e).height() }).get() ) )
}
$('#aside, #pageWrapper').equalizeHeights();

// Drop Menu
$("#nav ul ul").css({display: "none"});

$("#nav ul li").hover(function(){
	$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideDown(400);
},function(){
	$(this).find('ul:first').css({visibility: "visible"}).slideUp(400);
});

// Disable Parent li if has child items
$("#nav ul li:has(ul)").hover(function () {
	$(this).children("a").click(function () {
return false;
});
});

// Style selects
$("select#cat").customStyle();
$("select#tag").customStyle();

// Image hovers
var opacity = 1, toOpacity = 0.8, duration = 500;
  //set opacity ASAP and events
  $('#content a img').css('opacity',opacity).hover(function() {
      $(this).fadeTo(duration,toOpacity);
    }, function() {
      $(this).fadeTo(duration,opacity);
    }
  );

// Tooltip
$('.tooltip').tipsy({
	gravity: $.fn.tipsy.autoNS,
	fade: true,
	html: true
});

$('.tooltip-s').tipsy({
	gravity: 's',
	fade: true,
	html: true
});

$('.tooltip-e').tipsy({
	gravity: 'e',
	fade: true,
	html: true
});

$('.tooltip-w').tipsy({
	gravity: 'w',
	fade: true,
	html: true
});

// Scroll
jQuery.localScroll();

// Pretty Photo
$('.images a:has(img)').addClass('prettyPhoto');
$("a[class^='prettyPhoto']").prettyPhoto();

// Validate
$("#contact_form").validate({
		rules: {
			name: { required: true, minlength: 3 },
			email: { required: true, email: true },
			message: { required: true, minlength: 4 }
		},
			
		messages: {
			name: { required: "Please enter your name", minlength: "Your name must consist of at least 3 characters" },
			email: "Please enter a valid email address",
			message: { required: "Please enter a message", minlength: "Your message must consist of at least 4 characters" }
		}
	});
	
$("#new_post").validate({
		rules: {
			title: { required: true, minlength: 3 },
			description: { required: true, minlength: 5 },
		},
			
		messages: {
			title: { required: "Please enter a title", minlength: "Title must consist of at least 3 characters" },
			description: { required: "Please enter a review", minlength: "Your review must consist of at least 5 characters" }
		}
	});

// Form submit
$('#contact_form').ajaxForm(function() { 
   alert("Thank you. Your message was sent successfully!"); 
});

// Accordion
$('.accordion .accordion_title').click(function() {
$(this).next().toggle('slow');
return false;
}).next().hide();

// Toggler

// choose text for the show/hide link - can contain HTML (e.g. an image)
var showText='More...';
var hideText='Less...';

// 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;
});

});
