/* Author: 

*/

jQuery(function($){
	
	$(".gallery li a").prettyPhoto({animation_speed:'normal',theme:'light_square',slideshow:3000, autoplay_slideshow: false});
	$(".popup").prettyPhoto({animation_speed:'normal',theme:'light_square'});

	function notIE6() {
		if (typeof document.body.style.maxHeight === "undefined") {
			return false;
		} else {
			return true;
		}
	}
	
	if ($("#google-map").size() > 0) {		
		var latlng = new google.maps.LatLng(51.46722, -0.2201599);
	    var myOptions = {
	      zoom: 14,
	      center: latlng,
	      mapTypeId: google.maps.MapTypeId.ROADMAP
	    };
	    var map = new google.maps.Map(document.getElementById("google-map"),
	        myOptions);

		var image = new google.maps.MarkerImage('img/map-icon.png',
	      // This marker is 20 pixels wide by 32 pixels tall.
	      new google.maps.Size(40, 57),
	      // The origin for this image is 0,0.
	      new google.maps.Point(0,0),
	      // The anchor for this image is the base of the flagpole at 0,32.
	      new google.maps.Point(20, 56));
	      
		var marker = new google.maps.Marker({
	        position: latlng,
	        map: map
	    });
	};
		
	// SUB-NAVIGATION
	
		/*
		$("#top-nav li").hover(function() {
			var subnav = $(this).attr("rel");
			if (!$(this).hasClass("on")) {
				$(".sub-nav-wrapper.on").hide();
			}
			$("#" + subnav).addClass("hover");
			$('#logo').addClass('noclick');
			$(this).addClass("hover");
		}, function() {
			var subnav = $(this).attr("rel");
			$("#" + subnav).removeClass("hover");
			$('#logo').removeClass('noclick');
			$(".sub-nav-wrapper.on").show();
			$(this).removeClass("hover");
		});
	
		$("#sub-nav-1").hover(function() {
			
			$(this).addClass('hover');
			$('#logo').addClass('noclick');
			$('li[rel="sub-nav-1"]').addClass('hover');
			}, function() {
			$(this).removeClass('hover');
			$('#logo').removeClass('noclick');
			$('li[rel="sub-nav-1"]').removeClass('hover');
		});
		
		$("#sub-nav-2").hover(function() {
			$(this).addClass('hover', function(){
				$('#logo').addClass('noclick');
			});
			$('li[rel="sub-nav-2"]').addClass('hover');
			}, function() {
			$(this).removeClass('hover' , function(){
				$('#logo').removeClass('noclick');
			});
			$('li[rel="sub-nav-2"]').removeClass('hover');
		});
		*/
		
		
		// TOP NAV
		
		$("#top-nav li").mouseenter(function() {
			$(this).addClass("on");
			var subnav = $(this).attr("rel");
			$("#" + subnav).stop(true, true).show();
			$(this).children("img").stop(true, true).show();
		});
		

		$("#top-nav li").mouseleave(function() {
			$(this).removeClass("on");
			var subnav = $(this).attr("rel");
			$("#" + subnav).delay(100).hide(10);
			$(this).children("img").delay(100).hide(10);
		});
		
		
		// SUB NAV
		
		$("#sub-nav-1, #sub-nav-2").mouseenter(function() {
			$(this).stop(true, true).show();
		})
		
		$("#sub-nav-1, #sub-nav-2").mouseleave(function() {
			$(this).delay(100).hide(10);
		})
		
		
		// SUB NAV DOESN'T DISAPPEAR WHEN ON LOGO
		
		$("#logo").mouseenter(function() {
			if ($("#sub-nav-1").is(":visible")) {
				$("#sub-nav-1").stop(true, true);
			}
			
			if ($("#sub-nav-2").is(":visible")) {
				$("#sub-nav-2").stop(true, true);
			}
		});
		
		
		
		
		

	$(".twitterbtn").click(function() {
		$('#twitter').css('z-index','2');
		$('#facebook').css('z-index','1');
		$(this).addClass('on');
		$('.facebookbtn').removeClass('on');
		$('#arrow').removeClass('facebook').addClass('twitter');
	});

	$(".facebookbtn").click(function() {
		$('#twitter').css('z-index','1');
		$('#facebook').css('z-index','2');
		$(this).addClass('on');
		$('.twitterbtn').removeClass('on');
		$('#arrow').removeClass('twitter').addClass('facebook');
	});

	// FORM SELECT STYLING - not in IE6
	
	if (!$("html").hasClass("ie6")) {
		
		$('select.select').each(function(){     
		var title = $(this).children('option:selected').html();
	  
			$(this).css({'z-index':10,'opacity':0,'-khtml-appearance':'none'})
			  .after('<span class="select">' + title + '</span>')
			  .change(function(){
				val = $('option:selected',this).text();
				$(this).next().text(val);
			});
		 });
		 
	}

	$('a#hideimage').live('click', function(){
		$('#rotation').animate({height: '68px'}, 800, 'swing');
		$(this).fadeOut();
		$('#rotationimages').animate({opacity: '0'}, 600, 'swing', function(){
			$('a#hideimage').attr('id', 'showimage').html('show image').fadeIn();
		});
	});
	$('a#showimage').live('click', function(){
		$(this).fadeOut(function(){
		$('#rotation').animate({height: '333px'}, 800, 'swing');
		$('#rotationimages').animate({opacity: '1'}, 1000, 'swing');
		$(this).delay(200).fadeIn('slow');
		$(this).attr('id', 'hideimage').html('hide image');
		});
	});
	
	if ($('#sub-nav-1').hasClass('on')) {
		$('#logo').addClass('noclick');
	};
	if ($('#sub-nav-2').hasClass('on')) {
		$('#logo').addClass('noclick');
	};


	$('#rotationimages').cycle({fx: 'fade'});

	$.getJSON('https://graph.facebook.com/thehalfmoonputney/statuses?limit=1&callback=?&date_format=d.m.y&access_token=AAACEdEose0cBAOUbDdL1ZCY8vf56TrH9vEEW6YCefKRiiPhQ4EhmsClHG3XGHLtiubeaAwidhS9O9Cb3gLzwYzQLZA1FgZD&expires_in=0&scope=offline_access', {},  function (json) {  
		$.each(json.data,function(i,fb){
			$('#facebook p').html(fb.message);
			$('#facebook span').html("<a target='_blank' href='https://www.facebook.com/thehalfmoonputney/posts/" + fb.id + "'>" + fb.from.name + ", " + fb.updated_time + "</a>");
		});
	})

	//events slider in footer	
	var imagesWidth = 0;
	var imageWidth = $('.slide:first').width();
	var count = 0;
	var imageCount = 0;
	var imageLeft = 0;
	var slideCount = 0;

	$(".slides .slide").each(function(){
		imagesWidth = imagesWidth + $(this).width() + 65;
		slideCount ++;
	});


	$('.artistlink').click(function(e){

		var id = $(this).attr('href');
		var target = $(id);
        if (target.length)
        {
            var top = target.offset().top;
            $('html,body').animate({scrollTop: top}, 1000);
            return false;
        }



	});

	$('.slides').width(imagesWidth);	

	$("#events-slider #slider-next").click(function(){				
		if (slideCount < 4) { return; };
		if($('.slides .slide:not(":animated")').length != 0) {			
			$(this).parent().parent().find(".slide:first").addClass('move-0');
			firstImage = $(".slides").children('.slide.move-0').clone();
			$(".slides").append(firstImage);
			$(".slide").animate({'left':'-=230'},function(){
				$(".slide:last").removeClass('move-0');
				$(".slide.move-0").remove();
				$(".slide").css('left','-0px');
				$(".move-0").removeClass('move-0');
			})
		}
		return false;
	});

	$("#events-slider #slider-prev").click(function(){						
		if (slideCount < 4){ return; };
		if($('.slides .slide:not(":animated")').length != 0) {
			$(".slide:last").addClass('move-1');
			firstImage = $(".slide.move-1").clone();									
			$(".slides").prepend(firstImage);	
			$(".slide").css('left','-230px');
			$(".slide").animate({'left':'+=230'},function(){
				$(".slide:first").removeClass('move-1');
				$(".slide.move-1").remove();
				$(".slide").css('left','-0px');
				$(".move-1").removeClass('move-1');
			})
		}
		return false;
	});

	$('#datesearch').datePicker();
})
