/*
Site-wide JS
*/

var flash_duration = 6000;

$(function(){
	$("#SearchQ").hint();
	
	$("#meet").each(function(){
		var suffix = '_link';
		$('a[id$="' + suffix + '"]', $(this)).bind('click', function(e){
			e.preventDefault();
			if(!$(this).parent().hasClass('open')){
				$(this).parent().parent().children('li').removeClass('open');
				$(this).parent().addClass('open');
				$('.home_top').fadeOut('fast');
				$('#'+this.id.split(suffix)[0]).fadeIn();
			}
		}).hover(function(){
			$(this).parent().addClass('hover');
		},function(){
			$(this).parent().removeClass('hover');
		});
	});
	
	// do some javascript trickery with the main menu.
	$("body").filter(".parent_section, .parent_interior").each(function(){
		$("#menu a.currentPage").each(function(){
			$(this).parents("ul.sitemap").add($(this).find("+ ul")).show();
		});
	});
	
	$("body").filter(".interior").each(function(){
		$("#menu a.currentPage").each(function(){
			$(this).parents("a").parent("ul").prev("a").parent().andSelf().addClass('currentSection');
		});
	});
	
	// on embedded slideshows, do a bit of lookup to get the correct information
	$('img[src=/media/img/slideshow_placeholder.gif]').each(function(i, o){
	  var link = $(o).parent('a').attr('href');
	  link = link.split('/');
	  var id = link[link.length-1];
	  $.getJSON('/work/view/'+id+'.json', function(data){
	    $(o).attr('src', '/assets/asset_thumbnails/width_183/'+data.Thumb.file_name).addClass('embedded_slideshow').click(function(){
	      loadSlideshow(id, data);
	      return false;
	    });
	  })
	});
	
	setTimeout('hideMessages()', flash_duration);
});
var centerSlideshow = function(){
  $('#embedded_slideshow').css('top', $(window).height()/2-$('#embedded_slideshow').height()/2).css('left', $(window).width()/2-$('#embedded_slideshow').width()/2);
}

var currentSlideshow = false;
var loadSlideshow = function(id, portfolio_info){
  // attach the stuff we need to the body
  if($('#slideshow_blocker').length==0){
    $('body').append("\
    <div id='embedded_slideshow'>\
      <div class='slides_container'>\
    	</div>\
    	<div class='slide_description'>\
    		<span class='description_background'></span>\
    		<div class='slide_description_text'></div>\
    		<div class='slideshow_controls'>\
    			<a href='#' class='prev_slide_control'><span></span>Back</a>\
    			<a href='#' class='next_slide_control'><span></span>Forward</a>\
    			<a href='#' class='close_display_control'><span></span>Close Display</a>\
    			<a href='#' class='large_display_control'><span></span>Main Display</a>\
    		</div>\
    		<div class='position_display'><div class='current_position'></div></div>\
    		<div class='clear'>\
    		</div>\
    	</div>\
    	\
    </div><div id='slideshow_blocker'></div>\
    ");
  }
  $('#slideshow_blocker, #embedded_slideshow').hide();
  if(currentSlideshow != id || currentSlideshow == false){
    $("#embedded_slideshow .slides_container").empty();
    $.each(portfolio_info.PortfolioSlide, function(i, o){
      $("#embedded_slideshow .slides_container").append('<img src="/assets/asset_thumbnails/height_380/'+o.Asset.file_name+'" id="fullscreen_img_'+i+'">');
    });
    $('#embedded_slideshow .slides_container').cycle({
      next:'.next_slide_control',
      prev:'.prev_slide_control',
      timeout: 0,
      before: function(currSlideElement, nextSlideElement, options, forwardFlag){
        $('.current_position').css({
          width: function(){
            return $(".position_display").width()/options.slideCount;
          }
        });
      },
      prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){
        $('.current_position').css({
          width: $(".position_display").width()/this.slideCount,
          left: $(".position_display").width()/this.slideCount*zeroBasedSlideIndex
        });
        $(".slide_description_text").html(portfolio_info.PortfolioSlide[zeroBasedSlideIndex].slide_text);
      }
    });
    currentSlideshow = id;    
  }
  $('.large_display_control').click(function(){
    document.location = '/work/index/'+id;
  })
  $('#slideshow_blocker, #embedded_slideshow').fadeIn('medium');
  $('#slideshow_blocker, .close_display_control').click(function(){
    $('#slideshow_blocker, #embedded_slideshow').fadeOut('medium');
  })
  centerSlideshow();
  return false;
}
function hideMessages() {
	$("div.message").filter(function(){
		return $(this).css("display") != "none"; // don't do the fade action if it's already hidden!
	})
	.animate({
		top: '-=75px'
	}, 'normal', 'swing', function(){
		$(this).remove();
	});
}
