/**
 * REW Forms
 */
var rewForm = function (rewform) {
    var l = rewform.length;
    for (var i = 0; i < l; i++) {
        var $form = $(rewform[i]);
        // these three lines cause a strange flash in top left corner (ON search.html ONLY)
        $form.wrapInner('<div class="form-wrap"></div>');
        $form.find('.field').wrapInner('<span class="field-iwrap"></span>');
        $form.find('.field select').wrap('<span class="select-wrap"></span>');
        //
        $form.find('.field input:text,.field input:password').wrap('<span class="input-wrap"></span>');
        $form.find('.field textarea').not('.richtext').wrap('<span class="textarea-wrap"></span>');
        $form.find('.token').wrapInner('<span class="token-iwrap"></span>');
        $form.find('legend').each(function() {
            var content = $(this).html();
            $(this).after('<h4 class="legend">'+ content +'</h4>').hide();
            $(this).remove();
         });
    }
};




$(document).ready(function() {

    
	 
       // Tab rotation speed defined below in milliseconds
    	$("#tabs").tabs({ fx: { opacity: 'toggle' } }).tabs('rotate', 5000);
      

      // Initialize superfish dropdown menu
      
            $('ul.sf-menu').superfish();
			/* Business Directory, Categories */
    $('#directory-categories').splitList({'itemsPerColumn':3});
	$('#content .directory-categories:nth-child(4n)').addClass('last');

     

       $(function(){
    			$('.two-column-auto').columnize({columns:2});
    			$('.three-column-auto').columnize({columns:3});
    			$('li').addClass("dontsplit");
    		});

	
	/* REW Forms */
    rewForm($('form.rewfw'));
	
	/* Site Search */
    if($('#dir-listing-results').length == 1 && $('#dir-listings-toggle').length == 1) {

        var dirToggle = $('#dir-listings-toggle');
        dirToggle.bind('click', function() {
            if($.cookie('dir-listings-toggle') == 'show' || $.cookie('dir-listings-toggle') == undefined) {
                $.cookie('dir-listings-toggle', 'hide');
                $('#dir-listing-results').slideUp();
                dirToggle.html('Show Directory Results');
            } else {
                $.cookie('dir-listings-toggle', 'show');
                $('#dir-listing-results').slideDown();
                dirToggle.html('Hide Directory Results');
            }
        });

        if($.cookie('dir-listings-toggle') == 'hide') {
            $.cookie('dir-listings-toggle', 'hide');
            $('#dir-listing-results').hide();
            dirToggle.html('Show Directory Results');
        }

    }
	

	
	
	
	
	$('#nav-secondary li ul:visible').hide();
				$('#post-menu .submenu:visible').hide();
				$('#content-user ul').wrap('<div class="tl"><div class="tr"><div class="br"><div class="bl"><div class="wrap"></div></div></div></div></div>');
				$('#comments, #formset').tabs();

				$('#post-menu>ul>li').hoverIntent(
					function(){ $('.submenu', this).fadeIn(); },
					function(){ $('.submenu', this).fadeOut('fast'); }
				);
				$('#nav-secondary li').hover(
					function(){ $('ul', this).fadeIn('fast'); },
					function(){ $('ul', this).fadeOut('fast'); }
				);
				$("div.item").wrap('<div class="dialog">'+'<div class="bd">'+'<div class="c">'+'<div class="s">'+'</div>'+'</div>'+'</div>'+'</div>');
				//$("div.thbset,div.widget").wrap('<div class="dialog alt">'+'<div class="bd">'+'<div class="c">'+'<div class="s">'+'</div>'+'</div>'+'</div>'+'</div>');

				$('div.dialog').prepend('<div class="hd">'+'<div class="c"></div>'+'</div>').append('<div class="ft">'+'<div class="c"></div>'+'</div>');
				
				$('#nav-primary li').hover(function() {
					$(this).addClass('over');
				}, function() {
					$(this).removeClass('over');
				});
				$('.button').hover(function(){$(this).attr("src", $(this).attr("src").split('-off').join('-on'))}, function(){$(this).attr("src", $(this).attr("src").split('-on').join('-off'))});
				
				$('legend').each( function() {
					val = $(this).text();
					$(this).after('<h5 class="legend">' + val + '</h5>');
					$(this).html('');
				});
				
				$('#post img').addClass('floated');
				$('#post img[@align=right]').addClass('alt');

});
