function getMarkerContent() {
	var locationcontent = $("#location .markercontent").html();
	var template = 
		'<div id="locationmarker">'+
			'<div class="locationaddress">'+
				locationcontent+
				'<div style="margin-top: 5px; font-size: 12px;">'+
					
					'<form id="locationform" action="locationsearch.php" enctype="multipart/form-data" method="post" onsubmit="return locformsubmit();"><label for="startingaddress">Directions from:</label><input type="text" id="startingaddress" name="startingaddress" /></form>'+
				'</div>'+
			'</div>'+
			'</form>'+

		'</div>'
	return template;
}

function openLocation() {
	if (!$("#location").hasClass("open")) {
		toggleItem("#location", "large");
		$("#map_canvas").animate( {
			width: "320px",
			height: "420px"
		}, function() {
			$("#map_canvas").css("width", "320px").css("height", "420px");
			google.maps.event.trigger(map, 'resize');
			map.setCenter(centerlatlnglg);

			

			window.setTimeout(function() {
				$("#map_canvas").css("width", "320px").css("height", "420px");
				google.maps.event.trigger(map, 'resize');
				marker.infowindow.open(map,marker);
			}, 300);
		});
	}
}



function closeLocation() {
	if ($("#location").hasClass("open")) {
		map.setZoom(17);
		toggleItem("#location", "small");
		
		$("#map_canvas").animate( {
			width: "155px",
			height: "203px"
		}, function() {
			google.maps.event.trigger(map, 'resize');
			marker.infowindow.close();
			map.setCenter(centerlatlngsm);
			
	
		});
	}
}

function locformsubmit() {
	var startaddress = $("#startingaddress").val()
	var endaddress = "Via Privata Massimo Bontempelli 20125 Milano, Italy";
	
	var gmapurl = "http://maps.google.com/?daddr="+locationlat+","+locationlng+"&saddr="+startaddress;
	window.open(gmapurl);
	return false;
}

var questionsubmit = true;
var postertemplate;
var itemoffset = 200;
var map;
var marker;
var markerbuilt = false;

/*
var locationlat = 45.4840668;
var locationlng = 9.2435261;
var locationlatlng = new google.maps.LatLng(locationlat, locationlng);
var centerlatlngsm = new google.maps.LatLng(45.48410319145017, 9.243559002018754);
var centerlatlnglg = new google.maps.LatLng(45.484847870810455, 9.243966697789018);
*/
var locationlat;
var locationlng;
var locationlatlng;
var centerlatlngsm;
var centerlatlnglg;

function initMap() {
	locationlat = parseFloat($("#location input[name=location_lat]").val());
	locationlng = parseFloat($("#location input[name=location_lng]").val());
	locationlatlng = new google.maps.LatLng(locationlat, locationlng);

	centerlatsm = locationlat - .00000363914502;
	centerlngsm = locationlng - .00000329020188;

	centerlatlg = locationlat - -0.00078107081;
	centerlnglg = locationlng - -0.000440597789;
	
	centerlatlngsm = new google.maps.LatLng(centerlatsm, centerlngsm);
	centerlatlnglg = new google.maps.LatLng(centerlatlg, centerlnglg);

	var myOptions = {
		zoom: 17,
		center: centerlatlngsm,
		scrollwheel: false,
		mapTypeControl: false,
		mapTypeId: google.maps.MapTypeId.ROADMAP,
		navigationControl: true,
		navigationControlOptions: {
			style: google.maps.NavigationControlStyle.SMALL
		}

	};
	
	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	var contentString = getMarkerContent();
	
	marker = new google.maps.Marker({
		position: locationlatlng, 
		map: map, 
		draggable: false,
		infowindow: new google.maps.InfoWindow({
			content: contentString
		})
	});
	
	google.maps.event.addListener(marker.infowindow, 'closeclick', function(event) {
		closeLocation();
	});
}



