$(document).ready(function(){

    $(".slider_item:first").show(); 
    
    /** Start Jquery Functions */
    
        jQuery.fn.moveBlock = function(top , left){
            this.css("position" , "absolute");
            this.css("top" , top + "px");
            this.css("left" , left + "px");
            return this;
        }
    
    /** End Jquery Functions */
    
	/** Start Datepicker */
        
        function showEvent(date){
            $.ajax({
              type: "POST",
              url: getEventPostUrl(),
              data: "date="+date,
              success: function(response){
                $('#results').html('');
                $.each(response, function(index, value){
                  $('#results').append('<p><a href="' + value.url + '"><span class="date">' + value.date + '</span><br /><span class="title">' + value.title + '</span></a></p>');
                });
              },
              dataType: 'json'
            });
            return false;
        }
        
        /**
          *     The Jquery UI plugin uses month system 0 to 11 and also for days , so `tricks` had to be
          *    implemented for functionality
          */
        function availableEvents(date){
            var curent_day = date.getFullYear()+"-";
            curent_day += (date.getMonth()+1) < 10 ? "0"+(date.getMonth()+1)+"-" : (date.getMonth()+1)+"-";
            curent_day += (date.getDate()) < 10 ? "0"+(date.getDate()) : (date.getDate());
            if(jQuery.inArray(curent_day , available_days) == -1) {
                return [false , ""];
            }
            return [true , ""];
        }
        
        var pickerOpts = {
            inline: true ,
            closeText: 'Fermer', closeStatus: 'Fermer sans modifier',
            prevText: '&lt;Pr&eacute;c', prevStatus: 'Voir le mois pr&eacute;c&eacute;dent',
            nextText: 'Suiv&gt;', nextStatus: 'Voir le mois suivant',
            currentText: 'Courant', currentStatus: 'Voir le mois courant',
            monthNames: ['Janvier','F&eacute;vrier','Mars','Avril','Mai','Juin',
            'Juillet','Ao&ucirc;t','Septembre','Octobre','Novembre','D&eacute;cembre'],
            monthNamesShort: ['Jan','F&eacute;v','Mar','Avr','Mai','Jun',
            'Jul','Ao&ucirc;','Sep','Oct','Nov','D&eacute;c'],
            monthStatus: 'Voir un autre mois', yearStatus: 'Voir un autre ann&eacute;e',
            weekHeader: 'Sm', weekStatus: '', firstDay: 1,
            dayNames: ['Dimanche','Lundi','Mardi','Mercredi','Jeudi','Vendredi','Samedi'],
            dayNamesShort: ['Dim','Lun','Mar','Mer','Jeu','Ven','Sam'],
            dayNamesMin: ['Di','Lu','Ma','Me','Je','Ve','Sa'],
            onSelect : showEvent,
            beforeShowDay : availableEvents
        }
         
        if ($('#datepicker').length > 0) {
            /**
              * getAvailableEvents function is defined in HP template.
              */
            
            var available_days = getAvailableEvents();
    
            $('#datepicker').datepicker(pickerOpts);   
        }   
    
    /** End Datepicker */
    
    /** Start Search Bullets */
        
        $(".search_choice").click(function(event){
            $(".block_search").hide();
            var search_block = "#block_" + $(this).attr('id');
            var position     = $(this).position(); 
            var top          = position.top + $(this).height() - 10;
            var left         = position.left - 13;
            var bullet = "#"+$(this).attr('id') + " img:first";
            $(search_block).width($(bullet).width()-4);
            $(search_block).height($(bullet).width()-4);
            $(search_block).moveBlock(top , left);
            $(search_block).show();
        });
        
        $(".search_items_block .search_items img.close").live("click", function(event){
            $("#"+$(this).parent().attr("id")).hide();
        });
        
        $(".block_search").hover(function(){
            
        }, function(){
          $(this).hide();    
        });
        
    /** End Search Bullets */
    
    /** Start Slider */
    
        $(".slider_toggle_buttons a").live("click", function(event){
            var id = $(this).attr('id');
            var item = id.replace('toggle_' , '');
            $('.slider_item').hide();
            $('.toggle_slider_item').removeClass('active_item');
            $('#'+id).addClass('active_item');
            $('#'+item).show();
        }); 
    
    /** End Slider */
    
    /** Event Page */
        
        $('#evenement .box_expand, #evenement .box_collapse').hover(function(){
            $(this).css('background-color', '#FFFF9B');
            $(this).find('.middle_box_image img').show();
        }, function(){
            $(this).css('background-color', '#FFFFFF');
            $(this).find('.middle_box_image img').hide();
        });
        
        $('#evenement .box_expand').live('click', function(){
          var src = $(this).find('.middle_box_image img').attr('src');
          $(this).find('.middle_box_image img').attr('src', src.replace('plus', 'minus'));
          partial = $(this).find('.partial');
          full = $(this).find('.full');
          var final_full_height = full.height();
          full.css("height",partial.height());
          partial.hide();
          full.show().animate({height:final_full_height+"px"}).css('display', 'inline-block');
          $(this).removeClass('box_expand');
          $(this).addClass('box_collapse')
        });
        
        $('#evenement .box_collapse').live('click', function(){
          var src = $(this).find('.middle_box_image img').attr('src');
          $(this).find('.middle_box_image img').attr('src', src.replace('minus', 'plus'));
          partial = $(this).find('.partial');
          full = $(this).find('.full');
          var final_full_height = full.height();
          full.hide();
          partial.show();
          $(this).removeClass('box_collapse');
          $(this).addClass('box_expand')
        });



        $('.conferencier').click(function(event){
          event.stopPropagation();
          var src = $(this).find('.conferencier_hover img').attr('src');
          $(this).find('.conferencier_hover img').attr('src', src.replace('plus', 'minus'));
          $(this).find('.conferencier_information').toggle();
        });

        if ($(".register_button").length>0) {
          var $object   = $(".register_button"),
              $window    = $(window),
              offset     = $object.offset(),
              topPadding = 150;
        
          $window.scroll(function() {
            if ($window.scrollTop() > offset.top) {
              $object.stop().animate({
                marginTop: $window.scrollTop() - offset.top + topPadding
              });
            } else {
              $object.stop().animate({
                marginTop: 0
              });
            }
          });
        }        

        $('.register_lightbox').click(function(event){
          event.preventDefault();
          $(document).scrollTop(0);
          $('#main').append('<div class="register_lightbox_container"><div id="popup_layer" style="height: ' + $(document).height() + 'px; width: 100%; display: block; opacity: 0.5;"></div>' +
              '<div id="popup_container"><div id="popup"><a class="close"></a>' +
                '<div id="popup_content"><iframe class="register_iframe" src="' + $(this).attr('href') + '"></iframe></div>' +
              '</div></div></div>');
          $('.close').live('click',function(){
            $('.register_lightbox_container').remove();
          });             
        })
    /** End Event Page */
    
    /** List of Events */
    
        $('#filter_form input').click(function(event){
            // Get the form action url
            var type_filter = $('.option_list_block input[value="all"][name="type"]:checked').length;
            var tag_filter = $('.option_list_block input[value="all"][name="tag"]:checked').length;
            var action_url_class = '.filter_url_';
            
            if ((type_filter==0)&&(tag_filter==0)) {
                action_url_class += 'both';
            } else if (type_filter==0) {
                action_url_class += 'type';   
            } else if (tag_filter==0) {
                action_url_class += 'tag';
            } else action_url_class += 'none';
            
            var action = $(action_url_class).text() + '';
            
            // Replace the type_name and tag_name
            var type_name = $('.option_list_block input[name="type"]:checked').prev().text();
            action = action.replace("type_name", type_name);
            var tag_name = $('.option_list_block input[name="tag"]:checked').prev().text();
            action = action.replace("tag_name", tag_name);
            
            // Set the action and submit the form
            $('#filter_form').attr('action', action);
            $('#filter_form').submit();
        });
        
        $('.search_order').click(function(){
            $('#filter_form input#order_by').val($(this).attr('id'));
            $('#filter_form').submit();
        });
    
    /** End List of Events */

    /** Header */
    
        $('#filter_form_header a').click(function(event){
            event.preventDefault();
            
            // Set the id of the filter
            var id = $(this).prev().text();
            $(this).parents('.search_items').find('input').val(id);
            
            // Set the action and submit the form
            $('#filter_form_header').attr('action', $(this).attr('href'));
            $('#filter_form_header').submit();
        })

    /** End Header */
});
