(function($) {
	$(function() {
		
		if(Modernizr.touch) {
			/*
			$('#content div.thumbContainer a').bind('touchend', function(e) {
					var link = $(this).attr('href');
					if(link) {
						window.location = link;
					}
			});
			*/
		}
		
		if (!Modernizr.csscolumns) {
			$('.projectContent').columnize({ columns: 2, lastNeverTallest: true });
			//$('.projectContent').columnize({ width:100 });
			//$('.projectContent').columnize();
			
			$('.projectContent .column:last').css('float', 'right');
			//$('.projectContent .column p').addClass('dontend');
		}
		else if ($.browser.mozilla)
			$('.projectContent').css('margin-bottom', '0px');
			// eliminato il margine inferiore per Firefox, che
			// usa quello dei paragrafi
			
			
		// nella landing page, intercettato click nella casella di inserimento email
		$('#emailLabel').click(function() {
			
			$(this).val('');
		});
		
		// nella landing page, se non si è inserito un indirizzo email e si
		// esce, viene ripristinato il valore precedente
		$('#emailLabel').focusout(function() {
			
			if (!$(this).val())
				$(this).val('Enter your email!');
		});
		

		var slideContainer = $("div.slideShowContainer").css({lineHeight:0}),
				slideView = slideContainer.find("div.slideShowView"),
				items = slideView.find("li"),
				currentItem = 1;

		if(items.length == 1) {
			slideContainer.find("div.slideShowControls").hide();
		}
		
		var first = items.eq(0);
		var wrapper = first.find("div.imgVidWrap").eq(0);
		var angles = wrapper.children("div:not(.topImageContainer,.topVideoContainer)").clone();
		var top = wrapper.height()-angles.height();
		angles.css({zIndex: 100});
		angles.prependTo(slideContainer);		

		//items.find("img[id]").after("<div class='oembed'>");

    function hideOembed() {
    	var citem = items.eq(currentItem);
      var iframe = citem.find("div.oembed").hide().find("iframe");
      Froogaloop(iframe[0]).api("pause");
      citem.find("div.topImageContainer").show();
    }
    
    function showOembed() {
    	if(currentItem == 0) {
    		currentItem = items.length - 2;
    		slideView.scrollTo(items.eq(currentItem));
    	} else if (currentItem == (items.length - 1)) {
    		currentItem = 1;
    		slideView.scrollTo(items.eq(currentItem));    		
    	}
      var item = items.eq(currentItem);
      var oembed = item.find("div.oembed").show();
      oembed.find("iframe").detach().appendTo(oembed);
      window.setTimeout(function() {
        if(item.find("div.oembed").length) {
          item.find("div.topImageContainer").hide();
          var controls = $("div.slideShowControls");
          controls.find("a").detach().appendTo(controls);
          bindSlideShowControls();
        }
      },1000);
    }

		function bindSlideShowControls() {
			$("#slideShowPrev").unbind("click").click(function() {
				if(currentItem != 0) {
					hideOembed();
					currentItem--;
					slideView.scrollTo(items.eq(currentItem),400,{onAfter:showOembed});
				}
				return false;
			});
			$("#slideShowNext").unbind("click").click(function() {
				if(currentItem != (items.length-1)) {
					hideOembed();
					currentItem++;
					slideView.scrollTo(items.eq(currentItem),400,{onAfter:showOembed});
				}		
				return false;
			});
		}
		
		bindSlideShowControls();
		slideView.scrollTo(items.eq(currentItem));
		showOembed();
		if(window.location.hash) {
			$(window).load(function() {
				$.scrollTo(window.location.hash);
			});
		}
		
		//calcola larghezza barra
		var dummy = $("<div><br /><br /><br /></div>").css({overflow:"auto"});
		$("body").append(dummy);
		var w1 = dummy[0].clientWidth;
		dummy.css({overflow:"scroll"});
		var w2 = dummy[0].clientWidth;
		dummy.remove();
		var scrollbarWidth = w1-w2;
		
		function fixMenuPosition() {
			if(!$.browser.msie && $(document).height()<=$(window).height()) {
				//c'è la barra - sposto a sx dei pixel della barra precedentemente calcolati
				var cl = $("#container").css("margin","0 auto").offset().left;
				$("#container").css({marginLeft:Math.ceil(cl-scrollbarWidth/2)+"px"});
			}
		}; 		

		fixMenuPosition();
		$(window).resize(function() {
			slideView.scrollTo(items.eq(currentItem));
			fixMenuPosition();
		});
	});
})(jQuery);
