$(function() {

	// Prevent Default # clicks
	$('a[href="#"]').click( function(e) {e.preventDefault();});
});


$(document).ready( function (){

	$('.print').click(function(){window.print();});

	$('.ad-placeholder').click(function(){
			window.location = "http://www.jewishjournal.com/newspaper/advertise";
	});
	
	if($(window).width() > 1985)
	{$('.home-box-bg').addClass('home-box-bgR');}

	// Prevent Image Squishing
	$(window).resize(function() {
        if ($(window).width() < 1985) {
         	$('.home-box-bg').addClass('home-box-bgM');
	    	$('.home-box-bg').removeClass('home-box-bgR');
        } else {
            $('.home-box-bg').removeClass('home-box-bgM');
	    	$('.home-box-bg').addClass('home-box-bgR');
        }
    });

	if ($('.total_article_share_count').length) {
		$('.total_article_share_count').sharrre({
			share:	{
				googlePlus: true,
				facebook: true,
				twitter: true
				},
			enableHover: false,
			enableTracking: true,
			urlCurl: 'http://www.jewishjournal.com/js/sharrre.php'
		});
	}

	$(document).on('focusin', '.field, textarea', function() {
		if(this.title==this.value) {
			this.value = '';
		}
	}).on('focusout', '.field, textarea', function(){
		if(this.value=='') {
			this.value = this.title;
		}
	});

	$('.sort-buttons a').on('click', function(){
		$('.sort-buttons a').removeClass('selected');
		$(this).addClass('selected');
		return false;
	});

	/* ADDTHIS TRACKER */

  //  $(document).on('click', '.addthis_toolbox', function(){
  //  $(document).on('click', function(){
	$(document).ready( function (){

		function eventHandler(evt) {
			if (evt.type == 'addthis.menu.share') {
				url = evt.data.url;
				url = url.split('/').pop(); // split on "/" and get the last item
				$.get('http://www.jewishjournal.com/site/track/' + url);
				console.log(evt.data); // evt.data is an object hash containing all event data
				console.log('user shared to ' + evt.data.service);
			}

		}

		// Make sure AddThis exists before we use it; this is because it might be blocked by some browser addons
		if (typeof addthis === 'object') {
			addthis.addEventListener('addthis.menu.share', eventHandler);
		}

	});


	/* HOP-IM */
	$(window).scroll(function(){
			var navi_offset = $('.navigation').offset();

			if (!$('.navigation').hasClass('mainnav_shadow')) {
				if(navi_offset.top >= 102)
				{$('.navigation').addClass('mainnav_shadow');}
			} else {

				if (navi_offset.top <= 99) {$('.navigation').removeClass('mainnav_shadow');}

			}

	});
	/* END HOP-IM */


	if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {$('.addthis_toolbox').addClass('addthis_toolbox-absolute');}

	var win_top = $(window);
	if( $('.share-container-absolute').length ){


		var top_pos = $('.share-container-absolute').offset().top;
		var bottom_pos =  $('.share-wrapper').height();
		var set_pos;

		$(window).scroll(function(){
			set_pos = (bottom_pos -  $('.at-floatingbar-inner').height());


			if( !$('.share-wrapper').length){
				if( win_top.scrollTop() >= top_pos - 66 ){$('.share-container-absolute').addClass('share-container-fixed');} else {$('.share-container-absolute').removeClass('share-container-fixed');}
			}
			if( $('.share-wrapper').length ){

				var top_pos2 = $('.share-wrapper').offset().top;
				if( win_top.scrollTop() < top_pos2 - 66 ){
					$('.at-floatingbar-inner').css({'position':'absolute', 'bottom': 'auto','top':20});
				} else if( win_top.scrollTop() > ($(window).height() - $('.at-floatingbar-inner').height()) ){
					$('.at-floatingbar-inner').css({'position':'absolute', 'top': set_pos});
				} else {
					$('.at-floatingbar-inner').css({'position':'fixed', 'top': 76});
				}
			}

			$('.share-container').find('.shell').css('left', - win_top.scrollLeft());

		});
	}

	$( ".datepicker" ).datepicker({
		dateFormat: 'yy/mm/dd',
		onSelect: function(date) {
                window.location = "http://www.jewishjournal.com/community_calendar/" + date
          	},
		inline: true
	});


	 var availableTags = [
		"Venue 1",
		"Venue 2",
		"Venue 3",
		"Venue 4",
		"Venue 5",
		"Venue 6",
		"Venue 7",
		"Venue 8",
		"Venue 9",
		"Venue 10",
		"Venue 11",
		"Venue 12",
		"Venue 13",
		"Venue 14",
		"Venue 15",
		"Venue 16",
		"Venue 17",
		"Venue 18",
		"Venue 19",
		"Venue 20",
		"Venue 21",
		"Venue 22"
		];
		$(".autocomplete").autocomplete({
		source: availableTags,
		select: function( event, ui ) {$('.popup-link').trigger('click');}
	});

	/* HOP IM */
	$(document).ready( function() {
		$(".navigation ul:eq(0) > li").hoverIntent({
			over: showDropdown_nav,
			timeout: 150,  /* Time for navigation to create effect */
			out: hideDropdown_nav
		});

	});

	function showDropdown_nav(){
		$(this).find('.dropdown').stop(true,true).slideDown(150);
		/*strong height
		$('.dropdown .item strong').each( function (){
			if ( $(this).outerHeight() >= 57 ){$(this).addClass('small')};
		}) */
		$('.dropdown .item strong')
		$(this).addClass('hovered');
	}

	function hideDropdown_nav(){
		$('.navigation ul li').removeClass('hovered');
		$(this).find('.dropdown').stop(true,true).slideUp(150);
	}

	$('.accordion > li > a').on('click', function(){

		if( $(this).parent('li').find('ul').is(':hidden') ){
			$('.accordion > li > a').removeClass('expanded');
			$(this).addClass('expanded');
			$('.accordion ul').stop(true,true).slideUp(150);
			$(this).parent('li').find('ul').stop(true,true).slideDown(150);
		} else {
			$('.accordion > li > a').removeClass('expanded');
			$('.accordion ul').stop(true,true).slideUp(150);
		}
		return false;
	});



	$(window).load(function(){

        /* HOP IM - JA10 */
        /* - for reference */
	/* 830px - Article Headline Segment $('.page-title') */
		
	function getInnerWidth(element) {
    	  /* Currently just using a standard jquery method.. for now */
            result = element.width();
            return result;
            
        }

		   // Main Feature
			   adjustByLineNumber(3, $('.home-box .shell a h2'), "smaller");
			   adjustByLineNumber(2, $('.home-box .shell a h2'), "small");

		   // Make all dropdown titles automatically size based on line count
		   var dropdownNavCollection = $('.dropdown .item a strong');
			   dropdownNavCollection.each(function() {
			if(getLineCount($(this)) == 1) {$(this).addClass("big");}
			if(getLineCount($(this)) == 5) {$(this).addClass("smallest");}
					  adjustByLineNumber(3, $(this), "smaller");
					  adjustByLineNumber(2, $(this), "small");
			   });

			// Make all dropdown titles automatically size based on line count
		   var homepageSliderCollection = $('.slider .entry h4 a');
			   homepageSliderCollection.each(function() {
		   // if(getLineCount($(this)) == 1) {$(this).addClass("big");}
			//if(getLineCount($(this)) == 5) {$(this).addClass("smallest");}
					  adjustByLineNumber(4, $(this), "smaller");
					  adjustByLineNumber(3, $(this), "small");
			//		  adjustByLineNumber(2, $(this), "small");
			   });


        

		function adjustByLineNumber (minimumSize, adjustedObject, adjustClass) {
			if(getLineCount($(adjustedObject)) > minimumSize)
			{$(adjustedObject).addClass(adjustClass);}
		}
		
		
		function adjustByElementWidth (minimumSize, adjustedObject, adjustClass) {
		    if(getInnerWidth($(adjustedObject)) > minimumSize) {$(adjustedObject).addClass(adjustClass);}
		
		}
		
		adjustByElementWidth(760, $('.page-title .shell h2 a'), "small");
		adjustByElementWidth(760, $('.page-title .shell h2 a'), "smaller");
		
		// Generic Line Count.
		function getLineCount(variElementBox) {
				// Temporarily make element visible (if not already)
				/* original jsbin.com/ihakid/2/edit */
				$.fn.actualLineHeight = function(){
				// find the closest visible parent and get it's hidden children
				var visibleParent = this.closest(':visible').children(),
					thisHeight;

				// set a temporary class on the hidden parent of the element
				visibleParent.addClass('temp-show');

				// get the line height
				// thisHeight = parseInt($(this).css('line-height'), 10);

				// get the height
				thisHeight = $(this).height();

				thisLineHeight = parseFloat($(this).css('line-height'));

				// remove the temporary class
				visibleParent.removeClass('temp-show');

				return [thisLineHeight, thisHeight];
				};

				var elementHeight = $(variElementBox).actualLineHeight();
				var elementLineCount = elementHeight[1] / elementHeight[0];

				return elementLineCount;
		}



		$('body').on('click','.popup-link',function(){
			$(this).colorbox({
				onComplete: function(){
					$(".custom-select select").chosen({disable_search: true});
					if ( $('.cboxPhoto').length == 0 ){$('#cboxClose').hide()};
				}
			});
		});

		if( $('.custom-select').length ){
			$(".custom-select select").chosen({disable_search: true});
		}

		if ($(".social-media ul").length != 0) {
			$(".social-media ul").carouFredSel({
				auto: 5000,
				items: 1,
				scroll: {
					items: 1,
					fx: "crossfade"
				},
				pagination: {
					container: ".pagination"
				}
			});
		}

		if ($(".slider ul").length != 0) {
			$(".slider ul").carouFredSel({
				auto: 10000,
				responsive: true,
				items: {
					visible: 4,
					width: 230,
					padding: [0, 20, 0, 20]
				},
				scroll: {
					items: 2,
					fx: "slide"
				},
				prev: ".prev",
				next: ".next"
			});

			$('.caroufredsel_wrapper .entry').hover(
				function() {
					$('.slider ul').trigger('pause', true); 
				
				}, function() {
					$('.slider ul').trigger('pause', false); 
			
				}			
			);
		}

		if ($(".calendar-tabs .tab-entries > ul").length != 0) {
			$(".calendar-tabs .tab-entries > ul").carouFredSel({
				auto: false,
				circular: false,
				items: 1,
				scroll: {
					items: 1,
					fx: "slide",
					pauseOnHover: true
				},
				pagination: {
					container: ".calendar-tabs .tab-nav ul",
					anchorBuilder: false
				}
			});
		}

		if ($(".trending .tab-entries").length != 0) {
			$(".trending .tab-entries > ul").carouFredSel({
				auto: false,
				circular: false,
				items: 1,
				scroll: {
					items: 1,
					fx: "slide"
				},
				pagination: {
					container: ".trending .tab-nav ul",
					anchorBuilder: false
				}
			});
		}


		if ($(".video-slider").length != 0) {
			$(".video-slider").each(function(){
				$(this).find('> ul').carouFredSel({
					auto: false,
					items: 1,
					infinite: false,
					circular: false,
					scroll: {
						items: 1,
						fx: "slide"
					},
					pagination: {
						container: $(this).parents('.boxes').find(".pages span"),
						anchorBuilder: false
					}
				});
			}); 
		}
	});

	if ( $('.boxes-three').length){$('.boxes-three').find('li:nth-child(4)').css('clear','both')};


	function unfix_socials() {
		var entry = $('.article-entry');
		if (entry.length) {
			var entry_top = entry.offset().top;
			var entry_bottom = entry_top+entry.outerHeight();

			var socials = $('.share-container-absolute .addthis_toolbox');
			var socials_height = socials.outerHeight();

			var win = $(window);
			var win_h = win.height();

			var nav_height = 70;

			var scroll_top = win.scrollTop();


//			if( scroll_top > entry_bottom - win_h + (win_h-socials_height-nav_height)) {
//				socials.parent().addClass('share-container-bottom');
//			} else {
//				socials.parent().removeClass('share-container-bottom');
//			}
			if( scroll_top > entry_bottom - win_h + (win_h-socials_height-nav_height)) {socials.parent().parent().addClass('share-container-bottom');} else {socials.parent().parent().removeClass('share-container-bottom');}
			
		}
	}

$(window).scroll( unfix_socials );
$(window).resize( unfix_socials );
 



});