// Ajaxification des liens des menus HTML et css dynamique
$j(document).ready(function() {   
  $j(".wu_menu_ul *[id^='wuGoTo'], .noStyleUL *[id^='wuGoTo']").each(function() {
    if($j(this).get(0).tagName == 'DIV') {     
      $j(this).replaceWith('<a id="'+$j(this).attr('id')+'" href="'+$j(this).attr('href')+'" target="_top" class="'+$j(this).attr('class')+'">'+$j(this).html()+'</a>');
    }  
  });
  $j(".wu_menu_ul *[id^='wuGoTo'], .noStyleUL *[id^='wuGoTo']").click(function(ev) {
    if($j(this).parents().find('.dgtl_home').length > 0 ) return true;
    /*var id = $j(this).attr('id');
    var array_id = id.split('_');*/ 
    var id = $j(this).attr('href');
    var array_id = id.split('_');
    dgtl_easing_swing(ev, 'ID='+array_id[1]+'&dgtl_ajaxFile=longName', 'ID='+array_id[1]+'&dgtl_ajaxFile=rub&dgtl_init=1');
  });
  setTimeout("dgtl_slidedeck_init();wuCleanUpHeight();myJqueryInit(); ",200);
  dgtl_input_value_replace();
});
jQuery.fn.dgtl_equalHeight = function (child) {
  var height       = 0;
  var maxHeight    = 0;
	// recupere la hauteur maximale
  $j(this).find('.wuc_col:first').each(function() {
    height = $j(this).dgtl_equalHeight();   
    maxHeight    = (height > maxHeight) ? height : maxHeight;    
    //$j('.dgtl_debug').append('1 '+$j(this).attr('id')+' - maxheight: '+maxHeight+' - height: '+height+"\n");
  });
  if (!child) {         
    $j(this).next('.wuc_col').each(function() {
      height = $j(this).dgtl_equalHeight(true);
      maxHeight    = (height > maxHeight) ? height : maxHeight;
      //$j('.dgtl_debug').append('2 '+$j(this).attr('id')+' - maxheight: '+maxHeight+' - height: '+height+"\n");
    });
  }
  if (!$j(this).hasClass('slide')) {
    height = $j(this).outerHeight(true);    
    maxHeight    = (height > maxHeight) ? height : maxHeight;
  }
  if (!child) {
    $j(this).nextAll('.wuc_col').each(function() {
      $j(this).setHeight(maxHeight);
    }); 
  }
  $j(this).setHeight(maxHeight);
  return (maxHeight);       
}

function dgtl_input_value_replace() {
	$j('input.valueReplace').each(function() { var o = new dgtl_value_replace($j(this)); });
	function dgtl_value_replace(e) {		
		var o = {
		  init : function() {
				o.field = $j(e); 
				o.isSet = false;
				o.defaultValue = o.field.val();
				o.monitor();
			},
			monitor : function() {
				o.field.focus(function(){
					if( $j(this)[0].value == o.defaultValue) { $j(this)[0].value = ''; }
				});
				o.field.blur(function(){
					if($j(this)[0].value == '') { $j(this)[0].value = o.defaultValue;}
				});
			}
		};
		o.init();		
	};
}

function dgtl_easing_swing(ev, LG_params, CONTENT_params, css) {
  ev.preventDefault();                                         
  $j('#wuContent').css('min-height', '').animate({height: '0px'}, 500, 'jswing', function(){
    // update content longName
    $j('.dgtl_LG h1').load('plugins/dgtl/php/dgtl-ajax.php?'+LG_params, function() {        
      // update content rub      
      $j('#wuContent').load('plugins/dgtl/php/dgtl-ajax.php?'+CONTENT_params, function() {
        // remove des css de l ancien contenu
        $j("head > link[href^='ressources/css/css-rub']").remove();
        $j("head > link[href^='ressources/css/css-ergo']").remove();
        $j("head > link[href^='ressources/css/css-window']").remove();  
        // recalcul hauteur contenu
        wuCleanUpHeight(); myJqueryInit();
        dgtl_slidedeck_init();
        // set new height content
        $j('#wuContent').css('min-height', '').css('height', '0px');
        // easing display
        $j('#wuContent').animate({height: parseInt($j('#wuRub').css('min-height'))+'px'}, 500, 'jswing', function(){
          //window.setTimeout(function() {
            wuCleanUpHeight();
            myJqueryInit();
          //}, 700);
        });           
      });                    
    });                
  });    
}

function dgtl_easing_swing_news(ev, CONTENT_params) {
  var LG_params ='';
  ev.preventDefault();                                         
  $j('#dgtl_N_content').css('min-height', '').animate({height: '0px'}, 500, 'jswing', function(){    
    // update content news
    $j('#dgtl_N_content').load('plugins/dgtl/php/dgtl-ajax.php?'+CONTENT_params, function() {
        // easing display         
        $j('#dgtl_N_content').animate({height: parseInt($j('.dgtl_news').height())+'px'}, 500, 'jswing', function(){
          wuCleanUpHeight(); myJqueryInit();
        });           
    });
  });    
}


function dgtl_easing_swing_annonce(ev, CONTENT_params) {
  ev.preventDefault();                                         
  $j('#dgtl_A #content_list').css('min-height', '').animate({height: '0px'}, 500, 'jswing', function(){    
    // update content annonce
    $j('#dgtl_A #content_list').load('plugins/dgtl/php/dgtl-ajax.php?'+CONTENT_params, function() {
      // easing display         
      $j('#dgtl_A #content_list').animate({height: parseInt($j('.#dgtl_A #annonce_line').height())+'px'}, 500, 'jswing', function(){
        wuCleanUpHeight(); myJqueryInit();
      });           
    });
  });    
}

