
$(document).ready(function(){
  lsb.initSlideshows();
  
  if (document.getElementById("tweets")) {
		var url = 'http://twitter.com/statuses/user_timeline.json?screen_name=girls_inc&callback=getTweets&count=3';
    var script = document.createElement('script');
    script.setAttribute('src', url);
    document.body.appendChild(script);
  }
  
  
  
});


var lsb = (function($){

  var slideshows = {};
  
      
      
  
  return {
    
      
    initSlideshows: function() {
      var sss = $(".lsb-slideshow"), i;
      for (i=0; i < sss.length; i++) {
        var ss = $(sss[i]),
          params = {
            duration: ss.hasClass("fast") ? 1000 : (ss.hasClass("normal") ? "normal" : (ss.hasClass("slow") ? "slow" : false)) 
          },
          ssObj = new lsb.Slideshow(ss, params);
        if (ss.id) {
          slideshows[ss.id] = ssObj;
        }
      }
    },
    
  
    getSlideshow: function(id) {
      return slideshows[id];
    }
    
    
  };

})(jQuery);



lsb.Player = function(startTab, startItem) {

  if (!(this instanceof lsb.Player)) {
    return new lsb.Player(startTab, startItem)
  }
  
  var self = this;

  $("#" + startTab + "-items").fadeIn("slow");
  $("#" + startTab).addClass("selected");
  
  $("#" + startItem).fadeIn("slow");
  $("#" + startItem + "-item").addClass("selected");
  
  
  var currentLoc,
    cacheSize,
    historyList = [],
    
    //isClick = false;
    _focusTab = "";
    _focusItem = "";
    
    targets = {
      tabItems: $(".tab-item"),
      itemsWrappers: $(".items-wrapper"),
      players: $(".player-media"),
      items: $(".item")
    };
  
  //$(window).bind("locationChange", function(evt) {
//console.log("window bind locationChange evt: ", evt);
    //ripple.ui.showInteraction(evt.rippleURI);
  //});

  
  this.tabClick = function(focusFolder, focusItem) {
//console.log("this.tabClick focusFolder: ", focusFolder);
//console.log("this.tabClick focusItem: ", focusItem);
    _focusTab = focusFolder;
    if (focusItem) {
      _focusItem = focusItem;
    }
    targets.itemsWrappers.hide();
    targets.players.hide();
    targets.tabItems.removeClass("selected");
    targets.items.removeClass("selected");
    $("#" + focusFolder + "-items").fadeIn("slow");
    $("#" + focusFolder + "-tab").addClass("selected");
    $("#" + (focusItem || lsb.playerConfig[focusFolder][0])).fadeIn("slow");
    $("#" + (focusItem || lsb.playerConfig[focusFolder][0]) + "-item").addClass("selected");
  },
  
  this.itemClick = function(focusItem) {
//console.log("this.itemClick focusItem: ", focusItem);
    _focusItem = focusItem;
    targets.players.hide();
    targets.items.removeClass("selected");
    $("#" + focusItem).fadeIn("slow");
    $("#" + focusItem + "-item").addClass("selected");
  }
  
  $(".tab-item").click(function(evt) {
    var focusFolder = this.id.substring(0, (this.id.length - 4));
    self.tabClick(focusFolder);
    //return false;
  });
  
  $(".item").click(function(evt) {
    var focusItem = this.id.substring(0, (this.id.length - 5));
//console.log("loadAjaxPage focusItem: ", focusItem);
    self.itemClick(focusItem);
    //return false;
  });


  var locationHandler = function() {
    if (location.href !== currentLoc) {
      currentLoc = location.href;
      var fragment = currentLoc.substring(currentLoc.indexOf('#'), currentLoc.length),
        playerMapItem = lsb.playerMap[fragment];
        
      //var evt = jQuery.Event("locationChange");
    
//console.log("locationHandler fragment: ", fragment);
//console.log("locationHandler playerMapItem: ", playerMapItem);
      if (playerMapItem) {
        if (_focusTab == playerMapItem.tabId) {
          self.itemClick(playerMapItem.itemId);
        } else {
          self.tabClick(playerMapItem.tabId, playerMapItem.itemId);
        }
      } else {
        self.tabClick(startTab, startItem);
      }
      
      /*var evt = new jQuery.Event("locationChange"); 
      evt.focusUri = substring(currentLoc.indexOf('#'), currentLoc.length);
console.log("locationHandler new evt: ", evt);  
      $(window).trigger(evt);*/
    }
  };


  setInterval(locationHandler, 200);

};



lsb.Slideshow = function(slideshowElem, params) {
  /**
  Required Params:
  slideshowElem: the HTML DOM object that contains a display area (.lsb-slideshow-player) and a list of slides (.lsb-slides)
  */

  if (!(this instanceof lsb.Slideshow)) {
    return new lsb.Slideshow(slideshowElem, params);
  }
  
  params = params || {};
  var i,
    hasSlides=false, 
    slides = [],
    duration = params.duration || 3000,
    transSpeed = params.transitionSpeed || 2000,
    position = params.startPostion || 0,
    
  transOut = function() {
    slides[position].fadeOut(transSpeed, transIn);
    position = (position === slides.length - 1) ? 0 : position + 1;
  },

  transIn = function() {
    slides[position].fadeIn(transSpeed).animate({opacity: 1.0}, duration, "linear", transOut);
  };
  
  var elems = slideshowElem.children(), elem;
  for (i=0; i < elems.length; i++) {
    elem = elems[i];
    elem.style.display = "none";
    elem.style.listStyleType = "none";
    slides.push($(elem));
  }
  
  transIn();
};



