$(document).ready(function() {
/******************** MENU E SUBMENU ********************/
	$(".menu-item a").mouseenter(function() {
		if(!$(this).parent().hasClass("atual")) $(this).css("color","#F78E1E");
	}).mouseleave(function() {
		if(!$(this).parent().hasClass("atual")) $(this).css("color","#FFF");
	});
	menuCoord = $("#menu-itens").offset();
	submenuCoord = $("#submenu-itens").offset();
	$("#submenu-itens").offset({left: menuCoord.left});
	
/******************** AJUSTES DO FUNDO ********************/
	$(".corpo").css({
		height : function() {
			swfWidth = $(window).width();
			floorHeight = ((130 * swfWidth) / 1920).toFixed();
			return $(window).height() - floorHeight - 150;
		},
		'padding-top' : function() {
			return(($(window).height() - floorHeight - 150 - 480) / 2);
		}
	});
	$("#background").html(function() {
		swfWidth = $(window).width();
		swfHeight = ((968 * swfWidth) / 1920).toFixed();
		swfTop = $(window).height() - swfHeight;
		swfLeft = 0;
		if(swfTop > 100) {
			swfHeight = $(window).height() - 100;
			swfWidth = ((1920 * swfHeight) / 968).toFixed();
			swfTop = 100;
			swfLeft = ($(window).width() - swfWidth) / 2
		}

		indice = location.href.indexOf("?s=") + 3;
		secAtual = location.href.substr(indice,8);
		if(secAtual=="portfoto" || secAtual=="portgraf") {
			indice = location.href.indexOf("&c=") + 3;
			portAtual = location.href.substr(indice,8);
			switch(portAtual) {
				case "idvisual":
					portAtual = "idvisual";
				break;
				case "impresso":
					portAtual = "impressos";
				break
				case "webdesig":
					portAtual = "webdesign";
				break;
				case "projet3d":
					portAtual = "projetos 3d";
				break
				default:
					portAtual = "idvisual";
				break;
			}
			if(secAtual=="portfoto") portAtual = "fotograf";
		}
		
		if(secAtual == "portgraf") $("#submenu-itens").show();
		else $("#submenu-itens").hide();
		
		switch (secAtual) {											//
			case ("portgraf"):										//
				swfMovie = "background_2.swf";						//
				$("#menuitem1").addClass("atual");					//
				xmlArquivo = 'portgrafico_' + portAtual + '.xml';	//
			break;													//
			case ("portfoto"):										//
				swfMovie = "background_2.swf";						//
				$("#menuitem2").addClass("atual");					//
				xmlArquivo = 'portfotografico.xml';					//
			break;													//
			case ("clientes"):										// VARIAÇÕES DE MENUS
				swfMovie = "background_4.swf";						// E FUNDOS NAS SEÇÕES
				$("#menuitem3").addClass("atual");					//
				xmlArquivo = 'clientes.xml';						//
			break;													//
			case ("contatos"):										//
				swfMovie = "background_3.swf";						//
				$("#menuitem4").addClass("atual");					//
			break;													//
			default:												//
				swfMovie = "background_1.swf";						//
			break;													//
		}															//
		
		codigo = "<!--[if !IE]> -->\n"
		codigo += "<object type=\"application/x-shockwave-flash\" data=\"images/fundos/" + swfMovie + "\" width=\"" + swfWidth + "\" height=\"" + swfHeight + "\">\n";
		codigo += "<!-- <![endif]-->\n"
		codigo += "<!--[if IE]>\n"
		codigo += "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0\" width=\"" + swfWidth + "\" height=\"" + swfHeight + "\">\n"
		codigo += "<param name=\"movie\" value=\"images/fundos/" + swfMovie + "\" />\n";
		codigo += "<!--><!--dgx-->"
		codigo += "<param name=\"quality\" value=\"high\" />\n";
		codigo += "<param name=\"wmode\" value=\"transparent\" />\n";
		codigo += "<p>Para melhor proveito do nosso site, você precisa instalar o Adobe Flash Player. <a href=\"http://get.adobe.com/br/flashplayer/\" target=\"_blank\">Clique aqui</a>.</p>"
		codigo += "</object>\n";
		codigo += "<!-- <![endif]-->"
		return codigo;
	}).css({'top': swfTop, 'left': swfLeft});
	
	$(window).resize(function() {
		swfWidth = $(window).width();
		swfHeight = ((968 * swfWidth) / 1920).toFixed();
		swfTop = $(window).height() - swfHeight;
		swfLeft = 0;
		if(swfTop > 100) {
			swfHeight = $(window).height() - 100;
			swfWidth = ((1920 * swfHeight) / 968).toFixed();
			swfTop = 100;
			swfLeft = ($(window).width() - swfWidth) / 2
		}
		$("#background").css({'top': swfTop, 'left': swfLeft});
		$("#objectflash").attr({
			width : swfWidth,
			height:swfHeight
		});
		$("#embedflash").attr({
			width : swfWidth,
			height:swfHeight
		});
	})
	
/******************** TWITTER ********************/
	
	$("#twitter").getTwitter({
		userName: "agenciaurock",
		numTweets: 5,
		loaderText: "Carregando...",
		slideIn: false,
		showHeading: false,
		showProfileLink: false,
		showTimestamp: true
	});
	
/******************** A UROCK ********************/
	$("#quadro2 a").click(function() {
		$("#conteudo").fadeOut("fast");
		$("#aurock").fadeIn("slow");
	});
	$("#zoomout").click(function() {
		$("#conteudo").fadeIn("slow");
		$("#aurock").fadeOut("fast");
	});

/******************** PORTIFÓLIO ********************/
	indice = location.href.indexOf("&call=") + 6;
	jobCalled = location.href.substr(indice);
	
	$("#menuitem2 a").click(function() {
		if(secAtual=="portgraf") {
			$("#corpo").fadeOut("fast", function() {
				secAtual = 'portfoto';
				portAtual = 'fotograf';
				xmlArquivo = 'portfotografico.xml';
				carregaXML();
			});
			$("#submenu-itens").fadeOut("fast");
			$("#menuitem2").addClass("atual");
			$("#menuitem2 a").css("color","#FFFFFF");
			$("#menuitem1").removeClass("atual");
			return false;
		}
	});
	$("#menuitem1 a").click(function() {
		if(secAtual=="portfoto") {
			$("#corpo").fadeOut("fast", function() {
				secAtual = 'portgraf';
				portAtual = 'idvisual';
				xmlArquivo = 'portgrafico_' + portAtual + '.xml';
				carregaXML();
			});
			$("#submenu-itens").fadeIn("slow");
			$("#menuitem1").addClass("atual");
			$("#menuitem1 a").css("color","#FFFFFF");
			$("#menuitem2").removeClass("atual");
			return false;
		}
	});

	$("#submenuitem1 a").click(function() {
		$("#corpo").fadeOut("fast", function() {
			portAtual = 'idvisual';
			xmlArquivo = 'portgrafico_idvisual.xml';
			jobCalled="";
			carregaXML();
		});
		return false;
	});
	$("#submenuitem2 a").click(function() {
		$("#corpo").fadeOut("fast", function() {
			portAtual = 'impresso';
			xmlArquivo = 'portgrafico_impressos.xml';
			jobCalled="";
			carregaXML();
		});
		return false;
	});
	$("#submenuitem3 a").click(function() {
		$("#corpo").fadeOut("fast", function() {
			portAtual = 'webdesig';
			xmlArquivo = 'portgrafico_webdesign.xml';
			jobCalled="";
			carregaXML();
		});
		return false;
	});
	$("#submenuitem4 a").click(function() {
		$("#corpo").fadeOut("fast", function() {
			portAtual = 'projet3d';
			xmlArquivo = 'portgrafico_projetos3d.xml';
			jobCalled="";
			carregaXML();
		});
		return false;
	});
	
/******************** ENVIAR FORMULÁRIO ********************/
	$("#btenviar").click(function() {
		var errors="";
		if ($("#fld-nome").val().length<1) errors+="- Nome\n";
		if ($("#fld-email").val().length<1) errors+="- Email\n";
		if ($("#fld-assunto").val().length<1) errors+="- Assunto\n";
		if ($("#fld-mensagem").val().length<1) errors+="- Mensagem\n";
	
		if (errors)	{
			alert("Os seguintes campos devem ser preenchidos:\n\n"+errors+"\n\n");
		} else {
			$("#bt_enviar").disabled=true;
			$("#formsite").submit();
		}
	});
	
/******************** CARREGAR XML ********************/
	function carregaXML() {
		$.ajax({
			type: 'GET',
			url: xmlArquivo,
			dataType: 'xml',
			success: function(xml) {
				qtdElementos = $(xml).find('job').length;
				diferenca = 18 - (qtdElementos % 18);
				tabela="\n\t<center>\n\t\t<table border='0' cellpadding='0' cellspacing='0' id='galeria'>\n\t\t\t";
				$(xml).find('job').each(function(i) {
					xmlCliente = $(this).attr("cliente");
					xmlDescricao = $(this).attr("descricao");
					xmlImagem = $(this).attr("imagem");
					xmlId = xmlImagem.lastIndexOf("/") + 1;
					xmlId = xmlImagem.substr(xmlId);
					xmlId = xmlId.replace("_","-");
					if(i%6==0) {
						tabela += "<tr>\n\t\t\t\t<td style='padding-left:0px' valign='top'>\n\t\t\t\t";
						if(secAtual!="clientes") {
							tabela += ("<a href='" + xmlImagem + ".jpg' title='" + xmlCliente + " - " + xmlDescricao + "' rel='" + portAtual + "' id='" + xmlId + "' class='zoomimg'><img src='" + xmlImagem + "_thumb.png' /></a><br />\n");
						} else {
							tabela += ("<img src='" + xmlImagem + ".png' /><br />\n");
						}
						tabela += ("\t\t\t\t\t" + xmlCliente + "\n");
						tabela += "\t\t\t\t</td>\n";
					} else if (i%6==5) {
						tabela += "\t\t\t\t<td style='padding-right:0px' valign='top'>\n\t\t\t\t\t";
						if(secAtual!="clientes") {
							tabela += ("<a href='" + xmlImagem + ".jpg' title='" + xmlCliente + " - " + xmlDescricao + "' rel='" + portAtual + "' id='" + xmlId + "' class='zoomimg'><img src='" + xmlImagem + "_thumb.png' /></a><br />\n");
						} else {
							tabela += ("<img src='" + xmlImagem + ".png' /><br />\n");
						}
						tabela += ("\t\t\t\t\t" + xmlCliente + "\n");
						tabela += "\t\t\t\t</td>\n\t\t\t</tr>\n\t\t\t";
					} else {
						tabela += "\t\t\t\t<td valign='top'>\n\t\t\t\t";
						if(secAtual!="clientes") {
							tabela += ("<a href='" + xmlImagem + ".jpg' title='" + xmlCliente + " - " + xmlDescricao + "' rel='" + portAtual + "' id='" + xmlId + "' class='zoomimg'><img src='" + xmlImagem + "_thumb.png' /></a><br />\n");
						} else {
							tabela += ("<img src='" + xmlImagem + ".png' /><br />\n");
						}
						tabela += ("\t\t\t\t\t" + xmlCliente + "\n");
						tabela += "\t\t\t\t</td>\n";
					}					
				});
				if(diferenca!=18) {
					for(i=0;i<diferenca;i++) {
						if(diferenca-i==7 || diferenca-i==13 || diferenca-i==1) tabela += "\t\t\t\t<td style='padding-right:0px' valign='top'></td>\n\t\t\t</tr>\n\t\t\t";
						else if(diferenca-i==6) tabela += "<tr>\n\t\t\t\t<td style='padding-left:0px' valign='top'>\n";
						else tabela += "\t\t\t\t<td style='padding-right:0px' valign='top'></td>\n";
					};
				}
				tabela += "\n\t\t</table>\n\t</center>\n";
				$("#titulo").html(function() {
					if(secAtual=="portgraf") {
						titleSec = "(portifólio gráfico";
						if(portAtual=="idvisual") titleCat = " - identidade visual)";
						else if (portAtual=="projetos3d") titleCat = " - projetos 3d)";
						else titleCat = " - " + portAtual + ")";
					} else if(secAtual=="portfoto") {
						titleSec = "(portifólio fotográfico";
						titleCat = ")"
					} else {
						titleSec = "(clientes";
						titleCat = ")";
					}
					return (titleSec + titleCat);
				});
				$("#conteudo").html(tabela);
				
				// GALERIA DE IMAGENS
				$("a.zoomimg").fancybox({
					'transitionIn'	    :	'elastic',
					'transitionOut'	    :	'elastic',
					'easingIn'          :   'easeOutBack',
					'easingOut'         :   'easeInBack',
					'speedIn'		    :	600, 
					'speedOut'		    :	200, 
					'overlayShow'	    :	true,
					'overlayOpacity'    :   0.75,
					'overlayColor'      :   '#000',
					'showCloseButton'   :   false,
					'hideOnContentClick':   true,
					'titlePosition'     :   'inside'
				}).mouseenter(function() {
					$(this).fadeTo("fast",0.75);
				}).mouseleave(function() {
					$(this).fadeTo("fast",1);
				});
				
				var options = {
					currPage : 1, 
					optionsForRows : [],
					rowsPerPage : 3,
					prevArrow : (new Image()).src="../images/link_back.gif",
					nextArrow : (new Image()).src="../images/link_next.gif"
				}
				$('#galeria').tablePagination(options);
				$("#tablePagination_paginater div").mouseenter(function() {
					if(!$(this).hasClass("disabled")) $(this).addClass("hover");
				}).mouseleave(function() {
					$(this).removeClass("hover");
				});
				if($("#corpo").css("display")=="none") $("#corpo").fadeIn("slow");
				if(jobCalled.length>=3) $("#" + jobCalled).trigger('click');
			}
		});
	}
	if(secAtual=="portgraf" || secAtual=="portfoto" || secAtual=="clientes") {
		carregaXML();
	}
});
