/* Author: 
	* Charles X. Morrissey
	* version: 081111
*/

(function($){
	
	$(function(){
		
		//$(".post-container article.listing").click(function(){
		//	window.location=$(this).find("a.arrow-link").attr("href");return false;
		//});
		
		// Placeholder Support for IE & Opera
		$('[placeholder]').focus(function() {
			var input = $(this);
			if (input.val() == input.attr('placeholder')) {
				input.val('');
				input.removeClass('placeholder');
			}
		}).blur(function() {
			var input = $(this);
			if (input.val() == '' || input.val() == input.attr('placeholder')) {
				input.addClass('placeholder');
				input.val(input.attr('placeholder'));
			}
		}).blur().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function() {
				var input = $(this);
				if (input.val() == input.attr('placeholder')) {
					input.val('');
				}
			})
		});
		
		// Mailing List Signup
		$(".custom-checkbox").each(function(){
			$(this).tristate({
				children: $(this).parent().find("ul>li>input[type='checkbox']"),
				classes: {
					checkbox: "customcheck",
					checked: "customcheckfull",
					partial: "customcheckpartial",
					unchecked: "customchecknone"
				}});
			});
		
		$('#join-btn, #ml-close-btn').click(function(e){
			e.preventDefault();
			$('#join-btn').toggleClass('selected');
			$('#mailing-list-container').fadeToggle(200); 
			return false;
		});
		
		// Soho Booking Tool Modal
		// $(".openBookingTool").fancybox({
		// 	'width': 970,
		// 	'height': 498,
		// 	'scrolling': 'no',
		// 	'padding': 0,
		// 	'margin': 0,
		// 	'overlayOpacity': 0.6,
		// 	'overlayColor': '#000',
		// 	'autoScale': false,
		// 	'centerOnScroll': true,
		// 	'transitionOut': 'none',
		// 	'type': 'iframe'
		// });
		
		
		// Hero Carousel
		$("#hero-carousel").jcarousel({
			scroll: 1,
			animation: 550,
			easing: 'easeOutQuad',
			wrap: "circular",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			auto: 3,
			initCallback: function(carousel, state) {
				$("#hero-carousel").carouselWithPager({
					autoHide: true,
					container: '#hero-container',
					obj: carousel,
					state: state
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			},
			itemVisibleInCallback: {
				onAfterAnimation: onCurrentSlide
			}
		});
		
		
		
		
		// Star Rating
		$('#star-rating').raty({
			//start: 3,
			number: 4,
			size: 20,
			path:	'_assets/img/neighborhood-guide/',
  			click: function(score, evt) {
    			log('ID: ' + this.attr('id') + '\nscore: ' + score + '\nevent: ' + evt);
  			}
		});
		
		// More - Less Overlays
		$('.more-btn, .less-btn').click(function(e){
			e.preventDefault();
			var container = $('.more-container.' + $(this).attr('href')); 
			$(container).css({
				top : ($('#find-filter-container').offset().top - $(container).height()/2) + 7
			}).fadeToggle(200);
			return false;
		});
		
		
		// Soho Carousels Autoplay
		$("#soho-carousel").jcarousel({
			scroll: 1,
			animation: 550,
			easing: 'easeInOutQuad',
			wrap: "circular",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			auto: 4,
			initCallback: function(carousel, state) {
				$("#soho-carousel").carouselWithPager({
					container: '#soho-carousel-container',
					obj: carousel,
					state: state
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			},
			itemVisibleInCallback: {
				onBeforeAnimation: onBeforeSlide,
				onAfterAnimation: onCurrentSlide
			}
		});
		
		// Soho Carousels Still
		$("#soho-carousel-still").jcarousel({
			scroll: 1,
			animation: 550,
			easing: 'easeInOutQuad',
			wrap: "circular",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			auto: 0,
			initCallback: function(carousel, state) {
				$("#soho-carousel-still").carouselWithPager({
					container: '#soho-carousel-container-still',
					obj: carousel,
					state: state
				});
			},
			itemVisibleInCallback: {
				onBeforeAnimation: onBeforeSlide,
				onAfterAnimation: onCurrentSlide
			}
		});
		
		// Soho Galleries
		$('#soho-carousel-thumb').roomGallery();
		
		$('#image-carousel-thumb').imageGallery();
		
		// Tribeca Carousels
		$("#tribeca-carousel").jcarousel({
			scroll: 1,
			animation: 550,
			easing: 'easeInOutQuad',
			wrap: "circular",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			auto: 4,
			initCallback: function(carousel, state) {
				$("#tribeca-carousel").carouselWithPager({
					container: '#tribeca-carousel-container',
					obj: carousel,
					state: state
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			},
			itemVisibleInCallback: {
				onBeforeAnimation: onBeforeSlide,
				onAfterAnimation: onCurrentSlide
			}
		});
		
		// Tribeca Carousels Still
		$("#tribeca-carousel-still").jcarousel({
			scroll: 1,
			animation: 550,
			easing: 'easeInOutQuad',
			wrap: "circular",
			buttonNextHTML: null,
			buttonPrevHTML: null,
			auto: 0,
			initCallback: function(carousel, state) {
				$("#tribeca-carousel-still").carouselWithPager({
					container: '#tribeca-carousel-container-still',
					obj: carousel,
					state: state
				});
			},
			itemVisibleInCallback: {
				onBeforeAnimation: onBeforeSlide,
				onAfterAnimation: onCurrentSlide
			}
		});
		
		// Tribeca Galleries
		$('#tribeca-carousel-thumb').roomGallery();
		
		// Culture Gallery
		$('#culture-carousel-thumb').imageGallery();
		
		// Culture Right-Column Carousels
		$('#contributors-carousel').jcarousel({
			vertical: true,
			scroll: 9,
			animation: 550,
			easing: 'easeInOutQuad'
		});
		
		$('.col-right #twitter_update_list').jcarousel({
			vertical: true,
			scroll: 3,
			animation: 600,
			easing: 'easeInOutQuad'
		});
				
		$('#twitter-feed #twitter_update_list').jcarousel({
			vertical: true,
			scroll: 2,
			animation: 600,
			easing: 'easeInOutQuad'
		});
		
		$('.parties #parties-list, .happenings #happenings-list').jcarousel({
			scroll: 1,
			animation: 500,
			easing: 'easeInOutQuad'
		});
	});
	// End Dom Ready
	
	
	$(window).load(function(){

		// Latest Widget
		$('#image-viewport').cycle({
			speed: 600,
			timeout: 0,
			//easing: 'easeInOutSine',
			pager: '#latest-navigation',
			pagerEvent: 'mouseover',
			//fastOnEvent: 250,
			pagerAnchorBuilder: function(id, slide) {
				$('#latest-navigation li:eq(' + id + ') a').makeLatestRollovers({ slideid: id });
			}
		});
		
		// Upcoming Events Carousel
		$("#upcoming-events-container ul").jcarousel({
			scroll: 1,
			animation: 400,
			easing: 'swing'
		});
		
		// Instagram Slideshow
		$("#instagram-container ul").cycle({
			fx: 'fade',
			speed: 'slow',
			timeout: 0,
			next: '#instagram-next-btn',
			prev: '#instagram-prev-btn'
		});
		
		// Following Carousel
		$('#following-container ul').jcarousel({
			vertical: true,
			scroll: 9
		});
		
		// Featured Listing Slideshow
		$("#featured-posts").cycle({
			fx: 'fade',
			timeout: 0,
			speed: 'slow',
			next: '#featured-next-btn',
			prev: '#featured-prev-btn'
		});
		
		// Best Of Slideshow
		$("#best-of").cycle({
			fx: 'fade',
			timeout: 0,
			speed: 'slow',
			next: '#bestof-next-btn',
			prev: '#bestof-prev-btn'
		});
		
		// Parties Slideshow
		$("#parties-widget ul").cycle({
			fx: 'fade',
			timeout: 0,
			speed: 1000,
			next: '#parties-next-btn',
			prev: '#parties-prev-btn'
		});
		
		// Post Equal Heights
		$('.post.equal-height').equalHeight();
		
		// Past Events Carousel
		$("#past-events-container ul").jcarousel({
			scroll: 5,
			wrap: "circular",
			animation: 600,
			easing: 'swing'
		});
		
		// The Locals Thumb RollOver
		$(".locals-thumbnail-grid li img").animate({opacity:1}, 500);
		
		// clone image
		$('.locals-thumbnail-grid li img').each(function(){
			var el = $(this);
			el
			.css({"position":"absolute"})
			.wrap("<div class='img_wrapper'>")
			.clone()
			.addClass('img_grayscale')
			.css({"position":"absolute","z-index":"998","opacity":"0"})
			.insertBefore(el)
			.queue(function(){
				var el = $(this);
				el.parent().css({"width":this.width,"height":this.height});
				el.dequeue();
			});
			if (Modernizr.canvas) {
				this.src = grayscale(this.src);
			}
		});
		
		// Fade image 
		$('.locals-thumbnail-grid li').mouseover(function(){
			if (!$(this).hasClass("selected")) {
				$(this).find('img').stop().animate({opacity:1}, 500);
			}
		});
		
		$('.locals-thumbnail-grid li').mouseout(function(){
			if (!$(this).hasClass("selected")) {
				$('.img_grayscale', this).stop().animate({opacity:0}, 500);
			}
		});
		
		var $activeInsert = null;
		$('.locals-thumbnail-grid li a').click(function(e){
			e.preventDefault();
			var $thumbLink = $(this);
			var container = $activeInsert = $(this).attr('data-target');
			var url = $(this).attr('href');
			
			$('.locals-thumbnail-grid li').removeClass('selected');
			$('.img_grayscale').stop().animate({opacity:0}, 500);
			$thumbLink.parent().addClass('selected')
			$thumbLink.parent().find('img').stop().animate({opacity:1}, 500);
			
			$('.selected-local-container').not($(container)).slideUp('slow', function() {
				$(this).find('.selected-local').hide();
			});
						
			$(container).find('.holder').load(url, function(){
				var parts = container.split("#");
				var trgt = parts[1];
				var target_offset = $("#"+trgt).offset();
				var target_top = target_offset.top;		
						
				$(this).parent().slideDown('fast', function() {
					$(this).find('.selected-local').show();
					target_offset = $("#"+trgt).offset();
					target_top = target_offset.top;
					$('html, body').animate({ scrollTop: target_top - 45 }, 500);				
					
				});
				$(this).parent().find('.selected-local').animate({
					left: $thumbLink.parent().position().left
				}, 200);
			});
			return false;
		});
		
		// Insert Close
		$('a#insert-close-btn').live('click', function(e){
			e.preventDefault();
			var container = $($activeInsert);
			$('.locals-thumbnail-grid li').removeClass('selected');
			$('.img_grayscale').stop().animate({opacity:0}, 500);
			$('.selected-local').hide();
			container.slideUp('normal', function() {
				$('html, body').animate({ scrollTop: container.offset().top - 200 }, 500);				
				$('#local-profile-container').remove();
			});
			return false;
		});
		
		// Map Cover Sliders
		$('#mapslide').click(function(){
				$(this).stop().animate({top:405}, 500);
		});
		
		$('#mapslide-tgh').click(function(){
				$(this).stop().animate({top:405}, 500);
		});
		
		$('#mapslide-sgh').click(function(){
				$(this).stop().animate({top:405}, 500);
		});
		
		$('#mapslide-soho').click(function(){
				$(this).stop().animate({top:424}, 500);
		});
		
		$('#mapslide-tribeca').click(function(){
				$(this).stop().animate({top:424}, 500);
		});
		
		// Culture Carousel Caption Show/HIde
		//$('#culture-carousel-gallery-container .inner').hover(function(){
		//		$('#caption-container').stop().animate({bottom:0}, 500);
		//	}, function() {
		//		$('#caption-container').stop().animate({bottom:-30}, 500);	
		//});
		
		$('#left-column .post:nth-child(2n)').addClass('left');		
		$('#left-column .post:nth-child(4n)').addClass('left');			

	});
	// End Window Ready
	
	if (Modernizr.canvas) {
		// Grayscale w canvas method
		function grayscale(src){
			var canvas = document.createElement('canvas');
			var ctx = canvas.getContext('2d');
			var imgObj = new Image();
			imgObj.src = src;
			canvas.width = imgObj.width;
			canvas.height = imgObj.height;
			ctx.drawImage(imgObj, 0, 0); 
			var imgPixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
			for(var y = 0; y < imgPixels.height; y++){
				for(var x = 0; x < imgPixels.width; x++){
					var i = (y * 4) * imgPixels.width + x * 4;
					var avg = (imgPixels.data[i] + imgPixels.data[i + 1] + imgPixels.data[i + 2]) / 3;
					imgPixels.data[i] = avg;
					imgPixels.data[i + 1] = avg;
					imgPixels.data[i + 2] = avg;
				}
			}
			ctx.putImageData(imgPixels, 0, 0, 0, 0, imgPixels.width, imgPixels.height);
			return canvas.toDataURL();
		}
	}
	else {
		// No Canvas Support
	}
	
})(jQuery);
