var divs_products = new Array();
function products()
{
	// Pobierz wszystkie DIVy
	var divs = document.getElementsByTagName("DIV");

	// Stworz liste tylko DIVow z produktami
	for( var a in divs )
	{
		var div = divs[a];
		if( div.nodeName == "DIV" && div.getAttribute("id") && div.getAttribute("id").substr(0,8) == "product:" )
		{
			// Ukryj DIVa z danymi produktu
			div.style.display = "none";

			// Wstaw DIVa do listy z produktami
			divs_products.push(div);
		}
	}

	// Wstaw dodatkowe DIVy z informacjami do produktow
	for( var a in divs_products )
	{
		var div = divs_products[a];

		if( typeof div == "object" && div.getAttribute )
		{

			// product_symbol - ustal
			var product_symbol = div.getAttribute("id").substr(8);
			//alert(product_symbol);

			// id - dodaj do DIVa z produktem
			var div_id = document.createElement('DIV');
			div_id.setAttribute("id","id");
			div_id.innerHTML = product_symbol;
			div.appendChild(div_id);

			// symbol - szukaj czy jest zdefiniowany dla tego produktu
			var symbol = "";
			var symbol_defined = false;
			var divs_product = div.getElementsByTagName("DIV");
			for( var b in divs_product )
			{
				var div_product = divs_product[b];
				if( typeof div_product == "object" && div_product.getAttribute && div_product.getAttribute("id") == "symbol" )
				{
					symbol = div_product.innerHTML;
					symbol_defined = true;
					break;
				}
			}

			// symbol - dodaj do DIVa z produktem
			var div_symbol = document.createElement('DIV');
			div_symbol.setAttribute("id","symboll");
			symbol_defined
				? div_symbol.innerHTML = symbol
				: div_symbol.innerHTML = product_symbol;
			div.appendChild(div_symbol);

			// name - szukaj czy jest zdefiniowany dla tego produktu
			var name = "";
			var name_defined = false;
			var divs_product = div.getElementsByTagName("DIV");
			for( var b in divs_product )
			{
				var div_product = divs_product[b];
				if( typeof div_product == "object" && div_product.getAttribute && div_product.getAttribute("id") == "name" )
				{
					name = div_product.innerHTML;
					name_defined = true;
					break;
				}
			}

			// name - dodaj do DIVa z nazwa
			if(!name_defined)
			{
				var div_name = document.createElement('DIV');
				div_name.setAttribute("id","name");
				div_name.innerHTML = "&nbsp;";
				div.appendChild(div_name);
			}

			// img - dodaj do DIVa z produktem
			var div_img = document.createElement('DIV');
			div_img.setAttribute("id","img");
			div_img.innerHTML = product_symbol;
			div.appendChild(div_img);

			// click_index - dodaj do DIVa z produktem
			var div_click_index = document.createElement('DIV');
			div_click_index.setAttribute("id","click_index");
			div_click_index.innerHTML = "products_index();";
			div.appendChild(div_click_index);

			// click_details - dodaj do DIVa z produktem
			var div_click_details = document.createElement('DIV');
			div_click_details.setAttribute("id","click_details");
			div_click_details.innerHTML = "products_details("+a+");";
			div.appendChild(div_click_details);

			// colours - szukaj czy sa zdefiniowane kolory dla tego produktu
			var colours = "";
			var divs_product = div.getElementsByTagName("TEXTAREA");
			for( var b in divs_product )
			{
				var div_product = divs_product[b];
				if( typeof div_product == "object" && div_product.getAttribute("id") == "colours" )
				{
					var colours_list = div_product.innerHTML.split("\n");
					for( c in colours_list )
					{
						var colour = colours_list[c].split("|");
						var colour_code = colour[0];
						var colour_name = colour[1];
						if( colour_code && colour_name )
						{
							colours += "<div style='float: left; width: 14px; background-color: "+colour_code+"; border: solid 1px #a0a0a0; margin-right: 10px;'>&nbsp;</div>";
							colours += "<p style='margin-bottom: 3px;'>"+colour_name+"</p>";
						}
					}
				}
			}

			// click_details - dodaj do DIVa z produktem
			var div_colours_list = document.createElement('DIV');
			div_colours_list.setAttribute("id","colours_list");
			div_colours_list.innerHTML = colours;
			div.appendChild(div_colours_list);
			//divs_products[a][colours_div_index].innerHTML = "123";
		}
	}

	// Stworz index produktow
	if( divs_products.length ) products_index();

	// Return
	return;
}


