var _productpages = new Array();
var _globalcarousel = "";

function mycarousel_initCallback(carousel) {
	_globalcarousel = carousel;
	jQuery(".jcarousel-control a").bind("click", function() {
	    carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
	    return false;
	});

	jQuery(".jcarousel-scroll select").bind("change", function() {
	    carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
	    return false;
	});

	jQuery("#mycarousel-next").bind("click", function() {
	    carousel.next();
	    return false;
	});

	jQuery("#mycarousel-prev").bind("click", function() {
	    carousel.prev();
	    return false;
	});
	jQuery(".prevPageBar").bind("click", function() {
	  //get the current _left's item # and activate it's left's src
	  if ( jQuery('.active-only-left').find('img').attr('rel') ) { 
	  _src = jQuery('.active-only-left').find('img').attr('rel'); 
	  
	  } else if ( jQuery('.active-left').find('img').attr('rel') ) {
		  _src = jQuery('.active-left').find('img').attr('rel'); 
	  }
	  var c=1;
	  for (i in _productpages) {
	    if (i == _src){ break; }
	    c++;
	  }
	  
	  if (c > 1) {
	    var _left = c-1;
	    l=1;
	    for (i in _productpages) {
	      if (l == _left){ _leftsrc = i; break;}
	      l++;
	    }
	    //alert("Going to item # "+_left+" coming from item #"+c);
	    activate(_leftsrc);
	  }
	    return false;
	});

	jQuery(".prevPageIE6").bind("click", function() {
	  //get the current _left's item # and activate it's left's src
	  if ( jQuery('.active-only-left').find('img').attr('rel') ) {
		  _src = jQuery('.active-only-left').find('img').attr('rel'); 
		  } else if ( jQuery('.active-left').find('img').attr('rel') ) {
			  _src = jQuery('.active-left').find('img').attr('rel'); 
		}
	  var c=1;
	  for (i in _productpages) {
	    if (i == _src){ break; }
	    c++;
	  }
	  
	  if (c > 1) {
	    var _left = c-1;
	    l=1;
	    for (i in _productpages) {
	      if (l == _left){ _leftsrc = i; break;}
	      l++;
	    }
	    //alert("Going to item # "+_left+" coming from item #"+c);
	    activate(_leftsrc);
	  }
	    return false;
	});

	jQuery(".prevPageBar").hover(
	  function(){
	    jQuery('.prevPage').stop('true','true');
	    jQuery('.prevPage').fadeIn('fast');
	  },
	  function() {
	    jQuery('.prevPage').stop('true','true');
	    jQuery('.prevPage').fadeOut('fast');
	});

	jQuery(".nextPageBar").bind("click", function() {
	  //get the current _left's item # and activate it's left's src
	  if ( jQuery('.active-right') ) { _src = jQuery('.active-right').find('img').attr('rel'); }
	  var c=1;
	  for (i in _productpages) {
	    if (i == _src) break;
	    c++;
	  }
	  if (c > 1) {
	    var _right = c+1;
	    l=1;
	    for (i in _productpages) {
	      if (l == _right){ _rightsrc = i; break;}
	      l++;
	    }
	    activate(_rightsrc);
	  }
	    return false;
	});


	jQuery(".nextPageIE6").bind("click", function() {
	  //get the current _left's item # and activate it's left's src
	  if ( jQuery('.active-right') ) { _src = jQuery('.active-right').find('img').attr('rel'); }
	  var c=1;
	  for (i in _productpages) {
	    if (i == _src) break;
	    c++;
	  }
	  if (c > 1) {
	    var _right = c+1;
	    l=1;
	    for (i in _productpages) {
	      if (l == _right){ _rightsrc = i; break;}
	      l++;
	    }
	    activate(_rightsrc);
	  }
	    return false;
	});

	jQuery(".nextPageBar").hover(
	  function(){
	    jQuery('.nextPage').stop('true','true');
	    jQuery('.nextPage').fadeIn('fast');
	  },
	  function() {
	    jQuery('.nextPage').stop('true','true');
	    jQuery('.nextPage').fadeOut('fast');
	});



	//replace thumbnails with just their image & set up the src/links array
	jQuery("#the_list").addClass('smp-lookbook');
	var _container = jQuery('.smp-lookbook');
	var t = 1;
	
	_container.children('li').each(function(i) {

	  var _mycontainer = jQuery(this);
	  var _a =  jQuery(this).find('a');
	  var _src = _a.attr('rel'); //actual source to full image.
	  var _productpage = _a.attr('href');	//source to product page.
	  _productpages[_src]= _productpage;
	  var _loader = new Image();
	  jQuery(_loader).load(function () {
	    var _thumb = _a.find('img').addClass('thumb noscale').css('display','none');
	    _a.remove();
	    // alert("replaced a with src "+_productpage+ "with image with src "+ _thumb.attr('src'));
	    _thumb.attr('rel',_src);
	    //set up click function
	    jQuery(_thumb).bind("click", function() {
	      activate(_src);
	    });

	    _thumb.css('display','block');
	    _mycontainer.prepend(_thumb);
	    if (t == _container.children('li').length) {

	     // alert("lol"+t+" / "+_container.children('li').length);
	      for (i in _productpages) {
		break;
	      }
	      activate(i);
	      jQuery('.nextPage').fadeIn(1000);
	    }
	    t++;
	  }).error(function () {
				
				// Error handling
			    _mycontainer.html('<span class="error" style="color:red">Error loading image: '+_src+'</span>');
			
			}).attr('src', _src);
	});

  
    };


