/* At load document */
$(document).ready
(
	// Function
  function () 
  {
  	// Inits functions
    priohpersia.init () ;
  } 
) ;


// Namespace
var priohpersia = {

  // Init
  init: function () 
  {
    // Add the last item to menus
    $("#top_menu li:last-child, .bottom_menu li:last-child").addClass ("last") ;
    
    
    // Top menu events
    $("#top_menu a").click (function (e)
    {
      // Gets the submenu to load
      var submenu = $(this).attr ('data-submenu') ;
      
      if ($(this).attr ('href').indexOf ("#") != -1)
      {
        // Prevents default behaviour
        e.preventDefault () ;

        
        // Removes the styles
        $("#top_menu a").removeClass ('active') ;
        $('.bottom_menu').hide () ;
        
        
        // Make appear the selected one
        $(this).addClass ('active') ;
        $('.bottom_menu[data-submenu="' + submenu + '"]').animate ({"height": "toggle"}, 100) ;
      }
    }) ;
    
    
    // Video gallery
    $(".video_gallery").each (function ()
    {
      // Vars
      var video = this ;
      
      
      // Events on the gallery
      $(video).find ('ul li a').click (function (e)
      {
        // Prevent the default behaviour
        e.preventDefault () ;

        
        // Change the SRC attribute
        $(video).find ('.main_video').attr ('src', 'http://www.youtube.com/embed/' + $(this).attr ('data-youtube-link')) ;
        $(video).find ('header h2').html ($(this).find ('img').attr ('title')) ;
        
      }) ; 
    }) ;


    // Gallery
    // ************************************************************************
    $(".photo_gallery").each (function ()
    {
      // Vars
      var gallery = this ;


      // Events on the gallery
      $(gallery).find ('.photos ul li a').click (function (e)
      {
        // Prevent the default behaviour
        e.preventDefault () ;


        // Show image
        $(gallery).find ('.main_image figure img').
          attr ('src', ($(this).attr ('href'))) ;

        
        // Puts the actual
        $(this).attr ('data-actual', 1) ;

      }) ;

      
      // Prev
      $(gallery).find ('.prev').click (function (e)
      {
        // Prevent the default behaviour
        e.preventDefault () ;

        priohpersia.galleryMove (gallery, 'prev') ;
      }) ;       

      
      // Next
      $(gallery).find ('.next').click (function (e)
      {
        // Prevent the default behaviour
        e.preventDefault () ;
      
        priohpersia.galleryMove (gallery, 'next') ;
      }) ;
      

      // Load image event
      // The behaviour of this events depends of the main_image container
      // is visible or not
      $(gallery).find ('.main_image figure img').load (function ()
      {
        // Not if spacer...
        if ($(this).attr ('src').indexOf ('spacer.gif') != -1)
        {
          return ;
        }
        
      
        // Takes the width and height of the screen
        var width = $(window).width() ;
        var height = $(window).height () ;
        var image = this ;
        var isVisible = $(gallery).find ('.main_image').is (':visible') ;
        
        
        // Ampliate
        $(gallery).find ('.ampliate').attr ('href', $(this).attr ('src')) ;
        
        
        // Sets the maxwidth to the image
        $(this).css ('max-width', width) 
        $(gallery).find ('.main_image figure img').hide ();
   
        
        // Position in the middle of the image
        if (isVisible)
        {
          $(image).fadeIn (300) ;
        }
        else
        {
          $(gallery).find ('.main_image').css 
          (
            {
              top: (height / 2) + 'px',
              left: (width / 2) + 'px',
              right: (width / 2) + 'px',
              bottom: (height / 2) + 'px',
              display: 'block'
            }
          ) ;
          
          
          // Animate
          $(gallery).find ('.main_image').animate 
          (
            {top: 0, left: 0, right: 0, bottom: 0}, 
            1000, 
            function () 
            {
              $(image).fadeIn (1000) ;
              $(gallery).find ('.close, .next, .prev, .ampliate').fadeIn () ;
            }
          ) ;
        }
      }) ;


      // Close
      $(gallery).find ('.close').click (function (e)
      {
        // Prevent the default behaviour
        e.preventDefault () ;

        // Hide and unbind the image
        $(gallery).find ('.main_image figure img').attr ('src', '') ;
        $(gallery).find ('.main_image').hide () ;
        $(gallery).find ('.close, .next, .prev, .ampliate').hide () ;
      }) ;
    }) ;
    
  },
  
  
  /**
   * galleryMove. Makes the next and prev movement on the gallery
   */
  galleryMove: function (gallery, next)
  {
    // Actual image
    var actual = $(gallery).find ('.photos ul li a[data-actual="1"]') ;
    
    
    // Calculate the nextOne element
    if (next == 'next')
    {
      var nextOne = $(actual).parents("li").next ().find ("a")
    }
    else
    {
      var nextOne = $(actual).parents("li").prev ().find ("a")
    }


    // Only if there is a next one element
    if ($(nextOne).size () > 0)
    {
      // Remove the actual attr
      $(gallery).find ('.photos ul li a').attr ('data-actual', '') ;
      $(nextOne).attr ('data-actual', 1) ;  
      $(gallery).find ('.main_image figure img').attr ('src', $(nextOne).attr ('href')) ;
    }  
  }
}
