// CONTENT
var ContentH = 0;
var ContW = 0;
var ContH = 0;
function setContent() {
	if (isMobile || isIE6) {
		$("#content").addClass("content_alt");
		$("#footer").addClass("footer_alt");
	}
	
	$(".cont_wrapper").show();
	switch (Sec) {
		case "home":
			
			break;
		case "quemsomos":
			setQuemsomos();
			break;
		case "luz":
		case "movimento":
		case "maquinaria":
			setProd();
			break;
		case "contato":
			setContato();
			break;
		case "links":
			setLinks();
			break;
		case "blog":
			setBlog();
			break;
	}
	$(".cont_wrapper").hide();
	ajustaContent();
	ajustaAbas();
	showCont();
}

function ajustaContent() {
	if ($(".cont").length) {
		ContW = $(".cont").innerWidth();
		ContH = $(".cont").innerHeight();
	}
}

function showCont() {
	if ($(".cont_wrapper").length) {
		$(".aba").delay(300).animate({marginTop: BotsH}, 1000);
		$("#prod_bots").delay(300).animate({height: BotsH}, 1000);
		$(".cont_wrapper").delay(300).show(0).animate({width: 0, height: 320}, 0).animate({width: ContW, height: ContH}, 1000);
	}
}

function hideCont() {
	if ($(".cont_wrapper").length) {
		$(".aba").animate({marginTop: 0}, 500);
		$("#prod_bots").animate({height: 0}, 500);
		$(".cont_wrapper").animate({width: 0, height: 320}, 500, function() {
			var hash = "";
			if (Cat) {
				hash += "#cat=" + Cat;
				if (Sub) {
					hash += "&sub=" + Sub;
				}
				if (Item) {
					hash += "&item=" + Item;
				}
			}
			window.location.href = "../" + Sec + Lang + "/" + hash;
		});
	} else {
		window.location.href = "../" + Sec + Lang;
	}
}

// quem somos
var QuemsomosW = 0;
var QuemsomosInW = 0;
function setQuemsomos() {
	QuemsomosW = $("#quemsomos_thumbs_wrapper").width();
	QuemsomosInW = $("#quemsomos_thumbs_inner").width();
	
	$(".quemsomos_thumb").fadeTo(0, .2).mouseenter(function() {
		$(this).fadeTo(200, 1);
	}).mouseleave(function() {
		$(this).fadeTo(0, .2);
	});
	
	setScroll("#quemsomos");
}

function thumbsAnt() {
	var thbsOrigem = $("#quemsomos_thumbs_inner").css("left");
	if (thbsOrigem < 0) {
		var thbsDestino = thbsOrigem + QuemsomosW;
		if (thbsDestino > 0) {
			thbsDestino = 0;
		}
		$("#quemsomos_thumbs_inner").animate({left: thbsDestino + 'px'}, 500);
	}
}

function thumbsProx() {
	var thbsOrigem = $("#quemsomos_thumbs_inner").css("left");
	if (thbsOrigem > QuemsomosW - QuemsomosInW) {
		var thbsDestino = thbsOrigem - QuemsomosW;
		if (thbsDestino < QuemsomosW - QuemsomosInW) {
			thbsDestino = QuemsomosW - QuemsomosInW;
		}
		$("#quemsomos_thumbs_inner").animate({left: thbsDestino + 'px'}, 500);
	}
}

function popFoto(num) {
	var mid = $("#quemsomos_link"+num).attr("midia");
	var w = $("#quemsomos_link"+num).attr("w");
	var h = $("#quemsomos_link"+num).attr("h");
	
	var mw = Math.round(($("#quemsomos_foto_wrapper").width() - w) / 2);
	var mh = Math.round(($("#quemsomos_foto_wrapper").height() - h) / 2);
	
	$("#quemsomos_foto_wrapper").show();
	$("#quemsomos_foto").hide();
	$("#quemsomos_foto").attr("src", "");
	
	$("#quemsomos_foto").css("margin-left", mw+"px").css("margin-top", mh+"px");
	$("#quemsomos_foto").attr("width", w).attr("height", h);
	
	$("#quemsomos_foto").load(function() {
		$(this).show();
	}).error(function() {
		$(this).show();
	});
	
	$("#quemsomos_foto").attr("src", "../midia_g/"+mid);
}

