
function PersonalizationSlider(options)
{
	// defaults (for PDP)
	this.container = '#content ';
	if(typeof options != 'undefined')
	{
		if(typeof options.container != 'undefined') this.container = options.container;
	}
	this.slideWindow = this.container+'#pdp_personalization';
	this.slideContent = this.container+'#pdp_personalization .personalization_list';
	this.pageButtonContainer = this.container+'.slider_nav .slider_nav_page_buttons';
	this.pageButtonPrefix = 'slider_nav_page_';
	this.pageButtonLabel = this.container+'#slider_nav_label';
	this.pageButtonWrapper = this.container+'.personalization_title_wrap .slider_nav';
	this.nextButton = this.container+'#personalizationArrowRight';
	this.prevButton = this.container+'#personalizationArrowLeft';
	this.titleBar = this.container+'#pdp_personalization_title';
	this.slideAmountAdjust = 1;
	
	this.sliderConfig = {};
	this.sliderConfig.cross_stylist = {};
	this.sliderConfig.cross_stylist.title = 'Wear them together';
	this.sliderConfig.cross_stylist.tagType = 'StylistPicks';
	this.sliderConfig.cross_stylist.tagName = 'Stylist Picks';
	this.sliderConfig.cross_popular = {};
	this.sliderConfig.cross_popular.title = 'People that bought this item also bought';
	this.sliderConfig.cross_popular.tagType = 'MostPopular';
	this.sliderConfig.cross_popular.tagName = 'Most Popular';
	
	// override default value if it is set in the options param
	if(typeof options != 'undefined')
	{
		if(typeof options.slideWindow != 'undefined') this.slideWindow = options.slideWindow;
		if(typeof options.slideContent != 'undefined') this.slideContent = options.slideContent;
		if(typeof options.pageButtonContainer != 'undefined') this.pageButtonContainer = options.pageButtonContainer;
		if(typeof options.pageButtonPrefix != 'undefined') this.pageButtonPrefix = options.pageButtonPrefix;
		if(typeof options.pageButtonLabel != 'undefined') this.pageButtonLabel = options.pageButtonLabel;
		if(typeof options.pageButtonWrapper != 'undefined') this.pageButtonWrapper = options.pageButtonWrapper;
		if(typeof options.nextButton != 'undefined') this.nextButton = options.nextButton;
		if(typeof options.prevButton != 'undefined') this.prevButton = options.prevButton;
		if(typeof options.slideAmountAdjust != 'undefined') this.slideAmountAdjust = options.slideAmountAdjust;
		if(typeof options.titleBar != 'undefined') this.titleBar = options.titleBar;
	}
	
	this.duration = .5;
	this.easing = 'swing';
	this.currentPage = 0;
	this.slideAmount = 0;
	this.debugEnabled = false;
	this.expressShop = $('body').hasClass('expressShop') ? true : false;
	this.sliderpProducts = [];
	this.type = []; // 'cross_popular' or 'cross_stylist'
	
	this.duration *= 1000; // convert to miliseconds
}

PersonalizationSlider.prototype.debug = function(output)
{
	if(this.debugEnabled) alert(output);	
}

PersonalizationSlider.prototype.slideTo = function(eventOrPageNumber, sendWebtrends, sendOmniture)
{
	if(typeof sendWebtrends == 'undefined') sendWebtrends = true;
	if(typeof sendOmniture  == 'undefined') sendOmniture  = true;
	var page = (typeof eventOrPageNumber === 'object') ? eventOrPageNumber.data.page : eventOrPageNumber;
	var thisSlider = (typeof eventOrPageNumber === 'object') ? eventOrPageNumber.data.slider : this;
	
	var sliderItems = $(thisSlider.slideContent);
	//var currX = (isArray(sliderItems) || (sliderItems.length > 1)) ? $(sliderItems[0]).offset() : $(sliderItems).offset();
	var nextX = 0-(page * thisSlider.slideAmount);
	
	$(sliderItems).each(function(i){
	thisSlider.debug('nextX for '+i+':'+nextX);
		$(this).animate({left:nextX+'px'}, thisSlider.duration, thisSlider.easing);
		nextX += thisSlider.slideAmount;
	});
	
	thisSlider.currentPage = page;
	thisSlider.updatePageDisplay(sendWebtrends, sendOmniture);
	$(thisSlider.pageButtonContainer+' li').removeClass('selected');
	$(thisSlider.container+'#'+thisSlider.pageButtonPrefix+thisSlider.currentPage).addClass('selected');
}

