// Don't break on browsers without console.log();
try { console.assert(1); } catch(e) { console = { log: function() {}, assert: function() {} } }

var brandstoggle = "companies";

jQuery(function($) {
   
   // Harmonica-en
   $('#main div.mainitem').bind('mouseenter, mouseover', function() {  
      $('#main div.mainitem').not(this).removeClass('open');
      $(this).addClass('open');   		
      $('#main div.mainitem').not(this).animate({ height: "100px" }, { queue:false, duration:200 } );
      $(this).animate({ height: "202px" }, { queue:false, duration:200 } );
   }).bind('click', function() {
      window.location = $(this).attr('rel');   
   });

   $('div.newsitem').bind('click', function() {
      window.location = $(this).attr('rel');   
   });

   if ($('#brands').is('*')) {
      initBrands('');

   }
   
   // Image rollovers initialiseren..   
   PEPS.rollover.init();   
   
   
   
   $('#labourstandards-icons a img').hover( function() {
      src= $(this).attr('src').replace('.jpg','_hover.png')
      $(this).attr('src', src );
   }, function() {
      src= $(this).attr('src').replace('_hover.png','.jpg')
      $(this).attr('src', src );
   });
   
   // Init functie voor de zoek-box
   $('#zoek').bind('focus', function() {
      $(this).addClass('focus');
      $(this).addClass('nobg');
   }).bind('blur', function() {
      $(this).removeClass('focus');
      if ($('#zoek').attr('value')=="") {
         $(this).removeClass('nobg');  
      }
   });
      
   
   
   // Init functie voor het brands-filter
   $('#brandfilter').bind('keyup', function() {
        //console.log( $('#brandfilter').attr('value'));
        initBrands($('#brandfilter').attr('value'));
   }).bind('focus', function() {
      $(this).addClass('focus');
      $(this).addClass('nobg');
   }).bind('blur', function() {
      $(this).removeClass('focus');
      if ($('#brandfilter').attr('value')=="") {
         $(this).removeClass('nobg');  
      }
   });
   
   
   $('#brands-button').bind('click', function() {
      brandstoggle = "companies";
      $('#companies-button').attr('src', '/graphics/search-companies.gif');
      $('#brands-button').attr('src', '/graphics/search-brands-act.gif');      
      $('#brandfilter').trigger('keyup');
   }).hover(function() {
      if ($('#brands-button').attr('src') == "/graphics/search-brands.gif") {
         $('#brands-button').attr('src', '/graphics/search-brands-hover.gif');
      }
   }, function() {
      if ($('#brands-button').attr('src') == "/graphics/search-brands-hover.gif") {
         $('#brands-button').attr('src', '/graphics/search-brands.gif');
      }
   });
   
   $('#companies-button').bind('click', function() {
      brandstoggle = "brands";
      $('#companies-button').attr('src', '/graphics/search-companies-act.gif');
      $('#brands-button').attr('src', '/graphics/search-brands.gif');
      $('#brandfilter').trigger('keyup');
   }).hover(function() {
      if ($('#companies-button').attr('src') == "/graphics/search-companies.gif") {
         $('#companies-button').attr('src', '/graphics/search-companies-hover.gif');
      }
   }, function() {
      if ($('#companies-button').attr('src') == "/graphics/search-companies-hover.gif") {
         $('#companies-button').attr('src', '/graphics/search-companies.gif');
      }
   });
   
   
   $('.redarrow').bind('click', function(){
      if ($(this).hasClass('open')) {
         $(this).removeClass('open');
		 if ($.browser.msie) {
			$(this).next('div').hide();
		}
      } else {
         $('.redarrow').removeClass('open');
         $(this).addClass('open')
		 
		if ($.browser.msie) {
			// This is why you can't have nice things, IE..
			$('.resource-list').not( $(this).next('div') ).hide();
			$(this).next('div').show();
		} else {
			$('.resource-list').not( $(this).next('div') ).slideUp();
			$(this).next('div').slideDown();
		}
      }
      
   });
   
   
});





