$(document).ready(function(){

	$("#blog #entry img, #blog.entry img").each(function(i){
		var url = $(this).attr("src");
		var tag = "<a rel='lightbox' class='lightbox' href='" + url + "'>";
		var parentTag = $(this).parent().get(0).tagName;
		if (parentTag != "A") {
			$(this).wrap(tag);
		};
	});
   
    //Controls the carousel, but first finds if it exists
    if($("#carousel").length > 0)
    {
        $("#carousel").jCarouselLite({
            btnNext: "#next a",
            btnPrev: "#previous a",
            circular: true,
            visible: 4,
            scroll: 1,
            speed: 500
            
        });
    }
    
    //Pretty photo is the image lightbox for the website
    if($(".lightbox").length > 0)
    {
        $(".lightbox").prettyPhoto({
            animationSpeed: 'fast', /* fast/slow/normal */
			padding: 25, /* padding for each side of the picture */
			opacity: 0.85, /* Value betwee 0 and 1 */
			showTitle: true, /* true/false */
			allowresize: true, /* true/false */
			theme: 'light_rounded', /* light_rounded / dark_rounded / light_square / dark_square */
			hideflash: false, /* Hides all the flash object on a page, set to TRUE if flash appears over prettyPhoto */
			modal: false /* If set to true, only the close button will close the window */
        });
    }

    $(".feature a.name").each(function(){
        $(this).click(function(e){
            e.preventDefault();
            var elem = $(this)
            $(this).parents("tr").next("tr").find(".featuredescription").slideToggle(function(){
                if($(elem).hasClass("up"))
                {
                    $(elem).removeClass("up");
                }
                else 
                {
                    $(elem).addClass("up");
                }
            });

        });
    });
    $("#blog .filter ul").hide();
    $("#blog .filter h2 a").click(function(e){
        e.preventDefault();
        var elem = $(this)
        $(this).parents("h2").next("ul").slideToggle(function(){
	        if($(elem).hasClass("active"))
	        {
	            $(elem).removeClass("active");
	        }
	        else 
	        {
	            $(elem).addClass("active");
	        }
        });
    });
     

    //Controls the slide downs, to add simply create a new anchor element in the template and
    //add the elements to this array
    var elems = [$("#beta_link"), $("#product_link"), $("#standard_link"), $("#pro_link")];
    $(elems).each(function(e){
        $(this).click(function(e){
            scrollWin($(this).attr("href"), e);
            console.log(e);
        });
    });
    
    function scrollWin(destination, event){
        event.preventDefault();
        $('html, body').animate({
            scrollTop: $(destination).parents("div").offset().top
        }, 1000);
    }
});

// Small jquery plugin that will search for a string and wrap it in a span containing the specified class.
jQuery.fn.spanwrap = function (str, className)
{
    var regex = new RegExp(str, "i");
    return this.each(function ()
    {
        this.innerHTML = this.innerHTML.replace(regex, "<span class=\"" + className + "\">" + str + "</span>");
    });
};

