var width;
var position;
var view_wid;
var num_page;
var curr_page;
var scrollby;
var col;
var min_pos;
var max_pos;
var col_width;
col_width = 209; //product width
position = 0;
uppos=0;

purl = readCookie('list_url');
purl = remside(purl);
curl = location.href;
curl = remside(curl);

if (purl == curl)
{
    uppos = readCookie('list_pos');
    createCookie('list_url','',0);
    createCookie('list_pos','',0);
}

//set position to cookie value if same page as before, reset cookie after position set

document.observe("dom:loaded", function() {
    try
    {
        update();
        createCookie('list_url',location.href,0);
        Event.observe(window, 'resize', update);
        Event.observe('plist_con', 'scroll', updatepage);
        Event.observe('plist_con', 'DOMMouseScroll', wheel); // mozilla
        Event.observe('plist_con', 'mousewheel', wheel); // IE/Opera
        slider.setValue(uppos);
        update();
    }
    catch(error)
    {
        //ignore if slider does not exist
    }
});

function moveP()
{
    slider.setValue($('plist_con').scrollLeft - scrollby);
}

function moveN()
{
    slider.setValue($('plist_con').scrollLeft + scrollby);
}

function update()
{
    width = ($('plist_con').scrollWidth - $('plist_con').offsetWidth);
    position = ($('plist_con').scrollLeft);
    //view_wid = ($('plist_con').offsetWidth);
    //if no width set width to 1 to avoid JS error
    if(width == 0)
    {
        width = 1;
    }
        
    if ($('plist_con').scrollWidth <= $('plist_con').offsetWidth) 
    {
        $('plist_mid').hide();
    }
    else
    {
        $('plist_mid').show();
    }
    
    try
    {
        slider.dispose();
    }
    catch(error)
    {
        //ignore if slider does not exist
    }
    
    try
    {
        slider = new Control.Slider('handle', 'track',
        {
            range: $R(0, width),
            sliderValue: position,
            onSlide: function(v)
            {
                scrollHorizontal(v, $('plist_con'), slider);
            },
            onChange: function(v)
            {
                scrollHorizontal(v, $('plist_con'), slider);
            }
        });
    }
    catch(error)
    {
        //no
    }
    updatepage();
}

function updatepage()
{   
    createCookie('list_pos',$('plist_con').scrollLeft,22);
    width = ($('plist_con').scrollWidth-$('plist_con').offsetWidth);
    col = Math.floor($('plist_con').offsetWidth / col_width) //find number of columns, times by width of col to get scroll size
    scrollby = col * col_width;
    view_wid = ($('plist_con').offsetWidth);
    position = ($('plist_con').scrollLeft);
    num_page = (Math.ceil(width / scrollby)+1);
    
    for (i=1; i<=num_page; i++)
    {
        min_pos = ((scrollby * i) - scrollby) - (scrollby - 1);
        max_pos = (scrollby * i) - (scrollby - 1);
        if (position > min_pos && position < max_pos)
        {
            curr_page = i;
            $('pageination').innerHTML = '' + curr_page + ' of ' + num_page
        }
    }
    
    if (position == 0) {
        $('page_prev').setStyle({
          opacity: '0.4',
          filter: 'alpha(opacity=40)'
        });
    }
    else
    {
        $('page_prev').setStyle({
          opacity: '1.0',
          filter: 'alpha(opacity=100)'
        });
    }
    
    if (position == (width)) {
        $('page_next').setStyle({
          opacity: '0.4',
          filter: 'alpha(opacity=40)'
        });
    }
    else
    {
        $('page_next').setStyle({
          opacity: '1.0',
          filter: 'alpha(opacity=100)'
        });
    }
}

//mouse scrolling
function handle(delta) {
	slider.setValueBy(-delta);
}

function wheel(event){
	var delta = 0;
	if (!event)
		event = window.event;
	if (event.wheelDelta) { // IE Opera
		if(event.wheelDelta > 0) {
		    delta = col_width;
		} else {
		    delta = -col_width;
		}
	} else if (event.detail) { // Mozilla
		if(-event.detail > 0) {
		    delta = col_width;
		} else {
		    delta = -col_width;
		}
	}
	if (delta)
		handle(delta);

	if (event.preventDefault)
		event.preventDefault();
	
	event.returnValue = false;
}

function remside(url) {
    if (url == null) {
        url = " ";
    }
    url = url.replace("&rside=soon", "");
    url = url.replace("&rside=best", "");
    url = url.replace("&rside=recent", "");
    url = url.replace("&rside=arrived", "");
    return url;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}