function initBrands(filter){

   // console.log(jQuery);

   var linewidth = 22; // in hele 'units'..
   var minwidth = 3; // Minimale aantal units voor afbreken..
   var unitwidth = 30; // breedte in pixels per unit.

   var currentline = 0;

   jQuery('#brands').html('');

   jQuery.each(brands, function(i, val){

       // Even het filter checken..
       if ( (filter!="") &&
           (strpos(strtolower(" "+brands[i]), strtolower(filter))==false) &&
           (strpos(strtolower(" "+keywords[i]), strtolower(filter))==false) ) {
           return true;
       }
   
       // console.log(i + ": " + val + " = " + sizes[i] + " - current: " + currentline);
   
       var size = sizes[i];
   
       var t = jQuery("<div>braaaand</div>");    
       t.addClass('brand');
       t.html("<span>"+val+"</span>");
       t.attr('rel', images[i]);
       t.attr('id', ids[i]);
   
       if ( (currentline + size) <= linewidth ) {
           t.css('width', (size * unitwidth) - 40 + "px")    
           jQuery('#brands').append(t);
           // console.log('past: ' + currentline);
           currentline += size;
       } else {
       
           if ( ((linewidth - currentline) >= minwidth) && ((size - (linewidth - currentline)) >= minwidth)  ) {
               // Als er nog een stuk van tenminste 'minwidth' past..

               // Stuk dat past..
               t.css('width', ((linewidth - currentline) * unitwidth) - 40 + "px")    
               t.addClass('begin');
               jQuery('#brands').append(t);
               // console.log('afbreken, past nog: ' + (linewidth - currentline) );
   
               // Cleaner..
               jQuery('#brands').append("<div class='cleaner'>&nbsp;</div>"); 
                       
               // Maak de rest van het blok aan. 
               var restwidth = size - (linewidth - currentline);
               var t = jQuery("<div>&nbsp;</div>");    
               t.css('width', (restwidth * unitwidth) - 40 + "px");
               t.addClass('brand');
               t.addClass('eind');
               t.attr('rel', images[i]);
               t.attr('id', ids[i]);
               jQuery('#brands').append(t);
              
               currentline = restwidth;            
           
           } else {
               // Helemaal naar de volgende regel..
         
               // Cleaner..
               jQuery('#brands').append("<div class='cleaner'>&nbsp;</div>");                 
           
               t.css('width', (size * unitwidth) - 40 + "px")    
               jQuery('#brands').append(t);
               // console.log('nieuwe regel: ' + currentline);
               currentline = size;            
           
           }
       
       }
       
   });

   jQuery('#brands').append("<div class='cleaner'>&nbsp;</div>");

   if (brandstoggle=="brands") {
      initBrandsMouseover();
   } else {
      initCompaniesMouseover();      
   }
   
}



function initCompaniesMouseover() {

   // even initialiseren..
   jQuery('.brand').each(function() {

      jQuery(this).attr('data' , jQuery(this).css('background-image'));

      var background = "url(/images/" + jQuery(this).attr('rel')+")";
    
      if (jQuery(this).hasClass('eind')) {
         
          jQuery(this).addClass('hover-eind').css('background-image', background);

      } else if (jQuery(this).hasClass('begin')) {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);

      } else {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);
         
      }

   });

   // Voor de brands pagina: Trigger een hover die over twee regels loopt,
   // als het blok is afgebroken..

   // mouseover voor brands..      
   jQuery('.brand').bind('mouseover', function() {
     
      var background = jQuery(this).attr('data');
          
      if (jQuery(this).hasClass('eind')) {           
          jQuery(this).removeClass('hover-eind').css('background-image', background);
          var otherbackground = jQuery(this).prev().prev().attr('data');
          jQuery(this).prev().prev().removeClass('hover-begin').css('background-image', otherbackground);
      } else if (jQuery(this).hasClass('begin')) { 
          jQuery(this).removeClass('hover-begin').css('background-image', background);
          var otherbackground = jQuery(this).next().next().attr('data');
          jQuery(this).next().next().removeClass('hover-eind').css('background-image', otherbackground);      
      } else {
          jQuery(this).removeClass('hover-begin').css('background-image', background);  
      
      }

   });
      
   // mouseout voor brands..
   jQuery('.brand').bind('mouseout', function() {

      var background = "url(/images/" + jQuery(this).attr('rel')+")";
    
      if (jQuery(this).hasClass('eind')) {
         
          jQuery(this).addClass('hover-eind').css('background-image', background);
          jQuery(this).prev().prev().attr('data' , jQuery(this).prev().prev().css('background-image'));
          jQuery(this).prev().prev().addClass('hover-begin').css('background-image', background);
          
      } else if (jQuery(this).hasClass('begin')) {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);
         jQuery(this).next().next().attr('data' , jQuery(this).next().next().css('background-image'));
         jQuery(this).next().next().addClass('hover-eind').css('background-image', background);
         
      } else {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);
            
      }

   });    




   jQuery('.brand').bind('click', function() {
      console.log('id: ' + jQuery(this).attr('id') );
      window.location = '/index.php?e=' + jQuery(this).attr('id');
   });   

}





