/*********************
//* jQuery Multi Level CSS Menu (horizontal)- By Dynamic Drive DHTML code library: http://www.dynamicdrive.com
//* Menu instructions page: http://www.dynamicdrive.com/dynamicindex1/ddlevelsmenu/
//* Last modified: Sept 6th, 08'. Usage Terms: http://www.dynamicdrive.com/style/csslibrary/tos/
*********************/

//Specify full URL to down and right arrow images (25 is padding-right to add to top level LIs with drop downs):
var arrowimages = {down:['rightarrowclass', 'http://www.monmouthpark.com/equibase/images/spacer.gif'], right:['rightarrowclass', 'http://www.monmouthpark.com/equibase/images/arrow-right.gif']}

var jquerycssmenu = {
	
	fadesettings: {overduration: 350, outduration: 100}, //duration of fade in/ out animation, in milliseconds
	
	buildmenu:function(menuid, arrowsvar){
		jQuery(document).ready(function($){
			var $mainmenu=jQuery("#"+menuid+">ul")
			var $headers=$mainmenu.find("ul").parent()
			$headers.each(function(i){
				var $curobj=jQuery(this)
				var $subul=jQuery(this).find('ul:eq(0)')
				this._dimensions={w:this.offsetWidth, h:this.offsetHeight, subulw:$subul.outerWidth(), subulh:$subul.outerHeight()}
				this.istopheader=$curobj.parents("ul").length==1? true : false
	
				$curobj.children("a:eq(0)").css(this.istopheader? {paddingRight: arrowsvar.down[2]} : {}).append(
					'<img src="'+ (this.istopheader? arrowsvar.down[1] : arrowsvar.right[1])
					+'" class="' + (this.istopheader? arrowsvar.down[0] : arrowsvar.right[0])
					+ '" style="border:0;display: none;" />'
				).mouseover(function(){jQuery("img",this).show();}).mouseout(function(){jQuery("img",this).hide();});
				$curobj.hover(
					function(e){
						var $targetul=jQuery(this).children("ul:eq(0)")
						this._offsets={left:jQuery(this).offset().left, top:jQuery(this).offset().top}
						var menuleft=this.istopheader? 0 : this._dimensions.w
						menuleft=(this._offsets.left+menuleft+this._dimensions.subulw>jQuery(window).width())? (this.istopheader? -this._dimensions.subulw+this._dimensions.w : -this._dimensions.w) : menuleft
						$targetul.css({left:menuleft+"px"}).fadeIn(jquerycssmenu.fadesettings.overduration)
					},
					function(e){
						jQuery(this).children("ul:eq(0)").fadeOut(jquerycssmenu.fadesettings.outduration)
					}
				) //end hover
			}) //end $headers.each()
			$mainmenu.find("ul").css({display:'none', visibility:'visible'})
		}) //end document.ready
	}
}

//build menu with ID="myjquerymenu" on page:
jquerycssmenu.buildmenu("navbar", arrowimages),
jquerycssmenu.buildmenu("more_njsea", arrowimages)


jQuery(function($){
	if($('#sub_page_background').length){
		var bkgd = $('#sub_page_background').outerHeight();
		var content = $('#right_column_subpage').outerHeight();
		if(content > bkgd){
			$('#sub_page_background').height((parseInt($('#right_column_subpage').outerHeight())+2000)+"px");
		};
	};
	
	// ===== IE6: Fix Misc Issues ==========================
	if($.browser.msie && $.browser.version < 7){
		// ===== MENU: Add :hover event ====================
		$.getScript('js/jquery.bgiframe.js', function(){
			$('#navbar ul').bgiframe();
		});
	};
	
	
	// ===== Carousel: If more than four images =====
	$('div.inlineMediaContainer').each(function(){
		$(this).carousel();
	});
});

// ===== InlineMediaContainer: Thumbnail Carousel ==================== 
(function($) {
	$.fn.carousel = function(options){
		// extend our default options with those provided
		var $options = $.extend({}, $.fn.carousel.defaults, options);

		return this.each(function() {
			var $this = $(this);
			var itemsVisible = 4;
			$options.container = $('.media_thumbs ul',$this);
			$options.thumbs = $('a',$options.container);
			
			if($options.thumbs.length === 1){
				$('.media_thumbs',$this).css('display','none');
			} else if($options.thumbs.length <= itemsVisible){ // == no carousel ==
				$('a.previous,a.next', $this).css('display','none');
				$('div.carousel-thumbs', $this).removeClass('carousel-thumbs');
				$('.media_thumbs',$this).css('display','block');
			} else { // == enable carousel ==
				// parameters
				$('.media_thumbs',$this).css('display','block');
				var itemSize = $($options.thumbs[0]).outerWidth();
				var itemsTotal = $options.thumbs.length;
				$options.visibleWidth = $('div.carousel-thumbs', $this).width();
				$options.container.css('width',((itemsTotal*itemSize)+itemSize)+'px');
				var pageSize = Math.floor($options.visibleWidth / itemSize);
				var pageTotal = (itemsTotal%pageSize==0)?itemsTotal:(pageSize-(itemsTotal%pageSize)+itemsTotal);
				$options.total = Math.floor(pageTotal / pageSize)-1;
				// controls
				$("a.previous", $this).css({'opacity':'0.5','cursor':'default'});
				$("a.next", $this).click(function(){
					animate("next");
					if($options.current >= $options.total) $(this).css({'opacity':'0.5','cursor':'default'});
					$("a.previous", $this).css({'opacity':'1','cursor':'pointer'});
					return false;
				});
				$("a.previous", $this).click(function(){		
					animate("prev");
					if($options.current <= 0) $(this).css({'opacity':'0.5','cursor':'default'});
					$("a.next", $this).css({'opacity':'1','cursor':'pointer'});
					return false;
				});
			}
		});
		function animate(dir){
			var p = 0;
			if(dir == "next"){
				$options.current = ($options.current >= $options.total) ? $options.total : $options.current+1;
			} else {
				$options.current = ($options.current <= 0) ? 0 : $options.current-1;
			};		
			p = ($options.current*$options.visibleWidth*-1);
			$($options.container).animate({ left: p }, 500);
		};
	};

	// default option
	$.fn.carousel.defaults = {
		container: null,
		thumbs: null,
		visibleWidth:0,
		current: 0,
		total: 0
	};
})(jQuery);