Shadowbox.init();

var timeout_lista_radios = null;

var hideListaRadios = function() {
    $('ul.lista-radios').fadeOut('slow');
}

$(document).ready(
    function() {
        if($('a.radio-ativa').length > 0) {
            $('a.radio-ativa').click(
                function() {
                    $('ul.lista-radios').fadeIn('slow');
                }
            );
                
            $('ul.lista-radios').hover(
                function() {
                    $('ul.lista-radios').show();
                    if(timeout_lista_radios != null)
                        clearTimeout(timeout_lista_radios);
                },
                function() {
                    timeout_lista_radios = setTimeout("hideListaRadios()",1000);
                }
            );
        }
        
        if($('input.replace-title').length > 0){
            $('input.replace-title').focus(
                function() {
                    var valor = $(this).val();
                    var title = $(this).attr('title');
                    
                    if(valor == title)
                        $(this).val('');
                }
                );
            $('input.replace-title').blur(
                function() {
                    var valor = $(this).val();
                    var title = $(this).attr('title');
                    
                    if(valor == '')
                        $(this).val(title);
                }
                );
        }
        
        if($('input.senha-over').length > 0) {
            $('input.senha-over').focus(
                function() {
                    $(this).hide();
                    $(this).next('input.senha-input').show().focus();
                }
                );
        }
        if($('input.senha-input').length > 0) {
            $('input.senha-input').blur(
                function() {
                    if($(this).val() == '') {
                        $(this).hide();
                        $(this).prev('input.senha-over').show();
                    }
                }
                );
        }
        
        if($('.banner-images').length > 0) {
            $('.banner-images').cycle({
                next:   '.banner-right',
                prev:   '.banner-left',
                pager:  '#banner-nav',
                pagerAnchorBuilder: function(idx, slide) { 
                    return '<li><a href="javascript:void(0);"></a></li>'; 
                } 
            });
        }
        
        var sliding = false;
        if($('.top-10 .artista').length > 0) {
            $('.top-10 .artista').hover(
                function() {
                    if(!$(this).hasClass('selected-artista') && !sliding) {
                        sliding = true;
                        var anterior = $('.top-10 .selected-artista');
                        anterior.animate({
                            width:'80px'
                        });
                        $(this).animate({
                            width:'230px'
                        },function(){
                            anterior.removeClass('selected-artista');
                            $(this).addClass('selected-artista');
                            sliding = false;
                        });
                        
                    }
                }
                );
        }
        
        if($('.patrocinadores-list').length > 0) {
            $('.patrocinadores-list').jcarousel(); 
        }
        
        $('#selectEstado').change(function(){
            //endereco = window.location.href.split('uniaofm.com.br');
            
            val = $(this).val().split('-');                        
            
            if($(this).val() != -1){
                $.ajax({
                    type: "POST",
                    url: '/contato/get-cidade',
                    data: 'estado=' + val[0],
                    success: function(retorno) {
                        $('#selectCidade').html(retorno).removeAttr('disabled');
                    },
                    beforeSend: function(){
                        $('#loading').fadeIn('slow');
                    },
                    complete: function(){
                        $('#loading').fadeOut('slow');
                    }
                });
            }else{
                $('#selectCidade').html('').attr("disabled","disabled");
            }
        });
    }
    );
    
$(window).load(
    function() {
        if($('.page-content').length > 0) {
            var sidebar_height = $('.sidebar').height();
            var content_height = $('.page-content .left-column').height();
            var fix_height = sidebar_height > content_height ? sidebar_height : parseInt(content_height)+15;
            
            $('.page-content').css('height',fix_height+'px');
        }
    }
    );
    
/*function checkRequired(id){    
    error = false;
    textError = 'O(s) campo(s) que deve(m) ser preenchido:  \n';
    $('.required').each(
        function(){
           if($(this).val() == ''){
               textError = textError+ $(this).attr('id') +' \n';
               error = true;
           }
        }
    );
    if(error == false){
        return true;
    }else{
        alert(textError);
        return false;
    }
}*/

/* Validação com tipsy */


$.fn.tipsy.defaults = {
    delayIn: 0,
    delayOut: 0,
    fade: true,
    gravity: "s",
    html: true,
    offset: 0,
    opacity: 1,
    trigger: 'focus',
    title: 'title',
    fallback: ''
};



$.validator.setDefaults({
    debug: false,
    ignoreTitle: true,
    errorClass: "invalid",
    validClass: "accept",
    unhighlight: function(element, errorClass, validClass) {
        $(element).attr("original-title",'');
        $(element).attr("title",'');
        $('.tipsy').hide();
        $(element).removeClass(errorClass).addClass(validClass);
        $(element).parent().parent().prev().removeClass(errorClass);
    },
    errorPlacement: function(error, element) {
        $(element).attr("title", error.html());
        $(element).tipsy({
            trigger: 'focus'
        });
        $('.tipsy').show();
    }
});

