window.onload = page_init;



/*************************************************************************
** PAGE_INIT *************************************************************
*************************************************************************/

function page_init() {

/*------------------------------------------------------------------------
-- ALL DIVS --------------------------------------------------------------
------------------------------------------------------------------------*/

	div_arr = document.getElementsByTagName('div');

	for (i = 0; i < div_arr.length; i++) {

/*----------- VIEWER ---------------------------------------------------*/	

		if (div_arr[i].className == 'viewer' || div_arr[i].className == 'viewer_visible') {


/*------------------- ALL IMAGES ---------------------------------------*/

			images_arr = div_arr[i].getElementsByTagName('img');

			images_arr[0].onclick = page_next;


/*------------------- EXCEPTIONS ---------------------------------------*/

			if (div_arr[i].id == 'schambra0') {

				div_arr[i].style.visibility	= 'visible';
			}

			if (div_arr[i].id == 'collection0') {

				div_arr[i].style.visibility 	= 'visible';
			}
		}
	}

/*------------------------------------------------------------------------
-- ALL TABLES ------------------------------------------------------------
------------------------------------------------------------------------*/

	table_arr = document.getElementsByTagName("table");

	for (i = 0; i < table_arr.length; i++) {

/*----------- PRODUCT --------------------------------------------------*/

		if (table_arr[i].className == 'product') {

/*------------------- ALL ANCHORS --------------------------------------*/

			anchor_arr = table_arr[i].getElementsByTagName("a");
			
			for (j = 0; j < anchor_arr.length; j++) {			
				
				if (anchor_arr[j].parentNode.id == "product_image") {

					anchor_arr[j].onclick = set_enlargement;

				} else if (anchor_arr[j].parentNode.className == "product_thumbs") {

					anchor_arr[j].onclick = set_image;

				} else if (anchor_arr[j].parentNode.className == "product_select") {

					anchor_arr[j].onclick = set_product;

				}
			}
		}
	}

/*------------------------------------------------------------------------
-- ALL BUTTONS -----------------------------------------------------------
------------------------------------------------------------------------*/

	button_arr = document.getElementsByTagName('button');

	for (i = 0; i < button_arr.length; i++) {

		if (button_arr[i].className == 'viewer_next') {

			button_arr[i].onclick = page_next;
			
		}

		if (button_arr[i].className == 'viewer_previous') {

			button_arr[i].onclick = page_previous;

		}

		if (button_arr[i].id == 'submit_add_id') {

			button_arr[i].onclick = cart_validator;

		}
	}

/*------------------------------------------------------------------------
-- ALL ANCHORS -----------------------------------------------------------
------------------------------------------------------------------------*/

	anchor_arr = document.getElementsByTagName("a");
			
	for (i = 0; i < anchor_arr.length; i++) {
		if (anchor_arr[i].className == "blank_page") {
			anchor_arr[i].target = "_blank";
		}
	}

	if (document.getElementById("mail_service")) {
		spanService = document.getElementById("mail_service")
		spanService.innerHTML = " at <a href='mailto:service@schambra.com' title='e-mail to service@schambra.com'>service@schambra.com</a>";
	}
	if (document.getElementById("mail_hello")) {
		spanService = document.getElementById("mail_hello")
		spanService.innerHTML = "<br /><a href='mailto:hello@schambra.com' title='e-mail to hello@schambra.com'>hello@schambra.com</a>";
	}
	if (document.getElementById("mail_hello2")) {
		spanService = document.getElementById("mail_hello2")
		spanService.innerHTML = ".<a href='mailto:hello@schambra.com' title='e-mail to hello@schambra.com'>HELLO@SCHAMBRA.COM</a>";
	}

}




/*************************************************************************
** PAGE_PREVIOUS *********************************************************
*************************************************************************/

function page_previous() {
	parent_obj	= this.parentNode.parentNode.parentNode;

	if (parent_obj.id.substring(0, 8) == "schambra") {
		if (parent_obj.id == "schambra0") {
			previous_obj = document.getElementById("schambra13");
		} else {
			previous_id = Number(parent_obj.id.substring(8)) - 1;
			previous_obj = document.getElementById('schambra' + previous_id);
		}
	} else if (parent_obj.id.substring(0, 10) == "collection") {
		if (parent_obj.id == "collection0") {
			previous_obj = document.getElementById("collection7");
		} else {
			previous_id = Number(parent_obj.id.substring(10)) - 1;
			previous_obj = document.getElementById("collection" + previous_id);
		}
	}
	previous_obj.style.visibility 	= "visible";
	parent_obj.style.visibility	= "hidden";
	return false;
}



/*************************************************************************
** PAGE_NEXT *************************************************************
*************************************************************************/

