/*
Copyright (c) 2011 Church Media Group, Inc. (http://churchmedia.cc) All rights reserved.

This code is distributed under the following terms:

Redistribution and use of this software in source and binary forms, with or without modification,
are not permitted without prior written permission by the copyright holders.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


$(document).ready(function() {

  //callbacks for carousel
  function mycarousel_initCallback(carousel) {

    $j('.series_carousel_btn_next').bind('click', function() {
      carousel.next();
      return false;
    });

    $j('.series_carousel_btn_prev').bind('click', function() {
      carousel.prev();
      return false;
    });
  }
	
  //j-carousel
  $j('#seriesCarousel').jcarousel({
    scroll: 4,
    wrap: "both",
    initCallback: mycarousel_initCallback,
    buttonNextHTML: null,
    buttonPrevHTML: null
  });

  setupTruncateHTML();
  setupTabify();
  setupEMailSignup();
  setupVideoTruncate();
  setupExpander();
  setupSocialListingTabify();
  setupSearch();
  setupBoxSlider();
  setupHideWalkInWordBroadcastVideo();

});

function setupTruncateHTML(){
  $j('.truncateHTML').each(function() {
    $j(this).textTruncate({width: 1400,tooltip: false});
  });
}

function setupTabify(){

    $j('.sideTabify').tabify();
    $j('.scroll-pane').show().jScrollPane({showArrows: true, autoReinitialise: true});
    $j('.scroll-text').jScrollPane({showArrows: true});

    //This is for the Collapse and Show
    $j('.collapse_body').hide();
    $j('.first_collapse').show();
    //This is for the Collapse and Show
    $j('.collapse_head').collapser({
      target: 'next',
      effect: 'slide',
      changeText: 0,
      expandClass: 'expand',
      collapseClass: 'collapse'
    }, function(){
      $j('.collapse_body').slideUp();
    });

    $j('.truncatable_2').truncatable({limit: 160, more: ' &hellip; read more', less: true, hideText: '<br /><br />close'});

}

function setupEMailSignup(){
  if (getCookie('witw_email')){
    $("#sidebar_email").hide();
    $(".home_email").hide();
  }
}

function getCookie(c_name)
{
  var i,x,y,ARRcookies=document.cookie.split(";");
  for (i=0;i<ARRcookies.length;i++)
  {
    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
    y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
    x=x.replace(/^\s+|\s+$/g,"");
    if (x==c_name)
    {
      return unescape(y);
    }
  }
  return null;
}

function setupVideoTruncate(){
  // For truncating video descriptions
  $j('.truncatable').truncatable({
    limit: 350,
    more: ' &hellip; read more',
    less: true,
    hideText: '<br /><br />close'
  });
   
}

function setupExpander(){
  $('.expandable').expander({
    slicePoint:       200,  // default is 100
    expandPrefix:     '',
    expandText:         '<p>Read More</p>', // default is 'read more...'
    collapseTimer:    0, // re-collapses after 5 seconds; default is 0, so no re-collapsing
    userCollapseText: '<p>Close</p>'  // default is '[collapse expanded text]'
  });
}

function setupSocialListingTabify(){
  $j('ul.connect_social_listing').tabify();
}

function setupSearch(){

  // This script is to clear the search input field for searches
  $j('.search_clear').click(function() {
    return;
    if (this.value == this.defaultValue) {
      this.value = '';
    }
  });

  $j('.search_clear').blur(function() {
    return;
    if (this.value == '') {
      this.value = this.defaultValue;
    }
  });
}

function setupBoxSlider(){

  //Daily Slider
  $j("#date1").hide();
  $j("#date2").hide();
  $j("#date3").hide();
  $j("#date4").hide();
  //$j("#last").hide();

  var dailySlider = $j('#daily_slider').bxSlider({
    startingSlide: 4,
    infiniteLoop: false,
    controls: false,
    tickerDirection: 'prev'
  });


  $j('.go-prev-daily').click(function() {


    if(  $j('#date1').is(":visible") ){
      //$('#date1').hide();
      $j('#date2').hide();
      $j('#date3').hide();
      $j("#date4").hide();
      $j('#last').hide();
      toggleSliderArrowClassPrevFirst();
      dailySlider.goToPreviousSlide();
    }

    if(  $j('#date2').is(":visible") ){
      $j('#date1').show();
      $j('#date2').hide();
      $j('#date3').hide();
      $j("#date4").hide();
      $j('#last').hide();
      toggleSliderArrowClassPrevFirst();
      dailySlider.goToPreviousSlide();
    }

    if(  $j('#date3').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').show();
      $j('#date3').hide();
      $j("#date4").hide();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToPreviousSlide();
    }

    if(  $j('#date4').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').show();
      $j("#date4").hide();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToPreviousSlide();
    }

    if(  $j('#last').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').hide();
      $j("#date4").show();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToPreviousSlide();
    }

    return false;
  });

  $j('.go-next-daily').click(function() {


    if(  $j('#last').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').hide();
      $j('#date4').hide();
      //$j('#last').show();
      toggleSliderArrowClassNextLast();
      dailySlider.goToNextSlide();
    }

    if(  $j('#date4').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').hide();
      $j('#date4').hide();
      $j('#last').show();
      toggleSliderArrowClassNextLast();
      dailySlider.goToNextSlide();
    }


    if(  $j('#date3').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').hide();
      $j('#date4').show();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToNextSlide();
    }

    if(  $j('#date2').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').hide();
      $j('#date3').show();
      $j('#date4').hide();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToNextSlide();
    }

    if(  $j('#date1').is(":visible") ){
      $j('#date1').hide();
      $j('#date2').show();
      $j('#date3').hide();
      $j('#date4').hide();
      $j('#last').hide();
      toggleSliderArrowClass();
      dailySlider.goToNextSlide();
    }
    return false;
  });


  //Ministry Slider
  var ministrySlider = $j('#ministry_slider').bxSlider({
    displaySlideQty: 3,
    moveSlideQty: 1,
    controls: false
  });

  $j('.go-prev-ministries').click(function() {
    ministrySlider.goToPreviousSlide();
    return false;
  });

  $j('.go-next-ministries').click(function() {
    ministrySlider.goToNextSlide();
    return false;
  });
}

function toggleSliderArrowClassPrevFirst(){
  //we can only go to the right
  $j('#arrow_left_small').removeClass('arrow_left_small').addClass('arrow_left_small_inactive');
  $j('#arrow_right_small').removeClass('arrow_right_small_inactive').addClass('arrow_right_small');

  $j('#arrow_left_big').removeClass('arrow_left_big').addClass('arrow_left_big_inactive');
  $j('#arrow_right_big').removeClass('arrow_right_big_inactive').addClass('arrow_right_big');

}

function toggleSliderArrowClassNextLast(){
  //we can only go to the left
  $j('#arrow_right_small').removeClass('arrow_right_small').addClass('arrow_right_small_inactive');
  $j('#arrow_left_small').removeClass('arrow_left_small_inactive').addClass('arrow_left_small');

  $j('#arrow_right_big').removeClass('arrow_right_big').addClass('arrow_right_big_inactive');
  $j('#arrow_left_big').removeClass('arrow_left_big_inactive').addClass('arrow_left_big');
}

function toggleSliderArrowClass(){
  //we can go left or right
  $j('#arrow_left_small').removeClass('arrow_left_small_inactive').addClass('arrow_left_small');
  $j('#arrow_right_small').removeClass('arrow_right_small_inactive').addClass('arrow_right_small');


  $j('#arrow_left_big').removeClass('arrow_left_big_inactive').addClass('arrow_left_big');
  $j('#arrow_right_big').removeClass('arrow_right_big_inactive').addClass('arrow_right_big');
}

function setupHideWalkInWordBroadcastVideo(){
  $('#divSpecialVideoFeatures').hide();

}

// no conflicts with other frameworks
var $j = jQuery;

