var storeCatStart = 0;
var storePostersPerPage = 20;
var currentStoreCat = 0;

$(document).ready(function(){
  var currentStoreCat = $('#storeCat').val();

  $('div#headlineSalesCategory h2').html($('#navSales a.active').html());

  if (typeof posterStoreTotals != 'undefined') {
    updateStoreNav(); 
  }


  // top poster area
  $('div#containerPostersHome #arrowRightHome a').click(function(){
    slideIt('next', $(this));
    $('div#containerPostersHome #arrowLeftHome').show();
    return false;
  });
  $('div#containerPostersHome #arrowLeftHome a').click(function(){
    slideIt('prev', $(this));
    $('div#containerPostersHome #arrowRightHome').show();
    return false;
  });

  $('#navSales a').click(function(){
    $('#navSales a').removeClass('active');
    $(this).addClass('active');
    var storeTitle = $(this).html();
    $('div#headlineSalesCategory h2').html(storeTitle);
  });

});


$(document).ready(function(){
  $('.down-list').width($('.dropdown-menu').width()-2);
  $('.down-list').css("width","200px");
  $('.down-list').css("display","none");
  $('.menu-first').hover(
    function () {
      $('.menu-first', this).addClass('slide-down');
      $('.down-list', this).slideDown(100);
    },
    function () {
      obj = this;
      $('.down-list', this).slideUp(100, function(){ $('.menu-first', obj).removeClass('slide-down'); });
  });
});



$(document).ready(function(){

  $("#nicemenuHome img.arrow").click(function(){ 
                
    $("span.head_menu").removeClass('active');
    
    submenu = $(this).parent().parent().find("div.sub_menu");
    
    if(submenu.css('display')=="block"){
      $(this).parent().removeClass("active");   
      submenu.hide();     
      $(this).attr('src','../posters/images/arrowArchiveHover.gif');                  
    }else{
      $(this).parent().addClass("active");   
      submenu.fadeIn();     
      $(this).attr('src','../posters/images/arrowArchiveSelect.gif');  
    }
    
    $("div.sub_menu:visible").not(submenu).hide();
    $("#nicemenuHome img.arrow").not(this).attr('src','../posters/images/arrowArchive.gif');
            
  })
  .mouseover(function(){ $(this).attr('src','../posters/images/arrowArchiveHover.gif'); })
  .mouseout(function(){ 
    if($(this).parent().parent().find("div.sub_menu").css('display')!="block"){
      $(this).attr('src','../posters/images/arrowArchive.gif');
    }else{
      $(this).attr('src','../posters/images/arrowArchiveSelect.gif');
    }
  });

  $("#nicemenuHome span.head_menu").mouseover(function(){ $(this).addClass('over')})
                 .mouseout(function(){ $(this).removeClass('over') });
  
  $("#nicemenuHome div.sub_menu").mouseover(function(){ $(this).fadeIn(); })
                 .blur(function(){ 
                     $(this).hide();
                  $("span.head_menu").removeClass('active');
                });    
                
  $(document).click(function(event){     
      var target = $(event.target);
      if (target.parents("#nicemenuHome").length == 0) {        
        $("#nicemenuHome span.head_menu").removeClass('active');
        $("#nicemenuHome div.sub_menu").hide();
        $("#nicemenuHome img.arrow").attr('src','../posters/images/arrowArchive.gif');
      }
  });         
                 
                   
});



/* ------------------------------------------------------- */
function slideIt (dir, obj) { 
  var step = 173;
  var spd = 400;
  var parentDiv = $(obj).parent().parent();;
  var movingObject = $(parentDiv).find('.slider');

  //console.log('sliding ' + dir);

  if ($(parentDiv).attr('id') == 'latest-trailers-videos') { 
    movingObject = $(parentDiv).find('div.slider');
    step = 287;
  } 

  var totalItems = $(movingObject).find('.poster').size();

  var x = 0;
  if (dir == 'next') { 
    x = $(movingObject).css('marginLeft').replace(/px/g, '') - step;
    $(parentDiv).find('img.prev').show();
  } else { 
    x = $(movingObject).css('marginLeft').replace(/px/g, '') * 1 + step;
    $(parentDiv).find('img.next').show();
  }

  $(movingObject).animate({marginLeft: x}, spd);

  
  if (x == 0) { $(parentDiv).find('.prev').hide(); }

  //console.log(x + " = " + ((totalItems-4) * step * -1 ));
  if (x == ((totalItems-6) * step * -1)) { $(parentDiv).find('.next').hide(); }

}


/* ------------------------------------------------------- */
function getPosters(tag) {
  tmp = "/posters/get-posters-new.php?tag=" + tag;
  $.ajax({
    url: tmp,
    cache: false,
    success: function(html){
      $("#postersHome .slider").html(html);
      $('#containerNav a').removeClass('active');
      tmp = "#containerNav .cat" + tag;
      $(tmp).addClass('active');
      $('div#containerPostersHome .slider').css('marginLeft', '0px');
      $('div#containerPostersHome .prev').hide();
      $('div#containerPostersHome .next').show();
    }
  });
}


/* ------------------------------------------------------- */
function getStorePosters(category, start) {
  if (category == 0) {
    var currentStoreCat = $('#storeCat').val();
    category = currentStoreCat;
  }
  else if (category != currentStoreCat) { 
    start = 0;
    storeCatStart = 0;
    currentStoreCat = category;
  }

  if (start == 'next') { start = storeCatStart + storePostersPerPage; }
  else if (start == 'prev') { start = storeCatStart - storePostersPerPage; }

  //if (start == '') { start = storeCatStart; }

  tmp = "/posters/get-posters.php?category=" + category + "&start=" + start ;
  $.ajax({
    url: tmp,
    cache: false,
    success: function(html){
      $("#containerPosters").html(html);
      storeCatStart = start;
      updateStoreNav();
    }
  });
}


/* ------------------------------------------------------- */
function updateStoreNav() {

  $('div.bottom #arrowRightSales a').hide();
  $('div.bottom #arrowLeftSales a').hide();

  var currentStoreCat = $('#storeCat').val();
 
  if (posterStoreTotals[currentStoreCat] - storePostersPerPage - storeCatStart > 0) { 
    $('div.bottom #arrowRightSales a').show();
  }
  if (storeCatStart - storePostersPerPage >= 0) { 
    $('div.bottom #arrowLeftSales a').show();
  }

  var links = "";


  for (i=0; i<= posterStoreTotals[currentStoreCat]; i+= 20) {
    var diff = Math.abs(i - storeCatStart);
    if (storeCatStart - i == 0) { links += "<b>" + (i/20 + 1) + "</b> | "; }
    else if (diff <= (storePostersPerPage * 4)  ) { 
      links += "<a href=\"javascript:getStorePosters(" + currentStoreCat + ", " + i + ");\">" + (i/20 + 1) + "</a> | ";
    }
    else if (i > storeCatStart +  (storePostersPerPage * 9)) { 
      links = links.substr(0, links.length-2); // remove final "|"
      links += " ... ";
      break;
    }
  }

  links += "<a href=\"javascript:getStorePosters(" + currentStoreCat + ", " + (posterStoreTotals[currentStoreCat] - 20) + ");\">" + (Math.floor(posterStoreTotals[currentStoreCat]/20) + 1) + "</a>";
  $('div.bottom div.pages').html(links);


}


