var cantTabs=0;
var tamContent = new Array();
var tamSubContent = new Array();
var elementID = new Array();
var contentElementID = new Array();
var subElementID = new Array();
var subContentElementID = new Array();
var subContentOpen =  new Array();
var showColors = 0;
var showFinishes = 0;
var activeLink = "";
var path = "";
var valor_inicial = new Array();
var error_validate_sample_request = 0;

var ID = "";
var contentOpen = new Array();
var oldID = "";
var elemento_thumb = new Array();

var ksj = new Array(0,0);
var ksk = new Array(-1,-1);
var ksscrolldown = new Array(7,7);
var ScrollStep = new Array(0,0);
var scrollbar_ks = new Array("scrollbar_lc","scrollbar_f");
var hadle_ks = new Array("handle_lc", "handle_f");
var h_old = 0;
var showks = new Array(0,0);

window.addEvent("resize", function(){
	if($("mainFrame").getSize().y < 640){
		$("mainFrame").setStyle("height", 640);
	}else{
		if(window.getSize().y > 640){
			$("mainFrame").setStyle("height", window.getSize().y);
		}
	}
	
});
window.addEvent("load", function(){
	
	if($("mainFrame").getSize().y < 640){
		$("mainFrame").setStyle("height", 640);
	}
	
	$("greenovative").style.cursor = "url(cursor.cur)";
	
	
	
	
	$$('a').addEvent("click", function(e){
		e.preventDefault();
		$("a_luccon").removeClass("bold");
		$("a_showrooms").removeClass("bold");
		var parent = "";
		if(oldID != ""){
			parent = $(oldID).getParent();
			parent.removeClass("over");
			parent.removeClass("bold");
		}
		
		oldID = this.id;
		
		
		
		if(!this.rel){
				parent = $(this.id).getParent();
				parent.addClass("over bold");
		}
		
		var array_url_1 = new String(this.get("href"));
		array_url = array_url_1.split("/");
		$("greenovative").setStyle("display", "none");
		if(this.rel == "primary"){
			var url = array_url[array_url.length-1];
			if(this.id == "home"){
				hide_menu();
				$("greenovative").setStyle("display", "");
			}
		}else{
			var url = array_url[array_url.length-2]+"/"+array_url[array_url.length-1];
		}
		if(url.trim() != ""){
			$('content').load(url+"?mxID=574372184f1ada4005e6b8156257ee73");
		}
	});
	
	$$(".title").each(function(element, i){
		elementID[i] = "title_"+i;
		$(element).id = elementID[i];
	});
	
	$$(".content").each(function(element, i){
		tamContent[i] = $(element).offsetHeight;
		contentElementID[i] = "content_"+i;
		$(element).id = contentElementID[i];
		contentOpen[i] = 0;
		$(elementID[i]).addEvent("mouseover", function(){
			if(contentOpen[i] == 0){
				$(elementID[i]).setStyle("font-weight","bold");
			}
		}, i);
		$(elementID[i]).addEvent("mouseleave", function(){
			if(contentOpen[i] == 0){
				$(elementID[i]).setStyle("font-weight","normal");
			}
		}, i);
		$(elementID[i]).addEvent("click", function(){
			$(elementID[i]).style.outlineStyle = "none";
  			$(elementID[i]).style.outlineWidth = "0";
			contentOpen.each(function(element, w){
				if(w == i){
					contentOpen[i] = 1;
					var myEffect = new Fx.Morph(contentElementID[i], {duration: 500, transition: Fx.Transitions.linear});
					myEffect.start({"height": tamContent[i]});
					$(elementID[i]).setStyle("font-weight","bold");
				}else{
					contentOpen[w] = 0;
			 		var myEffect = new Fx.Morph(contentElementID[w], {duration: 500, transition: Fx.Transitions.linear});
					myEffect.start({"height": "0"});
					$(elementID[w]).setStyle("font-weight","normal");
				}
			});
			
			for(x=0; x<subContentOpen.length; x++){
				for(w=0; w<subContentOpen[x].length; w++){
					subContentOpen[x][w] = 0;
					var myEffect = new Fx.Morph(subContentElementID[x][w], {duration: 500, transition: Fx.Transitions.linear});
					myEffect.start({"height": "0"});
					$(subElementID[x+"_"+w]).setStyle("font-weight","normal");
				}
			}
			
		}, i);
		$$("#"+"content_"+i+" .subTitle").each(function(subE, j){
			subElementID[i+"_"+j] = "subTitle_"+i+"_"+j; 
			$(subE).id = subElementID[i+"_"+j];
			subContentOpen[i] =  new Array();
			subContentOpen[i][j] = 0;
			tamSubContent[i] =  new Array();
			tamSubContent[i][j] = 0;
			subContentElementID[i] =  new Array();
			subContentElementID[i][j] = 0;
		}, i);
		$$("#"+"content_"+i+" .subContent").each(function(subC, j){
			tamSubContent[i][j] = $(subC).offsetHeight;
			subContentElementID[i][j] = "subContent_"+i+"_"+j;
			$(subC).id = subContentElementID[i][j];
			var myEffect = new Fx.Morph(subContentElementID[i][j], {duration: 500, transition: Fx.Transitions.linear});
			myEffect.start({"height": "0"});
			tamContent[i] = tamContent[i] - tamSubContent[i][j];
			
			$(subElementID[i+"_"+j]).addEvent("mouseover", function(){
				if(subContentOpen[i][j] == 0){
					$(subElementID[i+"_"+j]).setStyle("font-weight","bold");
				}
			}, i);
			$(subElementID[i+"_"+j]).addEvent("mouseleave", function(){
				if(subContentOpen[i][j] == 0){
					$(subElementID[i+"_"+j]).setStyle("font-weight","normal");
				}
			}, i);
			
			$(subElementID[i+"_"+j]).addEvent("click", function(){
				$(subElementID[i+"_"+j]).style.outlineStyle = "none";
	  			$(subElementID[i+"_"+j]).style.outlineWidth = "0";
				for(w=0 ; w < subContentOpen[i].length; w++){
					if(j == w){
						subContentOpen[i][j] = 1;
						var myEffect = new Fx.Morph(subContentElementID[i][j], {duration: 500, transition: Fx.Transitions.linear});
						myEffect.start({"height": tamSubContent[i][j]});
						$(subElementID[i+"_"+w]).setStyle("font-weight","bold");
						
						
						var myEffect = new Fx.Morph(contentElementID[i], {duration: 500, transition: Fx.Transitions.linear});
						myEffect.start({"height": (tamContent[i]+tamSubContent[i][j])});
						
					}else{
						subContentOpen[i][w] = 0;
						var myEffect = new Fx.Morph(subContentElementID[i][w], {duration: 500, transition: Fx.Transitions.linear});
						myEffect.start({"height": "0"});
						$(subElementID[i+"_"+w]).setStyle("font-weight","normal");
					}
				}
				
			}, i);
		}, i);
		
		var myEffect = new Fx.Morph("content_"+i, {duration: 500, transition: Fx.Transitions.linear});
		myEffect.start({"height": "0"});
	});

	showMenu.delay(550);
	
	if($("sizes")){
		changeSizes(category, product, $("txtcolors").value+"_"+$("txtfinishes").value);
	}
	
	new Swiff('images/sound.swf', {
    	id: 'soundControler',
    	width: 20,
	    height: 16,
		container: "soundContent",
    	params: {
        	wmode: 'transparent'
    	}
	});
	
});
function showMenu(){
	$("menusito").setStyle("text-indent","0px");
	$("footer").setStyle("position", "relative");
	$("footer").setStyle("bottom", "0");
}
function hide_menu(){

	contentElementID.each(function(e, i){
		subContentOpen.each(function(el, j){
			if(subContentOpen[i]){
				subContentOpen[i][j] = 0;
				var myEffect = new Fx.Morph(subContentElementID[i][j], {duration: 500, transition: Fx.Transitions.linear});
				myEffect.start({"height": "0"});
				$(subElementID[i+"_"+j]).setStyle("font-weight","normal");
			}
		}, i);
		contentOpen[i] = 0;
		var myEffect = new Fx.Morph("content_"+i, {duration: 500, transition: Fx.Transitions.linear});
		myEffect.start({"height": "0"});
		$(elementID[i]).setStyle("font-weight","normal");
	});

}
function change_colors(obj){
	$("colorName").innerHTML = "";
	$("finishName").innerHTML = "";
	$('selected_colors').src=obj.src;
	//show_colors();
	$("txtcolors").value = obj.alt;
	$("sampleView").src = path+$("txtcolors").value+"_"+$("txtfinishes").value+".jpg";
	//changeSizes(category, product, $("txtcolors").value+"_"+$("txtfinishes").value);
	//go_gallery($("txtcolors").value+"_"+$("txtfinishes").value);
	printColor(category, product, $("txtcolors").value);
}
function show_colors(){
	if(showColors == 0){
		$('listColors').style.display = '';
		$("typelist").style.display = "none";
		$("colorList").style.height = ($("listColors").offsetHeight+20)+"px";
		var my_mxScroll = new mxScroll('contentColors','listColors', false);
		showColors = 1;
		showks[0] = showColors;
	}else{
		$('listColors').style.display = 'none';
		$("typelist").style.display = "";
		$("typelist").style.height = 17+"px";
		$("colorList").style.height = 17+"px";
		showColors = 0;
		showks[0] = showColors;
	}
	if(showFinishes == 1){
		$('listFinishes').style.display = 'none';
		showFinishes = 0;
		showks[1] = showFinishes;
	}
}
var stateKS = new Array(0,0);
function keySelector(content, s, pre){
	ksj[s] = 0;
	$$("#"+content+" div").each(function(element, i){
		element.id = pre+"c"+i;
		ksj[s] = i;
		$$("#"+element.id+" img").each(function(e, w){
			e.id = pre+"c_"+ksj[s]+"_img";
		});
	});
	ksk[s] = -1;
	ksscrolldown[s] = 7;
	ScrollStep[s] = 0;
	showks[s] = 0;	
	if(stateKS[s] == 0){
		document.addEvent('keydown', function(miEvento){
			if(showks[s] == 1){
					if(miEvento.key == "up"){
							ksk[s]--;
							if(ksk[s] >= 0){
								$(pre+"c"+(ksk[s]+1)).setStyle("border", "1px solid #FFF");
								elementoActual = ksk[s];
								$(pre+"c"+ksk[s]).setStyle("border", "2px solid #000");
								if(ksk[s] <= ksscrolldown[s]){
									ScrollStep[s]-=20; 
									$(content).scrollTo(0,ScrollStep[s]);
								}
								step = (steps/ksj[s])*ksk[s];
								slider.set(step);
							}else{
								ksk[s] = -1;
							}
						}
						if(miEvento.key == "down"){
							ksk[s]++;
							if(ksk[s] <= ksj[s]){
								if(ksk[s] != 0){
									$(pre+"c"+(ksk[s]-1)).setStyle("border", "1px solid #FFF");
								}
								elementoActual = ksk[s];
								$(pre+"c"+ksk[s]).setStyle("border", "2px solid #000");
								//console.log("ksk[s]: "+ksk[s]+"    ksscrolldown[s]: "+ksscrolldown[s]);
								if(ksk[s] >= ksscrolldown[s]){
									ScrollStep[s]+=20; 
									$(content).scrollTo(0,ScrollStep[s]);
								}
								step = (steps/ksj[s])*ksk[s];
								slider.set(step);
							}else{
								ksk[s] = ksj[s];
							}
						}
						if(miEvento.key == "enter"){
							filter_by_color($(pre+"c_"+ksk[s]+"_img"));
						}
					}	
		});
		stateKS[s] = 1;
	}
}

