/** util */

function getIfDefined(item,def){
  return (typeof item == "undefined")? def : item;
};

function isDefined(item){
  return typeof item == "undefined";
};

/** MediaCenter ----------------------- */

var MediaCenter = function(){
  
  var _data;
  var _this;
  var _type;
  var _list;
  var _display;
  var _activeIndex;
  var _dom;
  var _container;

  function _main(el,dataUrl,cfg){ 
    _this = this;
    _cfg = cfg;
    _container = (typeof el=="string")? jq$("#"+el)[0] : el;
    _loadData(dataUrl);
    
  };
  
  function _createDom(mediaConfig){
    _dom = jq$("<div />").attr({"class":"media-center"})[0];
    jq$(_dom).append(_list.getDom());
    jq$(_dom).append(_display.getDom());
    jq$(_container).append(_dom);
    _list.show();
    _display.loadMediaItems(mediaConfig);
  };
  
  function _loadData(dataUrl){ 
    jq$.ajax({
      url: dataUrl,
      type: "GET",
      dataType: "json",
      success: _onDataLoadSuccess,
      error: _onDataLoadError
    });
  };
  
  function _initEvents(){
    jq$(_list).bind("MediaItemList:change",_onMediaListChange);
  };
  
  function _onDataLoadSuccess(res){ 
  
    var mediaConfig = res["media-center"]["media-items"];
    _display = new MediaDisplay();
    _list = new MediaItemList(mediaConfig);
    _createDom(mediaConfig);
    
    var firstIndex = _getFirstIndex(getIfDefined(res["media-center"]["frist-index"],0));

    _display.activate(firstIndex);
    _list.activate(firstIndex);
    _initEvents();
    
  };
  
  function _getFirstIndex(index){
  
    if(location.href.split("?").length==1) return index;
    var query = location.href.split("?")[1].split("&");
    jq$.each(query,function(){
      var q = this.split("="); 
      if(q[0]=="media_index") {
        index = q[1];
        return false;
          
      };
    });
    return index;
  };
  
  function _onDataLoadError(){ 
  //  console.info("json file didn't get loaded");
  };
  
  function _onMediaListChange(){
    _display.activate(_list.getActiveIndex());
  };
  
  
  function _onMediaItemDeactivate(){
  
  };
  
  
  _main.apply(this,arguments);
};


/** MediaDisplay ----------------------- */

var MediaDisplay = function(){
  
  var _this;
  var _dom;
  var _displayItems;
  var _listDom;
  var _activeIndex;
  
  function _main(){ 
    _this = this;
    _displayItems = [];
    _createDom();
    //_loadMediaItems(cfg);
  };
    
  function _createDom(){  
    _dom = jq$("<div />").attr({"class":"media-display"})[0];
    _listDom = jq$("<div />").attr({"class":"media-display-list"}).css({"position":"relative"})[0];
    jq$(_dom).append(_listDom);
  };
  
  function _loadMedia(cfg){
    
    switch(cfg.type){
      case "video": 
        var media = new Video(cfg["media-config"],_listDom);
        _displayItems.push(media);
      break;

      case "slidedeck":
        var media = new Slidedeck(cfg["media-config"],_listDom);
        _displayItems.push(media);
      break;  

		//if cfg is of type slidedeckswf
	  case "slidedeckswf":
		//create a new instance of slidedeckswf and assign it to media
		var media = new SlidedeckSwf(cfg["media-config"],_listDom);
		//append media to the _displayItems Array
		_displayItems.push(media);   
		break;

		//if cfg is of type image
	  case "image":
		//create an anchor tag for jakebox to parse
		var media = new jakeboximage(cfg["media-config"],_listDom);
		//append it to the display items
		_displayItems.push(media);      
    };
  };
  
  this.loadMediaItems = function(cfg){
    jq$(cfg).each(function(){
      _loadMedia(this.media);
    });
  };
  
  this.getDom = function(){
    return _dom;
  };
  
  this.render = function(el){
    _this.clear();
    jq$(_dom).append(el);
  };
  
  this.clear = function(){
    _dom.innerHTML = "";
  };
  
  this.activate = function(index){ 
     jq$(_displayItems[index].getDom()).css({"z-index":"1000"});
    _displayItems[index].activate();
    if(!isDefined(_activeIndex))_this.deactivate(_activeIndex);
    _activeIndex = index;
  };
  
  this.deactivate = function(index){
    var media = _displayItems[index];
    if(media.getType()=="video") media.stopVideo();
    jq$(_displayItems[index].getDom()).css({"z-index":"999"});
    _displayItems[index].deactivate();
  };
  
  _main.apply(this,arguments);
};


/* MediaItemList */