PersonalizationSlider.prototype.next = function(e)
{
	var thisSlider = (typeof e === 'object') ? e.data.slider : this;
	var nextPage = 0;
	if(thisSlider.currentPage + 1 < thisSlider.totalSets) nextPage = thisSlider.currentPage + 1;
	thisSlider.slideTo(nextPage);
}
PersonalizationSlider.prototype.previous = function(e)
{
	var thisSlider = (typeof e === 'object') ? e.data.slider : this;
	var nextPage = thisSlider.totalSets - 1;
	if(thisSlider.currentPage - 1 >= 0) nextPage = thisSlider.currentPage - 1;
	thisSlider.slideTo(nextPage);
}
PersonalizationSlider.prototype.updatePageDisplay = function(sendWebtrends, sendOmniture)
{
	if(typeof sendWebtrends == 'undefined') sendWebtrends = true;
	if(typeof sendOmniture  == 'undefined') sendOmniture  = true;
	if(this.totalSets > 1)
	{
		$(this.pageButtonLabel).html('Set&nbsp;'+(this.currentPage+1)+'&nbsp;of&nbsp;'+this.totalSets);
		var totalWidth = 0;
		$(this.pageButtonLabel+', '+this.pageButtonContainer+' li').each(function(i){
			totalWidth += $(this).width();
			totalWidth += parseInt($(this).css('margin-left'));
			totalWidth += parseInt($(this).css('margin-right'));
		});
		$(this.pageButtonWrapper).css({width:(totalWidth+19)+'px'}); // adding 19 to leave some room for what will look like a right margin
	}
	else
	{
		$(this.pageButtonLabel).html('&nbsp;');
		$(this.pageButtonContainer).html('&nbsp;');
	}
	
	$(this.titleBar).html(this.sliderConfig[this.type[this.currentPage]].title);
	if(sendOmniture)  this.callOmniture(this.currentPage);
}

PersonalizationSlider.prototype.callOmniture = function(setNum)
{
	var prods="";
	var corp_products="";
	var kefta_products="";
	var pc_products="";
	
	if(typeof omniture.pdpqv_products=='undefined') {} else {prods=omniture.pdpqv_products;}
	if(typeof omniture.corp_products=='undefined') {} else {corp_products=omniture.corp_products;}
	if(typeof omniture.kefta_products=='undefined') {} else {kefta_products=omniture.kefta_products;}
	if(typeof omniture.pc_products=='undefined') {} else {pc_products=omniture.pc_products;}
	
	if(setNum == 0) { prods += corp_products+kefta_products; } // MP
	else { prods += pc_products; }	// SP
	
	var s=s_gi(s_account);
	s.linkTrackVars='products,events';
	s.events='event11';
	s.products=prods;
	s.tl(this,'o','Cross-Sell Tab');
}

PersonalizationSlider.prototype.init = function()
{
	var currX = 0;
	var sliderItems = $(this.slideContent);
	var currY = '29px';
	var slideAmount = 0;
	var thisSlider = this;
	var count = 0;
	$(sliderItems).each(function(i)
	{
	    var jqObj = $(this);
		if (jqObj.children('li').length > 0) // only initialize it if there are items in the list
		{
			jqObj.css({top:currY});
			jqObj.css({left:currX+'px'});
			currX += jqObj.width();
			slideAmount = jqObj.width() > slideAmount ? jqObj.width() : slideAmount;
			$(thisSlider.pageButtonContainer).append('<li id="'+thisSlider.pageButtonPrefix+i+'"><a href="javascript:;">'+(i+1)+'</a></li>');
			$(thisSlider.container+'#'+thisSlider.pageButtonPrefix+i).bind('click', {page:i, slider:thisSlider}, thisSlider.slideTo);
			
			thisSlider.type[count] = jqObj.attr('id');
			
			thisSlider.sliderpProducts[count] = [];
			var childCount = 0;
			jqObj.children('li a').each(function(j)
			{
				var temp = $(this).attr('id').split('-');
				if(typeof temp[1] != 'undefined')
				{
					thisSlider.sliderpProducts[count][childCount] = temp[1];
					childCount++;
				}
			});
			
			count++;
		}
		else
		{
			jqObj.remove(); // remove the empty <ul>
		}
	});
	this.slideAmount = slideAmount+this.slideAmountAdjust;
	this.totalSets = count;
	if(this.totalSets > 0)
	{
		var firstTab = this.totalSets > 1 && (jsContextRoot == 'aerie' || jsContextRoot == '77kids') ? 1 : 0; // switch to the second tab for aerie if it exists
		var sendAnalytics = firstTab == 0 ? false : true; // if switching to the second tab for aerie, send webtrends as well
		this.slideTo(firstTab, sendAnalytics, false); // pass in false to disable webtrends tagging on page load (already done via meta tag)
		if(this.totalSets > 1)
		{
			$(this.slideWindow).after('<a class="personalizationArrow" id="personalizationArrowLeft" href="javascript:;">&lt;&mdash;</a><a class="personalizationArrow" id="personalizationArrowRight" href="javascript:;">&mdash;&gt;</a>');
			$(this.nextButton).bind('click', {slider:this}, this.next);
			$(this.prevButton).bind('click', {slider:this}, this.previous);
		}
	}
	else
	{
		$(this.container+'#crosswrap_td_containerProduct').hide();
	}
}
