//jQuery.noConflict();

function form_input_classes(){
    jQuery('input[type="text"]').addClass('text');
    jQuery('input[type="password"]').addClass('text');
    jQuery('input[type="checkbox"]').addClass('checkbox');
    jQuery('input[type="radio"]').addClass('radiobutton');
    jQuery('input[type="submit"]').addClass('submit');
    jQuery('input[type="image"]').addClass('buttonImage');
}

function clearingInput(){
    jQuery(".labelize input:text").clearingInput();
}

function equalBox(){jQuery('.equalbox').equalHeights();}

function cycleHome(){
    jQuery('#home-rotator').cycle({
        timeout:    7000,
        pager:      '#feature-pager',
        next:       '#pager-next',
        prev:       '#pager-prev'
    });
}

function tax_number(){
    function DateDiff(date1, date2) {
        return (date1.getTime() - date2.getTime()) / 1000; //return seconds
    }

    function addCommas(nStr)
    {
      nStr += '';
      x = nStr.split('.');
      x1 = x[0];
      x2 = x.length > 1 ? '.' + x[1] : '';
      var rgx = /(\d+)(\d{3})/;
      while (rgx.test(x1)) {
        x1 = x1.replace(rgx, '$1' + ',' + '$2');
      }
      return x1 + x2;
    }

    //taxes per second
    var taxesPerSecond = 135.63;

    var startDate = new Date(2012, 00, 01); //january 01, 2011
    var now = new Date();

    //get seconds between 2 dates
    var numSecondsSinceNewYear = DateDiff(now, startDate);

    //console.log("Seconds this year: " + numSecondsSinceNewYear);
    //console.log('seconds in day are 86400');
    //console.log("Days this year: " + numSecondsSinceNewYear / 86400);

    var totalTaxes = Math.floor(numSecondsSinceNewYear * taxesPerSecond);

    var commaNumber = addCommas(totalTaxes);
    //console.log("Taxes this year: $" + commaNumber);

    var taxnum = $('#tax-number').text('$' + commaNumber);

}

/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* 
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);

$(document).ready(function() {

    function megaHoverOver(){
        
        var thisSub = $(this).find(".sub");
        
        thisSub.stop().fadeTo('fast', 1).show();
        thisSub.removeClass('hidden');
            
        //Calculate width of all ul's
        (function($) { 
            jQuery.fn.calcSubWidth = function() {
                rowWidth = 0;
                //Calculate row
                $(this).find("ul").each(function() {                    
                    rowWidth += $(this).width(); 
                }); 
            };
        })(jQuery); 
        
        if ( $(this).find(".row").length > 0 ) { //If row exists...
            var biggestRow = 0; 
            //Calculate each row
            $(this).find(".row").each(function() {                             
                $(this).calcSubWidth();
                //Find biggest row
                if(rowWidth > biggestRow) {
                    biggestRow = rowWidth;
                }
            });
            //Set width
            $(this).find(".sub").css({'width' :biggestRow});
            $(this).find(".row:last").css({'margin':'0'});
            
        } else { //If row does not exist...
            
            $(this).calcSubWidth();
            //Set Width
            $(this).find(".sub").css({'width' : rowWidth});
            
        }
    }
    
    function megaHoverOut(){ 
      $(this).find(".sub").stop().fadeTo('fast', 0 );
      $(this).find(".sub").addClass('hidden');
    }


    var config = {
         sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)    
         interval: 100, // number = milliseconds for onMouseOver polling interval    
         over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
         timeout: 500, // number = milliseconds delay before onMouseOut    
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
    };

    //$("ul#topnav li .sub").css({'opacity':'0'});
    $("ul#topnav li").hoverIntent(config);

});


function biggerTarget(){
    
      $(".service-tout a").bigTarget({
          
          clickZone: 'div.service-tout:eq(0)'

      });

      $(".switch-rows-table a").bigTarget();  

}

