/****************************************************************************
eBIT-Site
-------------------
Copyright 2006, 2007, 2008 Davide Zaccaria, Orazio Langenbach

This file is part of eBIT-Site, a module of the framework POMO.

eBIT-Site is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

eBIT-Site is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with eBIT-Site.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

var gmap = false;

function chainSelectInit()
{
  $('#class_id').chainSelect('#species_id','ajax.php?mod='+mod+'&myaction=get_species',
  { 
    before:function (target)
    { 
      $("#species_loading").css("display","block");
      $(target).css("display","none");
    },
    after:function (target)
    { 
      $("#species_loading").css("display","none");
      $(target).css("display","inline");
    }
  });

  $('#continent_id').chainSelect('#country_id','ajax.php?mod='+mod+'&myaction=get_countries',
  { 
    before:function (target)
    { 
      $("#country_loading").css("display","block");
      $(target).css("display","none");
    },
    after:function (target)
    { 
      $("#country_loading").css("display","none");
      $(target).css("display","inline");
    }
  });

  $('#country_id').chainSelect('#region_id','ajax.php?mod='+mod+'&myaction=get_regions',
  { 
    before:function (target)
    { 
      $("#region_loading").css("display","block");
      $(target).css("display","none");
    },
    after:function (target)
    { 
      $("#region_loading").css("display","none");
      $(target).css("display","inline");
    }
  });

  $('#country_id').chainSelect('#location_id','ajax.php?mod='+mod+'&myaction=get_locations',
  { 
    before:function (target)
    { 
      $("#location_loading").css("display","block");
      $(target).css("display","none");
    },
    after:function (target)
    { 
      $("#location_loading").css("display","none");
      $(target).css("display","inline");
    }
  });
}

function jqInit() {
  if (gmap == 1)
  {    
    loadMap();
  }

  var loadGallery = function(hash){
    $('.gallery_unstyled').addClass('mygallery'); // adds new class name to maintain degradability
  
    $('ul.mygallery').galleria({
      history   : false, // activates the history object for bookmarking, back-button etc.
      clickNext : true, // helper for making the image clickable
      insert    : '#main_image', // the containing selector for our main image
      onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes
        
        // fade in the image & caption
        image.css('display','none').fadeIn(1000);
        caption.css('display','none').fadeIn(1000);
        
        // fetch the thumbnail container
        var _li = thumb.parents('li');
        
        // fade out inactive thumbnail
        _li.siblings().children('img.selected').fadeTo(500,0.3);
        
        // fade in active thumbnail
        thumb.fadeTo('fast',1).addClass('selected');
        
        // add a title for the clickable image
        image.attr('title','Next image >>');
      },
      onThumb : function(thumb) { // thumbnail effects goes here
        
        // fetch the thumbnail container
        var _li = thumb.parents('li');
        
        // if thumbnail is active, fade all the way.
        var _fadeTo = _li.is('.active') ? '1' : '0.3';
        
        // fade in the thumbnail when finnished loading
        thumb.css({display:'none',opacity:_fadeTo}).fadeIn(1500);
        
        // hover effects
        thumb.hover(
          function() { thumb.fadeTo('fast',1); },
          function() { _li.not('.active').children('img').fadeTo('fast',0.3); } // don't fade out if the parent is active
        )
      }
    });
  };

  $('#itembox_dlg').jqm({ajax:'@href', trigger:'a.item_card', toTop:true, onLoad:loadGallery});
  $('#searchbox_dlg').jqm({ajax: 'ajax.php?mod='+mod+'&myaction=get_advanced_search', trigger: 'a.advanced_search', toTop:true});
  $('#registerbox_dlg').jqm({ajax: 'ajax.php?mod='+mod+'&myaction=get_register_form', trigger: 'a.register_link', toTop:true});
}

$(document).ready(jqInit);
