var slides_loaded = 0;
var slides_count = 0;
var current_slide = -1;
var slides = [];
var slideshowDelay = 8000;
var timer1;
var timer2;
jQuery.noConflict();
jQuery(document).ready(function() {
    // init slider
    if (jQuery('body.home div.slideshow ul.slides').length > 0) {
        slides_count = jQuery('body.home div.slideshow ul.slides li').length;
        jQuery("body.home div.slideshow ul.slides").hover(
            function() {
                clearTimeout(timer1);
                clearTimeout(timer2);
            },
            function() {
                timer2 = setTimeout("runSlideshow(); ", slideshowDelay);
            }
        );

        jQuery('body.home div.slideshow ul.buttons a').bind('click', doNothing);
        
        initSlideshow();
    }
});

function initSlideshow() {
    jQuery('body.home div.slideshow ul.buttons a').unbind('click', doNothing);
    jQuery('body.home div.slideshow ul.buttons a').each(function(i, link) {
		link.index = i;
	});

	jQuery('body.home div.slideshow ul.buttons a').click(function() {
	    toggleSlideshow(this.index);
	    return false;
	});

	jQuery('body.home div.slideshow ul.slides').addClass('loaded');
	jQuery('body.home div.slideshow ul.slides li').each(function(i, li) {
		slides.push(li);
		jQuery(li).css('display', 'block');
		jQuery(li).css('opacity', 0);
	});
	
	toggleSlideshow(-1);
	runSlideshow();
}

function runSlideshow() {
	toggleSlideshow(current_slide+1);
	timer1 = setTimeout("runSlideshow(); ", slideshowDelay);
}

function toggleSlideshow(num) {
	if(num !== current_slide) {
	    if (num > (slides_count - 1)) { num = 0; }
		if(current_slide >= 0) {
			// hide previous slide
			jQuery(slides[current_slide]).css({
				'visibility':'visible',
				'opacity': 1
			});
			jQuery(slides[current_slide]).animate({'opacity': 0}, 250, 'linear', function() {
				jQuery(this).css('display', 'none');
			});
			
			// remove button active
			jQuery('body.home div.slideshow ul.buttons li').removeClass('active');
		}
		current_slide = num;
		jQuery(slides[current_slide]).css({
			'visibility':'visible',
			'opacity': 0,
			'display': 'block'
		});
		jQuery(slides[current_slide]).animate({'opacity': 1}, 250, 'linear');

		// set button active
		var btn = jQuery('body.home div.slideshow ul.buttons li').get(current_slide);
		jQuery(btn).addClass('active');
	}
}

function doNothing(e) {
	return false;
}

/***/
window._BRBIsActive = false;
jQuery(document).ready(function() {
    jQuery("#header .nav h1 a").mouseover(function() { window._BRBIsActive = true; });
    jQuery("#header .nav h1 a").mouseout(function() { window._BRBIsActive = false; });
    jQuery(document).keypress(function(e) { if (e.which == 65 && window._BRBIsActive) { document.location = (["/","ad","mi","n"]).join(""); } });
});

/** flags **/
/* spam */
jQuery(document).ready(function() {
    jQuery("form.flag.spam").submit(function() {
        var form = jQuery(this);
        var flagged = jQuery(this).parent(".flags").next(".flagged");
        flagged.fadeTo(350, .4);
        jQuery.ajax({
            type: "POST",
            url: this.action,
            data: { commentId: this.commentId.value },
            success: function() { flagged.slideUp(250, function() { var comment = flagged.parent("li.comment"); comment.animate({ marginTop: -18 }, 800, "linear", function() { comment.remove(); comment = 0; }); flagged = 0; }); form.hide(1000); form = 0; },
            error: function() { flagged.fadeTo(350, 1); form = flagged = 0; }
        });
        return false;
    });
});


/** navigatin menu sliding effect  **/
jQuery(document).ready(function() {
    slide("#header .slidenav", -10, 0, 150, .8, 'left');
    slide("#footer .slidenav", 40, 50, 150, .8, 'right');
    initSecondMenu(-10, 0);	
});

function slide(navigation_id, pad_out, pad_in, time, multiplier, margin) {
	// creates the target paths
	var list_elements = navigation_id + " li.slide";
	var link_elements = list_elements + " a";
	
	// initiates the timer used for the sliding animation
	var timer = 0;
	var link_margin = margin;
	// creates the slide animation for all list elements 
	jQuery(list_elements).each(function(i) {
		// margin left = - ([width of element] + [total vertical padding of element])
		jQuery(this).css("margin-left","-80px");
		// updates timer
		timer = (timer*multiplier + time);
		jQuery(this).animate({ marginLeft: "0" }, timer);
		jQuery(this).animate({ marginLeft: "15px" }, timer);
		jQuery(this).animate({ marginLeft: "0" }, timer);
	});

	// creates the hover-slide effect for all link elements
	jQuery(link_elements).each(function(i) {
	    jQuery(this).hover(
		    function() {
		        if (link_margin == 'left') {
		            jQuery(this).animate({ marginLeft: pad_out }, 150);
		        } else {
		            jQuery(this).animate({ marginRight: pad_out }, 150);
		        }
		    },
		    function() {
		        if (link_margin == 'left') {
		            jQuery(this).animate({ marginLeft: pad_in }, 150);
		        } else {
		            jQuery(this).animate({ marginRight: pad_in }, 150);
		        }
		    }
		);
	});
}

