function bannercarousel_initCallback(carousel){
	$('.mg-edibles-banner-control a').bind('click', function() {
		var id = $(this).attr('class')
   	carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
   	carousel.stopAuto();
    return false;
  });
};
		
function bannerControlsHighlight(carousel,objectli,liindex,listate){
	$('.mg-edibles-banner-control li:nth-child('+(liindex)+') a').attr("class","selected");
};

function bannerControlsremovehighlight(carousel, obejctli,liindex,listate){
  $('.mg-edibles-banner-control li:nth-child('+(liindex)+') a').removeAttr("class","selected");
};

function prepcarousel_initCallback(carousel){
	$('.jcarousel-skin-controls a').bind('click', function() {
		var id = $(this).attr('class')
		var active = $(this).parent().parent().find("a.selected");
		active.removeAttr("class","selected");
		
   	carousel.scroll(jQuery.jcarousel.intval(id.substr(5)));
    return false;
  });
};


/*added 3/23 cm */
function stopVideos(){
	var activeTabID = $('#edibles-vids .edibles-rbox-nav').find('a.current').attr('href');
	var activeFrame = $('#edibles-vids ul'+activeTabID).find("iframe")
	
	//this might be a better way of stopping the video - but it's a little buggy. It seems to remove the video
	//entirely until a different thumbnail is clicked.
	//activeFrame.contents().find('body').find('#player').flowplayer('player').unload();
	
	activeFrame[0].contentWindow.location.reload(true);

}

//moves the close link on the shadowbox to the top
function moveCloseLink(){ 
  var cb=document.getElementById('sb-nav-close'); 
  var tb=document.getElementById('sb-title'); 
  if(tb) tb.appendChild(cb);
  
  /*added 3/23 cm */
  stopVideos();	  
} 
			

Shadowbox.init({
	modal:true,
	overlayColor:"#000000",
	overlayOpacity:.7,
	skipSetup: true,
	viewportPadding:0,
	handleOversize:"drag",
	scrolling:"no",
	onOpen:moveCloseLink
});

$(function(){
	//recipe boxes
	//$("#erbox-1").organicTabs();
	$("#erbox-1").organicTabs({"speed": 200});
	$("#erbox-2").organicTabs({"speed": 200});
	$("#erbox-3").organicTabs({"speed": 200});
	
	$('.edibles-scrollable').scrollable(
		{
			keyboard:false,
			next:".edibles-rbox-next",
			prev:".edibles-rbox-prev"
		}
	);
				
	$(".edibles-scrollable .items a").click(function(event) {
			event.preventDefault();

			// see if same thumb is being clicked
			//if ($(this).hasClass("active")) { return; }
			var url = $(this).attr("href");
			var wrap = $(this).closest('.edibles-recipebox-main').find('.image_wrap').fadeTo('fast',0.5)

			wrap.fadeTo("fast", 1);
		
			// change the image
			wrap.find("iframe").attr("src", url);
			wrap.find("iframe").load();
		
			// activate item
			$(this).closest(".items").find('a').removeClass("active");
			$(this).addClass("active");
								
		// when page loads simulate a "click" on the first image
		}).filter(".first").click();
	
					
					
					
					
	//sliding banner on landing page
	$('#mg-edibles-banner').jcarousel({
		size:3,
		//auto:4,
		scroll:1,
		visible:1,
		wrap:"both",
		animation:"slow",
		initCallback:bannercarousel_initCallback,
		itemVisibleInCallback:bannerControlsHighlight,
		itemVisibleOutCallback: bannerControlsremovehighlight,
		buttonNextHTML: null,
   	buttonPrevHTML: null
	});
	
	
	$('.cta_anchor').click(function(event){
				event.preventDefault();
				
				var url = $(this).attr('href');
				
				Shadowbox.open({
					player:		"html",
					height:		806,
					width:		849,
					content: '<iframe marginwidth="0" marginheight="0" width="806" scrolling="no" height="849" id="modal-iframe" src="'+url+'" frameborder="0"></iframe>'
				})
	});
	
	window.onload = function(){
		$('#mg-edibles-banner').jcarousel('startAuto',4)
		//check for a hash tag
		var currentHashTag = window.location.hash.substr(1);
		if(currentHashTag){
			//parse the hash tag
			var container;
			if(currentHashTag.indexOf('recipe') > -1){
				container = '/smg/templateFramework/images/microSites/singletons/miraclegroedibles/mod_recipes_container.html';
			}else if(currentHashTag.indexOf('project') > -1){
				container = '/smg/templateFramework/images/microSites/singletons/miraclegroedibles/mod_projects_container.html';
			}
			//window.alert(container)
			Shadowbox.open(
				{
					player:		"html",
					height:		806,
					width:		849,
					content: '<iframe marginwidth="0" marginheight="0" width="806" scrolling="no" height="'+849+'" id="modal-iframe" src="'+container+'" frameborder="0"></iframe>'
				}
			);
		}
	}
	

	/* revised 3/24 js - only refreshing/stopping the video player when the "cooking" and "gardening" tabs are clicked...not all links */
	$('#edibles-vids li.ed-rbox-nav-1 a').click(function(){
		stopVideos();
	});
	
	$('#edibles-vids li.ed-rbox-nav-2 a').click(function(){
		stopVideos();
	});
	
	
});