//scrolls to, AND displays two images in the 'book'
function activate (_src) {
  //ok, lets find out where they are in the book (what item number & total #, so we can calculate page # and left & right images)
  if (! _productpages[_src] ) return false;	//couldn't find in our array D:

    var c=1;
    for (i in _productpages) {
      if (i == _src) break;
      c++;
    }
//is the current selected image on the left or right page?
    if (c % 2 ) {
      var _left = c;
      var _right = c+1;
    } else {
      var _right = c;
      var _left = c-1;
    }

    l = 1;
    _rightsrc = "";
    for (i in _productpages) {
      if (l == _left) _leftsrc = i;
      if (l == _right) _rightsrc = i;
      l++;
    }
    l--;
	
	if(_rightsrc == 'shuffle') _rightsrc = false;
	
	if(jQuery('#leftImage').attr('src') == _leftsrc) return;

   var ul =  jQuery('#the_list');
    var li = ul.children();
    var liWidth = 110;
    var first_midpoint = Math.floor(jQuery('#slider').width() / 2);
    var totalWidth = li.length * liWidth
    var offsetRight = parseInt( ul.css( "left" ) ) + ( liWidth * 4 );
    var pages = Math.ceil(li.length / 2);
    var curpage = Math.ceil(c/2);
    var full_per_page = Math.floor( jQuery('#slider').width()  / liWidth );

    //alert("Displaying page "+curpage+" out of "+pages+".  Showing items #"+_left+" and "+_right+" lsrc="+_leftsrc+" rsrc="+_rightsrc);

    //display main images
    var _leftimg =  jQuery(new Image()).attr('src',_leftsrc);
    _leftimg.bind("click",function () { document.location = _productpages[_leftsrc]; });
    _leftimg.css("cursor","pointer");
   // alert("what, left image("+_src+") has click of "+_productpages[_leftsrc]);
    jQuery('#leftImage').replaceWith(_leftimg);
    _leftimg.attr('id','leftImage');
    
    jQuery('.prevPageBar').css('display','block');
    if (_left <= 1) {
      jQuery('.prevPage').fadeOut(0);
      jQuery('.prevPageBar').css('display','none');
    }
    jQuery('.lb-hovertip-inside').css('width','100%');
    jQuery('.lb-hovertip-ie6').css('width','100%');
    if (_rightsrc) {
      var _rightimg =  jQuery(new Image()).attr('src',_rightsrc);
      _rightimg.bind("click",function () { document.location = _productpages[_rightsrc]; });
      _rightimg.css("cursor","pointer");
      jQuery('#rightImage').replaceWith(_rightimg);
      _rightimg.attr('id','rightImage');
      jQuery('.nextPageBar').css('display','block');
      //is this the very last image?
      if(_right >=l) {
	jQuery('.nextPage').fadeOut(0);
	jQuery('.nextPageBar').css('display','none');
      }
      
    } else {
      jQuery('#rightImage').css('display','none');
      jQuery('.nextPage').fadeOut(0);
      jQuery('.nextPageBar').css('display','none');
      jQuery('.lb-hovertip-inside').css('width','300px');
      jQuery('.lb-hovertip-ie6').css('width','300px');
    }

    
    //set active thumbs as active, deactivate previous thumbs
    jQuery('.active-left').removeClass('active-left');
    jQuery('.active-only-left').removeClass('active-only-left');
    jQuery('.active-right').removeClass('active-right');
    _leftthumb = jQuery('#the_list li:nth-child('+_left+')');
    if (_rightsrc) {
      _rightthumb = jQuery('#the_list li:nth-child('+_right+')');
      _rightthumb.addClass('active-right');
      _leftthumb.addClass('active-left');
    } else {
      _leftthumb.addClass('active-only-left');
      
    }

    //scroll to the items in the carousel.
    if (_left >= li.length ) {
      if (_globalcarousel.tail != null && !_globalcarousel.inTail)
	_globalcarousel.scrollTail(false);
    } else {
      if ( _left <=2 ){
	//alert(_left);
	_globalcarousel.scroll(1,true);
      } else _globalcarousel.scroll(_left-1,true);
    }
    if(jQuery('.active-left').length > 0){
    	left_text = jQuery('.active-left img').attr('title');
    }else{
		left_text = jQuery('.active-only-left img').attr('title');
    }
    if(jQuery('.active-right').length > 0){
    	right_text = jQuery('.active-right img').attr('title');
    }else{
		right_text = '';
    }
	jQuery('#titleLableOne').html('<a href="' + _productpages[_leftsrc] +'">' + left_text + '</a>').show();
	jQuery('#titleLableTwo').html('<a href="' + _productpages[_rightsrc] +'">' + right_text + '</a>').show();
}


    // Ride the carousel...
    jQuery(document).ready(function() {
	jQuery("#slider").jcarousel({
	    scroll: 2,
	    initCallback: mycarousel_initCallback,
	    // This tells jCarousel NOT to autobuild prev/next buttons
	    buttonNextHTML: null,
	    buttonPrevHTML: null
	});
      //activate the first item.
      
//       
    });

