$(document).ready(function() {
		// as of 1.4.2 the mobile safari reports wrong values on offset()
		// http://dev.jquery.com/ticket/6446
		// remove once it's fixed
		if ( /webkit.*mobile/i.test(navigator.userAgent)) {
		(function($) {
		 $.fn.offsetOld = $.fn.offset;
		 $.fn.offset = function() {
		 var result = this.offsetOld();
		 result.top -= window.scrollY;
		 result.left -= window.scrollX;
		 return result;
		 };
		 })(jQuery);
		}

		$('.scroll').click(function(e) {
			e.preventDefault();
			var loc = $(this).attr('href').replace(/.*#/, '');
			$('html,body').animate({scrollTop: $('a[name="' + loc + '"]').offset().top},'slow');
			return false;
		});

		
		var cycles = ["assets/main/images/front_balloons.jpg", "assets/main/images/front_group.jpg", "assets/main/images/front_dance.jpg", "assets/main/images/front_ballet.jpg"];
		//var current = cycles.length - 1;
		var current = 1;
		var delay_time = 5;
		var fade_time = 1;

		function cycle() {
			if(current == 0) {
				var temp = $('#cycle img').eq(cycles.length - 1);
				temp.fadeIn(fade_time * 1000, function() {
					current = cycles.length - 1;

					// Make all images visible
					$('#cycle img').fadeIn(0);
					
					setTimeout(cycle, delay_time * 1000);
				});
			} else {
				var temp = $('#cycle img').eq(current);
				temp.fadeOut(fade_time * 1000, function() {
					current -= 1;
					setTimeout(cycle, delay_time * 1000);
				});
			}
		}

		function loadFacebookPhotos() {
			var output = '<ul>';
			$.getJSON('fb-proxy', function(data) {
					var total = data.data.length;
					var start = 0;
					var counter = 0;
					if(total > 9) {
						start = Math.floor(Math.random() * (total - 9));
						total = 9;
					}
					//start = 3;
					for(i = start; i < start+total; i++) {
						output += '<li><a href="' + data.data[i].link + '" target="blank"><img src="' + data.data[i].picture + '" /></a></li>';
						//output += '<li><img src="' + data.data[i].picture + '" /></li>';
						if(counter % 3 == 2) {
							// IE has some kind of clear/float bug
							//output += '<li class="clear"></li>';
							output += '<span class="clear"></span>';
						}
						counter++;
					}
					output += '<span class="clear"></span>';
					output += '</ul>';

					$('.home #photos').html(output);
			});
		}

		// Load images and start cycle
		function loadImages() {
			var img = new Image();
			img = $(img);

			img.load(function() {
				current++;
				if(current == cycles.length && current > 0) {
					current--;
					setTimeout(cycle, delay_time * 1000);
					loadFacebookPhotos();
				} else {
					loadImages();
				}
			});

			$('#cycle').prepend(img);

			img.attr('class', 'cycle');
			img.attr('src', cycles[current]);
		}

		loadImages();

		var sliding = 'down';
		$('#nav_dd ul ul').each(function() {
				$(this).parent().hover(
					function() {
					sliding = 'down';
					$('#wrap_drop').css('height', '420px');
					$('ul', this).slideDown(200);
					},
					function() {
					//$('ul', this).hide();
					sliding = 'up';
					$('ul', this).slideUp(200, function() {
						if(sliding == 'up') {
							$('#wrap_drop').css('height', '70px');
						}
					});
					}
				);
		});

});


