/* INI: TIME REMAINS */
function restTime( YY , MM , DD , HH , MI, i ) {

    var 
        SS = 00,
        h = new Date(),
        ff = new Date( YY , MM-1 , DD , HH , MI , SS ),
        ss = parseInt( (ff - h) / 1000 ),
        mm = parseInt( ss / 60 ),
        hh = parseInt( mm / 60 ),
        dd = parseInt( hh / 24 ),
        ss = ss - ( mm * 60 ),
        mm = mm - ( hh * 60 ),
        hh = hh - ( dd * 24 );

    var remainingTime = [];

    remainingTime.push( ( dd && dd > 1 ) ? '<strong>' + dd + '</strong> dias, ' : ( dd == 1 ? '<strong>1</strong> dia, ' : '' ) );
    remainingTime.push( ( toString(hh).length ) ? '<strong>' + hh + '</strong> horas, ' : '' );
    remainingTime.push( ( toString(mm).length ) ? '<strong>' + mm + '</strong> minutos e ' : '' );
    remainingTime.push( '<strong>' + ss + '</strong> segundos para o Show.' );
	
	if( dd + hh + mm + ss > 0 ){
	
		$( '#restTime' + i ).html( 'Faltam ' + remainingTime.join('') );
		setTimeout(function(){restTime( YY , MM , DD , HH , MI , i )},1000);
	
	}else{
	
		$( '#restTime' + i ).html( '<em>Rush project... on stage.</em>' );
	
	}

}
/* END: TIME REMAINS */


function timeRemains(){

	/* INI: TIME REMAINS */
	$( '.showTime' ).each(function(){	
		var 
			item = $( this ).text(),
			item = item.split(',');
    
		restTime( item[0] , item[1] , item[2] , item[3] , item[4] , item[5] );
	})
	/* END: TIME REMAINS */

}


