$(function() {
	// Product search
	$('#product_search').click(function() {
		var product_results = $('#product_results');
		if (product_results.is(":hidden")) {
			$(this).css({ 'color' : '#000' });
			product_results.slideDown('fast');
		} else {
			$(this).css({ 'color' : '#555' });
			product_results.slideUp('fast');		
		}
	});
	
	// Site search
	$('#keywords input')
	.focus(function() {
		$(this).css({ color: '#000' }).val($(this).val() == 'Site Search' ? '' : $(this).val());
	})
	.blur(function() {
		$(this).css({ color: '#555' }).val($(this).val() == '' ? 'Site Search' : $(this).val());
	});
	
	if($.browser.msie && jQuery.browser.version <= 7) {
		$('#products_nav ul li').mouseleave(function() { $(this).find('ul').css('display', 'none'); });
		$('#products_nav ul li').mouseenter(function() { $(this).find('ul').css('display', '');     });
		$('#products_nav > li').mouseleave(function() {
			if($(this).children('ul').length > 0)
				$(this).next().next().next().css('visibility', '');
		});
		$('#products_nav > li').mouseenter(function() {
			if($(this).children('ul').length > 0)
				$(this).next().next().next().css('visibility', 'hidden');
		});
	}
});

function toggleTabs(link, which) {
	div = link.parentNode.parentNode;
	categories_div = document.getElementById('categories_content');
	manufacturers_div = document.getElementById('manufacturers_content');
	
	switch(which) {
		case 'categories':
			div.className = 'categories';
			categories_div.style.display = '';
			manufacturers_div.style.display = 'none';
			break;
		case 'manufacturers':
			div.className = 'manufacturers';
			categories_div.style.display = 'none';
			manufacturers_div.style.display = '';
			break;			
	}
	
	if(link.blur) link.blur();
}

function emailLink() {
	var argv = emailLink.arguments;
	return '<a href="mailto:'+argv[0]+'@'+argv[1]+'.'+argv[2]+'">'+((argv[3]) ? argv[3] : argv[0]+'@'+argv[1]+'.'+argv[2])+'</a>';
}

// Style switch functions

function changeFontSize(change) {
	var styles = new Array('font-small', 'font-medium', 'font-large');
	var keys = {'font-small' : 0, 'font-medium' : 1, 'font-large' : 2};

	current_size = getActiveStyleSheet();
	if(current_size == null) {
		setActiveStyleSheet('font-small');
		current_size = getActiveStyleSheet();		
	}
	
	size_number = keys[current_size];
		
	if(change == 'increase') {
		new_size = (size_number + 1);
		if(new_size > (styles.length - 1)) new_size = (styles.length - 1);
	}	
	else if(change == 'decrease') {
		new_size = (size_number - 1);		
		if(new_size < 0) new_size = 0;
	}
	
	new_style = styles[new_size];	
	setActiveStyleSheet(new_style);
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

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 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;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);