// JavaScript Document
var j = jQuery.noConflict(true);
j(document).ready(function() {
	randProducts();
	rotateBanner();
	j('input[type=radio].star').rating();
	
	toolTipStart(".tooltip-image-big","medium","large");
	
	xajax.doneLoadingFunction = function() {
		noRefresh = 0;
		startZoom = 0;   
	}
	j("#nav-buttons li").hover(function(){
		j(this).find('ul:first').fadeIn('slow');
	},function(){
		j(this).find('ul:first').fadeOut('slow');
	});
	
	j('#header-login-show').live('mouseenter mouseleave', function(event){
		if(event.type  == "mouseover") {
			j(this).find('div:first').slideDown('fast');
		} else  { 
			j(this).find('div:first').slideUp('fast');
		}
	});
	
	j('#header-login-register').live('mouseenter mouseleave', function(event){
		if(event.type == "mouseover") {
			j(this).find('div:first').slideDown('fast');
		} else {
			j(this).find('div:first').slideUp('fast');
		}
	});
	
	j('#list-title').live('click',function(){
		j('#public-post').slideUp('fast');
		j('#list-post').slideDown('fast');
	});
	j('#post-title').live('click',function(){
		j('#list-post').slideUp('fast');
		j('#public-post').slideDown('fast');
	});
	
	var fieldValue;
	
	j('#newsForm input').live('focusin',function(){
		fieldValue = j(this).val();
		j(this).val('');
	});
	j('#newsForm input').live('focusout',function(){
		if(j(this).val() == '') {
			j(this).val(fieldValue);
		}
	});
	
	j('#link-other-establishment').live('click',function(){		
		if(j("#other-establishment").css("display") == "none") {
			j('#other-establishment').slideDown('fast');			
		} else {
			j('#other-establishment').slideUp('fast');
			j('#other-establishment input').val("");
		}				
	});
	
	j('#post-title').live('click',function(){
		j('#list-post').slideUp('fast');
		j('#public-post').slideDown('fast');
	});
	
	j('#bt-sendFriend').live('click',function(){
		j('#send-friend-back').fadeIn('fast');
		j('#send-friend').slideDown('fast');
	});
	
	j('#bt-closeSendFriend').live('click',function(){
		j('#send-friend').slideUp('fast');
		j('#send-friend-back').fadeOut('slow');
	});
	
	siteSearch();
	showResults();
	
	j('.content-edit').textRead({
		href: '.show-content-description',
		buttonUpSize: '#btUpSize',
		buttonDownSize: '#btDownSize',
		buttonUpLineHeight: '#btUpLineHeight',
		buttonDownLineHeight: '#btDownLineHeight',
		buttonDefaultConstrast: '#btDefaultContrast',
		buttonConstrast: '#btContrast',
		contrastBackground: '#fff|#eee',
		contrastFontColor: '#000|#333'
	});
	
	if(j("#banner-highlight-home")[0]) {
		//alert("OI");
	}
	
	j('#show-form-natural').focus(function(){
		j('#form-legal-person').fadeOut('fast');
		j('#form-natural-person').fadeIn('fast');
	});
	j('#show-form-legal').focus(function(){
		j('#form-natural-person').fadeOut('fast');
		j('#form-legal-person').fadeIn('fast');
	});

	j('#banner-popup').css("top", parseInt(j(window).height())/2 - 150);
	j('#banner-popup').css("left", parseInt(j(window).width())/2 - 200);
	j('#close-banner').live('click',function(){
		j('#banner-popup').fadeOut('fast');
	});
});

function xajax_all() {
	j("#nav-buttons li").hover(function(){
        j(this).find('ul:first').fadeIn('slow');
    },function(){
		j(this).find('ul:first').fadeOut('slow');
    });
	siteSearch();
	// showResults();	
	j('input[type=radio].star').rating();
    toolTipStart(".tooltip-image-big","medium","large");
}

function xajax_display_default() {		
	randProducts();
	rotateBanner();
	j('#banner-popup').css("margin-top", parseInt(j(window).height())/2 - 150);
	j('#close-banner').live('click',function(){
		j('#banner-popup').fadeOut('fast');
	});
}

function randProducts() {
	j('.rotate').bxCarousel({
        display_num: 5,
        move: 1,
        speed: 2000,
        auto: true,
        controls: false,
        margin: 0,
        auto_interval: 5000,
        auto_hover: true
    }); 
}