function products_index()
{
	// Result
	var result = "";

	// Body contents - jesli juz jest wygenerowane, wstaw bez generowania indexu produktow
	if( document.getElementById("p_details") && document.getElementById("p_details").innerHTML.length )
	{
		document.getElementById("p_index").style.display='block';
		document.getElementById("p_details").style.display='none';
		return;
	}

	// Pobierz elementy HTML
	var index_body = document.getElementById("products_index_body").innerHTML;
	var index_product = document.getElementById("products_index_product").innerHTML;

	// product_no
	var product_no = 0;
	
	// Przetwarzaj produkty
	for( var a in divs_products )
	{
		if( typeof divs_products[a] == "object" && divs_products[a].getElementsByTagName )
		{
			var divs_product = divs_products[a].getElementsByTagName("DIV");
			var html_product = index_product;
			var replace_info = "";
			for( var b in divs_product )
			{
				var div_product = divs_product[b];
				if( typeof div_product == "object" && div_product.getAttribute && div_product.getAttribute("id") )
				{
					var replace_from = new RegExp("\/\/::"+div_product.getAttribute("id")+"::\/\/","g");
					var replace_to = div_product.innerHTML;
					replace_info += replace_from+"\n";
					//alert(div_product.getAttribute("id")+":"+replace_to);
					html_product = html_product.replace(replace_from,replace_to);
				}
				else
				{
					//if(div_product) alert(div_product.getAttribute);
				}
			}
			//alert(replace_info);

			// usun znaki nowej linii i tabulatory
			html_product = html_product.replace(/\n/g, "" );
			html_product = html_product.replace(/\r/g, "" );
			html_product = html_product.replace(/\t/g, "" );
			//alert( html_product );

			// usun znaczniki, jesli nie zostaly podmienione
			html_product = products_remove_empty_tags(html_product);

			// Wstaw nowa linie
			if( product_no > 3 )
			{
				result += "<br clear='all'/>";
				product_no = 0;
			}
			else
			{
				product_no++;
			}
			result += html_product;
		}
	}
	
	// Polacz index_products z index_body
	//var replace_from = new RegExp(/\/%index%\//g);
	//result = index_body.replace(new RegExp(/\/\/%index%\/\//i),result);
	var replace_from = new RegExp("//::index:://","g");
	result = index_body.replace(replace_from,result);

	// Printuj wynik
	document.write("<div id='p_index'>"+result+"</div>");
	document.write("<div id='p_details' style='display='none'></div>");

	// Return
	return;
}


function products_remove_empty_tags(str)
{
	// usun znaczniki, jesli nie zostaly podmienione
	str = str.replace( /\/\/::name::\/\//g, "" );
	str = str.replace( /\/\/::details::\/\//g, "" );
	str = str.replace( /\/\/::symbol::\/\//g, "" );
	str = str.replace( /\/\/::size::\/\//g, "" );
	return str;
}


function products_details(product_no)
{
	document.getElementById("p_index").style.display='none';
	document.getElementById("p_details").style.display='block';
	var divs_product = divs_products[product_no].getElementsByTagName("DIV");
	var html_details = document.getElementById("products_details").innerHTML;
	for( var b in divs_product )
	{
		var div_product = divs_product[b];
		if( typeof div_product == "object" && div_product.getAttribute && div_product.getAttribute("id") )
		{
			//var replace_from = new RegExp("\/\/%"+div_product.getAttribute("id")+"\/\/%","g");
			var replace_from = new RegExp("\/\/::"+div_product.getAttribute("id")+"::\/\/","g");
			var replace_to = div_product.innerHTML;

			if( div_product.getAttribute("id") == "name" )
			{
				//alert(replace_to);
				//if( String(replace_to).length == 0 ) replace_to = "x";
				replace_to = replace_to.replace(/\n/g,"");
				replace_to = replace_to.replace(/\r/g,"");
				replace_to = replace_to.replace(/\t/g,"");
				replace_to = replace_to.replace(/\s$/g,"");
				replace_to = replace_to.replace(/^\s/g,"");
				replace_to = replace_to.replace(/\s/g,"&nbsp;");
			}

			//alert(div_product.getAttribute("id")+":"+replace_to);
			html_details = html_details.replace(replace_from,replace_to);
		}
	}

	// usun znaczniki, jesli nie zostaly podmienione
	html_details = products_remove_empty_tags(html_details);

	document.getElementById("p_details").innerHTML = html_details;
	//alert(html_details);
	Lightbox.initialize(
		{
			containerId : document.getElementById("p_details")
		}
	);
	return;
}