/* Author: Andreas Philippi - Cubus Arts

*/

jQuery(window).load(function() {
  positionAssociatesLogos();
});

jQuery(document).ready(function() {      
  applyCss3Selectors();
  replaceEuro();
  deleteEmptyParagraphs();
  //adjustTravelsOnStartpage();
  
  enableDatePicker();  
  enableFormValidation();

  jQuery('select[multiple="multiple"]').chosen();  
  
});

jQuery(window).resize(function() {    
});

function deleteEmptyParagraphs(){
  jQuery("#main .tx-cubtravel-pi1 p.bodytext").each(function(index,element){
    console.log(jQuery.trim(jQuery(element).text()));
    
    if (jQuery.trim(jQuery(element).text()) == ''){
      jQuery(element).remove();
    }
  });
}

function enableFormValidation(){  
  jQuery('form.validatable').validate();  
}

function positionAssociatesLogos(){
  jQuery('#associates .logos ul li a').each(
    function(index, element){
      //console.log(((65-jQuery(element).height())/2)+"px");
      jQuery(element).css('margin-top', ((65-jQuery(element).height())/2)+"px");      
    });
}

function applyCss3Selectors(){  
  jQuery("#main .tx-cubtravel-pi1 .tx-cubtravel-pi1-listView.startpage .list-item-wrapper:nth-child(2n)").css("margin-left", "23px").css("margin-right", "0px");
}

function replaceEuro(){  
  jQuery("#main .tx-cubtravel-pi1").each(function(index, element){
    //console.log(element);
    jQuery(element).html(jQuery(element).html().replace(/euro/gi,'&euro;'));
  });
}

function adjustTravelsOnStartpage(){    
  
  //jQuery("#main .tx-cubtravel-pi1 .tx-cubtravel-pi1-listView.startpage .list-item-wrapper .list-item-wrapper-teaser a").truncatable({limit: 220});
   
  var prevElement;
  
  jQuery("#main .tx-cubtravel-pi1 .tx-cubtravel-pi1-listView.startpage .list-item-wrapper").each(function(index,element){            
    
    if (index %2 == 0) {
      prevElement = element;
    }else{
      if (jQuery(element).height() > jQuery(prevElement).height()){
	  jQuery(prevElement).css("height", jQuery(element).height()+"px");
      }else{
	jQuery(element).css("height", jQuery(prevElement).height()+"px");
      }
    }    
    
    //jQuery(element).css("border-bottom", "1px solid red")
  });          
  
}

function enableDatePicker(){
  
  //prepopulate datepicker
  // get today's date
  //var myDate = new Date();
  //var prettyDate =myDate.getDate() + '-'+(myDate.getMonth()+1) + '-' +  myDate.getFullYear();
  //jQuery('.tx-cubtravel-pi1 .tx-cubtravel-pi1-searchForm #tx_cubtravel_pi1_travel_date_display').val(prettyDate);    
  
  jQuery('.tx-cubtravel-pi1 .tx-cubtravel-pi1-searchForm #tx_cubtravel_pi1_travel_date_display').datepicker(({ dateFormat: 'dd-mm-yy' }));  
}



function assignNameAndSurname(){
  jQuery('#tx_cubtravel_pi1_name').val(jQuery(jQuery('#name_and_surname').val().split(' ')).last()[0]);
  jQuery('#tx_cubtravel_pi1_surname').val(jQuery('#name_and_surname').val().replace(' '+jQuery('#tx_cubtravel_pi1_name').val(), ''));
}




