//array and variable library 
Men = new Array(new Array("Small", "Medium", "Large", "XLarge", "XXLarge", "XXXLarge"),
				new Array("Short Sleeve", new Array("Organic Natural", "Blue Spruce Green")),
				new Array("Long Sleeve", new Array("Dark Orange", "Slate Purple")),
				new Array("Hoodie", new Array("Yam Orange", "Grass Green"))
				);
Women = new Array(new Array("XSmall", "Small", "Medium", "Large", "XLarge", "XXLarge"),
				new Array("Short Sleeve", new Array("Organic Natural", "Seafoam Green")),
				new Array("Long Sleeve", new Array("Distinct Red", "Lilac")),
				new Array("Hoodie", new Array("Seafoam Green", "Raspberry Pink"))
				);
Children = new Array(new Array("XXSmall", "XSmall", "Small", "Medium", "Large"),
				new Array("Short Sleeve", new Array("Organic Natural", "Kiwi Green")),
				new Array("Long Sleeve", new Array("Distinct Red", "Distinct Purple"))
				);
Infant = new Array(new Array("3 to 6 month", "6 to 12 month"),
				new Array("Onesie", new Array("Organic Natural"))
				);							
Designs = new Array("Eat More Kale", "EMK Micro", "EMK Grafitti", "Cheese", "Foodie", "Compost", "Farm", "Free Range", "Home Brewed", "Home Grown", "Localvore", "Organic");
color = new Array("<a class='carts colors' id='","' href='nojs.html'><img class='swatches' src='swatches/",".gif' width='30px' height='30px' alt='' /></a>");
size = new Array("<option value='","'>","</option>");
bgImageCode = new Array("url('./products/",".gif')");
layerArray = new Array("products/","layer.gif");
designPics = new Array('<a class="carts design" id="','" href="nojs.html"><img src="products/','.gif" width="100px" height="50px" alt="" /></a>');
radioShort = '<input type="radio" name="style" value="Short Sleeve" checked="checked" /> Short Sleeve ';
radioLong =  '<input type="radio" name="style" value="Long Sleeve" /> Long Sleeve ';
radioHoodie = '<input type="radio" name="style" value="Hoodie" /> Hoodie ';
onlyOnesie = '<input type="hidden" name="style" value="Onesie" /> Onesie Only ';
fwd = '<a class="carts" id="fwd" href="nojs.html"><img class="noBorder" src="includies/fwd.gif" width="75px" height="50px" alt="forward" /></a>';
bck = '<a class="carts" id="bck" href="nojs.html"><img class="noBorder" src="includies/bck.gif" width="75px" height="50px" alt="backward" /></a>';
spit = "x";
which = "y";
barpics = "";
page = 1;
maxPage = 1;
counted = Designs.length;
if (counted <= 5){maxPage = 1;} else if (counted <= 10){maxPage = 2;} else {maxPage = Math.ceil(((counted - 10)/4)+2);};

//
//fwd and bck buttons
//

function fwdArrow(){
	$("#shirtPics").empty();
	barpics = "";
		if (page == 1)
			{for (i=5;i<=8;i++)
				{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
				barpics = barpics + spit;
				};
			endArrow(bck, barpics, fwd, true);
			}
		else if ((page+1) == maxPage)
			{var howManyLeft = counted - (((page-1)*4)+6);
			var startHere = counted - howManyLeft - 1;
			for (i=startHere;i<=(counted - 1);i++)
				{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
				barpics = barpics + spit;
				};
			endArrow(bck, barpics, "", true);
			}
		else 
			{for (i=(5+(page-1)*4);i<=(8+(page-1)*4);i++)
				{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
				barpics = barpics + spit;
				};
			endArrow(bck, barpics, fwd, true);
			};									
layerClick();
};

function bckArrow(){
$("#shirtPics").empty();
	barpics = "";
		if ((page-1) == 1){
			for (i=0;i<=4;i++)
				{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
				barpics = barpics + spit;
				};
			endArrow("", barpics, fwd, false);
		}
		else 
			{for (i=(1+(page-2)*4);i<=(4+(page-2)*4);i++)
				{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
				barpics = barpics + spit;
				};
			endArrow(bck, barpics, fwd, false);
			};									
layerClick();
};