$.validator.addMethod(
    "selectMethod",
    function(value) {
        var ret = false;
        if(value != -1)
            ret = true;
        return ret;
    }, "Selecione uma op&ccedil;&atilde;o!"
    );

bindContatoForm = function(url) {
    $('#contato-form').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: url,
                data: $(form).serialize(),
                success: function() {
                    $('#dialog').html('Enviado com sucesso.');
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
								$('#contato-form')[0].reset();
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            mensagem: {
                required: true,
                minlength: 10
            },
            regiao: "selectMethod",
            enviarArea: "selectMethod"
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no m&iacute;nimo 5 caracteres!" 
            },
            email: "Email inv&aacute;lido",
            mensagem: {
                required: "Digite sua mensagem!",
                minlength: "Este campo deve ter no m&iacute;nimo 10 caracteres!" 
            },
            regiao: "Selecione uma regi&atilde;o!",
            enviarArea: "Selecione uma &aacute;rea!"
        }
    }); 
}
bindCadastroForm = function(url) {
	$('#cadastro-form').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: url,
                data: $(form).serialize(),
                success: function(data) {
                    $('#dialog').html(data);
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
								$('#cadastro-form')[0].reset();
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            senha: {
                required: true,
                minlength: 6,
				maxlength: 10
            },
            confirmarSenha: {
                required: true,
                equalTo: "#senha"				
            }
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no m&iacute;nimo 5 caracteres!" 
            },
            email: "Email inv&aacute;lido",
            senha: {
                required: "Digite uma senha!",
                minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!",
				maxlength: "Este campo deve ter no maximo 10 caracteres!"
            },
            confirmarSenha: {
                required: "Digite a confirma&ccedil;&atilde;o da senha!",
            	equalTo: "As senhas n&atilde;o coincidem!"
			}
        }
    }); 	   
}

//para cadastro promoção quando tem pergunta 
bindCadastroPerForm = function(url) {
	$('#cadastro-form-per').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "/cadastro",
                data: $(form).serialize(),
                success: function() {
                    $('#dialog').html('Enviado com sucesso.');
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            senha: {
                required: true,
                minlength: 6,
				maxlength: 10
            },
            confirmarSenha: {
                required: true,
                equalTo: "#senha"				
            },
            pergunta: {
                required: true,
				minlength: 10				
            }
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no m&iacute;nimo 5 caracteres!" 
            },
            email: "Email inv&aacute;lido",
            senha: {
                required: "Digite uma senha!",
                minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!",
				maxlength: "Este campo deve ter no maximo 10 caracteres!"
            },
            confirmarSenha: {
                required: "Digite a confirma&ccedil;&atilde;o da senha!",
            	equalTo: "As senhas n&atilde;o coincidem!"
			},
			pergunta: {
                required: "Digite sua resposta!",
                minlength: "Este campo deve ter no m&iacute;nimo 10 caracteres!" 
            }
        }
    }); 	   
}
// para quando tem endereço
bindCadastroEndForm = function(url) {
	$('#cadastro-form-end').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "/cadastro",
                data: $(form).serialize(),
                success: function() {
                    $('#dialog').html('Enviado com sucesso.');
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            senha: {
                required: true,
                minlength: 6,
				maxlength: 10
            },
            confirmarSenha: {
                required: true,
                equalTo: "#senha"				
            },
            selectCidade: "selectMethod",
			selectEstado: "selectMethod",
            cep: {
                required: true				
            },
            endereco: {
                required: true,
				minlength: 10			
            }
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no m&iacute;nimo 5 caracteres!" 
            },
            email: "Email inv&aacute;lido",
            senha: {
                required: "Digite uma senha!",
                minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!",
				maxlength: "Este campo deve ter no maximo 10 caracteres!"
            },
            confirmarSenha: {
                required: "Digite a confirma&ccedil;&atilde;o da senha!",
            	equalTo: "As senhas n&atilde;o coincidem!"
			},
			selectCidade: "Selecione uma cidade!",
			selectEstado: "Selecione um estado!",
			cep: "Informe seu cep!",
			endereco: {
                required: "Informe seu endere&ccedil;o!",
            	minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!"
            }
        }
    }); 	   
}