function page_next() {

	if (this.src) {
		parent_obj	= this.parentNode;
	} else {
		parent_obj	= this.parentNode.parentNode.parentNode;
	}

	if (parent_obj.id.substring(0, 8) == "schambra") {
		if (parent_obj.id == "schambra13") {
			next_obj = document.getElementById("schambra0");
		} else {
			next_id = Number(parent_obj.id.substring(8)) + 1;
			next_obj = document.getElementById("schambra" + next_id);
		}
	} else if (parent_obj.id.substring(0, 10) == "collection") {
		if (parent_obj.id == "collection7") {
			next_obj = document.getElementById("collection0");
		} else {
			next_id = Number(parent_obj.id.substring(10)) + 1;
			next_obj = document.getElementById("collection" + next_id);
		}
	}
	next_obj.style.visibility 	= "visible";
	parent_obj.style.visibility 	= "hidden";
	return false;
}



/*************************************************************************
** SET_IMAGE *************************************************************
*************************************************************************/

function set_image() {

	images_arr				= this.getElementsByTagName('img');

	for (i = 0; i < images_arr.length; i++) {
		if (images_arr[i].className != "product_inactive") {
			image_str = images_arr[i].src.substring(images_arr[i].src.lastIndexOf('/') + 1);
			image_id = image_str.substring(0, image_str.length - 10);
		}
	}

	image_parent			= document.getElementById('product_image');
	img_arr 			= image_parent.getElementsByTagName('img');
	for (i = 0; i < img_arr.length; i++) {
		if (img_arr[i].className == 'product_active') {
			img_arr[i].className = 'product_inactive';
			break;
		}
	}
	image_obj 			= document.getElementById(image_id);
	image_obj.className		= 'product_active';

	enlargement_parent		= document.getElementById('img_enlargement');
	enlargement_arr 		= enlargement_parent.getElementsByTagName('img');
	for (j = 0; j < enlargement_arr.length; j++) {
		if (enlargement_arr[j].className == 'product_active') {
			enlargement_arr[j].className = 'product_inactive';
			break;
		}
	}
	enlargement_img			= document.getElementById(image_id + '_large');
	enlargement_img.className	= 'product_active';
	
	return false;
}



/*************************************************************************
** SET_PRODUCT ***********************************************************
*************************************************************************/

function set_product() {

	product_str	= this.href.substring(this.href.lastIndexOf("/") + 1, this.href.lastIndexOf("."));
	product_str	= product_str.replace("-", "");
	
	product_id 	= this.href.substring(this.href.lastIndexOf("=") + 1);
	current_id	= document.getElementById("item_id");

	if (product_id != current_id.value && product_id != 15) {

		image_arr	= document.getElementsByTagName("img");

		inactivate_images ();

		for (i = 0; i < image_arr.length; i++) {

			if (product_id == 0) {
				thumb_inactive (image_arr[i], product_str, "01_beige", "01_brown", "04_brown");
				product_active (image_arr[i], product_str, "01_white");
				product_active (image_arr[i], product_str, "04_light");
			}
			if (product_id == 1) {
				thumb_inactive (image_arr[i], product_str, "01_white", "01_brown", "04_brown");
				product_active (image_arr[i], product_str, "01_beige");
				product_active (image_arr[i], product_str, "04_light");
			}
			if (product_id == 2) {
				thumb_inactive (image_arr[i], product_str, "01_white", "01_beige", "04_light");
				product_active (image_arr[i], product_str, "01_brown");
				product_active (image_arr[i], product_str, "04_brown");
			}

			if (product_id == 3 || product_id == 7) {
				thumb_inactive (image_arr[i], product_str, "01_beige", "01_canvas", "01_brown", "04_grey", "04_brown");
				product_active (image_arr[i], product_str, "01_white");
				product_active (image_arr[i], product_str, "04_croc");
			}
			if (product_id == 4 || product_id == 8) {
				thumb_inactive (image_arr[i], product_str, "01_white", "01_canvas", "01_brown", "04_grey", "04_brown");
				product_active (image_arr[i], product_str, "01_beige");
				product_active (image_arr[i], product_str, "04_croc");
			}
			if (product_id == 5 || product_id == 9) {
				thumb_inactive (image_arr[i], product_str, "01_white", "01_beige", "01_brown", "04_croc", "04_brown");
				product_active (image_arr[i], product_str, "01_canvas");
				product_active (image_arr[i], product_str, "04_grey");
			}
			if (product_id == 6 || product_id == 10) {
				thumb_inactive (image_arr[i], product_str, "01_white", "01_beige", "01_canvas", "04_croc", "04_grey");
				product_active (image_arr[i], product_str, "01_brown");
				product_active (image_arr[i], product_str, "04_brown");
			}

			if (product_id == 11) {
				thumb_inactive (image_arr[i], product_str, "01_striped");
				product_active (image_arr[i], product_str, "01_fuchsia");
			}
			if (product_id == 12) {
				thumb_inactive (image_arr[i], product_str, "01_fuchsia");
				product_active (image_arr[i], product_str, "01_striped");
			}
			if (product_id == 13) {
				thumb_inactive (image_arr[i], product_str, "01_gunmetal");
				product_active (image_arr[i], product_str, "01_gold");
			}
			if (product_id == 14) {
				thumb_inactive (image_arr[i], product_str, "01_gold");
				product_active (image_arr[i], product_str, "01_gunmetal");
			}
		}
	
		if (document.getElementById("price_id")) {
			price_obj	= document.getElementById("price_id");

			if (product_id == 3 || product_id == 4 || product_id == 6) {
				price_obj.innerHTML = "650.00";
			}
			if (product_id == 5) {
				price_obj.innerHTML = "495.00";
			}
			if (product_id == 7 || product_id == 8 || product_id == 10) {
				price_obj.innerHTML = "750.00";
			}
			if (product_id == 9) {
				price_obj.innerHTML = "595.00";
			}
		}

		anchor_arr	= document.getElementsByTagName("a");
		for (i = 0; i < anchor_arr.length; i++) {
			if (anchor_arr[i].parentNode.className == "product_select") {
				anchor_arr[i].className = "sample_inactive";
			}
		}
		this.className = "sample_active";
	}

	current_id.value	= product_id;
	return false;
}



