/*
* labels
*
**************************************/

var brandList = new Array();
var subList = new Array();

$("ul.tabListB01 li a").hover(
	function(){
		if(!$(this).hasClass("stay")){
			if($(this).parent("li").attr("id") == "allButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_04_o.gif) no-repeat left top");
			} else if($(this).parent("li").attr("id") == "mensButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_03_o.gif) no-repeat left top");
			} else if($(this).parent("li").attr("id") == "kidsButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_02_o.gif) no-repeat left top");
			} else if($(this).parent("li").attr("id") == "othersButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_04_o.gif) no-repeat left top");
			} else if($(this).parent("li").attr("id") == "artsButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_07_o.gif) no-repeat left top");
			} else {
				$(this).css("background", "url(/shared/img/bg_tabButton_06_o.gif) no-repeat left top");
			}
		}
	},
	function(){
		if(!$(this).hasClass("stay")){
			$(this).css("background", "none");
		}
	}
)
	.click(function(){
		if(!$(this).hasClass("stay")){
			$("ul.tabListB01 li a")
				.removeClass("stay")
				.css("background", "none");
			$(this).addClass("stay");
			if($(this).parent("li").attr("id") == "allButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_04_s.gif) no-repeat left top");
				$("#mensBlock").show();
				$("#womensBlock").show();
				$("#kidsBlock").show();
				$("#othersBlock").show();
				$("#beamsartsBlock").show();
				$("#servicesBlock").show();
				$("#giftcardBlock").show();
			} else if($(this).parent("li").attr("id") == "mensButton"){
				$(this)	.css("background", "url(/shared/img/bg_tabButton_03_s.gif) no-repeat left top");
				$("#mensBlock").show();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if($(this).parent("li").attr("id") == "womensButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").show();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if($(this).parent("li").attr("id") == "kidsButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_02_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").show();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if($(this).parent("li").attr("id") == "othersButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_04_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").show();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if($(this).parent("li").attr("id") == "artsButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_07_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").show();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if($(this).parent("li").attr("id") == "servicesButton"){
				$(this).css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").show();
				$("#giftcardBlock").hide();
			} else {
				$(this).css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").show();
			}
		}
		return false;
	});

function lowersort(a, b){
	a1 = a.charAt(0);
	b1 = b.charAt(0);
	if(a1 == "±" || a1 == "!" || a1 == '"' || a1 == "#" || a1 == "$" || a1 == "%" || a1 == "&" || a1 == "'" || a1 == "(" || a1 == ")" || a1 == "*" || a1 == "+" || a1 == "-" || a1 == "." || a1 == "/" || a1 == "<" || a1 == ">" || a1 == "?" || a1 == "@"){
		return true;
	} else if (b1 == "±" || b1 == "!" || b1 == '"' || b1 == "#" || b1 == "$" || b1 == "%" || b1 == "&" || b1 == "'" || b1 == "(" || b1 == ")" || b1 == "*" || b1 == "+" || b1 == "-" || a1 == "." || b1 == "/" || b1 == "<" || b1 == ">" || b1 == "?" || b1 == "@"){
		return false;
	} else {
		return a.toLowerCase() > b.toLowerCase();
	}
}

