$().ready(function(){
	// base url para chamar todos os scripts
	var baseurl = $( 'base' ).attr( 'href' );
	$( '.slide' ).show();	
	$( '#container' ).scrollTop(100);
		
	$('.menu_atual').click( function() {
		$('.slide').slideToggle();
	});

	if( $('.produto').attr('class') == undefined)
		scrollTo('.atual');
	else
		scrollTo( '.layer_atual' );

	//setTimeout( "$( '.slide' ).slideDown(1000);" , 100 );
	
	// ver dicas completas
	$( '#more_dicas' ).click( function() {
		$( '#dicas_complete' ).slideToggle( 'fast' );
		$( '#dicas_collapsed' ).slideToggle( 'fast' );
		scrollTo( '#more_dicas' );
	} );
	
	// voltar ao estado inicial de dicas
	$( '#less_dicas' ).click( function() {
		$( '#dicas_collapsed' ).slideToggle( 'fast' );
		$( '#dicas_complete' ).slideToggle( 'fast' );
		scrollTo( '#less_dicas' );
	} );
	
	// ver noticias completas
	$( '#more_news,.see_news' ).click( function() {
		$( '#noticia_collapsed' ).hide();
		$( '#noticia_complete' ).slideToggle( 'fast' );
		scrollTo( '#noticia_complete' );
	} );
	
	// voltar ao estado inicial das noticias
	$( '#less_news' ).click( function() {
		$( '#noticia_complete' ).slideToggle( 'fast' , function() {
			$( '#noticia_collapsed' ).show();
		} );
		scrollTo( '#noticia_collapsed' );

	} );
	
	// esconde a seta a esquerda pois no ie tem bug
	$('.left_arrow').hide();
	
	// seta esquerda
	$( '.left_arrow' ).click( function() {
		var reset = $(this).parent().parent().parent(); // reseto para o pai de todos do slide
		var t2 = parseInt(reset.find( '.slide_w' ).find( '.slide_container' ).find('.t2:visible').length) * 192; // total do width do slide
		var m = parseInt( reset.find( '.slide_w' ).find( '.slide_container' ).css( 'margin-left' ) ) + 768; // margin nova ( margin_left_atual + 768 (largura do espaco de amostragem))
		reset.find('.right_arrow').show();
		if(parseInt(t2 + m) == t2) {// se for menor que 768 quer dizer que nao tera mais conteudo
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { marginLeft: m + 'px' , width : t2 + 'px' } , { queue:false , duration:1500 } );
			$(this).hide();
		}else
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { marginLeft: m + 'px' , width : t2 + 'px' } , { queue:false , duration:1500 } );
	} );

	// seta direita
	$( '.right_arrow' ).click( function() {
		var reset = $(this).parent().parent(); // reseto para o pai de todos do slide
		var t2 = parseInt(reset.find( '.slide_w' ).find( '.slide_container' ).find('.t2:visible').length) * 192; // total do width do slide
		var m = parseInt( reset.find( '.slide_w' ).find( '.slide_container' ).css( 'margin-left' ) ) - 768; // margin nova ( margin_left_atual - 768 (largura do espaco de amostragem))
		reset.find('.left_arrow').show();
		if(parseInt(t2 + m) <= 768) {// se for menor que 768 quer dizer que nao tera mais conteudo
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { marginLeft: m + 'px' , width : t2 + 'px' } , { queue:false , duration:1500 } );
			$(this).hide();
		}else
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { marginLeft: m + 'px' , width : t2 + 'px' } , { queue:false , duration:1500 } );
	} );
	
	// ver mais
	$( 'a.more2' ).click( function() {
		var reset = $(this).parent().parent().parent().parent(); // reseto para o pai de todos do slide
		var t2 = parseInt(reset.find( '.slide_w' ).find( '.slide_container' ).find('.t2').length) * 192;
		if(reset.find( '.slide_w' ).find( '.slide_container' ).width() == 768){
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { height:'80px' , width:t2+'px' } , { queue:false , duration:500 } );
			reset.find( '.slide_w' ).css('overflow','hidden');
			reset.find( '.right_arrow' ).show();
			reset.find( '.left_arrow' ).hide();
			$(this).parent().parent().find( '.more' ).html( '[ + ]' );
		}else{
			var h = Math.ceil(((t2/192)/4)*96);
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { height:h+'px' } , { queue:false , duration:500 } ).width(768);
			reset.find( '.slide_w' ).find( '.slide_container' ).css( 'margin-left','0' );
			reset.find( '.slide_w' ).height('auto');
			//reset.find( '.slide_w' ).css('overflow','none');
			reset.find( '.right_arrow' ).hide();
			reset.find( '.left_arrow' ).hide();
			$( this ).parent().parent().find( '.more' ).html( '[ - ]' );
		}
		scrollTo2( reset );
	} );

	// ver mais
	$( 'a.more' ).click( function() {
		var reset = $(this).parent().parent().parent().parent(); // reseto para o pai de todos do slide
		var t2 = parseInt(reset.find( '.slide_w' ).find( '.slide_container' ).find('.t2').length) * 192;
		if(reset.find( '.slide_w' ).find( '.slide_container' ).width() == 768){
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { height:'80px' , width:t2+'px' } , { queue:false , duration:500 } );
			reset.find( '.slide_w' ).css('overflow','hidden');
			reset.find( '.right_arrow' ).show();
			reset.find( '.left_arrow' ).hide();
			$(this).html( '[ + ]' );
		}else{
			var h = Math.ceil(((t2/192)/4)*96);
			reset.find( '.slide_w' ).find( '.slide_container' ).stop().animate( { height:h+'px' } , { queue:false , duration:500 } ).width(768);
			reset.find( '.slide_w' ).find( '.slide_container' ).css( 'margin-left','0' );
			reset.find( '.slide_w' ).height('auto');
			//reset.find( '.slide_w' ).css('overflow','none');
			reset.find( '.right_arrow' ).hide();
			reset.find( '.left_arrow' ).hide();
			$(this).html( '[ - ]' );
		}
		scrollTo2( reset );
	} );
	
	
	$( '.opa50 ' ).css( 'visibility' , 'hidden' ).parent().hover(
		function() {
			$( this ).find( '.opa50' ).css( 'visibility' , 'visible' );
		},
		function() {
			$( this ).find( '.opa50' ).css( 'visibility' , 'hidden' );
		}
	);
	
	//$.preloadCssImages();
	var random = 0;
	
	if( $('#content_id').attr('id') == 'content_id' ) {
		
		/*var r = Math.floor( Math.random() * 2 ) + 1;
		if(r == 1)
			var shoe = 'doys';
		else
			var shoe = 'sutra';
		*/
		var shoe = 'doys';
		setCookie( shoe );
		var vel = 800;
		random++;
		if(random == 3)
			random = 1;
		$( '#content_id' ).fadeOut( vel , function() {
			$( this ).toggleClass( shoe + '_' + random + '_bg' ).fadeIn( vel );
		} );
		/*
		$( '#prod'    ).fadeOut( vel , function() {
			$( this ).attr( 'class' , 'produtos bg_' + shoe + '_' + random + '_a' ).fadeIn( vel );
		} );
		$( '#find'    ).fadeOut( vel , function() {
			$( this ).attr( 'class' , 'encontre bg_' + shoe + '_' + random + '_b' ).fadeIn( vel );
		} );
		$( '#contact' ).fadeOut( vel , function() {
			$( this ).attr( 'class' , 'contato  bg_' + shoe + '_' + random + '_c' ).fadeIn( vel );
		} );
		$( '#press'   ).fadeOut( vel , function() {
			$( this ).attr( 'class' , 'imprensa bg_' + shoe + '_' + random + '_d' ).fadeIn( vel );
		} );*/

		var intervalo = window.setInterval(function() {
			var vel = 800;
			random++;
			if(random > 3)
				random = 1;
			$( '#content_id' ).fadeOut( vel , function() {
				$( this ).attr( 'class' , shoe + '_' + random + '_bg' ).fadeIn( vel );
			} );
			/*
			$( '#prod'    ).fadeOut( vel , function() {
				$( this ).attr( 'class' , 'produtos bg_' + shoe + '_' + random + '_a' ).fadeIn( vel );
			} );
			$( '#find'    ).fadeOut( vel , function() {
				$( this ).attr( 'class' , 'encontre bg_' + shoe + '_' + random + '_b' ).fadeIn( vel );
			} );
			$( '#contact' ).fadeOut( vel , function() {
				$( this ).attr( 'class' , 'contato  bg_' + shoe + '_' + random + '_c' ).fadeIn( vel );
			} );
			$( '#press'   ).fadeOut( vel , function() {
				$( this ).attr( 'class' , 'imprensa bg_' + shoe + '_' + random + '_d' ).fadeIn( vel );
			} );
			*/
		}, 5000);
		
	}
	
	
	
	$('.top , .top_s').click( function() { 
		$( '.top_vision' ).show();
		$( '.variacoes' ).find( '.left_arrow' ).hide();
		var tot_t2 = $( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.top_vision').length;
		if( tot_t2 > 4 )
			$( '.variacoes' ).find( '.right_arrow' ).show();
		else
			$( '.variacoes' ).find( '.right_arrow' ).hide();

		if( $( '.side' ).css( 'display' ) == 'none' ){
			$( '.side_s' ).hide();
			$( '.side' ).show();
		}

	});
	$( '.top' ).click(function(){
		$( '.side_vision' ).hide();
		$( this ).hide();
		$( '.top_s' ).show();
		var t2 = ($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.top_vision').length) * 192;
		$( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).stop().animate( { marginLeft: '0px' } , { queue:false , duration:1500 } ).width(t2);
	});
	$( '.top_s' ).click(function(){
		$( '.side_vision' ).show();
		$( this ).hide();
		$( '.top' ).show();
		var t2 = (($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.top_vision').length) * 192) + (($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.side_vision').length) * 192);
		$( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).stop().animate( { marginLeft: '0px' } , { queue:false , duration:1500 } ).width(t2);
	});

	$('.side , .side_s').click( function() { 
		$( '.side_vision' ).show();
		$( '.variacoes' ).find( '.left_arrow' ).hide();
		var tot_t2 = $( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.side_vision').length;
		if( tot_t2 > 4 )
			$( '.variacoes' ).find( '.right_arrow' ).show();
		else
			$( '.variacoes' ).find( '.right_arrow' ).hide();
		
		if( $( '.top' ).css( 'display' ) == 'none' ){
			$( '.top_s' ).hide();
			$( '.top' ).show();
		}
	});
	
	$( '.side' ).click(function(){
		$( '.top_vision' ).hide();
		$( this ).hide();
		$( '.side_s' ).show();
		var t2 = ($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.side_vision').length) * 192;
		$( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).stop().animate( { marginLeft: '0px' } , { queue:false , duration:1500 } ).width(t2);
	});
	$( '.side_s' ).click(function(){
		$( '.top_vision' ).show();
		$( this ).hide();
		$( '.side' ).show();
		var t2 = (($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.top_vision').length) * 192) + (($( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).find('.side_vision').length) * 192);
		$( this ).parent().parent().parent().parent().find( '.variacoes' ).find( '.slide_container' ).stop().animate( { marginLeft: '0px' } , { queue:false , duration:1500 } ).width(t2);
	});
	
	
	$('.loja1').click( function() {
		var bg_random = Math.floor( Math.random() * 3 ) + 1;
		$('.encontre_bg').attr( 'class' , 'encontre_bg encontre_int' + bg_random );
	});
	$('.loja2').click( function() {
		var bg_random = Math.floor( Math.random() * 3 ) + 4;
		$('.encontre_bg').attr( 'class' , 'encontre_bg encontre_int' + bg_random );
	});

	if( $( '.layer_atual' ).attr( 'class' ) != undefined ){
		$( '.thumb' ).find( '.layer1' ).addClass( 'opacity35' );
		$( '.mid_button' ).addClass( 'opacity35' );
		$( '.variacoes' ).find( '.mid_button' ).removeClass( 'opacity35' );
		$( '.sub-produtos' ).find( '.mid_button' ).mouseenter(function(){
			$( this ).removeClass( 'opacity35' );
		});
		$( '.sub-produtos' ).find( '.mid_button' ).mouseleave(function(){
			$( this ).addClass( 'opacity35' );
		});
		
		$( '.thumb' ).mouseenter( function() {
			$( this ).find( '.layer1' ).removeClass( 'opacity35' );
			$( this ).parent().parent().parent().find( '.mid_button' ).removeClass( 'opacity35' );
		} );
		
		$( '.thumb' ).mouseleave( function() {
			$( this ).find( '.layer1' ).addClass( 'opacity35' );
			$( this ).parent().parent().parent().find( '.mid_button' ).addClass( 'opacity35' );
		} );
	}
	
	resizeImg( $( '.resize-image' ) );
	
});

$( window ).resize( function() {
	resizeImg( $( '.resize-image' ) );
});


function scrollTo( selector ) {
	var targetOffset = $( selector ).offset().top;
	$( 'html,body' ).animate( { scrollTop: targetOffset } , 500 );
}

function scrollTo2( selector ) {
	var targetOffset = selector.offset().top;
	$( 'html,body' ).animate( { scrollTop: targetOffset } , 500 );
}

// set cookie by number of days
function setCookie( c ) {
	var COOKIE_NAME = 'ciaomao_bg';
	var options = { path: '/' , expires: 10 };
	$.cookie( COOKIE_NAME , c , options );
}


// resizes the home img object
function resizeImg( el )
{
	var win_w = $(window).width();
	var win_h = $(window).height();
	var field_w = win_w;
	var field_h = win_h;
	if( el ) {
		// resize container (parent li)
		el.parent().css( 'height' , field_h + 'px' ).css( 'width' , field_w + 'px' );
		// get image size
		var image_w = $( el ).width();
		var image_h = $( el ).height();
		var ratio;
		if (image_w/image_h >= field_w/field_h)
			ratio = field_h/image_h;
		else
			ratio = field_w/image_w;
		var final_w = image_w*ratio;
		var final_h = image_h*ratio;
		$( el ).css('width', final_w + 'px');
		$( el ).css('height', final_h + 'px');
		if ( final_w > field_w)
			$( el ).css('margin-left', '-'+((final_w-field_w)/2)+'px');
		else
			$( el ).css('margin-left', '0px');
		if ( final_h > field_h)
			$( el ).css('margin-top', '-'+((final_h-field_h)/2)+'px');
		else
			$( el ).css('margin-top', '0px');
	}
}