function fechaFoto() {
	$("#quemsomos_foto_wrapper").hide();
}

// produtos
var BotsH = 0;
function setProd() {
	switch (Sec) {
		case "luz":
			var numBots = 11;
			break;
		case "movimento":
			numBots = 5;
			break;
		case "maquinaria":
			numBots = 4;
			break;
	}
	BotsH = 20 * numBots;
	var prodH = 460 + BotsH;
	$("#prod").css("height", prodH + "px");
	//$("#prod_bots").css("height", BotsH + "px");
	//$(".aba").css("margin-top", BotsH + "px");
	
	$(".prod_bot").mouseenter(function() {
		$(this).addClass("prod_bot_on");
	}).mouseleave(function() {
		if ($(this).attr("cat") != Cat) {
			$(this).removeClass("prod_bot_on");
		}
	});
	
	//$(".prod_coluna[cat='"+Cat+"']").show();
	
	$(".prod_coluna_item").mouseenter(function() {
		$(this).addClass("prod_coluna_item_over");
	}).mouseleave(function() {
		$(this).removeClass("prod_coluna_item_over");
	});
	
	$(window).bind('hashchange', function(e) {
		var ha = e.fragment;
		
		var param = $.deparam.querystring(ha, true);
		Cat = (param.cat ? param.cat : 0);
		Sub = (param.sub ? param.sub : 0);
		Item = (param.item ? param.item : 0);
		
		setSub();
		setCat();
		setItem();
	});
	$(window).trigger('hashchange');
}

function setCat() {
	$(".prod_bot_on").removeClass("prod_bot_on");
	$(".prod_coluna_atual").removeClass("prod_coluna_atual");
	
	$(".prod_bot[cat='"+Cat+"']").addClass("prod_bot_on");
	$(".prod_coluna[cat='"+Cat+"']").addClass("prod_coluna_atual");
	
	setScroll(".prod_coluna[cat="+Cat+"]");
}

function setSub() {
	var num = $(".prod_coluna_item_atual").attr("sub");
	num = (num ? num : 0);
	if (num && !Item) {
		$(".prod_coluna_subwrapper_atual").removeClass("prod_coluna_subwrapper_atual");
		$(".prod_coluna_item_atual img").attr("src", "../images/prod_coluna_setaf.png");
		$(".prod_coluna_item_atual").removeClass("prod_coluna_item_atual");
	}
	if (Sub != num) {
		$(".prod_coluna[cat='"+Cat+"'] .prod_coluna_subwrapper[sub='"+Sub+"']").addClass("prod_coluna_subwrapper_atual");
		$(".prod_coluna[cat='"+Cat+"'] .prod_coluna_item[sub='"+Sub+"']").addClass("prod_coluna_item_atual");
		$(".prod_coluna_item_atual img").attr("src", "../images/prod_coluna_setaa.png");
	}
	
	setScroll(".prod_coluna[cat="+Cat+"]");
}

function clickSub(num) {
	if (num == Sub) {
		location.hash = "#cat=" + Cat;
	} else {
		location.hash = "#cat=" + Cat + "&sub=" + num;
	}
}

function setItem() {
	$(".prod_coluna_item_on").removeClass("prod_coluna_item_on");
	if (Sub) {
		var el = $(".prod_coluna_subwrapper_atual .prod_coluna_item[item='"+Item+"']");
	} else {
		el = $(".prod_coluna_atual .prod_coluna_item[item='"+Item+"']");
	}
	el.addClass("prod_coluna_item_on");
	
	var texto = el.find(".dados_texto").html();
	$("#prod_painel_texto").html(texto);
	
	setItemImage(0)
	
	var mais = el.find(".dados_mais").html();
	if (mais) {
		$("#prod_painel_c").show();
	} else {
		$("#prod_painel_c").hide();
	}
	
	if (Item) {
		$("#prod_painel_col2_tapa").hide();
		$("#prod_painel_col2").css("background-image", "url('../images/prod_painel_bg.png')");
	} else {
		$("#prod_painel_col2_tapa").show();
		$("#prod_painel_col2").css("background-image", "");
	}
	$("#prod_painel_quant_num input").val("1");
}

