/*================================================== JAVASCRIPT */
/*
    JS Document for Farmfest
    Build: Adam Duncan April 2011
*/

$('html').addClass('js');
var loadCount = 2;

  Cufon.replace('h1', { fontFamily: 'Baskerville' });
  Cufon.replace('h2', { fontFamily: 'Baskerville' });
  Cufon.replace('h2 a', { hover: true, fontFamily: 'Baskerville' });
  Cufon.replace('h3 a', { hover: true, fontFamily: 'Baskerville' });
  Cufon.replace('#main_nav li a', { hover: true, fontFamily: 'NormaliseDin' });
  Cufon.replace('p.headline', { fontFamily: 'Baskerville' });
  //Cufon.replace('p#site_title', { fontFamily: 'NormaliseDin' });
  Cufon.replace('p#site_tagline', { fontFamily: 'GarageGothic' });

$(document).ready(function () {

  // CAROUSEL
  $('.gallery_carousel').cycle({
    fx: 'fade',
    speed: 3600,
    timeout: 5500,
    slideExpr: 'img'
  });

  // IMAGE ROLLOVERS
  imageRollovers();

  // LOAD MORE NEWS
  initLoadMore();

  // EXTERNAL LINKS
  externalLink();

  // MDASH FORMAT
  mdashFormat();

  // MDASH FORMAT
  clearField();

});

//---------------------------FUNCTIONS

// EXTERNAL LINKS
function imageRollovers() {
    $('.image_link').hover(function () {
        $('img', this).stop().animate({ opacity: 0.7 }, 200);
    }, function () {
        $('img', this).stop().animate({ opacity: 1 }, 350);
    });
};

function initLoadMore() {

    // We remove our pager and replace it with a "Load more" button.
    $("div.pageNavigation").replaceWith("<p class='loadMore'>Load more</p>");

    // We make it clickable. We'll make the p-tag look like 
    // an a-tag in the css later.
    $("p.loadMore").click(function () {
        // We show the loading image while we're getting our data.
        $("p.loadMore").replaceWith("<img class='loading' src='/css/img/loader.gif' alt='loading...' />");
        // newsAndEventsAjax is our alternate template.
        // we feed it query string so that it knows what set of
        // "records" we want. (Look under "VARIABLES" 
        // in ListNewsEventsAjax.xslt)
        $.get('ListingNewsAjax?page=' + loadCount, function (data) {
            // When everything is loaded we don't need this image anymore
            $("img.loading").remove();
            //create a div to insert (can't get the animation to run 
            // smoothly without a wrapping div :( )
            $('<div></div>')
            .html(data) // We insert the data we requested in to the div
            .addClass('loadedPosts') // Give at class so that we can css it
            .hide() // We make it visible again in the slideDown
            .appendTo($('div.newsList')) // Yes we do
            .slideDown(250, function () { // two and a half second slide down
                Cufon.refresh('h3 a', { hover: true, fontFamily: 'Baskerville' });
                loadCount++;
                initLoadMore();
            });
        });
    });
};


// EXTERNAL LINKS
function externalLink() {
  $('.blank').click(function(){
    var link = $(this).attr('href');
    window.open(link);    
    return false
  });
};

// MDASH FORMAT
function mdashFormat() {
  $('.cms_bodytext p').each(function () {
    var $this = $(this);
    var t = $this.html();
    $this.html(t.replace(/\u2014/g, '<span class="bodytext_dash">&mdash;</span>'));
    // use a global modifier as the first parameter for replace method - ie. /[string]/g
  });
};

function clearField() {

  InitInputDefaultValue("#mce-EMAIL", "#mc-embedded-subscribe", "Enter e-mail address");

  /* initialize the inputs which requires a 'default value' system */
  function InitInputDefaultValue(input_class, submit_class, default_value) {
    var input_text = $(input_class);

    //Init the input field
    if (input_text.val() == "") {
      input_text.val(default_value);
    }
    //Add input field events
    input_text.click(function () {
      if ($(this).val() == default_value)
        $(this).val("");
    }).blur(function () {
      if ($(this).val() == "")
        $(this).val(default_value);
    });
    //Add form submit event
    $(submit_class).click(function () {
      if (input_text.val() == default_value) {
        //input_text.val("");
        return false;
      }
    });
  }

}
