var tab = 0;

var tgs = new Array( 'div','td','tr','span','p','h1','h2','a' );

function ts( trgt, inc ) {
  var sz = 0;

  for ( i = 0 ; i < tgs.length ; i++ )
  {
    jQuery('#' + trgt + ' ' + tgs[ i ]).each(function()
    {
      var ourText = jQuery(this);
      var currFontSize = jQuery(this).css('fontSize');
      var finalNum = parseFloat(currFontSize, 10);
      var stringEnding = currFontSize.slice(-2);

      if(inc) {
        finalNum *= 1.2;
      }
      else {
        finalNum /=1.2;
      }
      jQuery(this).css('fontSize', finalNum + stringEnding);
    });
  }
}

function fromDatapicker(month, year, next)
{
  if (next > 0)
  {
    if (month == 12)
    {
      month = 1;
      year = (year * 1) + 1;
    }
    else
    {
      month = (month * 1) + 1;
    }
  }
  else
  {
    if (month == 1)
    {
      month = 12;
      year = (year * 1) - 1;
    }
    else
    {
      month = (month * 1) - 1;
    }
  }

  jQuery('.cls-date').attr('style', 'font-weight: normal !important; color: #222222 !important;');

  var curl = "http://www.csi360.com/set-colors-on-calendar.html";

  jQuery.ajax({
    type: "GET",
    url: curl,
    data: "month="+month+"&year="+year,
    success: function(dates) {

      var exploded = dates.split('::');
      for ( var i in exploded )
      {
        jQuery('.date-'+exploded[i]).attr('style', 'font-weight: bold !important; color: #669933 !important;');
      }
    }
  });
}

function dateClicked(cday, cmonth, cyear)
{
  if (cmonth == 12) cmonth = 1;
  else cmonth = (cmonth * 1) + 1;

  var curl = "http://www.csi360.com/news-and-events.html?tab=3";

  if ( (cday > 0) && (cmonth > 0) && (cyear > 0))
  {
    window.location = curl + "&month=" + cmonth + "&day=" + cday + "&year=" + cyear;
  }
  return false;
}

function showAddress(address, link) {
  var map = new GMap2(document.getElementById("map_canvas"));
  var geocoder = new GClientGeocoder();
  map.setUIToDefault();
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        //alert(address + " not found");
      } else {
        map.setCenter(point, 13);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        // As this is user-generated content, we display it as
        // text rather than HTML to reduce XSS vulnerabilities.
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindow(address + '<br /><br /><a href="' + link + '" target="_blank">Get Directions</a>');
        });
      }
    });
}

jQuery(document).ready(function() {
  jQuery("#root").treeview();

  jQuery("#datepicker").datepicker();

  jQuery("#duration_txt").focus(function() {
    if (jQuery(this).val() == "enter time (ie) 1 Hour") jQuery(this).val("");
  });

  jQuery("#edit-phone").mask("(999) 999-9999? x99999");

  jQuery('#when_dt').attr('readonly', 'readonly');
  jQuery('#when_dt').datepicker({
    duration: '',
    showTime: true,
    constrainInput: false,
    stepMinutes: 1
  });

  jQuery(".rotate-link").click(function() {
    var url = jQuery.trim(jQuery(this).attr("title"));
    if (url != "")
    {
      window.open(url);
    }
  });

  jQuery("#custom_pages_form #edit-preview").click(function() {
    var cp_form = jQuery("#custom_pages_form");
    if (cp_form != null && cp_form != 'undefined')
    {
      var cp_action = cp_form.attr("action");
      cp_form.attr("action", "http://www.csi360.com/preview-only.html");
      cp_form.attr("target", "_blank");
      jQuery("#edit-submit").click();
      cp_form.submit();
      cp_form.attr("target", "");
      cp_form.attr("action", cp_action);
    }
    return false;
  });

  jQuery("#events_articles_form #edit-preview").click(function() {
    var cp_form = jQuery("#events_articles_form");
    if (cp_form != null && cp_form != 'undefined')
    {
      var cp_action = cp_form.attr("action");
      cp_form.attr("action", "http://www.csi360.com/preview-event.html");
      cp_form.attr("target", "_blank");
      //jQuery("#edit-submit").click();
      cp_form.submit();
      cp_form.attr("target", "");
      cp_form.attr("action", cp_action);
    }
    return false;
  });

  jQuery('blockquote').each(function() {
    var bContent = jQuery(this).html();
    jQuery(this).html('<span class="default-text-12"><span class="quote-icon">" </span>'+bContent+'<span class="quote-icon"> "</span></span>');
  });
  
  // Events list front
  jQuery('.calendar').tabs();
  jQuery('.calendar').tabs('select', tab);
  
  // Events and presenter on front
  if ( jQuery.browser.msie ) {
    jQuery(".presenterHolder ul:last-child").addClass('last');
    jQuery("#content-left .calendar .monthTitle").addClass('ie');
  }
  jQuery('.eventData ul li').hover(
    function(){
      jQuery(this).children('span').show();
    },
    function(){
      jQuery(this).children('span').hide();
    }
    );
  //last-child for MSIE
  if ( $.browser.msie ) {
    jQuery(".presenterHolder ul:last-child").addClass('last');
  }
	
  $(".scrollable").jCarouselLite({
    vertical: true,
    hoverPause: true,
    mouseWheel: true,
    btnPrev: "#widget_up",
    btnNext: "#widget_down",
    speed: 500,
    auto: 4000,
    visible: 4
  });
});