/* 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 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('') );

}

$(function(){

    //$( 'html head' ).prepend( '<meta property="og:image" content="http://www.rushproject.com.br/extras/rush-project.gif" />' );
    timeRemains();
    
    if( $('#home').size() > 0 ){
        
        urlLike = 'www.rushproject.com.br'; 
        like = [];
        like.push( '<iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2F' + urlLike +'&amp;layout=button_count&amp;show_faces=false&amp;width=450&amp;action=like&amp;font&amp;colorscheme=light&amp;height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100%; height:21px;display:block;margin:0 0 15px 15px" allowTransparency="true">');
        like.push( '</iframe>');
        
        $( '#facebookLike' ).html( like.join('') );
    }
   
    /* INI: Audio */
    if( $('body#audio').size() >= 1 ){
    
        h = location.toString().match(/=(.*)/);
        if( !h || h == ""){

            audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=8929927' , '#music' );
            $( 'div#audiosURL ul li a' ).eq( 0 ).css('color','red');
            
        }else{
    
            audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=' + h[1] , '#music' );
            $( '#audiosURL h2' ).html( 'Audio' );
            
        }
        
        $( '#audiosURL ul li a' ).click(function(e){
            e.stopPropagation(); e.preventDefault();
            
            var 
                mediaId = $( this ).attr( 'href' ).replace('http://mais.uol.com.br/view/',''),
                mediaText = $( this ).html();
                
            $( 'div#audiosURL ul li a' ).css('color','#2A4F69');
            $( this ).css( 'color','red' );
            $( '#audiosURL h2' ).html( mediaText );
                
            if( history.pushState ){
            
                var stateObj = { foo: "bar" };
                history.pushState( stateObj , $( this ).text() , 'http://www.rushproject.com.br/audio.html?au=' + $( this ).attr('href').replace( 'http://mais.uol.com.br/view/' , '' ) );
                
            }
                
            audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=' + mediaId , '#music' );
        
        })

    }
    
    if( $('body#home').size() >= 1 ){
    
        audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=8854306' , '#music1' );
    
    }   
    /* END: Audio */

    $( 'a#irTopo').click(function(e){
        
        e.preventDefault();
        move_scroll(0);
    
    })
    
    $( '.homeLastPost').mouseover(function(){
    
        $( this ).next( '.homeCommentShow' ).fadeIn('fast');
    
    })
    
    $( '.homeLastPost').mouseout(function(){
    
        $( this ).next( '.homeCommentShow' ).fadeOut('fast');
    
    })

	$("div#language p img").eq(0).css("margin-left","10px");
    
    /* videos URL */
    $('div#videosURL ul li a').click(function(e){
    
        e.stopPropagation(); e.preventDefault();
        $( 'div#videosURL ul li a' ).css('color','#2A4F69');
        $( this ).css( 'color' , 'red' );
        var 
            link = 'http://www.youtube.com/v/' + $( this ).attr( 'href' ).replace( 'http://www.youtube.com/watch?v=' , '' ),
            text = $( this ).html();
            
        if( history.pushState ){
        
            var stateObj = { foo: "bar" };
            history.pushState( stateObj , $( this ).text() , 'http://www.rushproject.com.br/videos.html?vd=' + $( this ).attr('href').replace( 'http://www.youtube.com/watch?v=' , '' ) );
            
        }
        
        $( 'h2' ).html( text );
        $( "div#videoShow" ).html('<object width="600" height="420"><param value="' + link +'" name="movie"/><param value="transparent" name="wmode"/><embed width="600" height="420" wmode="transparent" type="application/x-shockwave-flash" src="' + link + '"/></object>');
        
        
    })
    $('p#song').html('<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"><param name="play" value="true"/><param name="allowScriptAccess" value="sameDomain" /><param value="transparent" name="wmode"/><param name="movie" value="http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=363406" /><param name="quality" value="high" /><embed src="http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=363406" 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" play="true" /></embed></object>');

})

    function videoShow(vid){

        $("div#videoShow").html('<object width="600" height="420"><param value="' + vid + '" name="movie"/><param value="transparent" name="wmode"/><embed width="600" height="420" wmode="transparent" type="application/x-shockwave-flash" src="' + vid + '"/></object>');

    }

    var
        er = new RegExp(/\b[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,4}\b/),
        red = '4px solid red',
        green = '4px solid #01CF00';

// Guestbook

function validaGuest(){
    if($("input#nome").val() == ""){
        $("p#validGuest").html('O Campo <strong>Nome</strong> deve ser preenchido');
        $("input#nome").focus();
        $("input#nome").css('border',red);
        return false;
    }else{ 
        $("input#nome").css('border',green); 
    }
    if($("textarea#texto").val() == ""){
        $("p#validGuest").html('O Campo <strong>Texto</strong> deve ser preenchido');
        $("textarea#texto").focus();
        $("textarea#texto").css('border',red);
        return false;
    }else{ 
        $("textarea#texto").css('border',green); 
    }
    if($("input#ft").val() == ""){
        $("p#validGuest").html('O <strong>código</strong> da imagem deve ser preenchido');
        $("input#ft").focus();
        $("input#ft").css('border',red);
        return false;
    }else{ 
        $("input#ft").css('border',green); 
    }
}
function validaGuest_eng(){
    if($("input#nome").val() == ""){
        $("p#validGuest").html('The Field <strong>Name</strong> must be completed');
        $("input#nome").focus();
        $("input#nome").css('border',red);
        return false;
    }else{ 
        $("input#nome").css('border',green); 
    }
    if($("textarea#texto").val() == ""){
        $("p#validGuest").html('The Field <strong>Comment</strong> must be completed');
        $("textarea#texto").focus();
        $("textarea#texto").css('border',red);
        return false;
    }else{ 
        $("textarea#texto").css('border',green); 
    }
    if($("input#ft").val() == ""){
        $("p#validGuest").html('The <strong>image code</strong> must be completed');
        $("input#ft").focus();
        $("input#ft").css('border',red);
        return false;
    }else{ 
        $("input#ft").css('border',green); 
    }
}

// Contato