function endArrow(fst, scn, trd, frt){
	$("#shirtPics").append(fst, scn, trd);
	if (frt == true){page++;} else {page--};
	$("#fwd").click(function(e){e.preventDefault();fwdArrow();});
	$("#bck").click(function(e){e.preventDefault();bckArrow();});
};

//
//function to reinstall the designs after having selected infant
//

function uninfant(){
	if (!($('a[id="fwd"]').length > 0)){
		barpics = "";
		$("#shirtPics").empty();
		for (i=0;i<=4;i++)
			{spit = designPics[0] + Designs[i] +  designPics[1] + Designs[i].toLowerCase().replace(/\s/g,'') + designPics[2];
			barpics = barpics + spit;
			};
		page = 0;
		endArrow("", barpics, fwd, true);
	};
	layerClick();
};

//
//function used to change background pic of shirt
//

function picChanger(){	
	spit = bgImageCode[0] + $("#whoWears").text().toLowerCase() + $("#whatKind").text().toLowerCase().replace(/\s/g,'') + $("#whatColor").text().toLowerCase().replace(/\s/g,'') + bgImageCode[1];
	$("#shirtPanel").css('background-image', spit);
	toRealPic = 'realphotos/' + $("#whoWears").text().toLowerCase() + $("#whatKind").text().toLowerCase().replace(/\s/g,'') + $("#whatPic").text().toLowerCase().replace(/\s/g,'') + ".jpg";
	$("a#realPic").attr('href', toRealPic);
};
	
//	
//design bar behaviors
//

function layerClick(){
	$("a.design").click(function(e) {e.preventDefault();});
	$("a.design").click(
		function(){
			spit = layerArray[0] + ($(this).attr("id")).toLowerCase().replace(/\s/g,'') + layerArray[1];
			$("#shirtPanel img").attr("src",spit);
			$("#whatPic").text($(this).attr("id"));
			$("#hdesign").attr('value', $(this).attr("id"));
			toRealPic = 'realphotos/' + $("#whoWears").text().toLowerCase() + $("#whatKind").text().toLowerCase().replace(/\s/g,'') + $("#whatPic").text().toLowerCase().replace(/\s/g,'') + ".jpg";
			$("a#realPic").attr('href', toRealPic);
		}
	);
	
}
	
//	
//function for use by cut button
//changes description, and form values for cut and color to default
//

function cutChange(kind, size, whose, color){
	$("#whoWears").text(whose);
	$("#whatKind").text(kind);
	$("#whatSize").text(size);
	$("#whatColor").text(color);
	$("#hcut").attr('value', whose);
	$("#hcolor").attr('value', color);
	if (kind == "Short Sleeve"){$("#howMuch").text('$25');} 
	else if (kind == "Long Sleeve"){$("#howMuch").text('$28');} 	
	else if (kind == "Hoodie"){$("#howMuch").text('$45');} 
	else{$("#howMuch").text('$20');};
	picChanger();
};
	
//	
//empties colors and sizes, repopulates with new colors for cut and style
//
	
function fixColorSize(whose, where){
			$('#colorBar').empty();
			len = whose[where][1].length;
			for (i=0;i<len;i++)
				{
				scrap = color[0] + whose[where][1][i] + color[1] + whose[where][1][i].toLowerCase().replace(/\s/g,'') + color[2];
				$('#colorBar').append(scrap);
				}; 
			$('#size').empty();
			len = whose[0].length;
			for (i=0;i<len;i++)
				{
				scrap = size[0] + whose[0][i] + size[1] + whose[0][i] + size[2];
				$('#size').append(scrap);
				picChanger();
				}; 
};	

//
//color swatch behaviors
//

function colorClick(){
	$("a.carts").click(function(e) {e.preventDefault();});
	$("a.colors").click(
		function(){
			spit=($(this).attr("id"));
			$("#hcolor").attr('value', spit);
			$("#whatColor").text(spit);
			picChanger();
		}
	);
};

