/* INI: SCROLL UP FOR RESULTS */
window.move_scroll = function( top ){
	
	if( !top )
		top = 0;
	
	if( typeof top == "string" )
		top = parseInt( $( '#'+ top ).offset().top - 10 );
	
	var scroll = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	
	if( top == scroll )
		return;
	
	if( window.int_scroll )
		clearInterval( window.int_scroll );

	var 
		time = new Date(),
		time_start = time.getTime(),
		time_final = time_start + 1000,
		func = 'window.move_scroll_update(' + top + ',' + time_start + ',' + time_final + ')';
	
	window.int_scroll = setInterval( func ,10 );
};

window.move_scroll_update = function(prop_dest, time_start, time_dest){
	
	var 
		time = new Date(),
		time_now = time.getTime(),
		prop_start = self.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop,
		t = time_now - time_start,
		b = prop_start,
		c = prop_dest - prop_start,
		d = time_dest - time_start,
		newValue = Math.round( ( t == d ) ? b + c : c * ( -Math.pow( 2, -10 * t / d ) + 1 ) + b );
	
	if ( time_now >= time_dest || prop_start == prop_dest ){
		clearInterval( window.int_scroll );
		window.int_scroll = undefined;
	} else
		window.scrollTo(0, newValue);
		
};
/* END: SCROLL UP FOR RESULTS */

if( top != self ){ top.location.href = location.href; } //remove iframe

$(function(){

    $( '#goTop a' ).click(function(e){
        
        e.preventDefault();
        move_scroll(0);
    
    })
    if( $('#formContato').size() > 0 ){ contato(); }
    
    
    // call swf foto
    if( $('#fotos').size() == 1 ){

        $( '.ftLinks a' ).click(function(e){
        
            e.preventDefault();
            
            $( '.vdLinks a' ).css( 'color' , '#FFF' );
            $( this ).css( 'color' , '#6AD1F0' );
            var 
                
                link = $( this ).attr( 'href' ).replace( 'img/fotos/img' , '' );

            foto( 'contentFoto' , link );
        
        })
    
    }
    
    // call swf video
    if( $('#videos').size() == 1 ){
       
	    firstVideo = 'http://www.youtube.com/v/' + $( '.vdLinks a' ).eq(0).attr( 'href' ).replace( 'http://www.youtube.com/watch?v=' , '' )
        video( 'contentVideo' , firstVideo , '420' , '320' );
		
        $( '.vdLinks a' ).eq(0).css( 'color' , '#D46F29' );
        $( '.vdLinks a' ).click(function(e){
        
            e.preventDefault();
            
            $( '.vdLinks a' ).css( 'color' , '#2980B5' );
            $( this ).css( 'color' , '#D46F29' );
            var 
                
                link = 'http://www.youtube.com/v/' + $( this ).attr( 'href' ).replace( 'http://www.youtube.com/watch?v=' , '' ),
                text = $( this ).html();

            $( 'h3' ).text( 'Video - ' + text );
            
            // elemento, video, largura, altura
            video( 'contentVideo' , link , '420' , '320' );
        
        })
    
    }
    
    /* INI: MP3 */
    if( $('body#mp3').size() >= 1 ){
    
		firstAudio = $( 'ul.mp3Links li a' ).eq(0).attr( 'href' ).replace('http://mais.uol.com.br/view/','')
		audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=' + firstAudio , '#contentAudio' );
		$( 'ul.mp3Links li a' ).eq(0).css( 'color' , '#D46F29' );
        
        $( 'ul.mp3Links li a' ).click(function(e){
            e.stopPropagation(); e.preventDefault();
            
            var 
                mediaId = $( this ).attr( 'href' ).replace('http://mais.uol.com.br/view/',''),
                mediaText = $( this ).html();
                
            $( 'ul.mp3Links li a' ).css('color','#2980B5');
            $( this ).css( 'color' , '#D46F29' );
            $( 'h3' ).html( mediaText );
                
            audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=' + mediaId , '#contentAudio ' );
        
        })

    }


})
/* INI: CALL MP3 */
function audioMP3( url , place ){

	var audio = [];
	audio.push('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="330" height="50" align="middle">');
	audio.push('<param name="allowScriptAccess" value="sameDomain" />');
	audio.push('<param value="transparent" name="wmode"/>');
	audio.push('<param name="movie" value="' + url + '" />');
	audio.push('<param name="quality" value="high" />');
	audio.push('<embed src="' + url + '" quality="high" width="330" height="50" swLiveConnect=true align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" wmode="transparent" /></embed>');
	
	$( place ).html( audio.join('') );

}

/* INI: CALL SWF VIDEO */

function video( elm , vid , larg , alt ){

    $( '#' + elm ).html('<object width="' + larg + '" height="' + alt + '"><param value="' + vid + '" name="movie"/><param value="transparent" name="wmode"/><embed width="' + larg + '" height="' + alt + '" wmode="transparent" type="application/x-shockwave-flash" src="' + vid + '"/></object>');

}

/* INI: CALL PHOTO */

function foto( elm , foto ){

    $( '#' + elm ).html( '<img src="img/fotos/img' + foto + '" alt="" />' );

}

function contato(){
    
    $( '#assunto' ).focus();
    
    $('#formContato').submit(function(){
        
        var
            er = new RegExp(/\b[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,4}\b/),
            assunto = $('input#assunto').val(),
            nome = $('input#nome').val(),
            email = $('input#email').val(),
            texto = $('textarea#texto').val();
            
        if( assunto == "" ){
            
            statusM('Assunto');
            return false;
            
        }else if( nome == "" ){
            
            statusM('Nome');
            return false;
            
        }else if( !er.test( email ) ){
            
            statusM('Email');
            return false;
            
        }else if( texto == "" ){
            
            statusM('Texto');
            return false;
            
        }else{
            
            $('#statusMessage').html( '<p> Aguarde... </p>' );
            
            $.ajax({
                
                type: 'POST',
                url: 'mail.php',
                data: 'assunto=' + assunto + '&nome=' + nome + '&email=' + email + '&texto=' + texto,
                success: function( msg ){
                
                    $('#statusMessage').html( msg );
                    
                }
                
            });
            
        }
        return false;
        
    });
}

function statusM( campo ){
    
    $('#statusMessage').html( '<p>O campo <strong>' + campo + '</strong> deve ser preenchido corretamente!</p>' );
    $( '#' + campo.toLowerCase() ).focus();

}