function setItemImage(num) {
	switch (num) {
		case 0:
			var midia = $(".prod_coluna_item_on .dados_midia").html();
			var w = $(".prod_coluna_item_on .dados_w").html();
			var h = $(".prod_coluna_item_on .dados_h").html();
			break;
		case 1:
			midia = $(".prod_coluna_item_on .dados_mais").html();
			w = $(".prod_coluna_item_on .dados_mw").html();
			h = $(".prod_coluna_item_on .dados_mh").html();
			break;
	}
	
	if (midia) {
		var url = "../midia_g/" + midia;
		
		$("#prod_foto img").attr("num", num);
	} else {
		url = "../images/noimage.gif";
		w = 445;
		h = 320;
		
		$("#prod_foto img").attr("num", 0);
	}
	
	var imagem = '<IMG src="'+url+'" width="'+w+'" height="'+h+'" border="0">';
	var mw = Math.round(($("#prod_foto").width() - w) / 2);
	var mh = Math.round(($("#prod_foto").height() - h) / 2);
	
	$("#prod_foto").html(imagem);
	$("#prod_foto img").css("margin-left", mw + "px");
	$("#prod_foto img").css("margin-top", mh + "px");
	$("#prod_foto img").fadeTo(0, 0).fadeTo(1000, 1);
}

function maisInfo() {
	if ($("#prod_foto img").attr("num") == 1) {
		setItemImage(0);
	} else {
		setItemImage(1);
	}
}

function quant(incr) {
	var q = parseInt($("#prod_painel_quant_num input").val()) + incr;
	if (q < 1) {
		q = 1;
	}
	$("#prod_painel_quant_num input").val(q);
}

function adiciona() {
	if (Cliente.cod) {
		if ($(".lista_item[cod="+Item+"]").length) {
			alert(Lang == "_en" ? "This item is already in your list" : "Este item já consta na lista");
		} else {
			ListaC.push(Item);
			ListaD.push($(".prod_coluna_item_on .dados_titulo").html().replace(",", "&#44;"));
			ListaQ.push($("#prod_painel_quant_num input").val());
			
			montaLista();
			
			setCookie("cods", ListaC.join(","), null, "/");
			setCookie("quant", ListaQ.join(","), null, "/");
		}
	} else {
		alert(Lang == "_en" ? "Please log-in" : "Por favor se identifique ao lado");
		if (!ListaIsOpen) {
			popLista();
		}
	}
}

// contato
function setContato() {
	setScroll("#contato");
}

// links
function setLinks() {
	setScroll("#links");
}

// blog
function setBlog() {
	var param = $.deparam.querystring(true);
	Cat = (param.cat ? param.cat : 0);
	//Sub = (param.sub ? param.sub : 0);
	Item = (param.item ? param.item : 0);

	var hei = Math.max($("#blog_main").innerHeight(), $("#blog_coluna").innerHeight(), 320);
	$("#blog").css("height", hei+"px");
	$("#blog_main").css("height", "100%");
	$("#blog_coluna").css("height", "100%");
	
	$("#langbot").hide();
}

function maisPosts(ca, sk) {
	$.get("../blog_posts.php",
	{ cat: ca, skip: sk },
	function(data){
		$('#blog_bot_mais').replaceWith(data);
		$("#blog_main").css("height", "auto");
		var hei = Math.max($("#blog_main").innerHeight(), $("#blog_coluna").innerHeight(), 320);
		$("#blog_main").css("height", "100%");
		$("#blog").css("height", hei+"px");
		ajustaContent();
		setYoutube();
		$(".cont_wrapper").delay(300).animate({height: ContH}, 1000);
	},
	"html"
	);
}

// youtube
var ytvideos = new Array();
function onYouTubePlayerAPIReady() {
	setTimeout(function() { setYoutube(); }, 500);
}

function setYoutube() {
	$(".ytvideo").each(function() {
		if (String($(this).html()) == "") {
			var placer = $(this).attr("id");
			var ytid = $(this).attr("ytid");
			var cod = parseInt(placer.substr(7));
			ytvideos[cod] = new YT.Player(placer, {
				height: '252',
				width: '395',
				videoId: ytid/*,
				events: {
					'onReady': onPlayerReady,
					'onStateChange': onPlayerStateChange
				}*/
			});
		}
	});
}