function initBrand(){
	i = 0;
	$(".brandBlockA01 ul li").each(function(){
		brandList[i] = $(this).text();
		i++;
	});
	brandList.sort(lowersort);
	
	for(i=0; i<brandList.length; i++){
		if(brandList[i].charAt(0) >= 0 && brandList[i].charAt(0) <= 9){
			if(!subList["num"]){
				subList["num"] = new Array();
			}
			subList["num"].push(brandList[i]);
		} else if((brandList[i].charCodeAt(0) >= 65 && brandList[i].charCodeAt(0) <= 90) || (brandList[i].charCodeAt(0) >= 97 && brandList[i].charCodeAt(0) <= 122)){
			if(!subList[brandList[i].charAt(0).toUpperCase()]){
				subList[brandList[i].charAt(0).toUpperCase()] = new Array();
			}
			subList[brandList[i].charAt(0).toUpperCase()].push(brandList[i]);
		} else {
			if(!subList["other"]){
				subList["other"] = new Array();
			}
			subList["other"].push(brandList[i]);
		}
	}

	$("#brandListTitle").after('<ul class="tabListC01">\
		<li id="all" class="stay">ALL</li>\
	</ul>');
	if(subList["num"]){
		$("ul.tabListC01").append("<li>0</li>");
	}
	for(i in subList){
		if(i != "num" && i != "other") {
			$("ul.tabListC01").append("<li>" + i + "</li>");
		}
	}
	if(subList["other"]){
		$("ul.tabListC01").append("<li>#</li>");
	}
	
	var array = new Array();
	$.each(brandList, function(i,val){
		array[i] = brandList[i];
	});
	generateColumn(array);
	
	$(".tabListC01 li").click(function(){
		array = [];
		if($(this).text() == "ALL"){
			$.each(brandList, function(i,val){
				array[i] = brandList[i];
			});
			generateColumn(array);
		} else if($(this).text() == "#"){
			$.each(subList["other"], function(i,val){
				array[i] = subList["other"][i];
			});
			generateColumn(array);
		} else if($(this).text() == "0"){
			$.each(subList["num"], function(i,val){
				array[i] = subList["num"][i];
			});
			generateColumn(array);
		} else {
			var char = $(this).text()
			$.each(subList[char], function(i,val){
				array[i] = subList[char][i];
			});
			generateColumn(array);
		}
		setStay($(this).text());
	});
}

function setStay(char){
	$("ul.tabListC01 li").removeClass("stay");
	$("ul.tabListC01 li").each(function(){
		if($(this).text() == char){
			$(this).addClass("stay");
		}
	});
}

function generateColumn(array){
	var currentArray = new Array();
	currentArray = array;
	var lineNum = Math.floor(array.length / 4);
	var lineRest = array.length % 4;
	
	var line = new Array();
	line[1] = new Array();
	firstNum = lineRest > 0 ? lineNum + 1 : lineNum;
	for(i=0; i<firstNum; i++){
		line[1].push(array.shift());
	}
	line[2] = new Array();
	secondNum = lineRest > 1 ? lineNum + 1 : lineNum;
	for(i=0; i<secondNum; i++){
		line[2].push(array.shift());
	}
	line[3] = new Array();
	thirdNum = lineRest > 2 ? lineNum + 1 : lineNum;
	for(i=0; i<thirdNum; i++){
		line[3].push(array.shift());
	}
	line[4] = array;
	for(i=1; i<5; i++){
		selector = ".brandBlockA01 .col" + i;
		$(selector).empty();
		$(selector).append("<ul></ul>");
		selector += " ul";
		for(j=0; j<line[i].length; j++){
			$(selector).append("<li>" + line[i][j] + "</li>");
		}
	}
}

$(document).ready(function(){
	if(document.URL.indexOf("?cat=") != -1){
		var category = document.URL.substr(document.URL.indexOf("?cat=") + 5, 1);
		if(category >= 1 && category <= 7){
			$("ul.tabListB01 li a")
				.removeClass("stay")
				.css("background", "none");
			if(category == 1){
				$("ul.tabListB01 li#mensButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_03_s.gif) no-repeat left top");
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if(category == 2){
				$("ul.tabListB01 li#womensButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if(category == 3){
				$("ul.tabListB01 li#kidsButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_02_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if(category == 4){
				$("ul.tabListB01 li#othersButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_04_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if(category == 5){
				$("ul.tabListB01 li#artsButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_07_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#servicesBlock").hide();
				$("#giftcardBlock").hide();
			} else if(category == 6){
				$("ul.tabListB01 li#servicesButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#giftcardBlock").hide();
			} else {
				$("ul.tabListB01 li#cardButton a")
					.addClass("stay")
					.css("background", "url(/shared/img/bg_tabButton_06_s.gif) no-repeat left top");
				$("#mensBlock").hide();
				$("#womensBlock").hide();
				$("#kidsBlock").hide();
				$("#othersBlock").hide();
				$("#beamsartsBlock").hide();
				$("#servicesBlock").hide();
			}
		}
	}

	if($(".brandBlockA01").length){
		initBrand();
	}
});