var re_link = /\#(.+)$/,
	e_activeTab;

function f_tabMenuInit () {
	var e_tabs = document.getElementById('navTabs');
	if (!e_tabs) return;
	var a_tabs = e_tabs.getElementsByTagName('LI');
	var s_activeLink = String(window.location).match(re_link) ? RegExp.$1 : null;

	var a_links, e_link, e_content, s_linkName, b_doScroll, b_scrollTo, a_allLinks, l;
	for (var i = 0; i < a_tabs.length; i++) {
		s_linkName = f_getLinkName (a_tabs[i]);
		if (s_activeLink) {
			b_scrollTo = false;
			e_content = document.getElementById('tab-' + s_linkName);
			if (!e_content) continue;
			if (!b_doScroll) {
				a_allLinks = e_content.getElementsByTagName('A');
					for (var l = 0; l < a_allLinks.length; l++) {
					if (a_allLinks[l].name == s_activeLink) {
						b_doScroll = b_scrollTo = true;
						break;
					}
				}
			}

			if (s_activeLink == s_linkName || b_scrollTo) {
				a_tabs[i].id = 'currentTab';
				e_activeTab = a_tabs[i];
				e_content.className = 'tabContentCurrent';
			}
			else {
				a_tabs[i].id = null;
				e_content.className = 'tabContent';
			}
		}
		var a_links = a_tabs[i].getElementsByTagName('A');
		a_links[0].onmousedown = function () { this.blur(); return false; };

		a_tabs[i].onmouseover = f_tabMouseOver;
		a_tabs[i].onmouseout = f_tabMouseOut;
		a_tabs[i].onclick = f_tabMouseClick;
	}
	if (!s_activeLink)
		e_activeTab = a_tabs[0];

	if (b_doScroll)
		f_scrollTo(s_activeLink);
}

function f_tabMouseOver () {
	if (this != e_activeTab)
		this.id = 'tabHover';
}
function f_tabMouseOut () {
	if (this != e_activeTab)
		this.id = null;
}

var e_prevContent, e_newContent, o_tabTimerFO, o_tabTimerFI;

function f_tabMouseClick () {

	var e_prevTab = document.getElementById('currentTab');
	if (e_prevTab == this) return;

	var s_prevLinkName = f_getLinkName(e_prevTab);

	e_prevTab.id = null;
	this.id = 'currentTab';
	e_activeTab = this;
	
	var s_newLinkName = f_getLinkName(this);
	if (!s_newLinkName || !s_prevLinkName) return;
	
	var e_from = document.getElementById('tab-' + s_prevLinkName);
	var e_to   = document.getElementById('tab-' + s_newLinkName);
		
	if (e_from && e_to && !o_tabTimerFO && !o_tabTimerFI)
		f_tabFadeOut(null, e_from, e_to);
}

function f_tabShow (s_href, s_anchor) {
	var e_prevTab = document.getElementById('currentTab');

	var s_prevLinkName = f_getLinkName(e_prevTab);
	e_prevTab.id = null;

	var e_tabs = document.getElementById('navTabs');
	var a_links = e_tabs.getElementsByTagName('A');
	var e_currentTab;
	for (var i = 0; i < a_links.length; i++) {
		if (String(a_links[i].href).indexOf(s_href) != -1) {
				e_currentTab = a_links[i];
				break;
		}
	}
	if (!e_currentTab) return;
	e_currentTab = e_currentTab.parentNode;
	
	e_currentTab.id = 'currentTab';
	e_activeTab = e_currentTab;

	var s_newLinkName = f_getLinkName(e_currentTab);
	if (!s_newLinkName || !s_prevLinkName) return;
	
	var e_from = document.getElementById('tab-' + s_prevLinkName);
	var e_to   = document.getElementById('tab-' + s_newLinkName);
		
	if (e_from && e_to && !o_tabTimerFO && !o_tabTimerFI) {
		window.s_scrollTo = s_anchor;
		f_tabFadeOut(null, e_from, e_to);
	}
	return true;
}

