/*
 * McGowan - Nov 2010
 *
 */

var PUB_CART = null; // define later if implemented


// mcgowan - show 3 twitter tweets in right box of News
function initRightBoxTwitter()
{
  var tweetsEl = $("#tweetsOuter");
  $.ajax({
    url : "http://twitter.com/statuses/user_timeline/nclr.json?callback=?",
    dataType : "json",
    timeout:15000,
    success : function(data)
    {
      var newText ='';
      //for (i=0; i< data.length; i++)
      for (i=0; i< 3; i++)
      {
        var date = data[i].created_at;
        var text = data[i].text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i,'<a target="_blank" href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1@<a href="http://www.twitter.com/$2">$2</a>').replace(/(^|\s)#(\w+)/g,'$1<a target="_blank" href="http://twitter.com/search?q=%23$2">#$2</a>');
        newText +=('<div class="tweet">' + text +"</div>");
        newText += '<div class="tweetDate"> Posted ' + (prettyDate(date) +"</div>");
      }
      tweetsEl.html(newText);
      tweetsEl.hide().fadeIn(800);
    },
    error : function()
    {
      tweetsEl.html("Error: Twitter could not be initialized.");
    }
  });
}  


// mcgowan - show 1 twitter feed on home page.
function initHomePageTwitter()
{
  var tweetsEl = $("#feed");
  $.ajax({
    url : "http://twitter.com/statuses/user_timeline/nclr.json?callback=?",
    dataType : "json",
    timeout:15000,
    success : function(data)
    {
      var newText ='';
      //for (i=0; i< data.length; i++)
      for (i=0; i< 1; i++)
      {
        var date = data[i].created_at;
        var text = data[i].text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/i,'<a target="_blank" href="$1">$1</a>').replace(/(^|\s)@(\w+)/g,'$1@<a href="http://www.twitter.com/$2">$2</a>').replace(/(^|\s)#(\w+)/g,'$1<a target="_blank" href="http://twitter.com/search?q=%23$2">#$2</a>');
        newText += '<b>Posted ' + (prettyDate(date) +": </b>"+text);
        //newText +=('<div class="tweet">' + text +"</div>");
      }
      tweetsEl.html(newText);
      tweetsEl.hide().fadeIn(800);
    },
    error : function()
    {
      tweetsEl.html("Error: Twitter could not be initialized.");
    }
  });
}  


function log(s,v)
{
  if (v == 'fatal' && true) {
    try {console.log('FATAL - '+s);} catch (e) {}} else if (true) {
    try {console.log('DEBUG - '+s);} catch (e) {}} else return;
}

/*
 * McGowan - handles features for publications purchasing and third-party shopping cart
 */
function PublicationShoppingCart() {
  this.PUB_SHOPPING_CART_URL = '/index.php/site/shopping_cart';
  this.CART_SITE_HASH = '';
  this.SESSION_ID='f9450fb2ca55d5b5fc56453a38acff83';
  this.PUBS_IN_CART_URL = "http://nclr.globalthinking.com/fulfillment/itemsincart.js?session="+this.SESSION_ID+"&hash="+this.CART_SITE_HASH+"&callback=?";
  this.PUBS_IN_CART = new Array();
  var that = this;

  this.addPubToCart = function(a, id)
  {
    jqA = $(a);
    if (!jqA.hasClass('addToCartActive'))
    {
    $.getJSON('http://nclr.globalthinking.com/fulfillment/addtocart/'+id+'.js?session='+that.SESSION_ID+'&hash='+that.CART_SITE_HASH+'&callback=?',
              function(data){
                if (data.response.error) {
                  log(data.response.error);
                  alert(data.response.error);
                } else {
                  that.switchAddPubToCartButton(jqA);
                  jqA.attr('href',that.PUB_SHOPPING_CART_URL);
                  that.PUBS_IN_CART[that.PUBS_IN_CART.length] = id;
                  that.updatePubsInCartDisplay();
                }
              });
      return false;
    } else {
      return true;
    }
  };

  this.switchAddPubToCartButton = function(jqA)
  {
    jqA.addClass('addToCartActive');
    jqA.attr('title','View Cart');
    jqA.attr('href',that.PUB_SHOPPING_CART_URL);
  };

  this.updatePubsInCartDisplay = function()
  {
    if (that.PUBS_IN_CART.length > 0)
    {
      var cartSummary = $('#cartSummary');
      if (cartSummary.length == 0)
      {
        cartSummary = $('<div id="cartSummary">(<span id="cartItemTotal"></span>) <a href="'+that.PUB_SHOPPING_CART_URL+'">View Cart</a></div>');
        cartSummary.hide();
        $('#m_title').prepend(cartSummary);
        cartSummary.fadeIn(200);
      }
      $('#cartItemTotal').html(that.PUBS_IN_CART.length);
    }
  };

  this.initPubsInCart = function() {
    var url = 'http://nclr.globalthinking.com/fulfillment/authorize/F98H2372H0F0.js?session='+that.SESSION_ID+'&callback=?';
    log(url);
    $.getJSON(url,
              function(data){
                that.CART_SITE_HASH = data['hash'];
                log('hash: '+that.CART_SITE_HASH);
                that.initPubsDisplay();
              });
  };
  
  this.initPubsDisplay = function() {
    var url = "http://nclr.globalthinking.com/fulfillment/itemsincart.js?session="+that.SESSION_ID+"&hash="+that.CART_SITE_HASH+"&callback=?";
    log(url);
    $.getJSON(url,
              function(data){
                var items = data['pub_ids'];
                if (items == null) {
                  log('no items returned','fatal');
                  return;
                }
                for (var i=0;i<items.length;i++) {
                  var item=items[i];
                  that.PUBS_IN_CART[that.PUBS_IN_CART.length] = item['itemID'];
                }
                var addButtons = $('a.addToCart');
                for (var i=0;i<that.PUBS_IN_CART.length;i++)
                {
                  var pub_id = that.PUBS_IN_CART[i];
                  that.switchAddPubToCartButton(addButtons.filter('#print_id_'+pub_id));
                }
                addButtons.hide().css('visibility','visible').fadeIn(200);
                that.updatePubsInCartDisplay();
              });
  };
  
  this.setCartFrameHeight = function(height) {
    $('#ifCartWrapper').css('height', height+'px');
  };
}


function initPublicationCartFeatures() {
  PUB_CART = new PublicationShoppingCart();
  $(document).ready(function() {PUB_CART.initPubsInCart();});
}

