﻿$('body').append('<div id="loading" style="z-index:10;position:absolute;width:100%;height:100%;background-color:#E3C9E0"></div>');

var prepare_scroll = function(){
    if ( $('.slider-frame').length ){
        $('.slider').cycle({
            slideExpr: '.slider-frame li',
            next: 'a#slider-right',
            prev: 'a#slider-left',
            timeout: 2000
        });
    }
	
	$('ul.carousel').jcoverflip({
		titles: $.ui.jcoverflip.defaults.titles,
		titleAnimateIn: $.ui.jcoverflip.defaults.titleAnimateIn,
		titleAnimateOut: $.ui.jcoverflip.defaults.titleAnimateOut,
		controls: $.ui.jcoverflip.defaults.controls,
		current: 1,
		time: 800,
		beforeCss: function( el, container, offset ){
			return [
				$.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 110*offset - 100)+'px', bottom: '10px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,50-20*offset*offset) + 'px' }, {} )
			];
		},
		afterCss: function( el, container, offset ){
			return [
				$.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 110*offset +40)+'px', bottom: '10px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,50-20*offset*offset) + 'px' }, {} )
			];
		},
		currentCss: function( el, container ){
			return [
				$.jcoverflip.animationElement( el, { left: ( (container.width( )/2) - 40 )+'px', bottom: '0px' }, { } ),
				$.jcoverflip.animationElement( el.find( 'img' ), { width: '70px' }, { } )
			];
		}
	});

	$('a#carousel-left').click(function(){
		$('.carousel').jcoverflip('previous');
		var idx = $('.carousel').jcoverflip('current');
		var li = $('.carousel').data('jcoverflip').itemsCache[idx];
		var item = $($(li).attr('rel')+'.carousel');		
		if ( item.length ){
			$('.carousel-container').find('h3').html(item.find('h3').html());
			$('.carousel-container').find('p.description').html(item.find('p').html());
		}
		return false;
	});
	
	$('a#carousel-right').click(function(){
		$('.carousel').jcoverflip('next');
		var idx = $('.carousel').jcoverflip('current');
		var li = $('.carousel').data('jcoverflip').itemsCache[idx];
		var item = $($(li).attr('rel')+'.carousel');	
		if ( item.length ){
			$('.carousel-container').find('h3').html(item.find('h3').html());
			$('.carousel-container').find('p.description').html(item.find('p').html());
		}
		return false;
	});
	
	$('.update-category').click(function(){
		var to_exclude = '.carousel_'+$(this).attr('rel');
		$('.carousel li').css({opacity:0});
		$('.carousel li'+to_exclude).css({opacity:1});
		$('.carousel').data('jcoverflip').itemsCache = $('.carousel li'+to_exclude);
		$('a#carousel-right').click();
		return false;
	});
};

var prepare_main_divs = function(){
	var bg_div = $('<div>');
	bg_div.addClass('header_background');
	bg_div.attr('id', 'header_background_left');
	$('#header_left').before(bg_div.clone());
	
	bg_div.attr('id', 'header_background_right');
	$('#header_right').after(bg_div.clone());

	$('#header_background_left,#header_background_right').css('width', '0px');
	$('#header_background_right').css('z-index',2);
	
	var offset = $('#topo_cont').offset().left;
	$('#header_left').css('left', '-'+(offset+$('#header_left').width())+'px');
	$('#header_right').css('right', '-'+($('#header_right').width()+offset)+'px');
	
	$('#header_background_left').css('height', $('#header_left').css('height'));
	$('#header_background_left').css('left', '-'+$('#topo_cont').offset().left+'px');
	$('#header_background_left').css('top', '1px');
	
	$('#header_background_right').css('height', parseFloat($('#header_right').css('height'))-1 + 'px');
	var right = $(document).width() - $('#topo_cont').offset().left - $('#topo_cont').outerWidth(true);
	$('#header_background_right').css('right', '-'+(Math.abs(right-$('#header_right').width())-1)+'px');
	$('#header_background_right').css('top', '36px');

	$('#header_right').hide();
	$('.main_logo').hide();
	$('ul.menu li').hide();
}

