$(document).ready(function(){
  
  $('h2.brand a, #brand_list a').click(function(e){

    var x = e.pageX;
  	var y = e.pageY;
  	
  	var bw = $('body').width();

    //$('#choice .select').html(bw - x +', '+ y);
  
    var content_offset = $('#content').offset();    
    var content_bottom = content_offset.top + 413;
    var content_right = bw - ((bw / 2) + 389); // e.g. bw - 1153

    var offset = $(this).offset(); 
    
    var top = y - 20;
    var right = bw - x - 355;
    
    if(right < content_right)
      right = content_right;
    
    if((top + 221) > content_bottom)
      top = content_bottom - 221;
    

    var brand_id = $(this).attr('brand');
    var url = $(this).attr('rel');
    
    $('#brand_name').text($('#i'+brand_id).attr('alt'));
    $('#brand_logo').attr('src', $('#i'+brand_id).attr('src'));
   
    $.ajax({
       type: "GET",
       url: url,
       success: function(imgs){
         //alert(imgs);
         $('#carousel_small').html(imgs);
         load_carousel(3);
          
          // initCallback: show_popup it also flickers this way too
          
          if($('.arrowless').length){
            $('.jcarousel-next-horizontal, .jcarousel-prev-horizontal').addClass('hidden');
          }
       }
     });
     
     //$('#brand_popup').show();
     
     $('#brand_popup').css('top', top + 'px');
     $('#brand_popup').css('right', right + 'px');
     
     $('#brand_popup').show();
     /*
     $('#watch_imgs').jcarousel({
          buttonNextEvent: "mouseover click",
          buttonPrevEvent: "mouseover click",
          scroll: 3
          //initCallback: show_popup
       });
     */
     
     
          
     $('#brand_popup').mouseleave(function(){ // mouseleave
       $('#carousel_small *').remove();
       $(this).hide();
     });
    
  });
  

  function show_popup(a,b)
  {
    $('#brand_popup').show();
  }

  
});

/*
Event.observe(window, 'load', function() {
  Event.observe('drop_down_arrow', 'click', function() {
    $('brand_list').toggle();
  });
});
*/