jQuery(document).ready( function() {
  sproingGallery();
  loadPage();
  ourWorksMenu();
  leftNavigationMenu();
  sidebarGallery();
  beforeAfterRollover();
  addWModeToFlash();
});

function addWModeToFlash() {
  jQuery('embed').each(function() {
    if(jQuery(this).attr('wmode') == '') {
      jQuery(this).attr('wmode', 'transparent'); 
    }
  });
}

function beforeAfterRollover() {
  var firstImage = jQuery('#rollThis').attr('src');
  
  rolloverRetouched(firstImage);
}

function rolloverRetouched(image) {
  jQuery('#rollThis').hover(function() {
    var imageOn = image.replace('off', 'on');
    jQuery(this).attr('src', imageOn);
  }, function() {
    jQuery(this).attr('src', image);
  });
}


function sidebarGallery() {
  jQuery('#sproingGallery a').click(function() { 
    if(jQuery(this).hasClass('video')) {
      playVideo(jQuery(this).attr('href'));
      return false;
    } else {
      if(jQuery('.retouched').length > 0) {
        jQuery('.entry-content').html('<img id="rollThis" class="aligncenter" src="' + jQuery(this).find('img').attr('src') + '" />');
        rolloverRetouched(jQuery(this).find('img').attr('src'));
      } else {
        jQuery('.entry-content').html('<img class="aligncenter" src="' + jQuery(this).find('img').attr('src') + '" />');
      }
      return false;
    }
  });
  jQuery('.galleryInfo .page').click(function() {
    var page = jQuery(this).find('.pageNumber').text();
    var counter = 0;
    var pageCounter = 1;
    jQuery('#sproingGallery .thumbnail').each(function() {
      if(counter < 6) {
        if(pageCounter == page) {
          //-- Show images
          jQuery(this).removeClass('hidden');
        } else {
          //-- Hide images
          jQuery(this).addClass('hidden');
        }
      }      
      if((counter + 1) / 6 == 1) {
        counter = 0;
        pageCounter += 1;
      } else {
        counter += 1;
      }
    });
  });
}

function playVideo(link) {
  var videoEmbed = '<object width="900" height="506"><param name="allowfullscreen" value="true" />';
  videoEmbed += '<param name="allowscriptaccess" value="always" />';
  videoEmbed += '<param name="movie" value="';
  videoEmbed += link;
  videoEmbed += '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" />';
  videoEmbed += '<embed wmode="transparent" src="';
  videoEmbed += link;
  videoEmbed += '&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=1&amp;color=00ADEF&amp;fullscreen=1" type="application/x-shockwave-flash" allowfullscreen="true" allowscriptaccess="always" width="900" height="506"></embed>';
  videoEmbed += '</object>';
  
  jQuery('.videoContent p').html(videoEmbed);
}


function leftNavigationMenu() {
  jQuery('.leftNavigation').click(function() {
    if(!jQuery(this).hasClass('noMenu')) {
      if(jQuery(this).hasClass('collapsed')) {
        jQuery(this).removeClass('collapsed');
        jQuery(this).find('.menu').slideDown();
      } else { 
        jQuery(this).addClass('collapsed');
        jQuery(this).find('.menu').slideUp();
      }
    }
  });
  
  jQuery('.menu-leftNavigation a').click(function() {
    var gallery = jQuery(this).attr('title');
    var title = jQuery(this).text();
    var url = jQuery(this).attr('href');
    jQuery.ajax({
      url: url,
      type: "POST",
      data: ({gallery: gallery, title: title}),
      beforeSend: function() {
//        jQuery('#container').slideUp();
        //jQuery('.entry-content').fadeOut();
        jQuery('.entry-content').animate({ opacity: 0 });
      },
      success: function(data) {
        jQuery('#gallerySidebar').html(data);
        var firstImage = jQuery('#sproingGallery').find('.thumbnail a:first').attr('href');

        if(jQuery('.retouched').length > 0) {
          jQuery('.entry-content').html('<img id="rollThis" class="aligncenter" src="' + firstImage + '" />');
          rolloverRetouched(firstImage);
        } else {
          jQuery('.entry-content').html('<img class="aligncenter" src="' + firstImage + '" />');
        }
                
        sproingGallery();
        sidebarGallery();
      },
      complete: function() {
//        jQuery('.entry-content').fadeIn();
        jQuery('.entry-content').animate({opacity: 1.0 });
  //      jQuery('#container').slideDown(); 
      }
    });
    return false;
  });
}