var slide_main_divs = function(){
	$('#header_left').animate({'left':'5px'}, 1500, 'easeOutExpo');
	$('#header_background_left').animate({'width': $('#topo_cont').offset().left + 20+'px'}, 1200,function(){
		$('#flor-esquerda-ctn').css({
			top: parseFloat($('#header_background_left').offset().top)+'px',
			left: parseFloat($('#header_left').offset().left)-171+'px'
		});
		$('#flor-esquerda-ctn').show();
		$('.main_logo').fadeIn('slow');
		$('.barra_menu').fadeIn('slow');
		$('ul.menu li').delay(500).fadeIn('slow');
	});
	
	$('#header_right').show();
	$('#header_right').animate({'right':'2px'}, 1500, 'easeOutExpo');
	var width = $(document).width() - $('#topo_cont').offset().left - $('#topo_cont').outerWidth(true);
	$('#header_background_right').animate({'width': Math.abs(width-$('#header_right').width())+20+'px'}, 1200, function(){
		$('#flor-direita-ctn').css({
			top: parseFloat($('#header_background_right').offset().top)+6+'px', 
			right: Math.abs(parseFloat($('#header_background_right').css('right')))-132+'px'
			});
		$('#flor-direita-ctn').show();
	});
	
	
};

var do_innerfade = function(new_src){
	if ( !$('div.background_image').length ){
		var div = $('<div>');
		
		div.addClass('background_image');
		div.append('<img class="back" />');
		div.append('<img class="front" />');
		div.css('top', $('#fundo').offset().top-38+'px');
		$('body').append(div.clone());
		return $('div.background_image img.front').attr('src', new_src).load(function(){
			$('div.background_image img.back').css('height', 0);
			adjust_footer();
		});
	}
	var div = $('div.background_image');
	$('div.background_image img').css('height', 'auto');
	$('div.background_image img').unbind();
	$('div.background_image img.back').attr('src', new_src).load(function(){
		$('div.background_image img.front').animate({'opacity': 0}, 1000, function(){
			$('div.background_image img').toggleClass('front back');
		}).animate({'opacity': 1}, 1000);
		adjust_footer();
	});
};

var adjust_footer = function(){
	$('#conteudo').css('height', $('img.front').height()-38 +'px');
}

var animate_flowers = function(){

};

var animate_butterfly = function(){};

var prepare_content = function(){
	$('#principal').css({opacity: '0'});
}

var prepare_widgets = function(){
	if ( $('#id_msg_assunto').length ){
		var input = $('#id_msg_assunto');
		input.autocomplete({
			minLength:0,
			source: ['Contato via e-mail', 'Solicite uma visita', 'Seja revendedor' ]
		});
		
		$('.select_button').click(function(){
			if ( input.autocomplete('widget').is(':visible') ){
				input.autocomplete('close');
				return false;
			}
			input.autocomplete('search','');
			input.focus();
			return false;
		});
	}
}

var loader_on_mouse = function(){
	if ( !$('.loading').length ){
		var div = '<div class="loading"><img src="img/ajax-loader.gif"/></div>';
		$('body').append(div);
		$('.loading').fadeOut('fast');
	}
	$(document).mousemove(function(e){
		if ( e.pageX + 82 >= $(document).width() ||
			 e.pageY + 82 >= $(document).height() ){
			var offset = -32;
		} else
			var offset = 20;
		$('.loading').css('left', e.pageX+offset+'px');
		$('.loading').css('top', e.pageY+offset+'px');
	})
	.ajaxStart(function(){
		$('.loading').fadeIn('fast');
	})
	.ajaxStop(function(){
		$('.loading').fadeOut('fast');
	});
};

var animate_content = function(){
	if ( !window.location.hash ){
		$('#principal').animate({'opacity': '1'}, 500, function(){
			$(this).css({'opacity': 'auto'});
		});
	}
};