function rotateBanner() {

	j("#banner-rotate").cycle({
		fx: "fade",
		speed: 1000,
		timeout: 10000,
		pager: "#content-numbers-banner",
		pagerEvent: "click",
		pauseOnPagerHover: true
	});
	
	j("#banner-play-bt").toggle( function(){
		j("#banner-rotate").cycle('pause');
		j("#banner-play-bt" + ' img').attr("src","templates/impetus/images/home/home-btn-banner-pause.jpg");
	}, function(){
		j("#banner-rotate").cycle('resume');
		j("#banner-play-bt" + ' img').attr("src","templates/impetus/images/home/home-btn-banner-play.jpg");
	});
	
	j("#banner-rotate" + " .hide img").css("display","block");
}

function defaultValueBlur(campo,value) {
	if(j(campo).val() == value || j(campo).val() == '') {
		j(campo).val(value);
	} 
}

function defaultValueClick(campo,value) {
	if(j(campo).val() == value) {
		j(campo).val("");
	}
}

function emptyNewsletter() {
    if(checkMail(document.getElementById('email')) &&  j('#first_name').val != "" &&  j('#last_name').val != "") {
         j('#first_name').val('Digite seu nome...');
         j('#last_name').val('Digite seu nome...');
         j('#email').val('Digite seu e-mail...');
    }   
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
            return true; 
        }
    }else{
        return false;
    }
}

function siteSearch() {
	j('#search-submit').live('click',function(){
		if(j('#header-search-select').val() == ""){
			xajax_execDModule('internal','wl_search','list_result','','','find='+ j('#header-search-input').val());
		} else {
			barValue = j('#header-search-select').val();
			sessionSearch = barValue.substr(0,barValue.indexOf('_'));
			valueSearch = barValue.substr(barValue.indexOf('_') + 1, barValue.length);
			valueFind = j('#header-search-input').val();
			
			if(sessionSearch == 'liv') {
				xajax_execDModule('products','jpf_ec_product','list','','','start=0&l_find='+ valueFind +'&l_id_category=&l_id_category_master=&l_id_flag=&l_status_product=&l_find_type=' + valueSearch);            
				/*showNavBto('catalogo');*/
			} 
			else if (sessionSearch == 'aut') {
				xajax_execDModule('author','jpf_ec_product_book','list_author','','','find_author='+ valueFind +'&start_author=0');
				/*showNavBto('autores');*/
			} 
			else if (sessionSearch == 'art') {
				xajax_execDModule('articles','jpf_article','list','','','start=0&find='+valueFind+'&id_category');
				/*showNavBto('artigos');*/
			} 
			else if (sessionSearch == 'not') {
				xajax_execDModule('news','jpf_news','list','','','start=0&find='+valueFind+'&id_category=');
			   /* showNavBto('news');*/
			} 
			else if (sessionSearch == 'atu') {
				xajax_execDModule('downloads','jpf_file_manager','list','','','find='+valueFind+'&key_category=&start=0');
			   /* showNavBto('atualizacao');*/
			}
		}    
	});		
}

function showResults() {
	j('#showResults').live('click',function(){
		j(this).parent().find('.hide').fadeIn('fast');
	});
	j('#hideResults').live('click',function(){
		j(this).parent().find('.hide').fadeOut('fast');
	});
}

function onlyNumber(event) {
    if(document.all) key = event.keyCode;
    else key = event.which;
    if ( key>=48 && key<=57 ) return 1;
    else if ( key==8 || key==0) return 2;
    else return false;
}

function autoChange(frm,source,target){
	if (source.maxLength>0){
        if ( source.value.length >= source.maxLength){
			var formObj = j('#'+frm);
			if(target == 'address') {
				// formObj[target].value="Aguarde...";
				j('#'+target).val('Aguarde...');
				xajax_execModule('jpf_address','','check_postal_code','','postal_code='+document.getElementById('postal_code').value);
			}
            // formObj[target].focus();
			j('#'+target).focus();
        }
    }
}

function toolTipStart(href,imageStart,imageTrade) {
    if(j(href)[0]) {
        j(href).tooltip({
            track: true,
            delay: 0,
            showURL: false,
            positionRight: true,
            bodyHandler: function() {
                return j("<img/>").attr("src", j(this).attr("src").replace(imageStart,imageTrade));
            }
        });
    }
}

function viewPhoto(){	
}