//
//radio button behaviors
//

function radioClick(which){		
	$("a.carts").click(function(e) {e.preventDefault();});
	$("#choices input").click(
		function(){
			spit=($(this).attr("value"));
			$("#whatKind").text(spit);
			if (spit == "Short Sleeve")
				{fixColorSize(which,1);
				$("#hcolor").attr('value', which[1][1][0]);
				$("#whatColor").text(which[1][1][0]);
				$("#howMuch").text('$25');
				picChanger();
				};
			if (spit == "Long Sleeve")
				{fixColorSize(which,2);
				$("#hcolor").attr('value', which[2][1][0]);
				$("#whatColor").text(which[2][1][0]);
				$("#howMuch").text('$28');
				picChanger();
				};
			if (spit == "Hoodie")
				{fixColorSize(which,3);
				$("#hcolor").attr('value', which[3][1][0]);
				$("#whatColor").text(which[3][1][0]);
				$("#howMuch").text('$45');
				picChanger();
				};
			colorClick();	
		}
	);
};

//start jQuery object
//disable all cart links
//set radio and drop down in case of back button
	
$(function(){		
	$("a.carts").click(function(e) {e.preventDefault();});
	document.BosCart.style[0].checked = true; 
	$("option[value='Small']").attr("selected", "selected");

//cut button behaviors	
//checks for button pushed 
//adds radio buttons
//changes description and form values for cut and color via cutChange()
//empties and repopulates color and size via fixColorSize()
//refreshes radio button function
//refreshes color swatch function

	$("#cut a").click(
		function(){
			$("#cut a").removeClass("thisButton");
			spit=($(this).text());
			$(this).addClass("thisButton");
			switch(spit)
			 {case "Men": which = Men; 
				uninfant();
				$('#stylie').empty().append(radioShort, radioLong, radioHoodie); 
				cutChange("Short Sleeve","Small","Men","Organic Natural");
				fixColorSize(Men,1);
				break;
			 case "Women": which = Women; 
				uninfant();
				$('#stylie').empty().append(radioShort, radioLong, radioHoodie); 
				cutChange("Short Sleeve","XSmall","Women","Organic Natural");
				fixColorSize(Women,1);
				break;
			 case "Children": which = Children; 
				uninfant();
				$('#stylie').empty().append(radioShort, radioLong); 
				cutChange("Short Sleeve","XXSmall","Children","Organic Natural");
				fixColorSize(Children,1);
				break;
			 case "Infant": which = Infant; 
				$("#shirtPics").empty().append(designPics[0] + Designs[0] +  designPics[1] + Designs[0].toLowerCase().replace(/\s/g,'') + designPics[2]);
				$("#shirtPanel img").attr("src",layerArray[0] + Designs[0].toLowerCase().replace(/\s/g,'') + layerArray[1]);
				$("#whatPic").text(Designs[0]);
				$("#hdesign").attr('value', Designs[0]);
				$('#stylie').empty().append(onlyOnesie); 
				cutChange("Onesie","3 to 6 month","Infant","Organic Natural");
				fixColorSize(Infant,1);
				break;};
			radioClick(which);
			colorClick();
		});
		
//defaults layer button behavior
	layerClick();		

//defaults the fwd button
	$("#fwd").click(function(){fwdArrow();});	
	
//style behaviors defaulted to men
	radioClick(Men);
	
//size bar script	
	$("#choices select").change(
		function(){
			spit = ($(this).attr("value"));
			$("#whatSize").text(spit);
		}
	);	
		
//color bar behavior instantiation	
	colorClick();	

//hover for cart buttons
	$("img[src='includies/cart.gif']").hover(
		function () {$(this).attr("src", "includies/carthover.gif");}, 
		function () {$(this).attr("src", "includies/cart.gif");}
	);
	$("input[src='includies/add.gif']").hover(
		function () {$(this).attr("src", "includies/addhover.gif");}, 
		function () {$(this).attr("src", "includies/add.gif");}
	);	
	
});	