function dgtl_calendar(dgtl_news_date, dgtl_lg) { 
  // recupere var date actu 
  
  var array_dgtl_news_date = dgtl_news_date.split('/');
  var news_date = array_dgtl_news_date[2]+'/'+array_dgtl_news_date[1]+'/'+array_dgtl_news_date[0];
  var months = new Array();
  months['fr'] = new Array ('janv.', 'f&eacute;vr.', 'mars', 'avr.', 'mai', 'juin', 'juil.', 'ao&ucirc;t', 'sept.', 'oct.', 'nov.', 'd&eacute;c.');
  months['en'] = new Array ('jan.', 'feb.', 'march', 'april', 'may', 'june', 'july', 'aug.', 'sept.', 'oct.', 'nov.', 'dec.');
  months['nl'] = new Array ('jan.', 'feb.', 'maart', 'april', 'mei', 'juni', 'juli', 'aug.', 'sept.', 'okt.', 'nov.', 'dec.');
  var newsDate = new Date(news_date);            
  var newsDay = parseInt(newsDate.getDate()) < 10 ? '0'+newsDate.getDate() : newsDate.getDate();
  var newsMonth = newsDate.getMonth();
  var newsMonthNum = parseInt(newsMonth+1) < 10 ? '0'+parseInt(newsMonth+1) : parseInt(newsMonth+1);    
  var newsMonthLabel = months[dgtl_lg][parseInt(newsMonth)];
  var newsYear = newsDate.getFullYear();
  
  // recupere var date calendar 
  var firstDayNum = new Date(newsYear, newsMonth, 1).getDay();
  var firstDayIndex = parseInt(firstDayNum) == 0 ? 7 : firstDayNum;            
  var nbDaysOnMonth = 32 - new Date(newsYear, newsMonth, 32).getDate();
  var lastDayIndex = Math.round(parseInt(nbDaysOnMonth) + parseInt(firstDayIndex) - 1);
  var newsDayIndex = Math.round(parseInt(newsDay) + parseInt(firstDayIndex) - 1);
  
  // affiche les var date 
  $j('.year').html(newsMonthLabel+' '+newsYear);
  $j('.month').html(newsMonthNum);
  $j('.day').html(newsDay);
  
  // affiche calendar 
  $j('.dgtl_cal').find('td').each(function(){
    var trIndex = $j(this).parent().index();
    var tdIndex = $j(this).index() + 1 + (trIndex*7);
    if(tdIndex >= firstDayIndex && tdIndex <= lastDayIndex)
      $j(this).addClass("isDay");
    if(tdIndex == newsDayIndex)
      $j(this).addClass("selected");
  });
}


function dgtl_slide_nav(ev, bt, slide, slideID) {
  // navigation
  if(bt.attr('class') == 'prev') { slide.prev(); }
  if(bt.attr('class') == 'next') { slide.next(); }
  dgtl_slide_bt(slide, slideID);
}
function dgtl_slide_bt(slide, slideID) {
  var nbSlide = $j('#wuSlidedeck_'+slideID).find('dd').length;
  // update bt
  if(slide.current == 1) { $j('#dgtl_S_nav_'+slideID).find('.prev').addClass('off'); }
  else {  $j('#dgtl_S_nav_'+slideID).find('.prev').removeClass('off'); }
  if(slide.current == nbSlide) { $j('#dgtl_S_nav_'+slideID).find('.next').addClass('off'); }
  else { $j('#dgtl_S_nav_'+slideID).find('.next').removeClass('off'); }
}
function dgtl_slide_pagination(slide, slideID) {  
  var nbSlide = $j('#wuSlidedeck_'+slideID).find('dd').length;  
  $j('#dgtl_S_nav_'+slideID+' .pagination').find('a').removeClass('on'); 
  $j('#dgtl_S_nav_'+slideID+' .pagination').find('a.'+slide.current).addClass('on');  
}

function dgtl_loca(ev, CONTENT_params) {
  $j('#dgtl_L').fadeOut('fast', function() {
    $j('#dgtl_L').load('plugins/dgtl/php/dgtl-ajax.php?'+CONTENT_params, function() {
      $j('#dgtl_L').fadeIn('fast', function() {
        wuCleanUpHeight(); myJqueryInit();
      });
    });
  });
}

function dgtl_search(form) {
  form.ajaxSubmit({        				
		success : function (flow) {
      $j('#dgtl_A').html(flow);
      wuCleanUpHeight();
      myJqueryInit();     	 			
	  }
	});
}

function dgtl_slidedeck_init() {
  //Hauteur du contenu des slides + navigation  
  //-------------------------------------------            
  var heightProperty    = $j.browser.msie && $j.browser.version < 7 ? 'height' : 'min-height';
  // affect les hauteurs des enfants de chaque dd
  // --------------------------------------------
  var ddMaxHeight = 0;          
  var ddHeight = 0;
  $j('dd').each(function() {
    ddHeight = $j(this).dgtl_equalHeight()+50;
    $j('.dgtl_debug').append('ddHeight '+ddHeight+' ');
    //var ddHeight = Math.round(parseInt($j(this).css(heightProperty)) + 20); 
    ddMaxHeight = ( ddHeight > ddMaxHeight) ? ddHeight : ddMaxHeight;                                  
  }); 
  // affect la ddMaxHeight a chaque dd
  //----------------------------------
  $j('dd, dl').css(heightProperty, ddMaxHeight + 'px');
  $j('dd, dl').css('max-height', ddMaxHeight + 'px');
  $j('dd, dl').css('height', ddMaxHeight + 'px');
  $j('.wuSlide_index').height('1px');
 
}