//para qaundp p cadastro de pomoção é inteiro
bindCadastroPerEndForm = function(url) {
	$('#cadastro-form-per-end').validate({
        submitHandler: function(form) {
            $.ajax({
                type: "POST",
                url: "/cadastro",
                data: $(form).serialize(),
                success: function() {
                    $('#dialog').html('Enviado com sucesso.');
                    $('#dialog').dialog({
                        title: 'Aviso',
                        modal: true,
                        draggable: false,
                        resizable: false,
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                                $('#dialog').html('');
                            }
                        }
                    });                     
                },
                beforeSend: function(){
                    $('#loading').fadeIn('slow');
                },
                complete: function(){
                    $('#loading').fadeOut('slow');
                }
            });
        },
        rules: {
            nome: {
                required: true,
                minlength: 5
            },
            email: {
                required: true,
                email: true
            },
            senha: {
                required: true,
                minlength: 6,
				maxlength: 10
            },
            confirmarSenha: {
                required: true,
                equalTo: "#senha"				
            },
            selectCidade: "selectMethod",
			selectEstado: "selectMethod",
            cep: {
                required: true				
            },
            endereco: {
                required: true,
				minlength: 10			
            },
            pergunta: {
                required: true,
				minlength: 10				
            }
        }, 
        messages: {
            nome: {
                required: "Informe seu nome!",
                minlength: "Este campo deve ter no m&iacute;nimo 5 caracteres!" 
            },
            email: "Email inv&aacute;lido",
            senha: {
                required: "Digite uma senha!",
                minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!",
				maxlength: "Este campo deve ter no maximo 10 caracteres!"
            },
            confirmarSenha: {
                required: "Digite a confirma&ccedil;&atilde;o da senha!",
            	equalTo: "As senhas n&atilde;o coincidem!"
			},
			selectCidade: "Selecione uma cidade!",
			selectEstado: "Selecione um estado!",
			cep: "Informe seu cep!",
			endereco: {
                required: "Informe seu endere&ccedil;o!",
            	minlength: "Este campo deve ter no m&iacute;nimo 6 caracteres!"
            },
			pergunta: {
                required: "Digite sua resposta!",
                minlength: "Este campo deve ter no m&iacute;nimo 10 caracteres!" 
            }
        }
    }); 	   
}





showLocutores = function() {
    if ($('.locutores').is(":hidden")){
        $('.locutores').slideDown("slow");
        $('#arrow').fadeOut(function(){
            $('#arrow').removeClass('left-arrow');
            $('#arrow').addClass('down-arrow').fadeIn();
        });
    }else{
        $('.locutores').slideUp("slow");
        $('#arrow').fadeOut(function(){
            $('#arrow').removeClass('down-arrow');
            $('#arrow').addClass('left-arrow').fadeIn();
        });
    }    
}

appendVideo = function(id, title) {
    $("#vid").animate({"opacity": 0}, 500, function() {
        $("#vid object").css("visibility", "hidden");
        $("#vid #description").css("visibility", "hidden");
        $("#vid object [name=movie]").attr("value","http://www.youtube.com/v/"+id+"?version=3&amp;hl=en_US&amp;rel=0");
        $("#vid object embed").attr("src","http://www.youtube.com/v/"+id+"?version=3&amp;hl=en_US&amp;rel=0");
        $("#vid #description").html(title);
        $("#vid object").css("visibility", "visible");
        $("#vid #description").css("visibility", "visible");
        $("#vid").delay(500).animate({"opacity": 1}, 500 );
	var targetOffset = $('#vid').offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 1000);
	
    });
}

appendVideoToNotice = function(objeto,idYoutube) {
    var object = '<object id="object" height="385" width="480"><param name="movie" value="http://www.youtube.com/v/'+idYoutube+'?version=3&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/'+idYoutube+'?version=3&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" height="385" width="480" wmode="transparent" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
    objeto.parent().prev().prev().children().html(object);
}

appendLogin = function() {
    var targetOffset = $('.login').offset().top - 5;
    $('html,body').animate({scrollTop: targetOffset}, 1000);
    $('[name=login]').focus();
}

$(document).ready( 
    function (){	
        if($('#contato-form').length > 0) {
            endereco = window.location.href.split('uniaofm.com.br');
            bindContatoForm(endereco[1]);
        }
		if($('#cadastro-form').length > 0) {
            endereco = window.location.href.split('uniaofm.com.br');
            bindCadastroForm(endereco[1]);
        }
		if($('#cadastro-form-per').length > 0) {
            endereco = window.location.href.split('uniaofm.com.br');
            bindCadastroPerForm(endereco[1]);
        }
		if($('#cadastro-form-end').length > 0) {
            endereco = window.location.href.split('uniaofm.com.br');
            bindCadastroEndForm(endereco[1]);
        }
		if($('#cadastro-form-per-end').length > 0) {
            endereco = window.location.href.split('uniaofm.com.br');
            bindCadastroPerEndForm(endereco[1]);
        }
    }
);