/*************************************************************************
** INACTIVATE_IMAGES *****************************************************
*************************************************************************/

function inactivate_images () {
	image_parent			= document.getElementById("product_image");
	img_arr 			= image_parent.getElementsByTagName("img");
	for (j = 0; j < img_arr.length; j++) {
		if (img_arr[j].className == "product_active") {
			img_arr[j].className = "product_inactive";
		}
	}
	enlargement_parent		= document.getElementById("img_enlargement");
	enlargement_arr 		= enlargement_parent.getElementsByTagName("img");
	for (k = 0; k < enlargement_arr.length; k++) {
		if (enlargement_arr[k].className == "product_active") {
			enlargement_arr[k].className = "product_inactive";
		}
	}
}



/*************************************************************************
** THUMB_INACTIVE ********************************************************
*************************************************************************/

function thumb_inactive (obj, product_str, material_str0, material_str1, material_str2, material_str3, material_str4) {

	obj_src 	= obj.src.substring(obj.src.lastIndexOf("/") + 1);

	if (obj_src == product_str + material_str0 + "_thumb.jpg") {
		obj.className = "product_inactive";
	}
	if (obj_src == product_str + material_str1 + "_thumb.jpg") {
		obj.className = "product_inactive";
	}
	if (obj_src == product_str + material_str2 + "_thumb.jpg") {
		obj.className = "product_inactive";
	}
	if (obj_src == product_str + material_str3 + "_thumb.jpg") {
		obj.className = "product_inactive";
	}
	if (obj_src == product_str + material_str4 + "_thumb.jpg") {
		obj.className = "product_inactive";
	}
}



/*************************************************************************
** PRODUCT_ACTIVE ********************************************************
*************************************************************************/

function product_active (obj, product_str, material_str) {
	type_arr	= new Array("_thumb", "", "_large");
	obj_src 	= obj.src.substring(obj.src.lastIndexOf("/") + 1);

	for (l = 0; l < type_arr.length; l++) {
		
		if (obj_src == product_str + material_str + type_arr[l] + ".jpg") {
			if (material_str.substring(0, 2) == "04") {
				if (type_arr[l] == "_thumb") {
					obj.className = "product_active";
				}
			} else {
				obj.className = "product_active";
			}
		}
	}
}


/*************************************************************************
** SET_ENLARGEMENT *******************************************************
*************************************************************************/

function set_enlargement () {

	enlargement_parent			= document.getElementById('img_enlargement');
	enlargement_parent.style.display	= "block";
	enlargement_parent.onclick		= remove_enlargement;
	return false;
}



/*************************************************************************
** REMOVE_ENLARGEMENT ****************************************************
*************************************************************************/

function remove_enlargement () {

	this.style.display 		= "none";
	return false;
}



/*************************************************************************
** CART_VALIDATOR ********************************************************
*************************************************************************/

function cart_validator () {

	current_id			= document.getElementById("item_id");
	soldout_obj			= document.getElementById('soldout_id');

	if (soldout_obj.value.length > 0) {
		soldout_arr = soldout_obj.value.split("|");
		for (i = 0; i < soldout_arr.length; i++) {
			if (soldout_arr[i] == current_id.value) {
				alert("This specific product is currently sold out.", "lala");
				return false;
			}
		}
	}

}



/*************************************************************************
** GO_BACK ***************************************************************
*************************************************************************/

function go_back () {

	history.go(-1);
	return false;
}






function cart_cookie () {

	cookie_name = "schambra_cart";
	var varss;

	if(document.cookie) {

		index 		= document.cookie.indexOf("schambra_cart");
		start_str	= (document.cookie.indexOf("=", index) + 1);
		end_str		= document.cookie.indexOf(";", index);
		if (end_str == -1) {
			end_str = document.cookie.length;
		}
		shopping_cart	= document.cookie.substring(start_str, end_str);


	} else {
		var shopping_cart
		document.cookie = "schambra_cart="+shopping_cart;
		index 		= document.cookie.indexOf(cookie_name);


	}

}