$(function(){ 

    if( $('#contact').size() == 1 ){
    
        $("#assunto").focus(); 
        
        $("input#tel").keyup(function(){

            n = $(this).val().replace(/[^0-9]+/g,'');
            $(this).val(n);

        })
    }
})

function verifica(){

    var
        assunto = $("input#assunto"),
        lang = $("input#msgForm"),
        message = $("div#message"),
        nome = $("input#nome"),
        email = $("input#email"),
        comentario = $("textarea#comentario"),
        endereco = $("input#endereco"),
        ft = $("input#ft");

    if( assunto.val() == "" ){
    
        if( lang.val() == '1'){
            message.html('<p>O Campo <strong>Assunto</strong> deve ser preenchido</p>');
        }else{
            message.html('<p>The Field <strong>Subject</strong> must be completed</p>');
        }
        assunto.focus();
        assunto.css( 'border' , red );
        return false;
        
    }else{ 
        assunto.css( 'border' , green ); 
    }
    
    if( nome.val() == "" ){
        if( lang.val() == '1'){
            message.html('<p>O Campo <strong>Nome</strong> deve ser preenchido</p>');
        }else{
            message.html('<p>The Field <strong>Name</strong> must be completed</p>');
        }       
        nome.focus();
        nome.css( 'border' , red );
        return false;
    }else{ 
        nome.css('border',green); 
    }
    
    if( !er.test( email.val() ) ){
        if( lang.val() == '1' ){
            message.html('<p>O Campo <strong>E-mail</strong> deve ser válido</p>');
        }else{
            message.html('<p>The Field <strong>E-mail</strong> must be completed</p>');
        }
        email.focus();
        email.css('border',red);
        return false;
    }else{ 
        email.css('border',green); 
    }
    
    if( comentario.val() == "" ){
        if( lang.val() == '1' ){
            message.html('<p>O Campo <strong>Comentário</strong> deve ser preenchido</p>');
        }else{
            message.html('<p>The Field <strong>Commentary</strong> must be completed</p>');
        }
        comentario.focus();
        comentario.css('border',red);
        return false;
    }else{ 
        comentario.css('border',green); 
    }
    
    if( ft.val() == "" ){
        if( lang.val() == '1' ){
            message.html('<p>O Campo <strong>Comentário</strong> deve ser preenchido</p>');
        }else{
            message.html('<p>The Field <strong>Commentary</strong> must be completed</p>');
        }
        ft.focus();
        ft.css('border',red);
        return false;
    }else{ 
        ft.css('border',green); 
    }
    
    if( assunto.val() != "" || nome.val() != "" || email.val() != "" || comentario.val() != "" ) {
    
        if( lang.val() == '1'){
            message.html('<p style="color:#000">Enviando mensagem...</p>');
        }else{
            message.html('<p style="color:#000">Sending message...</p>');
        }
        $.ajax({
            type: "POST",
            url: "formmail.html",
            data: "assunto=" + assunto.val() + "&nome=" + nome.val() + "&email=" + email.val() + "&tel=" + $("input#tel").val() + "&endereco=" + endereco.val() + "&comentario=" + comentario.val() + "&msgForm=" + lang.val() + "&ft=" + ft.val() + "&r=" + $('input#r').val() + "&jmore=sim",
            success: function(msg){
                
                $("div#message").html('<p style="color:green">' + msg + '</p>');
                
            }
        })
        return false;
    }
    
}

window.onpopstate = function(event) {

    //console.info( "state: " + JSON.stringify( event.state ) );
    
    if( $('body#videos').size() >= 1 ){
        
        h = location.toString().match(/=(.*)/);
        $( 'h2' ).html( 'Videos' );
        videoShow( 'http://www.youtube.com/v/' + h[1] );
        
    }
    if( $('body#audio').size() >= 1 ){
        
        h = location.toString().match(/=(.*)/);
        $( 'h2' ).html( 'Audios' );
        audioMP3( 'http://storage.mais.uol.com.br/embed_audio2.swf?mediaId=' + h[1] , '#music' );
    
    }
  
}