/********************************************************************************************/
//Carosel
/********************************************************************************************/
$.fn.jCarouselLite = function(o) {
  o = $.extend({
    btnPrev: null,
    btnNext: null,
    btnGo: null,
    mouseWheel: false,
    auto: null,
    hoverPause: false,

    speed: 200,
    easing: null,

    vertical: false,
    circular: true,
    visible: 3,
    start: 0,
    scroll: 1,

    beforeStart: null,
    afterEnd: null
  }, o || {});

  return this.each(function() {                           // Returns the element collection. Chainable.

    var running = false, animCss=o.vertical?"top":"left", sizeCss=o.vertical?"height":"width";
    var div = $(this), ul = $("ul", div), tLi = $("li", ul), tl = tLi.size(), v = o.visible;

    if(o.circular) {
      ul.prepend(tLi.slice(tl-v+1).clone())
      .append(tLi.slice(0,o.scroll).clone());
      o.start += v-1;
    }

    var li = $("li", ul), itemLength = li.size(), curr = o.start;
    div.css("visibility", "visible");

    li.css({
      overflow: "hidden", 
      "float": o.vertical ? "none" : "left"
    });
    ul.css({
      margin: "0", 
      padding: "0", 
      position: "relative", 
      "list-style-type": "none", 
      "z-index": "1"
    });
    div.css({
      overflow: "hidden", 
      position: "relative", 
      "z-index": "2", 
      left: "0px"
    });

    var liSize = o.vertical ? height(li) : width(li);   // Full li size(incl margin)-Used for animation
    var ulSize = liSize * itemLength;                   // size of full ul(total length, not just for the visible items)
    var divSize = liSize * v;                           // size of entire div(total length for just the visible items)

    li.css({
      width: li.width(), 
      height: li.height()
    });
    ul.css(sizeCss, ulSize+"px").css(animCss, -(curr*liSize));

    div.css(sizeCss, divSize+"px");                     // Width of the DIV. length of visible images

    if(o.btnPrev) {
      $(o.btnPrev).click(function() {
        return go(curr-o.scroll);
      });
      if(o.hoverPause) {
        $(o.btnPrev).hover(function(){
          stopAuto();
        }, function(){
          startAuto();
        });
      }
    }


    if(o.btnNext) {
      $(o.btnNext).click(function() {
        return go(curr+o.scroll);
      });
      if(o.hoverPause) {
        $(o.btnNext).hover(function(){
          stopAuto();
        }, function(){
          startAuto();
        });
      }
    }

    if(o.btnGo)
      $.each(o.btnGo, function(i, val) {
        $(val).click(function() {
          return go(o.circular ? o.visible+i : i);
        });
      });

    if(o.mouseWheel && div.mousewheel)
      div.mousewheel(function(e, d) {
        return d>0 ? go(curr-o.scroll) : go(curr+o.scroll);
      });

    var autoInterval;

    function startAuto() {
      stopAuto();
      autoInterval = setInterval(function() {
        go(curr+o.scroll);
      }, o.auto+o.speed);
    };

    function stopAuto() {
      clearInterval(autoInterval);
    };

    if(o.auto) {
      if(o.hoverPause) {
        div.hover(function(){
          stopAuto();
        }, function(){
          startAuto();
        });
      }
      startAuto();
    };

    function vis() {
      return li.slice(curr).slice(0,v);
    };

    function go(to) {
      if(!running) {

        if(o.beforeStart)
          o.beforeStart.call(this, vis());

        if(o.circular) {            // If circular we are in first or last, then goto the other end
          if(to<0) {           // If before range, then go around
            ul.css(animCss, -( (curr + tl) * liSize)+"px");
            curr = to + tl;
          } else if(to>itemLength-v) { // If beyond range, then come around
            ul.css(animCss, -( (curr - tl) * liSize ) + "px" );
            curr = to - tl;
          } else curr = to;
        } else {                    // If non-circular and to points to first or last, we just return.
          if(to<0 || to>itemLength-v) return;
          else curr = to;
        }                           // If neither overrides it, the curr will still be "to" and we can proceed.

        running = true;

        ul.animate(
          animCss == "left" ? {
            left: -(curr*liSize)
          } : {
            top: -(curr*liSize)
          } , o.speed, o.easing,
          function() {
            if(o.afterEnd)
              o.afterEnd.call(this, vis());
            running = false;
          }
          );
        // Disable buttons when the carousel reaches the last/first, and enable when not
        if(!o.circular) {
          $(o.btnPrev + "," + o.btnNext).removeClass("disabled");
          $( (curr-o.scroll<0 && o.btnPrev)
            ||
            (curr+o.scroll > itemLength-v && o.btnNext)
            ||
            []
            ).addClass("disabled");
        }

      }
      return false;
    };
  });
};

function css(el, prop) {
  return parseInt($.css(el[0], prop)) || 0;
};

function width(el) {
  return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
};

function height(el) {
  return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
};

