/*
Dean Wallace - Mediaglow, Key Experiences
7:55 PM 11/24/2009
*/

function BrandView() { this.init() };

BrandView.prototype.init = function() {
  var parent_reference = this;
  this.brand = [];
  this.config = {'category_all' : $('#brandview_variables input[name="category_all"]').val(),'brands_per_page' : parseInt($('#brandview_variables input[name="brands_per_page"]').val()),'controls_1' : $('#brandview_variables input[name="controls_1"]').val(),'controls_2' : $('#brandview_variables input[name="controls_2"]').val(),'tooltips' : $('#brandview_variables input[name="tooltips"]').val(),'pagination' : $('#brandview_variables input[name="pagination"]').val(),'category' : $('#brandview_variables input[name="starting_category"]').val(),'categories' : $('#brandview_variables input[name="categories"]').val().split(','),'dynamically_created_logos' : $('#brandview_variables input[name="dynamically_created_logos"]').val(),global_page : 1};
  $.get("/brandview/categories.xml", {}, function(xml) {
    $('#brandview').append(((parent_reference.config.controls_1=='on') ? '<div class="controls"><span class="controls"><select id="filter">' + ((parent_reference.config.category_all=='on') ? '<option value="all">All</option>' : '') + '</select></span></div>' : ''));
    $('#brandview').append(((parent_reference.config.controls_2=='on') ? '<div class="controls_2"><div class="categories">' + ((parent_reference.config.category_all=='on') ? '<a href="#" alt="all">All</a>' : '') + '</div></div>' : ''));
    $(xml).find('category').each(function(){ 
      if(parent_reference.config.categories.length!=1 && parent_reference.config.categories[0]!='') {
        for(var i=0;i<parent_reference.config.categories.length;i++) {
          if(parent_reference.config.categories[i]==$(this).find('title').text()) {
            $('#filter').append('<option value="' + $(this).find('title').text() + '">' + $(this).find('label').text() + '</option>');
            $('.controls_2 .categories').append('<a href="#" alt="' + $(this).find('title').text() + '">' + $(this).find('label').text() + '</a>');
          }
        }
      } else {
        $('#filter').append('<option value="' + $(this).find('title').text() + '">' + $(this).find('label').text() + '</option>');
        $('.controls_2 .categories').append('<a href="#" alt="' + $(this).find('title').text() + '">' + $(this).find('label').text() + '</a>');
      }
    });
    parent_reference.get_brands();
    $("#brandview .categories>a:last").addClass('last');
  });
};

BrandView.prototype.get_brands = function() {
  var parent_reference = this;
  $.get("/brandview/brands.xml", {}, function(xml) {
    $('#brandview').append('<ul id="brand_container"></ul>');
    $('#brandview').append(((parent_reference.config.pagination=='on') ? '<div class="page_numbers"><div class="numbers"></div></div>' : ''));
    $(xml).find('item').each(function(){$('#brandview > #brand_container').append('<li class="' + $(this).find('category').text() + '"><a href="' + $(this).find('url').text() + '" target="_blank"><span title="' + $(this).find('title').text() + '" style="background-position: ' + $(this).find('coordinates').text() + '">&nbsp;</span></a><p>' + $(this).find('description').text() + '</p></li>');}); 
    parent_reference.init_tooltip();
    parent_reference.init_filter();
  });
  $('body').append('<div id="brandview_tooltip"><div class="header"></div><div class="body"></div><div class="footer"></div></div>');
  $('#brandview_tooltip .footer').mouseover(function(){$('#brandview_tooltip').show()}).mouseout(function(){$('#brandview_tooltip').hide()});
}

BrandView.prototype.init_filter = function() {
  var parent_reference = this;
  $('#brandview #filter').change(function() { $("#brandview #filter option:selected").each(function () { parent_reference.change_category($(this).val()) }) });
  $('#brandview .controls_2 .categories a').click(function() {
    parent_reference.change_category($(this).attr('alt'));
    return false;
  });
  parent_reference.change_category(parent_reference.config.category);
}

BrandView.prototype.change_category = function(new_category) {
  var parent_reference = this;
  parent_reference.config.category = new_category;
  parent_reference.config.global_page = 1;
  $('#brandview .controls_2 .categories a').each(function() { 
    $(this).removeClass('selected');
    if($(this).attr('alt')==parent_reference.config.category) $(this).addClass('selected');
  });
  $('#brandview #filter option[value="' + new_category + '"]').attr('selected','selected');
  parent_reference.page(parent_reference.config.global_page);
}

BrandView.prototype.set_pagination = function(create_tags) {
  var parent_reference = this;
  if(create_tags) {
    var items = [];
    $('#brandview .page_numbers .numbers').html('');
    if(parent_reference.config.category=="all") items = parent_reference.brand;
    else {
      for(var i=0; i<parent_reference.brand.length; i++) {
        var temp_array = parent_reference.brand[i].categories.split(' ');
        for(var j=0; j<temp_array.length; j++) if(temp_array[j] == parent_reference.config.category) items.push(parent_reference.brand[i]);
      }
    }
    if(($(items).length)>parent_reference.config.brands_per_page) { for(var i=0; i<$(items).length; i+=parent_reference.config.brands_per_page) { $('.numbers').append('<a href="#">' + ((i/parent_reference.config.brands_per_page)+1) + '</a>') } }; //(i/parent_reference.config.brands_per_page) = page number
  }
  $('#brandview .page_numbers .numbers').children().unbind().each(function() {
    if($(this).html()==parent_reference.config.global_page+1) $(this).addClass('selected');
    else $(this).removeClass('selected');
  }).click(function() {
    parent_reference.page($(this).html());
    return false;
  });
}

BrandView.prototype.page = function(num) {
  var parent_reference = this;
  var index = parent_reference.config.global_page = num - 1;
  var start=index*parent_reference.config.brands_per_page;
  var limit = parent_reference.config.brands_per_page+(index*parent_reference.config.brands_per_page);
  
  $('#brand_container > li').hide();
  if(parent_reference.config.category=="all") var items = $('#brand_container > li');
  else var items = $('.'+parent_reference.config.category);
  
  var j=0;
  $(items).each(function() {
    if(j>=start&&j<limit) { $(this).show() };
    j++;
  });
  
  parent_reference.init_tooltip();
  parent_reference.set_pagination(true);
}

BrandView.prototype.init_tooltip = function() {
  var parent_reference = this;
  if(parent_reference.config.tooltips=="on") {
    $('#brand_container li').mouseout(function() { $('#brandview_tooltip').hide() }).mouseover(function() {
      var offset = $(this).offset();
      $('#brandview_tooltip').show().css('position','absolute').find('.body').html($(this).find('p').html()); // beware of the jitterbug
      $('#brandview_tooltip').css('top', offset.top - $('#brandview_tooltip').height()+20).css('left', offset.left - ($('#brandview_tooltip').width()/2) + ($(this).width()/2));
    });
  }
}