function change_finishes(obj){
	$('selected_finishes').src=obj.src;
	$("txtfinishes").value = obj.alt;
	$("sampleView").src = path+$("txtcolors").value+"_"+$("txtfinishes").value+".jpg";
	go_gallery($("txtcolors").value+"_"+$("txtfinishes").value);
	showFinishes = 0;
	$('listFinishes').style.display = 'none';
		$("typelist").style.height = 17+"px";
	printFinish(category, product, $("txtfinishes").value);
	//show_finishes();
}
function filter_by_color(obj){
	$('typelist').load("collections/filter_by_color.php?directorio="+obj.get("alt")+"&collection="+collection+"&category="+category);
	show_colors();
}
function show_finishes(){
	if(showFinishes == 0){
		$('listFinishes').style.display = '';
		$("typelist").style.height = ($("listFinishes").offsetHeight+20)+"px";
		var my_mxScroll = new mxScroll2('contentFinishes','listFinishes', false);
		showFinishes = 1;
		showks[1] = showFinishes;
	}else{
		$('listFinishes').style.display = 'none';
		$("typelist").style.height = 17+"px";
		showFinishes = 0;
		showks[1] = showFinishes;
	}
	if(showColors == 1){
		$('listColors').style.display = 'none';
		showColors = 0;
		showks[0] = showColors;
	}
}