function showMetaNav(){

  jQuery('#general-info-target, #generalinfo').hover(
    function(){jQuery('#generalinfo').removeClass('hidden').fadeIn();},
    function(){
      jQuery('#generalinfo').fadeOut(
        function(){$(this).addClass('hidden')});
    });

}

function calendarFilter(){

  jQuery('#cat-picker').bind('click',function(){
    
    var a = jQuery('#cal-comm-select').val();
    var b = jQuery('#cal-event-select').val();
    
    if(a.length >0 ){
      var aSegment = '/' + a;
    } else {
      var aSegment = ''
    }

    if(b.length >0 ){
      var bSegment = '/' + b;
    } else {
      var bSegment = ''
    }

    window.location = 'http://www.dccouncil.us/calendar/c' + aSegment + bSegment;

  });

}

function initCalendar(){

    //use anchors but not really
    $(".prevent-default").live('click',function(event){event.preventDefault();});

    //no need to repeat ourselves
    var scroller = $('#cal_event_display').scrollTop();

    //scroll up plz
    $('#arrow-up').live('click',function () {
        $('#cal_event_display').animate({
            'scrollTop': scroller-100},
            1000,
            function(){
                
                scroller = $('#cal_event_display').scrollTop();
                //console.log('scroller up is: ' + scroller);

            }
        );

    });

    //scroll down plz
    $('#arrow-down').live('click',function () {
        $('#cal_event_display').animate({
            'scrollTop': scroller+100},
            1000,
            function(){
                
                scroller = $('#cal_event_display').scrollTop();
                //console.log('scroller down is: ' + scroller);

            }
        );

    });
/*
    //scroll down plz
    $('#arrow-down').live('click',function () {
        $('#cal_event_display').animate({'scrollTop': scroller+100},1000);
    });
*/
    //user clicks on different date and we want to grab new items from that url
    $('.mc_calendar_display.has_events').live('click',function(){

        var thisDate = $(this);

        $('.cal_event').addClass('visuallyhidden');
        $('.ajax-spinner').addClass('active');

        $('#mc_calendar').find('.cal_active').removeClass('cal_active');
        thisDate.addClass('cal_active');

        var anchor = $('.mc_calendar_display.cal_active a').attr('href');
        //console.log('anchor is: ' + anchor);

        jQuery.get(
            anchor,
            function(data) {
                if (data != undefined && data != "") {

                    //console.log('data: ' + data);

                    $('.ajax-spinner').removeClass('active');
                    jQuery('#cal_event_holder').replaceWith(data);

                }
            }

        );


    });

    //user clicks on different month so we want to grab that month
    $('#mc_next_month, #mc_prev_month').live('click',function(){

        var changeURL = $(this).attr('href');
        //console.log('changeURL is: ' + changeURL);
        
        $('.ajax-spinner').addClass('active-month');

        //off we go into ajax
        jQuery.get(
            changeURL,
            function(data) {
                if (data != undefined && data != "") {

                    //console.log('data: ' + data);

                    $('.ajax-spinner').removeClass('active-month');
                    jQuery('#mc_wrap').replaceWith(data);
                    $('#cal_event_display').replaceWith('<div id="cal_event_display"><p>Please select a highlighted date from the calendar to display that day&rsquo;s entries.</p></div>');
                }

            }

        );

    });//end click function for the month

  //put the 'no events' text in with jquery since i can't figure out how to do it with the calendar module
  function noEvents(){
      var calLength = $('#cal_event_display').html().length;

      if(calLength < 40){
        //$('#cal_event_display').text('There are no events today.');
        $('#cal_event_display').replaceWith('<div id="cal_event_display"><p>There are no events scheduled for today</p></div>');
      }
  }

if ($('#mini-calendar').length ) {noEvents();}

}//end initCalendar()

jQuery(document).ready(function() {
    form_input_classes();
    clearingInput();
    equalBox();
    cycleHome();
    tax_number();
    biggerTarget();
    calendarFilter();
    initCalendar();
});


/*
                *   *   *
                =========
                =========
*/
