(function( $ ){
  $.fn.newsSlide = function() {
    $(this).each(function() {
      var $oList = $(this).find('ul');
      var iDistance = $oList.children(':first').outerWidth();
      var oChild;
      
      oChild = $oList.children(':last').remove().prependTo($oList);
      oChild.remove().prependTo($oList);
      $oList.css('margin-left','-='+iDistance+'px');
      
      $(this).prev().find('.next').click(function(e){
        e.preventDefault();
        $oList.stop(true,true).animate({
          marginLeft: '-='+iDistance+'px'
        },function(){
          oChild = $oList.children(':first');
          oChild.remove().appendTo($oList);
          $oList.css('margin-left','+='+iDistance+'px');
        });
      });
      
      $(this).prev().find('.prev').click(function(e){
        e.preventDefault();
        $oList.stop(true,true).animate({
          marginLeft: '+='+iDistance+'px'
        },function(){
          oChild = $oList.children(':last').remove().prependTo($oList);
          oChild.remove().prependTo($oList);    
          $oList.css('margin-left','-='+iDistance+'px');
        });
      });
    });
  };
  return false;
})( jQuery );

/**
 * Set select value to span in forms
 */
function vFillSelectSpan (poSelect) {
  var sName  = 'show-text-' + poSelect.id;
  var iIndex = poSelect.selectedIndex;
  var sValue = poSelect.options[iIndex].text;
  document.getElementById(sName).innerHTML = sValue;
};

$(function() {
  $("#flow").wSlide();
  $('.newsSlideContainer').newsSlide();
  //Forms
  $(document).ready(function(){
   var $oSelect;
   $('select').change(function(){
     $oSelect = $(this);
     $oSelect.prevAll('span').text($oSelect.children('option:selected').text());
   });
   $('select').each(function(){
     $oSelect = $(this);
     $oSelect.prevAll('span').text($oSelect.children('option:first').text());
   });
   //select buttons
   $('.select').find('img').each(function(){
     var sSrc = $(this).attr('src');
     var sSrcH = sSrc.replace('arrow.select','arrow.select.hover');

     $(this).parent().hover(function(){
       $(this).find('img').attr('src',sSrcH);
     },function(){
       $(this).find('img').attr('src',sSrc);
     });
   });
   //ipad fix
   
   if('ontouchstart' in document.documentElement){
     $('#lang-select a').click(function(){
     window.location.href =  $(this).attr('href');
     return false;
     });
     $('#language-selector').toggle(function() {
       $(this).addClass('language-hover');
     }, function() {
       $(this).removeClass('language-hover');
     });
   }else{
     $('#language-selector').hover(function() {
       $(this).addClass('language-hover');
     }, function() {
       $(this).removeClass('language-hover');
     });
   }
 });
});