function change_content(url){
	$('content').load(url+"?mxID=574372184f1ada4005e6b8156257ee73");	
}
function changeBG(){
	Swiff.remote(myswf.toElement(), "loadBg", ID);
}
function addLink(){
	if($("listColors")){
		$$("#listColors img").each(function(element, i){
			element.addEvent("click", function(){
				change_colors(element);
			}, element);
			//element.setStyle("cursor", "pointer");
		});
		if($("txtcolors").value.trim != "")
		printColor(category, product, $("txtcolors").value);
	}
	
	if($("listFinishes")){
		$$("#listFinishes img").each(function(element, i){
			element.addEvent("click", function(){
				change_finishes(element);
			}, element);
			//element.setStyle("cursor", "pointer");
		});
	}
	
	
//	printFinish(category, product, $("txtfinishes").value);
}
function printColor(c, p, cf){
	if(cf.trim() != ""){
		new Request.HTML({
			method: 'get', 
			url: "color.php?collection="+c+"&category="+p+"&color_finish="+cf, 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
				$("colorName").innerHTML = responseHTML;
			}
		}).send();
	}
}
function printFinish(c, p, cf){
	if(cf.trim() != ""){
		new Request.HTML({
			method: 'get', 
			url: "finish.php?collection="+c+"&category="+p+"&color_finish="+cf, 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
				$("finishName").innerHTML = responseHTML;
			}
		}).send();
	}
}
function createLinks(content){
	$$('#'+content+' a').addEvent("click", function(e){
		e.preventDefault();
		if(this.rel != "pdf"){
			var array_url_1 = new String(this.href);
			array_url = array_url_1.split("/");
			if(this.rel == "primary" || this.rel == "onContentLeft"){
				var url = array_url[array_url.length-1];
			}else{
				var url = array_url[array_url.length-2]+"/"+array_url[array_url.length-1];
			}
			if(url.trim() != "" && this.rel != "onContentLeft"){
				$('content').load(url+"?mxID=574372184f1ada4005e6b8156257ee73");
			}else{
				$('photo_gallery').load(url+"?mxID=574372184f1ada4005e6b8156257ee73");
			}
		}else{
			window.location=this.href;
		}
	});
}
function fn_search(){
	text = new String($("input_search").value.trim());
	if(text.length > 0){
		$('content').load("search.php?mxID=574372184f1ada4005e6b8156257ee73&to_find="+$("input_search").value);
		$("greenovative").setStyle("display", "none");
	}
}
function changeSizes(collection, category, colorFinish){
	if($("sizes")){
		var returnvalue = "";
		new Request.HTML({
			method: 'get', 
			url: "sizes.php?collection="+collection+"&category="+category+"&color_finish="+colorFinish, 
			onSuccess: function(responseTree, responseElements, responseHTML, responseJavaScript){
				$("sizes").innerHTML = responseHTML;
			}
		}).send();
	}
}
function validate_sample_request(){
	error_validate_sample_request = 0;
	$$("#request_sample input, #request_sample textarea").each(function(element, i){
		if(element.type == "text" || element.type == "textarea"){
			if(element.id != "frm_address2" && element.id != "frm_website" && element.id != "frm_mobile"){	
				if(element.value.trim() == "" || element.value.trim() == valor_inicial[element.id]  || element.value.trim() == valor_inicial[element.id]+"*"){
					$(element.id).value = valor_inicial[element.id]+"*";
					$(element.id).style.color="#F00";
					error_validate_sample_request =  1;
				}else{
					$(element.id).style.color="#FFF";
				}
			}
		}
	});
	if(error_validate_sample_request == 0){
		$("frm_collection").value = product;
		$("frm_address2").value = "";
		$("frm_website").value = "";
		$("frm_mobile").value = "";
		new Request({
			method: 'post', 
			url: $("request_sample").get("action"), 
			data: $("request_sample").toQueryString(), 
			onSuccess: function(){
				$("request_sample").reset();
				$("msn").innerHTML = "Your message has been sent";
				send_page.delay(3000);
			}
		}).send();
	}else{
		$("msn").innerHTML = "Mandatory Fields";
	}
}
function send_page(){
	$('content').load("collections/"+actual_page+"?mxID=574372184f1ada4005e6b8156257ee73");
}
function create_labels(){
	j = 1;
	$$("#request_sample input, #request_sample textarea").each(function(element, i){
		element.setAttribute("tabindex",j);
		if(element.type == "text" || element.type == "textarea"){
			valor_inicial[element.id] = element.value;
			$(element.id).addEvent("blur", function(){
				 if(element.value.trim() == ''){
				 	element.value=valor_inicial[element.id];
				 }
			});
			$(element.id).addEvent("focus", function(){
				if(element.value.trim() == valor_inicial[element.id]  || element.value.trim() == valor_inicial[element.id]+"*"){
					element.value='';
				}
			});
		}
		j++;
	},j);
}
function call_lucon(){
	$("title_1").fireEvent("click");
	$("a_luccon").addClass("bold");
	$('content').load("collections/luccon.php?mxID=574372184f1ada4005e6b8156257ee73");
	//$('content').load("collections/metalgres_sable.php?mxID=574372184f1ada4005e6b8156257ee73");
}
function call_showrooms(){
	$("title_0").fireEvent("click");
	$("a_showrooms").addClass("bold");
	$('content').load("showrooms.php?mxID=574372184f1ada4005e6b8156257ee73");
}
function set_over_thumbspan(){
	el = new Array;
	$$(".thumbspan_3").each(function(element, i){
		element.id = "thumbspan_3_"+i;
		el[i] = element.id;
		$$("#"+el[i]+" .thumbspan_3_div").each(function(e,j){
			e.id = "thumbspan_3_div_"+i;
		},i);
		
		$(element.id).addEvent("mouseover", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(38);
			$("thumbspan_3_div_"+i).setStyle("height", "57px");
		});
		$(element.id).addEvent("mouseout", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(101);
			$("thumbspan_3_div_"+i).setStyle("height", "101px");
		});
		
	});
	$$(".details_thumbs").each(function(element, i){
		element.id = "details_thumbs_"+i;
		$(element.id).addEvent("mouseover", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(38);
			$("thumbspan_3_div_"+i).setStyle("height", "57px");
		});
		$(element.id).addEvent("mouseout", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(101);
			$("thumbspan_3_div_"+i).setStyle("height", "101px");
		});
	});
	$$(".collection_thumbs").each(function(element, i){
		element.id = "collection_thumbs_"+i;
		$(element.id).addEvent("mouseover", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(38);
			$("thumbspan_3_div_"+i).setStyle("height", "57px");
		});
		$(element.id).addEvent("mouseout", function(){
			//new Fx.Tween($("thumbspan_3_div_"+i), {duration: 300, property: "height", transition: Fx.Transitions.linear}).start(101);
			$("thumbspan_3_div_"+i).setStyle("height", "101px");
		});
	});
	
}





