$(document).ready(function() {
			$('#descText').hide();
			$('#share1').hide();
			$('#share2').hide();
			$('#descTitle').css("margin-top", "0px");
			$('#showInfo').html('Show Info');
			showText = false;
		});

$(document).ready(function() {
 
	$(function(){
            $("#grid-content").vgrid({
                easeing: "easeOutQuint",
                useLoadImageEvent: true,
                time: 400,
                delay: 20,
                fadeIn: {
                    time: 500,
                    delay: 50
                }
            });
        });
		
	var sidebarHeight = $('#sidebarContent').height()
	var windowHeight = $(window).height()	
							
	
	
	$(window).scroll(function () {

					   	
	});

	
	var showText = false;
		
	$('#showInfo').click(function(){
		if(showText == false){
			$('#descText').slideDown();
			$('#share1').show();$('#share2').show();
			$('#descTitle').animate({marginTop: "45px"}, 400);
			$('#showInfo').html('Hide Info');
			showText = true;
		}else{
			$('#descText').slideUp();
			$('#share1').hide();
			$('#share2').hide();
			$('#descTitle').css("margin-top", "0px");
			$('#showInfo').html('Show Info');
			showText = false;
		}
		
		return false;
		
	})
	
  lightbox();
});

function lightbox() {
		var btlinks = $( 'a[rel^=lightbox]' );
		var btflash =  $( '.flash' );
		var btvimeo = $('.vimeo')
		var itemLightBox = $( 'a[rel^=lightbox], .flash' );
		var overlay = $( jQuery( '<div id="overlay" style="display: none"></div>' ) );
		var container = $( jQuery( '<div id="lightbox" style="display: none"></div>' ) );
		var target = $( jQuery( '<div class="target"></div>'));
		var btclose = $( jQuery( '<a href="#btclose" class="btclose"><img src="http://dollyrogers.nl/wp-content/themes/dollyrogers/media/close.png" alt="Close"/></a>' ) );
		var prev = $( jQuery( '<a href="#prev" class="prev"><img src="http://dollyrogers.nl/wp-content/themes/dollyrogers/media/prev.png" alt="Prev"/></a>' ) );
		var next = $( jQuery( '<a href="#next" class="next"><img src="http://dollyrogers.nl/wp-content/themes/dollyrogers/media/next.png" alt="Next"/></a>' ) );

	btflash.each(function(){
				var object = $(this).children("object")
				var swf = object.children("param:eq(0)").attr('value');
				$(this).attr('href',swf)
	})
	
	btvimeo.each(function(){
				var iframe = $(this).children("iframe")
				var video = iframe.attr('src');
				$(this).attr('href',video)
	})
	
	$('body').append(overlay).append(container);
  container.append(btclose).append(target).append(prev).append(next);
  container.show().css({'top': Math.round((($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - container.outerHeight()) / 2) + 'px', 'left': Math.round(($(window).width() - container.outerWidth()) / 2) + 'px', 'margin-top': 0, 'margin-left': 0}).hide();
  
  btclose.click(function(c) {
    c.preventDefault();
    overlay.add(container).fadeOut('normal');
        target.children().remove();
		target.html('');
	});

  prev.add(next).click(function(c) {
    c.preventDefault();
		var current = parseInt( itemLightBox.filter( '.selected' ).attr( 'lb-position' ));
		var maxItem = itemLightBox.size() - 1
		var next = ''
		var prev = ''
		current - 1 < 0 ? prev = maxItem : prev = current - 1
		current + 1 > maxItem ? next = 0 : next = current + 1
		var to = $( this ).is( '.prev' ) ? itemLightBox.eq(prev) : itemLightBox.eq(next);
		
		var selected =  $( this ).is( '.prev' ) ? 'prev' : 'next';
		target.children().remove()
		target.html('');
		itemLightBox.filter( '.selected' ).removeClass( 'selected' );
		selected == 'prev' ? itemLightBox.eq(prev).addClass( 'selected' ) : itemLightBox.eq(next).addClass( 'selected' );
		//alert(to.attr('href'))
		loadImage(to.attr('href'),to.attr('title'))
		
	});
  
shortcut.add("Right",function(c) {
  c.preventDefault();
		var current = parseInt( itemLightBox.filter( '.selected' ).attr( 'lb-position' ));
		var maxItem = itemLightBox.size() - 1
		var next = ''
		var prev = ''
		current - 1 < 0 ? prev = maxItem : prev = current - 1
		current + 1 > maxItem ? next = 0 : next = current + 1
		var to = $( this ).is( '.prev' ) ? itemLightBox.eq(prev) : itemLightBox.eq(next);
		var selected =  $( this ).is( '.prev' ) ? 'prev' : 'next';
		target.children().remove()
		target.html('');
		itemLightBox.filter( '.selected' ).removeClass( 'selected' );
		selected == 'prev' ? itemLightBox.eq(prev).addClass( 'selected' ) : itemLightBox.eq(next).addClass( 'selected' );
		loadImage(to.attr('href'),to.attr('title'))
});

shortcut.add("Left",function(c) {
  c.preventDefault();
		var current = parseInt( itemLightBox.filter( '.selected' ).attr( 'lb-position' ));
		var maxItem = itemLightBox.size() - 1
		var next = ''
		var prev = ''
		current - 1 < 0 ? prev = maxItem : prev = current - 1
		current + 1 > maxItem ? next = 0 : next = current + 1
		var to = $( this ).is( '.next' ) ? itemLightBox.eq(prev) : itemLightBox.eq(prev);
		var selected =  $( this ).is( '.prev' ) ? 'prev' : 'prev';
		target.children().remove()
		target.html('');
		itemLightBox.filter( '.selected' ).removeClass( 'selected' );
		selected == 'prev' ? itemLightBox.eq(prev).addClass( 'selected' ) : itemLightBox.eq(prev).addClass( 'selected' );
		loadImage(to.attr('href'),to.attr('title'))
});

shortcut.add("esc",function(c) {
  c.preventDefault();
    overlay.add(container).fadeOut('normal');
        target.children().remove();
		target.html('');
});

  btlinks.add(btflash).each(function(index) {
    var btlink = $(this);
	btlink.click(function(c) {
						  
      c.preventDefault();
      btopen(btlink.attr('href'),btlink.attr('title'));
      btlinks.filter('.selected').removeClass('selected');
      btlink.addClass('selected');
    });
    btlink.mousedown(function() {
      btopen(btlink.attr('href'),btlink.attr('title'));
      btlinks.filter('.selected').removeClass('selected');
      btlink.addClass('selected');
    });
	btlink.attr({'lb-position': index});
  });
  
  var btopen = function(url,title) {
	//  if(flash == true){alert('flash')}else{alert('not flash')}
    if(container.is(':visible')) {
      target.children().fadeOut('normal', function() {
        target.children().remove();
		target.html('');
        loadImage(url, title);
      });
    } else {
      target.children().remove();
      overlay.add(container).fadeIn('normal',function(){
        loadImage(url, title);
      });
    }
  }
  
  var loadImage = function(url, title) {
		if(container.is('.loading')) { return; }
		container.addClass('loading');
		
		var maxWidth = ($(window).width() - parseInt(container.css('padding-left'),10) - parseInt(container.css('padding-right'), 10)) - 100;
		var maxHeight = (($(window).height() > window.innerHeight ? window.innerHeight : $(window).height()) - parseInt(container.css('padding-top'),10) - parseInt(container.css('padding-bottom'), 10)) - 100;
		  
		// we load a swf
		if(url.substr(-3,3) == 'swf'){
			
		var object = ($('.flash[href='+url+']').children("object"))
		var swfHeight= object.attr('height')
		var swfWidth= object.attr('width')					
	
		if( swfWidth*2 > maxWidth || swfHeight*2 > maxHeight ) { // One of these is larger than the window
				if( swfHeight*2 >= maxHeight ) {
					var ratioHeight = swfWidth / swfHeight;
					swfHeight = maxHeight/2;
					swfWidth = (maxHeight * ratioHeight)/2;
				}
				if( swfWidth*2 >= maxWidth ) {
					var ratioHeight = swfHeight / swfWidth;
					swfWidth = maxWidth/2;
					swfHeight = (maxWidth * ratioWidth)/2;
				}
		}
		
	container.animate( {'width': parseInt(swfWidth)*2,'height': parseInt(swfHeight)*2+40, 'top': Math.round( ($( window ).height() - parseInt(swfHeight)*2 - parseInt( container.css( 'padding-top' ),10 ) - parseInt( container.css( 'padding-bottom' ),10 ) ) / 2 ) + 'px', 'left': Math.round( ($( window ).width() - parseInt(swfWidth)*2 - parseInt( container.css( 'padding-left' ),10 ) - parseInt( container.css( 'padding-right' ),10 ) ) / 2 ) + 'px'},'normal',function(){
		
			$(target).flash({swf:url,height: parseInt(swfHeight)*2,width: parseInt(swfWidth)*2})
			container.removeClass( 'loading' )
	
		})
	

	}else{
		
		//we load a video
		if(url.substr(0,24) == "http://player.vimeo.com/"){
			
			var iframe = $('.vimeo[href='+url+']').children("iframe")
			var vimeoWidth = iframe.attr('width')
			var vimeoHeight =iframe.attr('height')
			
	container.animate( {'width': vimeoWidth,'height': vimeoHeight, 'top': Math.round( ($( window ).height() - vimeoHeight - parseInt( container.css( 'padding-top' ),10 ) - parseInt( container.css( 'padding-bottom' ),10 ) ) / 2 ) + 'px', 'left': Math.round( ($( window ).width() - vimeoWidth - parseInt( container.css( 'padding-left' ),10 ) - parseInt( container.css( 'padding-right' ),10 ) ) / 2 ) + 'px'},'normal',function(){
			
			
			//alert(iframe.html())
			target.append($('.vimeo[href='+url+']').html())
			container.removeClass( 'loading' )
	
		})
		
	// we load an image
	}else{
		
		var img = new Image();
			img.src = url;	
		
			img.onload = function() {		
			  img.style.display = 'none';
		
			  if(img.width > maxWidth || img.height > maxHeight) { // One of these is larger than the window
				if(img.height >= maxHeight) {
				var ratio = img.width / img.height;

				  img.height = maxHeight;
				  img.width = maxHeight * ratio;
				} else {
				var ratio = img.height / img.width;

				  img.width = maxWidth;
				  img.height = maxWidth * ratio;
				}
			  }
	// taille max image			  
				img.width = img.width;
			  img.height = img.height;
			  
				container.animate( {'width': img.width,'height': img.height+30, 'top': Math.round( ($( window ).height() - img.height - parseInt( container.css( 'padding-top' ),10 ) - parseInt( container.css( 'padding-bottom' ),10 ) ) / 2 ) + 'px', 'left': Math.round( ($( window ).width() - img.width - parseInt( container.css( 'padding-left' ),10 ) - parseInt( container.css( 'padding-right' ),10 ) ) / 2 ) + 'px'},'normal', function(){
					target.append(img);
					target.append('<p>'+title+'</p>');
					$( img ).fadeIn( 'normal', function() {
						container.removeClass( 'loading' );
					} );
				} )
			}			
		}
	}

	
	}//end lightbox
	
	$(window).stop().resize(function(){
 container.css( {'top': Math.round( ($( window ).height() - container.height() - parseInt( container.css( 'padding-top' ),10 ) - parseInt( container.css( 'padding-bottom' ),10 ) ) / 2 ) + 'px', 'left': Math.round( ($( window ).width() - container.width() - parseInt( container.css( 'padding-left' ),10 ) - parseInt( container.css( 'padding-right' ),10 ) ) / 2 ) + 'px'},'normal')
	
	}) 
	
		
	
}
