
var pandaTheme = 'list';
var pandaApikey = 'nbE6zpH';
var pandaAssetUrl = 'http://pluspanda.com/data/nbE6zpH/tstmls';
var pandaStructure = '<div id="pluspanda-testimonials">  <div class="panda-header">    <div class="title">      <div class="lft"></div>      <div class="mdl">        <h2>Testimonials</h2>        <ul class="switcher">          <li class="grid"><a href="#grid">G-View</a></li>          <li class="list current"><a href="#list">L-View</a></li>        </ul>      </div>      <div class="rgt"></div>    </div>    <div class="panda-tags">      <span>Show Testimonials from: </span>            <ul>        <li><a href="#all" class="active">Everyone</a></li>      <li><a href="#2">Web Services</a></li><li><a href="#1">E-commerce Stores</a></li><li><a href="#8">Local Businesses</a></li>      </ul>        </div>    <div class="panda-avg-rating">      <span>(Total: <b>14</b>)</span>      <span class="panda-rating-stars s4">****</span>    </div>  </div>  <div class="panda-container list"></div></div>';
function pandaItemHtml(item){
  return '<div id="t-' + item.id + '" class="t-single">  <div class="image">    <img height="148" width="148" src="'+ item.image +'" alt="" />  </div>  <div class="content">    <span class="arrow"></span>    <div class="head">      <div class="t-rating">        <span class="panda-rating-stars s'+item.rating+ '" title="Rating: '+item.rating+ ' stars"></span>      </div>      <div class="t-details-meta">        <span class="t-name">'+ item.name +',</span>        <span class="t-position">'+ item.position +', ' + item.company + '</span>        <a href="'+ item.url +'" class="t-website" target="_blank">'+ item.url +'</a>      </div>    </div>    <div class="t-text">      <span class="p1"></span>      <p>' +item.body+ '</p>    </div>  </div></div>';
};
/*02.25.10 5:38pm*/

/* Event delegation*/
jQuery.delegate = function(rules) {return function(e) { var target = jQuery(e.target); for (var selector in rules) if (target.is(selector)) return rules[selector].apply(this, jQuery.makeArray(arguments));}}

/* For testimonial widget mode =P */
jQuery('head').append('<link type="text/css" id="pandaTheme" href="' + pandaAssetUrl + '/css/' + pandaTheme + '.css" media="screen" rel="stylesheet" />');

// attach event triggers.
jQuery('body').click(jQuery.delegate({
  'a.show-more' : function(e){  
    var is_sort = e.target.href.indexOf('#');    
    var parent = (-1 == is_sort)
      ? 'a.show-more'
      : '.panda-testimonials-sorters a';
    var spltr = (-1 == is_sort) ? '?' : '#';

    // get GET params from links TOD0: optimize this?
    var hash = e.target.href.split(spltr)[1].split('&');
    var params = {"tag":"all","sort":"newest","page":1};
    for(x in hash){
        var arr = hash[x].split('=');
        params[arr[0]] = arr[1]; 
    }
    jQuery(e.target).remove();
    pandaGetTstmls(params.tag, params.sort, params.page);
    return false;
  }
}));

// build the initial interface.
jQuery('#plusPandaYes').html('<div class="ajax_loading">Loading...</div>');   
function buildIt() {   
  jQuery('#plusPandaYes').html(pandaStructure);
  
  // ajaxify tag list.  
  jQuery('div.panda-tags ul a').click(function(e){
    var tag = e.target.hash.substring(1);
    // highlight the tag link
    jQuery('div.panda-tags ul a').removeClass('active');
    jQuery(this).addClass('active');

    // load the testimonials based on selection.
    jQuery('div.panda-container').empty();
    pandaGetTstmls(tag,'newest',1);
    return false;  
  });
  // init getting the data.
  jQuery('div.panda-tags ul a:first').click();
  // track
  jQuery.get('http://pluspanda.com/track',{key:pandaApikey,url:parent.location.href});
}
buildIt(); // init the build!

// get the testimonials as json.
function pandaGetTstmls(tag, sort, page){
    jQuery('div.panda-container').append('<div class="ajax_loading">Loading...</div>');
    
    jQuery.ajax({ 
        type:'GET', 
        url: 'http://pluspanda.com/', 
        data:"apikey="+pandaApikey+"&service=testimonials&tag="+tag+"&sort="+sort+"&page="+page+"&jsoncallback=pandaLoadTstml", 
        dataType:'jsonp'
    }); 
}


// --------- jsonp callbacks ------------  

// callback to format and inject testimonials data.
function pandaDisplayTstmls(tstmls){
  var content = '';
  var date = new Date();
  jQuery(tstmls).each(function(i){
    this.created = new Date(this.created*1000).toGMTString();
    //this.created = d.getDate() + '/' + (1+d.getMonth()) + '/' + d.getFullYear();
    this.image = (0 == this.image.length) ? 'http://pluspanda.com/static/images/stock.png' : pandaAssetUrl + '/img/' + this.image;
    this.url = (0 == this.url.length) ? '' : 'http://' + this.url;
    this.alt = (0 == (i+1) % 2) ? 'even' : 'odd';
    content  += pandaItemHtml(this);
  });
  jQuery('#plusPandaYes div.panda-container .ajax_loading')
    .replaceWith(content);
  pandaInteractions();
  pandaClean();
}

// callback to display the pagination html.
function pandaShowMore(nextPage, tag, sort){
  if(!nextPage)
    return false;
    
  var link = '<a href="http://pluspanda.com/?apikey='+pandaApikey+'&service=testimonials&tag='+tag+'&sort='+sort+'&page='+ nextPage +'" class="show-more">Show More</a>';
  jQuery('div.panda-container').append(link);
}


/*
 * utilities
 */
 
// bind/unbind javascript interactions based on theme.
function pandaInteractions(){
  jQuery('div.t-details div.image img').unbind();
  var theme = ('' == window.location.hash.substring(1))
    ? pandaTheme
    : window.location.hash.substring(1);
    
  if('gallery' == theme){
    jQuery('div.t-details div.image img').hover(function(){
        jQuery('div.t-content').hide();
        jQuery(this).parent().parent().next('.t-content').toggle();
      },
      function(){
        jQuery('div.t-content').hide();
      });
  }
}

// cleanup our jsonp scripts after execution.
function pandaClean(){
  jQuery('head script[src^="http://pluspanda.com/"]').remove();
}



//04.18.10 10:24pm/*=D*/