// remap jQuery to $
(function($){})(window.jQuery);

var currentGroupStartIndex = 0;

/* trigger when page is ready */
$(document).ready(function (){
    
    // your functions go here
    
    function modifyFBlike(layout,faces,width,action,font,colorScheme) {
      var frame = $(".addthis_button_facebook_like iframe");
      var frameSrc = $(".addthis_button_facebook_like iframe").attr("src");
      
      // set iframe width
      $(".addthis_button_facebook_like iframe").css("width", width + "px");
      // set height for "faces"
      if(faces=="true")
        $(".addthis_button_facebook_like iframe").css("height", "80px");
    
      frameSrc="//www.facebook.com/plugins/like.php?href="+addthis_share.url+"&layout="+layout+"&show_faces="+faces+"&width="+width+"&action="+action+"&font="+font+"&colorscheme="+colorScheme;
      $(".addthis_button_facebook_like iframe").attr("src", frameSrc);

    }
    if (typeof addthis_share != 'undefined')
        modifyFBlike("button_count","false","140","recommend","","light");

    $('.blink').fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500);
    $('.blink').mouseover(function() {
        $('.blink').css({color: "#fff"});
    });
    
    $('.ui-state-default').mouseover(function() {
	$('.ui-state-default').css({background: "#000 url(http://www.wearefree.ca/wp-content/themes/free/images/black.jpg) 0 0 no-repeat"});
    });

    $('.prev img').delay(8000).fadeOut(500);
    $('.next img').delay(8000).fadeOut(500);

    $("div.slideshow_main a")
        .mouseover(function() {
	    $('a.prev').css({"background": "url(http://www.wearefree.ca/wp-content/themes/free/images/slideshow_prev.jpg) 0 159px no-repeat"});
	    $('a.next').css({"background": "url(http://www.wearefree.ca/wp-content/themes/free/images/slideshow_next.jpg) 0 159px no-repeat"});
	})
	.mouseout(function() {
	    $('a.prev').css({"background": "url(http://www.wearefree.ca/wp-content/themes/free/images/slideshow_placeholder.png) 0 159px no-repeat"});
	    $('a.next').css({"background": "url(http://www.wearefree.ca/wp-content/themes/free/images/slideshow_placeholder.png) 0 159px no-repeat"});
	})
    
    $("ul.navigation > li > a")
        .css({backgroundPosition: "-245px 0px"})
        .mouseover(function() {
    	    $(this).stop().animate({ color: "#fff", backgroundPosition: "0px 0px" }, 500, function() {
	    	$(this).css({"background-color": "#000", "background-image": "none"});
	    })
    	})
	.mouseout(function() {
	    $(this).stop().css({"background-color": "#000", "background-image": "none"});
	    $(this).animate({ backgroundColor: "#fff", color: "#231f20" }, 1000); 
	    $(this).css("background", "url(http://www.wearefree.ca/wp-content/themes/free/images/nav_item_bkg.jpg) -245px 0px no-repeat");
    	});
   
    $("#social_nav a.fb")
        .css({backgroundPosition: "-245px 0px"})
        .mouseover(function() {
    	    $(this).stop().animate({ color: "#fff", backgroundPosition: "0px 0px" }, 500, function() {
	    	$(this).css({"background-color": "#2c518d", "background-image": "none"});
	    })
    	})
	.mouseout(function() {
	    $(this).stop().css({"background-color": "#2c518d", "background-image": "none"}); 
	    $(this).animate({ backgroundColor: "#fff", color: "#939598" }, 1000); 
	    $(this).css("background", "url(http://www.wearefree.ca/wp-content/themes/free/images/nav_item_bkg_fb.jpg) -245px 0px no-repeat");
    	});
    
    $("#social_nav a.tw")
        .css({backgroundPosition: "-245px 0px"})
        .mouseover(function() {
    	    $(this).stop().animate({ color: "#fff", backgroundPosition: "0px 0px" }, 500, function() {
	        $(this).css({"background-color": "#46c8f5", "background-image": "none"});
	    })
    	})
	.mouseout(function() {
	    $(this).stop().css({"background-color": "#46c8f5", "background-image": "none"}); 
	    $(this).animate({ backgroundColor: "#fff", color: "#939598" }, 1000); 
	    $(this).css("background", "url(http://www.wearefree.ca/wp-content/themes/free/images/nav_item_bkg_tw.jpg) -245px 0px no-repeat");
    	});

    $(".fancybox").fancybox({
	nextEffect: 'fade',
	prevEffect: 'fade',

 	/* width and height options are for hard-coded vimeo videos - if height changes between different videos, we will have to figure something else out */
	width: 900,
	height: 506
    });

    $(".expand_content").hide();
    $(".click_to_expand").click(function() {
        $(this).next(".expand_content").slideToggle(500);
    });
});

function preloader() {
	if (document.images) {
	    for (i = 0; i < preload_imageArray.length; i++) {
		var img = new Image();
		img.src = preload_imageArray[i];
	    }
	}
}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}
addLoadEvent(preloader);

//assumes increments is sorted
function getNearestIncrement(ui, increments) {
    
   var differencesArray = new Array();

   for (i = 0; i < increments.length; i++) {
	differencesArray[i] = Math.abs(ui.value - increments[i]);
   }

   var min = 100;
   var minIndex = 0;
   for (i = 0; i < differencesArray.length; i++) {
	if (differencesArray[i] <= min) { //defaults to second one if they are equal	
	    min = differencesArray[i];
	    minIndex = i;
	}
   }
   return increments[minIndex];
}

function getNextIncrement(currentCSValue, increments) {
    
    for (i = 0; i < increments.length; i++) {
        inc = increments[i];
	if (currentCSValue <= inc) {
	    return inc;
	}
    }
    return increments[0];
}

function getPrevIncrement(currentCSValue, increments) {

    for (i = increments.length-1; i >= 0; i--) {
        inc = increments[i];
	if (currentCSValue >= inc) {
	    return inc;
	}
    }
    return increments[increments.length-1]; 
}

/* optional triggers

$(window).load(function() {
	
});

$(window).resize(function() {
	
});

*/

