//sidenavigation collapsing menu
$(document).ready(function() {
  // We dont' want to record a users own hits
  if (readCookie('login_user_token') == null) {
    // We also don't want to record logging in and out
    if (document.URL.search('sign_') == -1) {
      // First thing, record every page hit
      $.ajax({url: '/hits', data: "hit[url]=" + document.URL + "&hit[referrer]=" + document.referrer, dataType:'script', type: 'POST'}); 
      
      if ($('a[href^=/media/]').length > 0) {
        var medium_id = $('a[href^=/media/]').first().attr('href').replace('/media/', '');
        $.ajax({url: '/hits', data: "hit[url]=" + document.URL + "&hit[referrer]=" + document.referrer + '&hit[medium_id]=' + medium_id, dataType:'script', type: 'POST'}); 
      }
    }
  }

	$(".scrollable-vertical").livequery(function() {
		$(this).scrollable({ 
			vertical: true, 
			items: '.thumbs',
			mousewheel: true 
		}).navigator();
	});
	
	$(".scrollable-horizontal").livequery(function() {
		$(this).scrollable({ 
			mousewheel: true 
		}).navigator();
	});

  // Turn all flv links into flashplayers
  $("a[href*=.flv]").livequery(function() {
    $(this).click(function() {
      $(this).addClass('video-player');
    });
    $(this).flowplayer("/flash/flowplayer-3.2.5.swf");
  });

  // Turn all mp4 links into pseudo-streaming flashplayers
  $("a[href*=.mp4]").livequery(function() {	
    $(this).click(function() {
			var resolution = $(this).attr('rel');
			var parts = resolution.split('x');
			$(this).css('width', parts[0] + 'px');
			$(this).css('height', parts[1] + 'px');
      $(this).addClass('video-player');
    });
	    $(this).flowplayer("/flash/flowplayer-3.2.5.swf",  { 
				id: $(this).attr('id'),
				name: $(this).attr('id'),
	      // fullscreen button not needed here 
	      plugins: {
						controls: {
							progressGradient: 'none',
							borderRadius: '1px solid #000000',
							timeSeparator: ' of ',
							volumeBorder: '1px solid #999999',
							volumeColor: '#ffffff',
							volumeSliderColor: '#c6c6c6',
							volumeSliderGradient: 'none',
							volumeSliderHeightRatio: 0.4,
							volumeBarHeightRatio: 1,
							durationColor: '#333333',
							timeColor: '#8e8e8e',
							autoHide: 'never',
							backgroundGradient: 'none',
							progressColor: '#c6c6c6',
							bufferGradient: 'none',
							sliderGradient: 'none',
							bufferColor: '#ffffff',
							sliderBorder: '1px solid #999999',
							sliderColor: '#333333',
							scrubberHeightRatio: 0.4,
							scrubberBarHeightRatio: 1,
							tooltipTextColor: '#ffffff',
							buttonColor: '#8e8e8e',
							buttonOverColor: '#777777',
							backgroundColor: '#FFFFFF',
							timeBorder: '0px solid rgba(0, 0, 0, 0.3)',
							timeBgColor: 'rgb(0, 0, 0, 0)',
							tooltipColor: '#000000',
							height: 22,
							opacity: 1.0,
							timeFontSize: '10',
							autoHide: true
						
						},
	          pseudo: { 
	              url: "/flash/flowplayer.pseudostreaming.swf" 
	          } 
	      },
	      // clip properties  
	      clip: { 
           
	          // make this clip use pseudostreaming plugin with "provider" property 
	          provider: 'pseudo',                 
           
	          // all videos under this baseUrl support pseudostreaming on the server side         
	          url: $(this).attr('href')       
	      } 
	   })
  });
  
  // Audio Player
  $("a[href*=.mp3]").livequery( function(){
    $(this).html('');
    $(this).addClass('audio-player');
    $(this).flowplayer("/flash/flowplayer-3.2.5.swf",  {
      // fullscreen button not needed here 
      plugins: { 
					audio: {
						url: "/flash/flowplayer.audio-3.2.2.swf"
					},
          controls: {
						progressGradient: 'none',
						borderRadius: '1px solid #000000',
						timeSeparator: ' of ',
						volumeBorder: '1px solid #999999',
						volumeColor: '#ffffff',
						volumeSliderColor: '#c6c6c6',
						volumeSliderGradient: 'none',
						volumeSliderHeightRatio: 0.4,
						volumeBarHeightRatio: 1,
						durationColor: '#333333',
						timeColor: '#8e8e8e',
						autoHide: 'never',
						backgroundGradient: 'none',
						progressColor: '#c6c6c6',
						bufferGradient: 'none',
						sliderGradient: 'none',
						bufferColor: '#ffffff',
						sliderBorder: '1px solid #999999',
						sliderColor: '#333333',
						scrubberHeightRatio: 0.4,
						scrubberBarHeightRatio: 1,
						tooltipTextColor: '#ffffff',
						buttonColor: '#8e8e8e',
						buttonOverColor: '#777777',
						backgroundColor: '#FFFFFF',
						timeBorder: '0px solid rgba(0, 0, 0, 0.3)',
						timeBgColor: 'rgb(0, 0, 0, 0)',
						tooltipColor: '#000000',
						height: 22,
						opacity: 1.0,
						timeFontSize: '10',	
						fullscreen: false,
						autoHide: false
					}
      }, 

      clip: { 
          autoPlay: false, 

          // optional: when playback starts close the first audio playback 
          onBeforeBegin: function() { 
            $("a[href*=.mp3]").close(); 
          } 
      } 
   })
  });

  // Facebox modal popup
  $('a[rel*=facebox]').livequery(function(){
  	$(this).facebox();
  });

  // Remote link
  $('a.remote_post').livequery(function() {
    $(this).click(function() {
      $.ajax({url: $(this).attr('href'), dataType:'script', type: 'POST'}); 
      return false;
    });
  });
 
  $('a.remote_get').livequery(function() {
    $(this).click(function() {
      $.ajax({url: $(this).attr('href'), dataType:'script'}); 
      return false;
    });
  });

	$(".pagination a").live("click", function() {
    //$(".pagination").html("Page is loading...");
    $.get(this.href, null, null, "script");
    return false;
  });
	
	$('.fit-to-image p.media-info').livequery(function(){
		var img = $(".medium img"); // Get my img elem
		var pic_real_width, pic_real_height;
		$("<img/>") // Make in memory copy of image to avoid css issues
		    .attr("src", $(img).attr("src"))
		    .load(function() {
		        pic_real_width = this.width;   // Note: $(this).width() will not
		        pic_real_height = this.height; // work for in memory images.
						$('.medium').width(pic_real_width);
						$('.fit-to-image p.media-info').width(pic_real_width);
		    });
	});

	$('a.toggle-details').livequery(function(){
		$(this).hover(
			function(){
				$('#details').fadeIn();
			},
			function(){
				$('#details').fadeOut();
			}
		);
	});
	
	$('.media-image').livequery(function(){
		$(this).mouseenter(function () {
			$('#details').fadeIn();}).mouseleave(function () {
			$('#details').fadeOut();
		});
	});

});

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


