
$().ready( function() {
	/* menu header
	 ------------------------------------------------------------ */
	$( '.menu_button' ).parent().mouseenter( function() {
		if( $( this ).find( 'ul' ).hasClass( 'none' ) ){
			$( this ).find( 'ul' ).show();
            $( '.m2' ).addClass( 'none' ).hide();
        }
	} );
    
	$( '.menu_button' ).parent().mouseleave( function() {
		if( $( this ).find( 'ul' ).hasClass( 'none' ) ){
			$( this ).find( 'ul' ).hide().removeClass( 'menu_hover' );
            $( '.m2' ).removeClass( 'none' ).show();
        }
	} );

    $( '.menu_button' ).mouseenter( function() {
		$( this ).addClass( 'menu_hover' ).removeClass( 'menu_out' );
	} );
	$( '.menu_button' ).mouseleave( function() {
		$( this ).removeClass( 'menu_hover' ).addClass( 'menu_out' );
	} );

	/* menu produtos
	 ------------------------------------------------------------ */

	// categoria
	$( '.submenu_button' ).click( function() {
		$( this ).parent().find( '.prod_sub' ).toggleClass( 'hide' );
	} );

	// subcategoria
	$( '.cat_button' ).click( function() {
        if ( $( this ).parent().find( 'ul' ).css( 'display' ) != 'none' )
            $( this ).parent().find( 'ul' ).hide();
        else
            $( this ).parent().find( 'ul' ).show();
	} );
	


	/* show image in target
	 ------------------------------------------------------------ */
	// funcao para fazer a troca do src
	$('a.sit:first').css( 'cursor' , 'default' ).addClass( 'active' );
	$('a.sit').click( function() {
		$( 'a.sit' ).css( 'cursor' , 'pointer' );
		$( 'a.sit' ).removeClass( 'active' );
		var img = new Image(); 
		img.onload = function(){
			$( '#img_large' ).find( 'img' ).attr( 'src' , $( this ).attr( 'href' ) );
			img.onload=function(){};
		};
		img.src = $( this ).attr( 'href' );
		$( '#img_large' ).html( img );
		$( this ).css( 'cursor' , 'default' ).addClass( 'active' );
		return false;
	} );

    /* estados e cidades
	---------------------------------------------------------------------------------------------------------------------------------- */
	// JSON responsavel por popular as cidades de acordo com o estado escolhido escolhida
	$( '#state' ).change( function() {
		$( '#city' ).find( 'option' ).remove();
                var u = window.document.location.href;
                var s = u.split('/');
                var session = s[s.length-1];
                var base_url = $( 'base' ).attr( 'href' );
		$( '#city' ).append( '<option value="0" selected="selected">carregando...</option>' )
		$.getJSON( base_url + 'index.php/json/city/by_state/' + $( this ).val() + '/' + session ,
			function( data ){
				var option = "";
				$.each( data , function( i , a ) { // i = indice | a = indices associativos
					option += '<option value="' + a.city + '">' + a.city+ '</option>';
				});
				$( '#city' ).find( 'option:first' ).html( 'selecione' ).parent().attr( 'disabled' , '' ).append( option );
		});

	} );

    $( '.img_home' ).cycle({
		fx: 'fade' // choose your transition type, ex: fade, scrollUp, shuffle, etc...
	});



} );