// scroll to anchor
function f_scrollTo (s_anchorName) {
	var a_allLinks = document.getElementsByTagName('A');
	var e_scrollTo;
	for (var i = 0; i < a_allLinks.length; i++) {
		if (a_allLinks[i].name == s_anchorName) {
			e_scrollTo = a_allLinks[i];
			break;
		}
	}
	if (e_scrollTo) {
		var n_pos = 0, n_offset, e_elem = e_scrollTo;
		while (e_elem) {
			n_offset = e_elem['offsetTop'];
			n_pos += n_offset;
			e_elem = e_elem.offsetParent;
		}
		window.scrollTo(0, n_pos);
	}
}

function f_tabFadeOut (n_opacity, e_from, e_to) {

	// end of transition
	if (n_opacity < 0) {
		if (window.b_IE)
			e_prevContent.style.filter='';
		e_prevContent.className = 'tabContent';
		e_prevContent = null;
		o_tabTimerFO = null;
		return f_tabFadeIn();
	}
	// new transition
	if (n_opacity == null && e_from && e_to) {
		e_prevContent = e_from;
		e_newContent  = e_to;

		if (window.o_tabTimerFO)
			clearTimeout(o_tabTimerFO);
		n_opacity = 99;
		if (window.b_IE)
			e_prevContent.style.filter='alpha(opacity=100)';
	}

	f_setOpacity(e_prevContent, n_opacity);
	n_opacity -= 10;

	// cycle
	o_tabTimerFO = setTimeout('f_tabFadeOut(' + n_opacity + ')', 10);
}

function f_tabFadeIn (n_opacity) {

	// end of transition
	if (n_opacity > 100) {
		f_setOpacity(e_newContent, 100);
		if (window.b_IE)
			e_newContent.style.filter= '';
		e_newContent = null;
		o_tabTimerFI = null;
		
		if (window.s_scrollTo) {
			f_scrollTo(window.s_scrollTo);
			window.s_scrollTo = null;
		}
		return;
	}

	// new transition
	if (n_opacity == null) {
		if (window.o_tabTimerFI)
			clearTimeout(o_tabTimerFI);
		n_opacity = 1;
		if (window.b_IE)
			e_newContent.style.filter='alpha(opacity=1)';
		e_newContent.className = 'tabContentCurrent';
	}
	f_setOpacity(e_newContent, n_opacity);

	n_opacity += 10;

	// cycle
	o_tabTimerFI = setTimeout('f_tabFadeIn(' + n_opacity + ')', 20);
}

var s_uaApp  = navigator.userAgent.toLowerCase();
if (s_uaApp.indexOf('opera') != -1 || s_uaApp.indexOf('safari') != -1)
	window.f_setOpacity = function (e_elem, n_opacity) {
		e_elem.style.opacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('gecko') != -1)
	window.f_setOpacity = function (e_elem, n_opacity) {
		e_elem.style.MozOpacity = n_opacity / 100;
	};
else if (s_uaApp.indexOf('msie') != -1) {
	window.b_IE = true;
	window.f_setOpacity = function (e_elem, n_opacity) {
		try {
			e_elem.filters.alpha.opacity = n_opacity;
		} catch (e) {};
	};
}
else
	window.f_setOpacity = null;

function f_getLinkName (e_elem) {
	var a_links = e_elem.getElementsByTagName('A');
	var e_link = a_links[0];

	if (!e_link.onmousedown)
		e_link.onmousedown = function () { this.blur(); }; 

	return e_link.href.match(re_link)
		? RegExp.$1
		: null;
}


if (document.addEventListener)
	window.addEventListener('load', f_tabMenuInit, false);
if (window.attachEvent)
	window.attachEvent('onload', f_tabMenuInit);
else
	window.onload = f_tabMenuInit;