var steps = 0;
var slider = {};
var steps_lc = 0;
var mxScroll = new Class({
						 
	initialize: function(div, div_content, horizontal){
		//$("scrollbar").style.display = "none";
		this.createScroll(div, div_content, horizontal);
	},
	createScroll: function(div, div_content, horizontal){
		if(window.ie6){
			$(div).style.height = ($(div_content).offsetHeight*0.97)+"px";
		}else{
			$(div).style.height = ($(div_content).offsetHeight)+"px";
		}
		$("scrollbar_lc").style.height = $(div).offsetHeight+"px";
		
		if(($(div).scrollHeight - $(div).offsetHeight) > 0){
			$('scrollbar_lc').style.display = "";
			$('handle_lc').style.display = "";
			steps = (horizontal?($(div).scrollWidth.x - $(div).offsetWidth):($(div).scrollHeight - $(div).offsetHeight));
			steps_lc = steps;
			slider = new Slider($("scrollbar_lc"), $("handle_lc"), {	
				steps: steps,
				mode: (horizontal?'horizontal':'vertical'),
				onChange: function(step){
					// Scrolls the content element in x or y direction.
					var x = (horizontal?step:0);
					var y = (horizontal?0:step);
					$(div).scrollTo(x,y);
				}
			}).set(0);
			$$("scrollbar_lc").addEvent('mousewheel', function(e){	
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					
					console.log("slider.step - e.wheel = "+slider.step +" "+ e.wheel)
					
					slider.set(step);					
			});
			$(div).addEvent('mousewheel', function(e){															   
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					
					console.log("slider.step - e.wheel = "+slider.step +" "+ e.wheel)
					
					slider.set(step);					
			});
			$(div_content).addEvent('mousewheel', function(e){															   
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					
					console.log("slider.step - e.wheel = "+slider.step +" "+ e.wheel)
					
					slider.set(step);					
			});
			$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		}else{
			
			$('handle_lc').style.display = "none";
		}
	}
});

