var windowHeight = $(window).height();

$(function() {

	$.param.fragment.ajaxCrawlable( true );

	var $itemsWrapper = $('#photostream ul');

	var headerHeight = $('#header').height();
	
	var contentHeight = windowHeight - headerHeight;
	
	var url = "";
	
	var cache = {'': $('.bbq-default')};
	
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
		{
			id: "48299261@N02",
			lang: "fr-fr",
			format: "json"
		},
		function(data) {
			$.each(data.items, function(i,item) {
				if (i == 0) {
					var $listItem = $('<li />').addClass('focused');
				} else {
					var $listItem = $('<li />');
				}
				$photo = $('<img />')
					.attr({'src':item.media.m.replace('_m', '_b'), 'alt':item.media.title})
					.appendTo($listItem)
					.load(function() {
						$(this).height(contentHeight).fadeIn();
					});
				$listItem.appendTo($itemsWrapper);
			});
		}
	);

	$(window).bind('hashchange', function(e) {
		var url = e.fragment;
		$('a.bbq-current').removeClass('bbq-current');
		$('.bbq-content').children(':visible').fadeOut(200);
		url && $('a[href="#!' + url + '"]').addClass('bbq-current');
		if ( cache[ url ] ) {
			cache[ url ].fadeIn(500);
		} else {
			$('.bbq-loading').show();
			cache[ url ] = $('<div class="bbq-item" style="display:none;"/>')
				.appendTo('.bbq-content')
				.load( url, function(){
					$('.bbq-loading').hide();
					if ($(this).height() < contentHeight) {
						$(this).height(contentHeight);
					}
					$(this).fadeIn(500);
					if (url == "boutiques.html") {
						gMapInit("boutiques");
					} else if (url == "contact.html") {
						gMapInit("contact");
						$("#contactform").validate({
							submitHandler: ajaxFormSubmit
						});
					} else if (url == "newsletter.html") {
						$("#newsletterform").validate({
							submitHandler: ajaxFormSubmit
						});
					}
				});
		}
		if ($(window).width() < 1024 && url != "") {
			$('#nextphoto').hide();
		} else {
			$('#nextphoto').show();
		}
	});

	$(window).bind('resize', function() {
		windowHeight = $(window).height();
		contentHeight = windowHeight-headerHeight;
		var $contentWrapper = $('#content div.bbq-item');
		$itemsWrapper.find('li img').height(contentHeight);
		if ($contentWrapper.height() < contentHeight) {
			$contentWrapper.height(contentHeight);
		}
		if ($(window).width() < 1024 || url != "") {
			$('#nextphoto').hide();
		} else {
			$('#nextphoto').css('top', windowHeight / 2).show();
		}
	});
	
	$('#nextphoto')
		.css('top', windowHeight / 2)
		.hover(
			function(){
				$(this).stop().fadeTo('fast', 0.9);
			}, function() {
				$(this).stop().fadeTo('fast', 0.7);
			}
		)
		.click(function(e) {
			var $items = $itemsWrapper.find('li');
			var $focusedItem = $items.filter('.focused');
			$itemsWrapper.filter(':not(:animated)').animate({
					left: '-='+($focusedItem.width()+5)
				}, 1000, function() {
					$focusedItem.removeClass('focused').next().addClass('focused');
					$focusedItem.clone().appendTo($itemsWrapper);
				}
			);
		})
		.show();	
	
	$('#newswrapper').rssfeed('http://lartisanfleuriste.wordpress.com/feed/', {
		limit: 4,
		header: false,
		date: false,
		snippet: false,
		content: false,
		showerror: false,
		media: false
	});
	
	$('form input.button').live('hover', function() {
		$(this).toggleClass("hover");
	});
	
	$(window).trigger('hashchange');

});

function gMapInit(rub) {
	// map 1
	var address1 = new google.maps.LatLng(48.860169, 2.361036);
	var mapDiv1 = document.getElementById(rub + '-map1');
	var map1 = new google.maps.Map(mapDiv1, {
		center: address1,
		zoom: 15,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var marker1 = new google.maps.Marker({
		position: address1,
		map: map1
	});
	// map 2
	var address2 = new google.maps.LatLng(48.852757, 2.32441);
	var mapDiv2 = document.getElementById(rub + '-map2');
	var map2 = new google.maps.Map(mapDiv2, {
		center: address2,
		zoom: 15,
		mapTypeId: google.maps.MapTypeId.ROADMAP
	});
	var marker2 = new google.maps.Marker({
		position: address2,
		map: map2
	});
}

function ajaxFormSubmit(form) {
	var $form = $(form);
	var values = $form.serialize();
	var $inputs = $form.find('input');
	var $fields = $form.find('input:text, textarea');
	$inputs.attr('disabled', 'disabled');
	$.post('app.php', values, function(data) {
		if (data.Response == "success") {
			$form.find('div.response-container').text(data.Msg).addClass("success").show("fast");
		} else if (data.Response == "error") {
			$form.find('div.response-container').text(data.Msg).addClass("error").show("fast");
		}
		$inputs.removeAttr('disabled');
		$fields.val("");
	}, 'json');	
	return false;
}
