$(document).ready(function(){
	$(".abrevideo").colorbox({iframe:true, innerWidth:425, innerHeight:344});
	$('#abrebanner').bind('click', function() {
      	$("#abrebanner").colorbox();
    });
    $('#abrebanner').trigger('click');
	
	
	$('#mascara').css('height', $(document).height()).hide();
		$('.interruptor').click(function(e) {
			e.preventDefault();
			$('#mascara').toggle();
				if ($('#mascara').is(':hidden')) {
					$(this).html('<img src=\"imgs/icone_lampada.png\" alt=\"Apagar as luzes\" title=\"Apagar a luz\" /> <br />Apagar a luz').removeClass('apaga-acende');
				} else {
					$(this).html('Acender a luz').addClass('apaga-acende');
				}
		});
	$('#portfolio').nivoSlider({
		effect:'random',
		slices:8,
		animSpeed:500,
		pauseTime:3000,
		startSlide:0, //Set starting Slide (0 index)
		directionNav:true, //Next & Prev
		directionNavHide:true, //Only show on hover
		controlNav:false, //1,2,3...
		controlNavThumbs:false, //Use thumbnails for Control Nav
		controlNavThumbsSearch: '.jpg', //Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', //...this in thumb Image src
		keyboardNav:true, //Use left & right arrows
		pauseOnHover:false, //Stop animation while hovering
		manualAdvance:false, //Force manual transitions
		captionOpacity:0.8, //Universal caption opacity
		beforeChange: function(){},
		afterChange: function(){},
		slideshowEnd: function(){} //Triggers after all slides have been shown
	});
	
	// We only want these styles applied when javascript is enabled
	$('div.navigation').css({'width' : '300px', 'float' : 'left'});
	$('div.content').css('display', 'block');

	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});
	
	// Initialize Advanced Galleriffic Gallery
	var gallery = $('#thumbs').galleriffic({
		delay:                     1500,
		numThumbs:                 8,
		preloadAhead:              30,
		enableTopPager:            true,
		enableBottomPager:         true,
		maxPagesToShow:            999,
		imageContainerSel:         '#slideshow',
		controlsContainerSel:      '#controls',
		captionContainerSel:       '#caption',
		loadingContainerSel:       '#loading',
		renderSSControls:          true,
		renderNavControls:         true,
		playLinkText:              'Tocar Slideshow',
		pauseLinkText:             'Pausar Slideshow',
		prevLinkText:              '&lsaquo; Foto anterior',
		nextLinkText:              'Próxima Foto &rsaquo;',
		nextPageLinkText:          'Mais &rsaquo;',
		prevPageLinkText:          '&lsaquo; Menos',
		enableHistory:             true,
		autoStart:                 false,
		syncTransitions:           false,
		defaultTransitionDuration: 300,
		onSlideChange:             function(prevIndex, nextIndex) {
			// 'this' refers to the gallery, which is an extension of $('#thumbs')
			this.find('ul.thumbs').children()
				.eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end()
				.eq(nextIndex).fadeTo('fast', 1.0);
		},
		onPageTransitionOut:       function(callback) {
			this.fadeTo('fast', 0.0, callback);
		},
		onPageTransitionIn:        function() {
			this.fadeTo('fast', 1.0);
		}
	});

	/**** Functions to support integration of galleriffic with the jquery.history plugin ****/

	// PageLoad function
	// This function is called when:
	// 1. after calling $.historyInit();
	// 2. after calling $.historyLoad();
	// 3. after pushing "Go Back" button of a browser
	function pageload(hash) {
		// alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			$.galleriffic.gotoImage(hash);
		} else {
			gallery.gotoIndex(0);
		}
	}

	// Initialize history plugin.
	// The callback is called at once by present location.hash. 
	$.historyInit(pageload, "advanced.html");

	// set onlick event for buttons using the jQuery 1.3 live method
	$("a[rel='history']").live('click', function(e) {
		if (e.button != 0) return true;
		
		var hash = this.href;
		hash = hash.replace(/^.*#/, '');

		// moves to a new page. 
		// pageload is called at once. 
		// hash don't contain "#", "?"
		$.historyLoad(hash);

		return false;
	});



});

validaForm = function (formulario){
	$("form fieldset input").trimFields();
	$("form fieldset textarea").trimFields();
	var form=document.getElementById(formulario);
	var i=0;
	for (i=0;i<form.elements.length;i++){
		if (form.elements[i].title!=""){
			if (form.elements[i].value==""){
				jAlert ('Atenção',form.elements[i].title,form.elements[i]);
				return false;
			}else if (form.elements[i].name=="email"){
					var validacao=/^[a-zA-Z0-9-_.]+@[a-zA-Z0-9-_]+\.([a-zA-Z0-9]{2,4})\.?([a-zA-Z0-9]{2,4})?$/;
					if (!validacao.test(form.elements[i].value)){
						jAlert ('Atenção',"E-mail inválido, por gentileza informe um e-mail válido!",form.elements[i]);
						return false;
					}
			}else if ( (form.elements[i].value=="_____-___") || (form.elements[i].value=="(__) ____-____")  || (form.elements[i].value=="__/__/____") || (form.elements[i].value=="__:__") || (form.elements[i].value=="___.___.___-__") || (form.elements[i].value=="_.___.___") || (form.elements[i].value=="__.___.___/____-__") || (form.elements[i].value=="___.___.___") || (form.elements[i].value=="___-____") ){
				jAlert ('Atenção',form.elements[i].title,form.elements[i]);
				return false;
			
				
			}
			
		}
	}
}

jQuery.fn.trimFields = function(){
  this.each(function(){
   $(this).val( jQuery.trim($(this).val()) );
  });
}

var jAlert = function(titulo, textoHtml, objetoFoco, largura) {
	if(!largura) { largura = 300; }

	var div = document.createElement('div');
	$(div).addClass('alerta').html(textoHtml).hide().appendTo('body');
	$('div.alerta').dialog({
			title: titulo,
			modal: true,
			width: largura,
			overlay: {backgroundColor:'#777',opacity:'.7'},
			close: function() {
					$('div.alerta').dialog('destroy').remove();
					$(objetoFoco).focus();
			},
			buttons: {
				'OK': function(){ 
					$('div.alerta').dialog('close');
					$(objetoFoco).focus();
				} 
			}
	});
}

var jConfirm = function(titulo, textoHtml, url, largura) {
	if(!largura) { largura = 300; }

	var div = document.createElement('div');
	$(div).addClass('alerta').html(textoHtml).hide().appendTo('body');
	$('div.alerta').dialog({
			title: titulo,
			modal: true,
			width: largura,
			overlay: {backgroundColor:'#777',opacity:'.7'},
			close: function() {
					$('div.alerta').dialog('destroy').remove();
			},
			buttons: {
				
				'Cancelar': function(){ 
					$('div.alerta').dialog('close');
				},
				'OK': function(){ 
					$('div.alerta').dialog('close');
					window.location=url;
				}
				
			}
	});
}

function confirma(msg,url){
		jConfirm('Atenção',msg,url);
}
function alerta(msg){
	jAlert('Atenção', msg, '', 450);
}