var steps = 0;
var slider = {};
var mxScroll2 = new Class({
						 
	initialize: function(div, div_content, horizontal){
		//$("scrollbar_f").style.display = "none";
		this.createScroll(div, div_content, horizontal);
	},
	createScroll: function(div, div_content, horizontal){
		if(window.ie6){
			$(div).style.height = ($(div_content).offsetHeight*0.97)+"px";
		}else{
			$(div).style.height = ($(div_content).offsetHeight)+"px";
		}
		$("scrollbar_f").style.height = $(div).offsetHeight+"px";
		
		if(($(div).scrollHeight - $(div).offsetHeight) > 0){
			$('scrollbar_f').style.display = "";
			$('handle_f').style.display = "";
			steps = (horizontal?($(div).scrollWidth.x - $(div).offsetWidth):($(div).scrollHeight - $(div).offsetHeight));
			slider = new Slider($("scrollbar_f"), $("handle_f"), {	
				steps: steps,
				mode: (horizontal?'horizontal':'vertical'),
				onChange: function(step){
					// Scrolls the content element in x or y direction.
					var x = (horizontal?step:0);
					var y = (horizontal?0:step);
					$(div).scrollTo(x,y);
				}
			}).set(0);
			$$("scrollbar_f").addEvent('mousewheel', function(e){	
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					slider.set(step);					
			});
			$(div).addEvent('mousewheel', function(e){															   
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					
					slider.set(step);					
			});
			$(div_content).addEvent('mousewheel', function(e){															   
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					
					slider.set(step);					
			});
			$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		}else{
			
			$('handle_f').style.display = "none";
		}
	}
});
function cancel_sample_request(){
	$('content').load("collections/"+actual_page+"?mxID=574372184f1ada4005e6b8156257ee73");
}