function toggleItem(item, size) {
	var speed = 400;
	
	if (size == "large") {
		var imgwidth = 320;
		var imgheight = 420;
		
		//if ($(item).hasClass("announcement")) imgwidth = 200;
		
		if ($(item).find(".itemcontentcontainer").length > 0) var itemwidth = 650;
		else var itemwidth = 320;
		$(item).find(".itemcontentcontainer").show("slow");
		
		$(item).unblock({
			fadeOut: 100
		});
				
	} else if (size == "small") {
		var imgwidth = 155;
		var imgheight = 203;
		var itemwidth = 155;
		$(item).find(".itemcontentcontainer").hide("fast");
	}

	
	if (((size == "small") && ($(item).hasClass("open"))) || ((size == "large") && (!$(item).hasClass("open")))) {
		if (size == "large") $(item).addClass("open");
		
		if ($(item).hasClass("announcement")) {
			$(item).css("height", "auto");
			$(item).animate({ width: (itemwidth+8)+"px" }, speed);
			
			if (size == "large") {
				var itemtextwidth = imgwidth - 8;
				var itemtextheight = $(item).find(".itemimgcontainer .itemtext").height()+20;
				if (itemtextheight < imgheight) itemtextheight = imgheight;
			} else {
				var itemtextwidth = imgwidth;
				var itemtextheight = imgheight;
			}
			
			//$(item).find(".itemimgcontainer").animate({ width: (imgwidth+8)+"px", height: (imgheight+8)+"px" }, speed, function() {
			$(item).find(".itemimgcontainer").animate({ width: (itemtextwidth+8)+"px", height: (itemtextheight+8)+"px" }, speed, function() {
				if (size == "small") $(item).removeClass("open");
				
				if (size == "large") {
					$.scrollTo($(item), speed, {
						offset: -itemoffset
					});
				}
			});
			
			//$(item).find(".itemimgcontainer").animate({ width: (itemtextwidth+8)+"px", height: (itemtextheight+8)+"px" }, speed);			
			//$(item).find(".itemimgcontainer").css("height", (itemtextheight)+"px");
			$(item).find(".itemimgcontainer .itemcontainer").animate({ width: (itemtextwidth)+"px", height: (itemtextheight+8)+"px" }, speed);
			$(item).find(".itemimgcontainer .itemcontent").animate({ width: (itemtextwidth)+"px", height: (itemtextheight)+"px" }, speed);
			$(item).find(".itemimgcontainer .itemcontent .posterimg").animate({ width: (itemtextwidth)+"px", height: (itemtextheight)+"px" }, speed);
			
			$(item).find(".itemimgcontainer .shadowrightside").animate({ height: (itemtextheight-8)+"px" }, speed);
			$(item).find(".itemimgcontainer .shadowbot").animate({ width: (itemtextwidth)+"px" }, speed);
			$(item).find(".itemimgcontainer .shadowbotside").animate({ width: (itemtextwidth-8)+"px" }, speed);
			
			$(item).find(".itemcontentcontainer .itemcontainer").animate({ width: (imgwidth)+"px", height: (imgheight+8)+"px" }, speed);
			$(item).find(".itemcontentcontainer .itemcontent").animate({ width: (imgwidth)+"px", height: (imgheight)+"px" }, speed);
			$(item).find(".itemcontentcontainer .itemcontent .posterimg").animate({ width: (imgwidth)+"px", height: (imgheight)+"px" }, speed);
			$(item).find(".itemcontentcontainer .shadowrightside").animate({ height: (imgheight-8)+"px" }, speed);
			$(item).find(".itemcontentcontainer .shadowbot").animate({ width: (imgwidth)+"px" }, speed);
			$(item).find(".itemcontentcontainer .shadowbotside").animate({ width: (imgwidth-8)+"px" }, speed);
			
		} else {
			$(item).css("height", "auto");
			$(item).animate({ width: (itemwidth+8)+"px" }, speed);
			$(item).find(".itemimgcontainer").animate({ width: (imgwidth+8)+"px", height: (imgheight+8)+"px" }, speed, function() {
				if (size == "small") $(item).removeClass("open");
				
				if (size == "large") {
					$.scrollTo($(item), speed, {
						offset: -itemoffset
					});
				}
			});
			$(item).find(".itemcontainer").animate({ width: (imgwidth)+"px", height: (imgheight+8)+"px" }, speed);
			$(item).find(".itemcontent").animate({ width: (imgwidth)+"px", height: (imgheight)+"px" }, speed);
			$(item).find(".itemcontent .posterimg").animate({ width: (imgwidth)+"px", height: (imgheight)+"px" }, speed);
			$(item).find(".shadowrightside").animate({ height: (imgheight-8)+"px" }, speed);
			$(item).find(".shadowbot").animate({ width: (imgwidth)+"px" }, speed);
			$(item).find(".shadowbotside").animate({ width: (imgwidth-8)+"px" }, speed);
		}
	}
	
	if ($(item).hasClass("pastevent")) {
		openpastevent(item);
	}
}

