/*global.js*/
var rotation_time = 8000;

jQuery.fn.rolloverSubmit = function(options){		
    settings = jQuery.extend( {
        postfix: "_hover"
    }, options);    
    $filename = new RegExp("^(.*?)\.(gif|jpg|png)", "i");
    $filename_hover = new RegExp("^(.*?)" + settings['postfix'] + "\.(gif|jpg|png)", "i");
    return this.each(function() {	
        jQuery(this).hover(function(){
            $m = $filename.exec(jQuery(this).attr('src'));
            jQuery(this).attr('src', $m[1] + settings['postfix'] + "." + $m[2]);
        }, function(){
            $m = $filename_hover.exec(jQuery(this).attr('src'));
            jQuery(this).attr('src', $m[1] + "." + $m[2]);        
        }); 
    });
};

jQuery.fn.splitList = function(cols, options){		
    
    settings = jQuery.extend({
        wrapClass: ""
    }, options);
    
	return this.each(function(){
		$lis = jQuery(this).find("> li");		
		$inc = parseInt(($lis.length/cols) + ($lis.length % cols > 0 ));
		for(var i=0; i<cols; i++)
			$lis.slice($inc*i, $inc*(i+1)).wrapAll("<div class='" + settings['wrapClass'] + "'></div>");
	});
};

// mwiik 2010-09-22 added $(document).ready
$(document).ready(function() {
    
    $("input[type='image']").rolloverSubmit( { postfix: "_hover" } );
    
    $("#billboard .slide ul li:first-child").addClass('first');
    $(".key_row:last-child, .safety_summary .col:last-child," +
      ".additive_list .alphalist li:last-child, .nah_testimonials .testimonial:last-child, " + 
      ".view_archive_dropdown ul li:last-child").addClass('last');
    $(".additive_list .additive:last-child").addClass('additive_last');
    $(".groceries_body ul li:first-child, .article_body .quick_links ul li:first-child").addClass('first');
    
    $(".archives .month:last, .widecol_body .item:last-child").addClass('last');
    
    $('li.print a').click(function(e){
        e.preventDefault();
        window.print();
    });
    
    $('.article_table tr:even').addClass('even');
    
    $("#main_nav > li").each(function(){
        var w = $(this).width() - 2;
        $(this).find('.subnav').prepend($('<div class="spacer">&nbsp;</div>')).find('.spacer').width(w);
    });
    
    $("#main_nav > li").hover(function(){
       $(this).addClass('hover');
       $(this).find('>a').addClass('hover');
       $(this).find('.subnav').css('display', 'block'); 
    }, function(){
        $(this).removeClass('hover');
        $(this).find('>a').removeClass('hover');
        $(this).find('.subnav').css('display', 'none');         
    });
    
    $(".view_archive_button").hover(function(){
        $(this).find('.button').addClass('button_hover');
        $(this).find('.view_archive_dropdown').css('display', 'block');
    }, function(){
        $(this).find('.button').removeClass('button_hover');
        $(this).find('.view_archive_dropdown').css('display', 'none');        
    });
    
    //secondary height match primary
    var primheight = $('#primary').height();
    $('.article_archives').height(primheight-$('.nah_favorites').height()-290);
    
    $('.recipe_breakdown').splitList(2, { wrapClass: "col" });
    
    //billboard  
    if ($('#billboard').length > 0) {
        var slides_num = $('#billboard .slide').length;
        var browserVersion = navigator.appVersion.toLowerCase();
        if(browserVersion.match('msie 8') ) { //IE 8
            $('#billboard .slides').cycle({
                timeout: rotation_time,
                speed: 10,
                fx: 'fade',
                next: '#bb_pager_next a',
                prev: '#bb_pager_prev a'
            });    
        } else {        
            $('#billboard .slides').cycle({
                timeout: rotation_time, 
                fx: 'fade',
                next: '#bb_pager_next a',
                prev: '#bb_pager_prev a'
            });
        }
        // mwiik added this line 2010-09-22
        $('#billboard .slides .slide').show();
    }
    
    if (!$('body').hasClass('pdf')) {
        //make best/worst columns match
        var worst = $('.widecol_red .item');
        var best = $('.widecol_blue .item');
        var limit = ( worst.length > best.length )? best.length : worst.length;
        for (var j=0; j < limit; j++) {
            $(worst[i]).css('height', 'auto');
            $(best[i]).css('height', 'auto');
        }
        for (var i =0; i < limit; i++) {
            var h = ( $(worst[i]).height() > $(best[i]).height()) ? $(worst[i]).height() : $(best[i]).height();
            $(worst[i]).height(h);
            $(best[i]).height(h);
        }
    }
} );