function initBrandsMouseover() {

   // Voor de brands pagina: Trigger een hover die over twee regels loopt,
   // als het blok is afgebroken..

   // mouseover voor brands..      
   jQuery('.brand').bind('mouseover', function() {
     
      jQuery(this).attr('data' , jQuery(this).css('background-image'));

      var background = "url(/images/" + jQuery(this).attr('rel')+")";
    
      if (jQuery(this).hasClass('eind')) {
         
          jQuery(this).addClass('hover-eind').css('background-image', background);
          jQuery(this).prev().prev().attr('data' , jQuery(this).prev().prev().css('background-image'));
          jQuery(this).prev().prev().addClass('hover-begin').css('background-image', background);
          
      } else if (jQuery(this).hasClass('begin')) {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);
         jQuery(this).next().next().attr('data' , jQuery(this).next().next().css('background-image'));
         jQuery(this).next().next().addClass('hover-eind').css('background-image', background);
         
      } else {
         
         jQuery(this).addClass('hover-begin').css('background-image', background);
            
      }

   });
      
   // mouseout voor brands..
   jQuery('.brand').bind('mouseout', function() {
      var background = jQuery(this).attr('data');
          
      if (jQuery(this).hasClass('eind')) {           
          jQuery(this).removeClass('hover-eind').css('background-image', background);
          var otherbackground = jQuery(this).prev().prev().attr('data');
          jQuery(this).prev().prev().removeClass('hover-begin').css('background-image', otherbackground);
      } else if (jQuery(this).hasClass('begin')) { 
          jQuery(this).removeClass('hover-begin').css('background-image', background);
          var otherbackground = jQuery(this).next().next().attr('data');
          jQuery(this).next().next().removeClass('hover-eind').css('background-image', otherbackground);      
      } else {
          jQuery(this).removeClass('hover-begin').css('background-image', background);  
      
      }
   });    




   jQuery('.brand').bind('click', function() {
      console.log('id: ' + jQuery(this).attr('id') );
      window.location = '/index.php?e=' + jQuery(this).attr('id');
   });   

}




function strpos (haystack, needle, offset) {
    // Finds position of first occurrence of a string within another  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strpos
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman    
    // +   bugfixed by: Daniel Esteban
    // *     example 1: strpos('Kevin van Zonneveld', 'e', 5);
    // *     returns 1: 14
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}


function strtolower (str) {
    // Makes a string lowercase  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/strtolower
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Onno Marsman
    // *     example 1: strtolower('Kevin van Zonneveld');
    // *     returns 1: 'kevin van zonneveld'
    return (str+'').toLowerCase();
}





PEPS = {};

PEPS.rollover =
{
   init: function()
   {
      this.preload();
     
      jQuery(".rollover").hover(
         function () { jQuery(this).attr( 'src', PEPS.rollover.newimage(jQuery(this).attr('src')) ); },
         function () { jQuery(this).attr( 'src', PEPS.rollover.oldimage(jQuery(this).attr('src')) ); }
      );
   },

   preload: function()
   {
      jQuery(window).bind('load', function() {
         jQuery('.rollover').each( function( key, elm ) {
            jQuery('<img>').attr( 'src', PEPS.rollover.newimage( jQuery(this).attr('src') ) );
         });
      });
   },
   
   newimage: function( src )
   {
      return src.substring( 0, src.search(/(\.[a-z]+)$/) ) + '-act' + src.match(/(\.[a-z]+)$/)[0];
   },

   oldimage: function( src )
   {
      return src.replace(/-act\./, '.');
   }
};