var MediaItemList = function(){
  
  var _this;
  var _dom;
  var _items;
  var _activeIndex;
  var _isFirstTime=true;
  
  function _main(cfg){
    _this = this;
    _items = [];
    _activeIndex = getIfDefined(cfg.activeIndex,0);
    _initList(cfg);
    _createDom();
    _initEvents();
  };
  
  
  function _initList(cfg){
    
    jq$(cfg).each(function(i){ 
    
      var mediaItem = new MediaItem(this,i);
      jq$(mediaItem).bind("MediaItem:click",_onMediaItemClick);
      _items.push(mediaItem);
      
    });
    
  };
  
  function _onMediaItemClick(arg){
    _this.activate(arg.target.getIndex());
   
   var mediaItem = $$("div.media-description p strong");
	
    i = arg.target.getIndex();
	 
    mediaItemTitle = mediaItem[i].innerHTML;

	//initialize the jakebox modal for any items with the jakebox class
	jakeboxInit(jq$('a.jakebox-link'));
		
    //omniture.slideDeckClick(mediaItemTitle); uncomment this if you want slide-deck clicks tracked
    
  };
  
  function _initEvents(){
  
  };
  
  function _createDom(){
    
    _dom = jq$("<div />").attr({"class":"media-list"}).css({"display":"none"});
    jq$(_items).each(function(){
      jq$(_dom).append(this.getDom());
    });
    
  };
  
  function _onMediaItemActivate(){
    //jq$(_this).trigger("MediaItemList:change");
  };
  
  /* public */
  
  this.renderTo = function(el){
    if(typeof el == "string")el = jq$("#"+el)[0];
    jq$(el).append(_dom);
  };
  
  this.getDom = function(){
    return _dom;
  };
  
  this.activate = function(newIndex){
    
    if(newIndex!=_activeIndex || _isFirstTime){
      _isFirstTime = false; 
      _items[_activeIndex].deactivate();
      _items[newIndex].activate();
      _activeIndex = newIndex;
      jq$(_this).trigger("MediaItemList:change");
    };
    
  };
  
  this.getActiveItem = function(){
    return _items[_activeIndex];
  };
  
  this.getActiveIndex = function(){
    return _activeIndex;
  };
  
  this.show = function(){
    //jq$(_dom).show("slide", { "direction": "right" }, 500);
    jq$(_dom).show("slow");
  }; 
  
  
  _main.apply(this,arguments);
};



/** MediaItem ----------------------- */

var MediaItem = function(){
  
  var _this;
  var _dom;
  var _media;
  var _id;
  var _type;
  var _active;
  var _index;
  
  function _main(cfg,i){
    _index = i;
    _active = false;
    if(typeof cfg.media=="undefined" || typeof cfg.media.type=="undefined" ) return;
    _this = this;
    _id = getIfDefined(cfg.id, "");
    _type = getIfDefined(cfg.media.type, "");
    _createDom(cfg);
    _initEvents();
  };
  
  function _createDom(cfg){
    
    var _thumbnail = getIfDefined(cfg.thumbnail, "");
    var _html = getIfDefined(cfg.html, "");
    var _itemClass = getIfDefined(cfg.cls, "");
    
    _dom = jq$("<div />").attr("class","media-item type-"+_type+" "+_itemClass)[0];
    if(_thumbnail!=""){
      jq$("<div />").addClass("media-thumbnail").append(jq$("<img />").attr({"src":_thumbnail})).appendTo(_dom);
    };
    jq$("<div />").addClass("media-description").html(_html).appendTo(_dom);
    


  };
  
  
  function _initEvents(){
    jq$(_dom).bind("mousedown",_onClick);
    jq$(_dom).bind("mouseover",_onMouseOver);
    jq$(_dom).bind("mouseout",_onMouseOut);
  };
  
  function _onClick(){
    jq$(_this).trigger("MediaItem:click"); 
  };
  
  function _onMouseOver(){
    jq$(_this).trigger("MediaItem:mouseover");
    jq$(_dom).addClass("item-over");
  };

  function _onMouseOut(){
    jq$(_this).trigger("MediaItem:mouseover");
    jq$(_dom).removeClass("item-over");
  };
  
  function _onMediaDomReady(){
   // jq$(_this).trigger("MediaItem:m");
  };
  
  /* public functions */
  
  this.getDom = function(){
    return _dom;
  };
  
  this.getMedia = function(){
    return _media;
  };
  
  this.getIndex = function(){
    return _index;
  };
  
  this.activate = function(){
    _active = true;
    jq$(_dom).addClass("active");
   // console.info("item "+_index+" activated");
  };
  
  this.deactivate = function(){
    _active = false;
    jq$(_dom).removeClass("active");
  //  console.info("item "+_index+" deactivated");
  };
   
  _main.apply(this,arguments);
  
  
};

