/**
 * JLibrary - Twisto Javascript Library
 *
 * @package Database
 * @author Jean Lalande, Nicolas Hamelin
 * @copyright Twisto Inc.
 *
 */
 
 
  /**
 *  Jquery Actions
 */
$(document).ready(function(){

	//Original Animate LastUpdateBox on Guides home pages => don't work !
/*
	$("#home .lastupdate").animate({left: "+=530"}, 700)
	.animate({height: "65", width: "400" , fontSize : "2em" , top: "+=20"}, 700)
	.delay(2000)
	.animate({height: "30", width: "150" , fontSize : "0.8em"}, 700)
	.animate({left: "-=530"}, 700)
	.animate({top: "-=20"}, 700)
	;
*/

		//Sexy Drop down Top Menu animation

	  //$("ul.subnav").parent().append("<span></span>"); //Only shows drop down trigger when js is enabled (Adds empty span tag after ul.subnav*)  
  
  	  $("ul.topnav li a").hover(function() { //When trigger is clicked...  
  
        //Following events are applied to the subnav itself (moving subnav up and down)  
        $(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click  
  
        $(this).parent().hover(function() {  
        }, function(){  
            $(this).parent().find("ul.subnav").slideUp('medium'); //When the mouse hovers out of the subnav, move it back up  
        });  
  
        //Following events are applied to the trigger (Hover events for the trigger)  
        }).hover(function() {  
            $(this).addClass("subhover"); //On hover over, add class "subhover"  
        }, function(){  //On Hover Out  
            $(this).removeClass("subhover"); //On hover out, remove class "subhover"  

 	});
        
        
      //Animate LastUpdateBox on Guides home pages 
	$("#home .lastupdate")
	.animate({height: "100" , fontSize : "1.5em"}, 500)
	.delay(2500)
	.animate({height: "30", fontSize : "0.8em"}, 500)
	;  
        
        
	$('.ic_mapDirectLink').live('click', function() {});
	$('.makeLinkExternal').nextAll('a').first().attr('target', '_blank');
	$('.hideNext').css('cursor', 'pointer').html('<img src="/ressources/images/two/unarchive.gif" alt="Cacher">').each(function() { $(this).nextAll('div').first().show() });
	$('.showNext').css('cursor', 'pointer').html('<img src="/ressources/images/two/archive.gif" alt="Voir">').each(function() { $(this).nextAll('div').first().hide() });
	
	$('.hideNext').next('h3').bind('click', function() { $(this).prev('span').click() }).css('cursor', 'pointer');
	$('.showNext').next('h3').bind('click', function() { $(this).prev('span').click() }).css('cursor', 'pointer');
	
	$('.hideNext').live('click', function() {
	$(this).nextAll('div').first().slideUp();
	$(this).removeClass('hideNext').addClass('showNext');
	$(this).html('<img src="/ressources/images/two/archive.gif" alt="Voir">');
	})
	
	$('.showNext').live('click', function() {
	$(this).nextAll('div').first().slideDown();
	$(this).removeClass('showNext').addClass('hideNext');
	$(this).html('<img src="/ressources/images/two/unarchive.gif" alt="Cacher">');
	})
                
	//Display GoTotop link if document is  long
	if($(document).height()>2000) $("#gototop").css({'display':'block'});
   		


if(typeof $("#admin .success_notification").attr('class') != 'undefined')
{
                if($("#admin .success_notification").children('p').first().text() != '')
                {
                                $("#admin .success_notification").hide();
                                title = $("#admin .success_notification").children('p').first().html();
                                $("#admin .success_notification").children('p').first().remove();
                                $.growlUI(title, $("#admin .success_notification").html(), 5000, 'success')
                }
}

if(typeof $("#admin .error_notification").attr('class') != 'undefined')
{
                if($("#admin .error_notification").children('p').first().text() != '')
                {
                                $("#admin .error_notification").hide();
                                title = $("#admin .error_notification").children('p').first().html();
                                $("#admin .error_notification").children('p').first().remove();
                                $.growlUI(title, $("#admin .error_notification").html(), 5000, 'error')
                }
}

                $('.autofocus').focus();
                if($.blockUI)
                {
                                $.blockUI.defaults = {
                                                // message displayed when blocking (use null for no message)
                                                message:  '<img src="/ressources/images/two/ajax-loader.gif" alt="Loading...." title="Loadding...." /><br /><span style="color:#F17717">Veuillez patienter...</span>',
                                            
                                                // styles for the message when blocking; if you wish to disable
                                                // these and use an external stylesheet then do this in your code:
                                                // $.blockUI.defaults.css = {};
                                                css: {
                                                    padding:        0,
                                                    margin:         0,
                                                    width:          '600px',
                                                    top:            '40%',
                                                    left:           '35%',
                                                    textAlign:      'center',
                                                    color:          '#000',
                                                    border:         '0px',
                                                    cursor:         'wait'
                                                },
                                            
                                                // styles for the overlay
                                                overlayCSS:  {
                                                    backgroundColor: '#FFF',
                                                    opacity:         0.92
                                                },
                                
                                                // styles applied when using $.growlUI
                                                growlCSS: {
                                                                width:    '100%',
                                                                top:      '0px',
                                                                bottom:      '',
                                                                left:     '0px',
                                                                right:    '',
                                                                border:   '',
                                                                borderBottom: '3px solid #eee',
                                                                padding:  '15px',
                                                                opacity:   0.90,
                                                                cursor:    "wait",
                                                                color:    '#fff',
                                                                backgroundColor: '#f17717'
                                                },
                                                
                                                // IE issues: 'about:blank' fails on HTTPS and javascript:false is s-l-o-w
                                                // (hat tip to Jorge H. N. de Vasconcelos)
                                                iframeSrc: /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank',
                                        
                                                // force usage of iframe in non-IE browsers (handy for blocking applets)
                                                forceIframe: false,
                                
                                                // z-index for the blocking overlay
                                                baseZ: 1000,
                                            
                                                // set these to true to have the message automatically centered
                                                centerX: true, // <-- only effects element blocking (page block controlled via css above)
                                                centerY: true,
                                            
                                                // allow body element to be stetched in ie6; this makes blocking look better
                                                // on "short" pages.  disable if you wish to prevent changes to the body height
                                                allowBodyStretch: true,
                                            
                                                // enable if you want key and mouse events to be disabled for content that is blocked
                                                bindEvents: true,
                                            
                                                // be default blockUI will supress tab navigation from leaving blocking content
                                                // (if bindEvents is true)
                                                constrainTabKey: true,
                                            
                                                // fadeIn time in millis; set to 0 to disable fadeIn on block
                                                fadeIn:  0,
                                            
                                                // fadeOut time in millis; set to 0 to disable fadeOut on unblock
                                                fadeOut:  200,
                                            
                                                // time in millis to wait before auto-unblocking; set to 0 to disable auto-unblock
                                                timeout: 4000,
                                        
                                                // disable if you don't want to show the overlay
                                                showOverlay: true,
                                            
                                                // if true, focus will be placed in the first available input field when
                                                // page blocking
                                                focusInput: true,
                                            
                                                // suppresses the use of overlay styles on FF/Linux (due to performance issues with opacity)
                                                applyPlatformOpacityRules: true,
                                            
                                                // callback method invoked when unblocking has completed; the callback is
                                                // passed the element that has been unblocked (which is the window object for page
                                                // blocks) and the options that were passed to the unblock call:
                                                //     onUnblock(element, options)
                                                onUnblock: null,
                                            
                                                // don't ask; if you really must know: http://groups.google.com/group/jquery-en/browse_thread/thread/36640a8730503595/2f6a79a77a78e493#2f6a79a77a78e493
                                                quirksmodeOffsetHack: 4
                                };
                                // End default blockUI
                }
                
                
                // Manage forgot password link.
                if($("#forgot_password").length != 0)
                {
                                $("#forgot_password").click(function() {
                                                
                                                $(this).hide().parent('form').attr("action", "/connexion/mot_de_passe_oublie").attr('id', 'forgot_password');
                                                $("#content").css('overflow', 'hidden');
                                                $("#motdepasse").slideUp("slow");
                                                $("#passwordLabel").slideUp("slow");
                                                $("#submit").attr('value', "M'envoyer un mot de passe temporaire");
                                                
                                                $(".info_notification").slideUp("slow");
                                                $(".info_notification").html('<p>Saisissez votre adresse courriel.</p><p>Vous devez utiliser l\&apos;adresse avec laquelle vous vous &ecirc;tes inscrit(e). Un mot de passe temporaire vous permettant de choisir un nouveau mot de passe vous sera envoy&eacute; &agrave; cette adresse. | <a href="connexion/inscription">Je ne suis pas inscrit(e)</a></p><p></p>');
                                                $(".info_notification").slideDown("slow");
                                                $("#content h1").html("Mot de passe oubli&eacute;");
                                                
                                                
                                })
                                
                                $("#forgot_password").live('submit', function() {
                                                //alert('asdf')
                                                $.blockUI();
                                                var lastEmail = $("#courriel").val();
                                                $.post('/connexion/mot_de_passe_oublie', {email: $("#courriel").val()}, function(data) {
                                                                $(".info_notification").slideUp().replaceWith(data).slideDown();
                                                                $(".alert_notification").slideUp().replaceWith(data).slideDown();
                                                                if(data.match(/.*success.*/))
                                                                {
                                                                     $("#forgot_password").slideUp().load('/connexion', function(){
                                                                                                $("#content h1").html("Mot de passe provisoire envoy&eacute;");
                                                                                                $.unblockUI();
                                                                                                $("#courriel").val(lastEmail)
                                                                                                $("#forgot_password").attr('action', '/connexion').attr('id', 'noID');
                                                                                }).slideDown();
                                                                     
                                                                }
                                                                else
                                                                $.unblockUI();
                                                })
                                                
                                                return false;
                                })
                }
                
                
                $('#tagFilter').keyup(function(event){
                    
                              $('.tagWord').each(function() {
                                                re = new RegExp($('#tagFilter').val());
                                                m = re.exec($(this).text());

                                                if(m != null)
                                                {
                                                                $(this).parent('.tag').prevAll('h3').first().show();
                                                                $(this).parent('.tag').show();
                                                }
                                                else
                                                {
                                                                $(this).parent('.tag').prev('h3').first().hide();
                                                                $(this).parent('.tag').hide();
                                                }
                              });
                              
                                                             
                })
                
                if($('#munFilter').length != 0 || $('#mrcFilter').length != 0)
                {
                                $('#munFilter').autocomplete('/obv/autocomplete', { extraParams: { type: 'mun'}, max: 60, minChars: 3});
                                $('#mrcFilter').autocomplete('/obv/autocomplete', { extraParams: { type: 'mrc'}, max: 60, minChars: 3});
                }
        
 });