function enableRollover() {

}


function ourWorksMenu() {
  jQuery('#workMenu .menu-header').find('a').hover(function() {
    var subMenu = jQuery(this).next('.sub-menu');
    subMenu.slideDown();
    jQuery(this).parent('li').hover(function() { }, 
      function() {
        subMenu.slideUp();
      });
  });
}


function sproingGallery() {
	jQuery('.thumbnail img').animate({width: 100}, 0); //Set all menu items to smaller size

	jQuery('.thumbnail').mouseover(function(){ //When mouse over menu item
		gridimage = jQuery(this).find('img'); //Define target as a variable
		gridimage.stop().animate({width: 200}, 150); //Animate image expanding to original size
	}).mouseout(function(){ //When mouse no longer over menu item
		gridimage.stop().animate({width: 100}, 150); //Animate image back to smaller size
	});
}

function loadPage() {
  jQuery('#menu-main-menu').find('a').click( function() {
    var link = jQuery(this).text().toLowerCase();


    jQuery.modal('<div class="loadingImage"></div>', {
      overlayClose: true,
      minHeight: 400, 
      maxHeight: 450,
      maxWidth: 850,
      minWidth: 850,
      opacity: 95,
      position: [90],
      onOpen: function (dialog) {
        dialog.overlay.fadeIn('slow', function () {
          dialog.data.hide();
          dialog.container.fadeIn('slow', function () {
            dialog.data.fadeIn('slow', function() {
              jQuery.get(link, function(data) {
                dialog.data.fadeOut('slow', function() {
                  jQuery('#simplemodal-container').html(data);
                  ajaxMenu();
                  dialog.data.slideDown('slow');
                });
                
              });
            
            
            });
          

          });
        });
      },
      onClose: function (dialog) {
        dialog.data.fadeOut('slow', function () {
          dialog.container.hide('slow', function () {
            dialog.overlay.slideUp('slow', function () {
              jQuery.modal.close();
            });
          });
        });
      }
    });

/*    jQuery.get(link, function(data) {
      jQuery.modal(data, {
        overlayClose: true,
        minHeight: 400, 
        maxHeight: 450,
        maxWidth: 850,
        minWidth: 850,
        opacity: 95,
        position: [90],
        onOpen: function (dialog) {
          dialog.overlay.fadeIn('slow', function () {
            dialog.data.hide();
            dialog.container.fadeIn('slow', function () {
              dialog.data.slideDown('slow', function() {
                ajaxMenu();
              });
            });
          });
        },
        onClose: function (dialog) {
          dialog.data.fadeOut('slow', function () {
            dialog.container.hide('slow', function () {
              dialog.overlay.slideUp('slow', function () {
                jQuery.modal.close();
              });
            });
          });
        }
      });
    });
    
    */
    return false;
  });
}

function ajaxMenu() {
  //-- Disable Bio link
  jQuery('#menu-item-55').find('a').each( function() {
    if(jQuery(this).attr('href') == 'http://joeybluebird.com/bios/') {
      jQuery(this).removeAttr('href');
    }
  });
  
  jQuery('#ajaxPage').find('a').click( function() {
    var link = jQuery(this).text().toLowerCase();
    jQuery('.menuSidebar').find('ul').slideUp( function() {
      jQuery('#ajaxPage').fadeOut( function() {      
        jQuery.get(link, function(data) {
          jQuery('#ajaxPage').html(data);
          jQuery('#ajaxPage').fadeIn();
          ajaxMenu();
        });
      });    
    });
    return false;
  });
}