// non tab function used in pages with tabs
function f_saveAdded (s_name, n_qty) {
	var d_exp = new Date();

	// display message
	if (!s_name || !n_qty) {
		d_exp.setTime(d_exp.getTime() -3600);
		if (String(document.cookie).match(/lastAddedProduct\=([^\;]+)/)) 
			s_name = urldecode(RegExp.$1);
		if (String(document.cookie).match(/lastAddedQuantity\=(\d+)/)) 
			s_qty = parseInt(RegExp.$1);

		if (s_name != null && s_qty != null) {
			var e_elem = document.getElementById('cartMessage');
			var b_sample = s_name.match(/sample/i);
			s_name = s_name.replace(/\s?sample/i, '');
			var b_single = (s_qty % 10 == 1) && (s_qty % 100 != 11);
			e_elem.innerHTML = s_qty + (b_sample ? ' sample' : ' pack')  + (b_single ? '' : 's') + ' of <b>' + s_name + '</b> ' + (b_single ? 'has' : 'have') + ' been added to <a href="/catalog/specialty.paper?Screen=BASK&Store_Code=LPC">Your Cart</a>.'
			e_elem.style.display = 'block';
		}
		else {
			s_name = '';
			s_qty = '';
		}
	}
	else {
		d_exp.setTime(d_exp.getTime() + 3600);
	}
	document.cookie = 'lastAddedProduct='  + s_name  + ';;expires=' + d_exp.toGMTString();
	document.cookie = 'lastAddedQuantity='  + n_qty  + ';;expires=' + d_exp.toGMTString();
}

var A_CACHEDREQS = [];
function f_hideCatPreview () {
	if (window.e_previewTO) {
		clearTimeout(window.e_previewTO);
		window.e_previewTO = null;
	}

	if (!o_prodPreview)  return;
	o_prodPreview.hide();
}

function f_getHTTPObject() {
	var xmlhttp;
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}
	}
		if(!xmlhttp && typeof ActiveXObject != "undefined"){
		try{ xmlhttp=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xmlhttp=false;}
		if(!xmlhttp)try{ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xmlhttp=false;}
	}
	return xmlhttp;
}

function f_showCatPreview (s_code, e_origin) {

	window.s_savedCode   = s_code;
	window.e_savedOrigin = e_origin;
	if (window.e_previewTO)
		clearTimeout(window.e_previewTO);
	window.e_previewTO = setTimeout("f_loadCatPreview()", 200);
}

function f_loadCatPreview () {
	if (A_CACHEDREQS[window.s_savedCode]) {
		var e_elem = document.getElementById('catPreviewContent');
		if (!window.o_prodPreview || !e_elem) return;
		e_elem.innerHTML = A_CACHEDREQS[window.s_savedCode];
		o_prodPreview.show('catPreview', window.e_savedOrigin);
		return;
	}
	
	window.o_http = f_getHTTPObject();
	window.o_http.onreadystatechange = f_updateCatPreview;
	window.o_http.open("GET",
		(String(window.location).indexOf('http:') == 0 ? 'http:' : 'https')
		+ '//www.lcipaper.com/php/category-preview.php?c=' + escape(window.s_savedCode));
	window.o_http.send(null);
}

function f_updateCatPreview () {
	if (!window.o_http)
		return;

	if (window.o_http.readyState != 4) return;
	if (window.o_http.status != 200 && o_http.status != 304)
		throw 'AJAX response error';

	var s_html = window.o_http.responseText
	var e_elem = document.getElementById('catPreviewContent');
	if (!window.o_prodPreview || !e_elem) return;

	e_elem.innerHTML = s_html;
	A_CACHEDREQS[window.s_savedCode] = s_html;
	o_prodPreview.show('catPreview', window.e_savedOrigin);
}

function urldecode(str) {
  
    var histogram = {};
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urlencode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    for (replace in histogram) {
        search = histogram[replace]; // Switch order when decoding
        ret = replacer(search, replace, ret) // Custom replace. No regexing   
    }
    
    // End with decodeURIComponent, which most resembles PHP's encoding functions
    ret = decodeURIComponent(ret);

    return ret;
}

function f_showMoreReviews () {
	var e_reviews = document.getElementById('reviews');
	if (!e_reviews)
		e_reviews = document.getElementById('tab-reviews');	
	if (!e_reviews)
		return;
	var a_links = e_reviews.getElementsByTagName('A');
	if (a_links.length)
		a_links[0].style.display = 'none';
	var a_divs = e_reviews.getElementsByTagName('DIV');
	for (var i = 0; i < a_divs.length; i++)
		if (String(a_divs[i].className).indexOf('morereviews') != -1)
			a_divs[i].style.display = 'block';
	
}