function openpastevent(item) {
	var itemdata = $(item).attr("id").split("_");
	var itemid = itemdata[itemdata.length-1];
	$(".participant_"+itemid).fadeIn("fast");
	$("#colofon_"+itemid).fadeIn("fast");
	
	
	if ($("#longposters_"+itemid).is(':hidden')) { 
		$("#longposters_"+itemid).fadeIn("fast");
		$("#longposters_"+itemid+" .itemposter").cycle({
			fx: 'fade',
			timeout: 2000,
			speed: 500
		});
	}
}


function initItem(item) {
	$(item).find(".itemcontent").click(function() {
		if ($(item).attr("id") != "moreposters") {
			if ($(item).hasClass("open")) toggleItem(item, "small");
			else toggleItem(item, "large");
			
		}
	});
	
}

function initItems() {
	$(".itemcontentcontainer").hide();
	var items = new Array();
	$(".item:not(#location)").each(function(i) {
		initItem(this);
		items.push($(this));

	});
	
	$("#location .itemcontent").click(function() {
		openLocation();
	});
	
	$(".item").css("display", "inline").css("opacity", 0);
	items.push($("#location"));
	
	
	$(".participant").each(function(i) {
		var participant = this;
		
		$(this).find(".itemcontent").hoverIntent({
			sensitivity: 1,
			interval: 30,
			over: function() {
				if (!$(participant).hasClass("open")) {
					$(".participant").unblock({
						fadeOut: 0
					});
					$(this).block({ 
						message: '<h4>'+$(participant).find(".participantname").html()+'</h4>',
						fadeIn: 300,
						centerX: false,
						centerY: false,
						overlayCSS:  { 
							backgroundColor: '#000', 
							opacity: 0.5,
							cursor: 'pointer'
						}, 
						css: { 
							padding: '5px', 
							margin: 0, 
							width: '100%', 
							top: '0', 
							left: '0', 
							textAlign: 'left', 
							color: '#FFF', 
							border: 'none', 
							backgroundColor: 'transparent', 
							cursor: 'pointer' 
						}
					}); 
				}
			},
			timeout: 30,
			out: function() {
				$(this).unblock({
					fadeOut: 100
				});
			}
		}); 
	});

	
	/*
	$(".participant").hover(function() {
		$(this).block({ 
			message: 'test',
			bindEvents: true
		}); 
	}, function() {
		$(this).unblock(); 		
	});
	*/
}

function fadeInPosters() {
	var items = new Array();
	$(".item").each(function(i) {
		items.push($(this));
	});
	
	
	items = $.shuffle(items);
	for (var i=0; i<items.length; i++) {
		var delaytime = Math.floor(Math.random() * 4000) + 1000;
		items[i].delay(delaytime).animate({ opacity: 1 });		
	}
}

function initImages() {
	$('body').supersleight({shim: '/inc/img/misc/transparent.gif'});
}