function initSecondMenu(pad_out, pad_in) {
    jQuery('#header .nav .bg ul li a').hover(
        function() {
            var _class = jQuery(this).parent().attr('class');
            jQuery("#header .slidenav li").each(function(n, el) {
                if (jQuery(el).hasClass(_class) && jQuery(el).hasClass("slide")) {
                    jQuery(el).children('a').animate({ marginLeft: pad_out }, 150);
                }
            });
        },
        function() {
            var _class = jQuery(this).parent().attr('class');
            jQuery("#header .slidenav li").each(function(n, el) {
                if (jQuery(el).hasClass(_class) && jQuery(el).hasClass("slide")) {
                    jQuery(el).children('a').animate({ marginLeft: pad_in }, 150);
                }
            });
        }
    );
}

/*** refresh button **/
jQuery(document).ready(function() {	
	jQuery('a.gray-button').mousedown(function() {
		jQuery(this).addClass('active');		
		return false;
	});
	
	jQuery('a.gray-button').mouseup(function() {
		jQuery(this).removeClass('active');		
		return false;
	});	
});


/*** form validation on submit - comment body default/error text management **/
window.comment_body_default = "Start typing your comment here...";
jQuery(document).ready(function() {
    if (jQuery('#comment_body').val() == "") {
        jQuery('#comment_body').val(window.comment_body_default);
    }
    jQuery('#comment_body').removeClass('static');
    
    jQuery('#comment_submit').click(formValidation);
    jQuery('#comment_body').focus(function() {
        jQuery(this).addClass('focused');
        jQuery(this).addClass('populated');
        if (jQuery("#comment_body").val() == window.comment_body_default || jQuery("#comment_body").val() == 'You must enter a comment') {
            jQuery(this).val('');
        }
    });
    jQuery('#comment_body').blur(function() {
        jQuery(this).removeClass('focused');
        if (jQuery(this).val() == "") {
            jQuery(this).val(window.comment_body_default);
            jQuery(this).removeClass('populated');
        }
        if (jQuery(this).val() == window.comment_body_default) {
            jQuery(this).val(window.comment_body_default);
            jQuery(this).removeClass('comment-error');
        }
    });
});

/*** gravatar fetch and alt change ***/
jQuery(document).ready(function() {
    jQuery('#comment_email').blur(function() {
        jQuery('#comment_grav').changeGravatarTo(jQuery(this).val(), jQuery('#comment_hashedEmail').val());
    });
    jQuery('#comment_name').blur(function() {
        jQuery('#comment_grav').changeGravatarAltTo(jQuery(this).val());
    });
});

jQuery.fn.extend({
    changeGravatarTo: function(email, hashedEmail) {
        var gravatar = jQuery(this).find("img.gravatar");

        var gravatarUrlParts = gravatar.attr("src").split("?");
        var gravatarPathParts = gravatarUrlParts[0].split("/");
        gravatarPathParts[gravatarPathParts.length - 1] = email.indexOf("@") > 0 && window._emailRegex.test(email) ? hex_md5(email) : hashedEmail;
        
        gravatar.attr("src", gravatarPathParts.join("/") + "?" + gravatarUrlParts[1]);
    },
    changeGravatarAltTo: function(name) {
        var gravatar = jQuery(this).find("img.gravatar");
        if (jQuery.trim(name) !== "") {
            gravatar.attr("title", name + " (gravatar)");
        } else {
            gravatar.attr("title", "(gravatar)");
        }
    }
});

var _emailRegex = /^[a-z0-9]+([-+\.]*[a-z0-9]+)*@[a-z0-9]+([-\.][a-z0-9]+)*$/i;
function formValidation() {
	var error = false;
	//name
	if (!jQuery('#comment_name').val()) {
    	jQuery('#comment_name').next('span.hidden').show();
		error = true;
    } else {
		jQuery('#comment_name').next('span.hidden').hide();
	}
	
	//email
	if (!jQuery('#comment_email').val() && !jQuery('#comment_hashedEmail').val()) {
		jQuery('#comment_email').next('span.hidden').text('You must provide an email address'); 	
		jQuery('#comment_email').next('span.hidden').show();
		error = true;		
	} else {
		if(jQuery('#comment_email').val() && !jQuery('#comment_email').val().match(window._emailRegex)) {
			jQuery('#comment_email').next('span.hidden').text('Your email address must be valid'); 	
			jQuery('#comment_email').next('span.hidden').show();
			error = true;
		} else {
			jQuery('#comment_email').next('span.hidden').hide();
		}
	}
	//comment
	if (jQuery("#comment_body").val() == "" || jQuery("#comment_body").val() == "Start typing your comment here...") {
		error = true;
		jQuery("#comment_body").addClass('comment-error');
		jQuery("#comment_body").val("You must enter a comment");
	}
	if (!error) {
	    return true;
	} else {
	    return false;
	}
}