function initNav() {
	$("#headercols").css("display", "inline").css("opacity", 0);
	var speed = 400;
	
	

	$("#nav_info").css("backgroundPosition", "0 0").empty();
	$("#nav_en").css("backgroundPosition", "0 0").empty();
	$("#nav_nl").css("backgroundPosition", "0 0").empty();
	$("#nav_press").css("backgroundPosition", "0 0").empty();
	$("#nav_contact").css("backgroundPosition", "0 0").empty();
	$("#nav_salone").css("backgroundPosition", "0 0").empty();
	$("#nav_sothebys").css("backgroundPosition", "0 0").empty();
	$("#nav_participants").css("backgroundPosition", "0 0").empty();
	$("#nav_location").css("backgroundPosition", "0 0").empty();
	$("#nav_events").css("backgroundPosition", "0 0").empty();
	
	

	$("#nav_info").hover(function() { $(this).css("backgroundPosition", "-128px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_en").hover(function() { $(this).css("backgroundPosition", "-11px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_nl").hover(function() { $(this).css("backgroundPosition", "-10px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_press").hover(function() { $(this).css("backgroundPosition", "-35px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_contact").hover(function() { $(this).css("backgroundPosition", "-46px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_participants").hover(function() { $(this).css("backgroundPosition", "-72px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_location").hover(function() { $(this).css("backgroundPosition", "-51px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_events").hover(function() { $(this).css("backgroundPosition", "-41px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_salone").hover(function() { $(this).css("backgroundPosition", "-132px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });
	$("#nav_sothebys").hover(function() { $(this).css("backgroundPosition", "-84px 0"); }, function() { $(this).css("backgroundPosition", "0 0"); });

	$("#nav_salone").click(function() {
		openpastevent("#event_47");
		toggleItem("#event_47", "large");
		
		$.scrollTo(".pastevent", speed, {
			offset: -itemoffset
		});
	});
	
	$("#nav_sothebys").click(function() {
		openpastevent("#event_50");
		toggleItem("#event_50", "large");
		
		$.scrollTo(".pastevent", speed, {
			offset: -itemoffset
		});
	});
	
	$("#nav_events").click(function() {
		$.scrollTo(".pastevent:first", speed, {
			offset: -itemoffset
		});
	});
	
	$("#nav_contact").click(function() {
		toggleItem("#contact", "large");
		$.scrollTo("#introduction", speed, {
			offset: -itemoffset
		});
	});
	
	$("#nav_location").click(function() {
		openLocation();
		$.scrollTo("#location", speed, {
			offset: -itemoffset
		});

	});
	
	$("#nav_participants").parent().click(function() {
		toggleItem(".participant:first", "large");
		$.scrollTo(".participant:first", speed, {
			offset: -itemoffset
		});
	});
	
	$("#loadmoreposters").click(function() {
		morePosters();
	});
}


function initQuestionForm() {
	$(".questioninput input").val("");
	
	$("#nav_questionform").hover(function() {
		$("#addposter").css("visibility", "visible");
	}, function() {
		$("#addposter").css("visibility", "hidden");
	});
	
	$("#addposterbtn").click(function() {
		$("#questionform").submit();
	});
	
	$(".questioninput input").focus(function() {
		$(this).addClass("questionfocused");
	});

	$(".questioninput input").blur(function() {
		if ($(this).val() == "") {
			$(this).removeClass("questionfocused");
		}
	});
	$(".questioninput input").change(function() {
		var cleanstring = $(this).val().replace(/\s+/g,'');
		$(this).val(cleanstring);
	});
	
	$(".questioninput input").live('keydown', function(e) { 
		var keyCode = e.keyCode || e.which; 
		if (keyCode == 32) { 
			e.preventDefault(); 
		} 
	});
	
	var options = { 
		beforeSubmit:  handleRequestSubmitQuestion,
		success:       handleResponseSubmitQuestion
	};
	$("#questionform").ajaxForm(options);
	
	var numcreated = parseInt($("#numposterscreated").val());
	if (numcreated > 2) {
		disableQuestionForm();
	}
	
}

function handleRequestSubmitQuestion(formData, jqForm, options) { 
	var numcreated = parseInt($("#numposterscreated").val());
	if (numcreated <= 2) {
	
		var allfilled = true;
		$(".questioninput input").each(function(i) {
			$(this).blur();
			if ($(this).val() == "") {
				allfilled = false;
				$(this).hide();
				$(this).parent().css("background", "red");
				
				$(this).parent().delay(1000).animate({ backgroundColor: "#FFFFFF" }, 1000, function() {
					$(this).find("input").show();
				});
			}
		});
	
		
		if ((allfilled) && (questionsubmit)) {
			var line1 = $("#question1").val();
			var line2 = $("#question2").val();
			var line3 = $("#question3").val();
			$(".questioninput input").val("");
			$(".questioninput input").removeClass("questionfocused");
			questionsubmit = false;
			$.ajax({
				type: "POST",
				url: "inc/php/utils/poster-create.php",
				data: "postertype=short&line1="+line1+"&line2="+line2+"&line3="+line3,
				dataType: "xml",
				success: options.success
			});
			
			return true;
			
		} else {
			return false;
		}
	}
}

function handleResponseSubmitQuestion(data, status) {
	questionsubmit = true;
	var success = $("success", data).text();	
	var message = $("message", data).text();	
	
	if (success == 1) {
		var poster = $("poster", data).text();
		
		var numcreated = $("numcreated", data).text();
		$("#numposterscreated").val(numcreated);
		
		
		if (numcreated > 2) {
			disableQuestionForm();
		}
		
		$("#content").prepend('<div class="item poster">'+postertemplate+'</div>');
		var item = $("#content .poster:first");
		$("#content .poster:first .itemposter").html('<img class="posterimg" src="/media/posters/'+poster+'" />');
		$("#content .poster:first").css("display", "inline").css("opacity", 0);
		$("#content .poster:first").animate({ opacity: 1 }, 1000);
		
		initItem(item);
	} else {
		if (message == "blacklist") {
			alert('no profanity please :)');
		}
	}
}



function link() { }
function openwindow(url) {
	window.open(url);
}


function initLogo() {
	var logoImages = new Array();
	for (var i=0; i<22; i++) {
		logoImages.push({ src: "inc/img/logo/dae-"+i+".gif", title: "logo "+i });
	}
	
	$("#logo").imageLoader({
	   images: logoImages
	}, function(){
		$("#logo").cycle({
			fx: 'none',
			timeout: 10,
			speed: 1,
			nowrap: true,
			after: function(currSlideElement, nextSlideElement, options, forwardFlag) {
				if ($(currSlideElement).index() == 10) {
					/*
					$("#header").fadeIn("slow", function() {
						$(this).find("#headershadow").css("display", "inline");
					});
					
					*/
					
					$("#headercols").animate({ opacity: 1 });
					
				}
				if ($(currSlideElement).index() == 10) {
					fadeInPosters();
				}
				
				if ($(currSlideElement).index() == 14) {
					$("#questioninputs").removeClass("questionphase1").addClass("questionphase2");
				}
			},
			end: function() {
				$("#logo").fadeOut("slow", function() {
					

					var numcreated = parseInt($("#numposterscreated").val());
					if (numcreated > 2) {
						disableQuestionForm();
					} else {
						$("#questioninputs").removeClass("questionphase2").addClass("questionphase3");
					}

					$(this).remove();
				});
			}
		});
	});
}

function initPoster() {
	postertemplate = $(".poster:first").html();
	$(".poster:first").remove();
}

var start = 100;

function morePosters() {
	
	var limit = 50;
	var type = "short";
	
	$.ajax({
		type: "POST",
		url: "inc/php/utils/posters-get.php",
		data: "type="+type+"&start="+start+"&limit="+limit,
		dataType: "xml",
		success: handleResponseGetPosters
	});
	
	start += limit;
}

function handleResponseGetPosters(data, status) {
	if ($("poster", data).length < 50) {
		$("#moreposters").remove();
	}
	
	$("poster", data).each(function(i, n) {
		var posterid = $("id", n).text();
		var posterimage = $("path", n).text();
		
		$(".threeword:last").after('<div class="item threeword poster">'+postertemplate+'</div>');
		
		var item = $("#content .threeword:last");
		$("#content .threeword:last .itemposter").html('<img class="posterimg" src="/media/posters/'+posterimage+'" />');
		$("#content .threeword:last").css("display", "inline").css("opacity", 0);
		$("#content .threeword:last").delay(i*200).animate({ opacity: 1 }, 1000);
		
		initItem(item);
		
	});
}

function disableQuestionForm() {
	$(".questioninput input").val("");
	$(".questioninput input").unbind('click');
	$(".questioninput input").unbind('focus');
	$(".questioninput input").unbind('blur');
	$(".questioninput input").unbind('change');
	$(".questioninput input").unbind('live');
	$(".questioninput input").attr("disabled", "true");
	$("#nav_questionform").unbind('hover');
	$("#addposter").remove();
	$("#questioninputs").removeClass("questionphase1").removeClass("questionphase3").addClass("questionphase2");

}

function initPastItems() {
	$(".pastparticipant").hide();
	$(".pastcolofon").hide();
	$(".pastlongposters").hide();
	
	
}

function autoFocusItems() {
	$(".autofocus").each(function() {
		toggleItem(this, "large");
	});
}

$(document).ready(function() {
	initItems();
	initPastItems();
	initLogo();
	initNav();
	initImages();
	initQuestionForm();
	initPoster();
	autoFocusItems();
	//initMap();

});