var prov_var = 'provider';
$(document).ready(function() {
       $(".jquery-selectbox").selectbox();

        $(".pesquisa").selectbox({
                className:"pesquisa"
        });
        $(".selectBig").selectbox({
                className:"selectBig"
        });
        $(".filtroLocais").selectbox({
                className:"filtroLocais"
        });

        cycle('imgs_ambiente_small', 'navigation', 0);
        cycle('imgs_ambiente', '', 0);

        $(".botaoFechar a").click(function(){
            if(!confirm(lang_msg['confirm_remove_local'])){
                return false;
            }
        });

        $(".deleteNota").click(function(){
            if(!confirm(lang_msg['confirm_remove_nota_local'])){
                return false;
            }
        });
        $(".deleteRoteiro").click(function(){
            if(!confirm(lang_msg['confirm_remove_roteiro'])){
                return false;
            }
        });

        $('img.captify').captify({
            speedOver: 'fast',
            speedOut: 'normal',
            hideDelay: 500,
            animation: 'slide',
            prefix: '',
            opacity: '0.7',
            className: 'caption-bottom',
            position: 'bottom',
            spanWidth: '100%'
        });

        $("a.fancy_link").live('click',function(){
            var href=$(this).attr('href');
            //startLoading();
            $.get(href, function(res) {
                processAjaxResult(res);
                loadAjaxScripts();
                //stopLoading();
            });
            return false;
        });

        $("a.fancy_iframe").live('click',function(){
            var title=$(this).attr('title');
            var href=$(this).attr('href');
            $.fancybox({
                'padding'		: 0,
                'autoScale'		: false,
                'transitionIn'	: 'none',
                'transitionOut'	: 'none',
                'title'			: title,
                'width'         : 680,
                'height'		: 495,
                'href'			: href,
                'type'			: 'iframe'
            });
            return false;
        });

        $(".foto").hover(function(){
            $(this).find(".title_prod").stop(true, true).slideDown();
        },function(){
            $(this).find(".title_prod").stop(true, true).slideUp();
        });

        //----------------------------
        $('.carrousel').cycle({
            fx:		 'scrollHorz',
            timeout: 0,
            next:   '#setaDireita',
            prev:   '#setaEsquerda'
        });

        $('.viewport #nav .foto').click(function(){
            var i=$(this).attr('id').split('_')[2];
            $(".galeriaFotos #galeriaBigFoto .foto:visible").fadeOut();
            $(".galeriaFotos #galeriaBigFoto .foto").eq(i).fadeIn();
        });

        $('.submitselect').change(function(){
            $(this).closest('form').submit();
        });

        loadAjaxScripts();
        if(gup(prov_var)!='' && $.cookie('EventCookie')!=null){
            var temp_link=$('<a></a>').addClass('fancy_link').attr('href',$.cookie('EventCookie'));
            temp_link.click();
        }
});


/**
 * @param imgs_id id do contentor com as imagens
 * @param menu_id id do contentor(<ul>) onde será gerada a paginacao
 * @param startingslide indice do slide a iniciar
 */
function cycle(imgs_id, menu_id, startingslide){
    $(document).ready(function(){
		$('#'+imgs_id).cycle({
			fx: 'fade',
			startingSlide: startingslide,
			pager: '#'+menu_id,
			pagerAnchorBuilder: function(idx, slide){
				return '<li><a href="#"></a></li>';
			},
			updateActivePagerLink: function(pager, activeIndex){
				$(pager).find('li:eq('+activeIndex+')').addClass('activeSlide').siblings().removeClass('activeSlide');
			}
		});
		if(imgs_id=='imgs_ambiente_small'){
            $('#'+imgs_id).children().each(function(){
                $(this).click(function(){
                    cycle('imgs_ambiente', '', $(this).attr("rel"));
                })
            });
        }
    });
}

function initAddThis(){
    var script = 'http://s7.addthis.com/js/250/addthis_widget.js#pubid=ra-4eeccef406de92ac&domready=1';
    if (window.addthis){
        window.addthis = null;
    }
    $.getScript(script);    
}

function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

var processAjaxResult=function(res){
    if(typeof res === "object"){
        //------------------
        if(typeof res.updateNotaLocal === "object"){
            $("#nota_"+res.updateNotaLocal.id+" .desc").html(res.updateNotaLocal.descricao);
        }
        if(typeof res.newNotaLocal === "object"){
            $("#notas_"+res.newNotaLocal.id_relacao).append(res.newNotaLocal.conteudo);
        }
        if(typeof res.deleteNotaLocal === "object"){
            $("#nota_"+res.deleteNotaLocal.id).fadeOut('slow',function(){
                $(this).remove();
            });
        }
        if(typeof res.deleteLocal === "object"){
            $("#detalheLocal_"+res.deleteLocal.id).fadeOut('slow',function(){
                $(this).remove();
                if($("#formPesquisaRoteiro").length > 0){
                    $("#formPesquisaRoteiro").submit();
                }
            });
        }
        if(typeof res.partilhaRoteiro === "object"){
            $("#partilha_roteiro_"+res.partilhaRoteiro.id+' a').html(res.partilhaRoteiro.message);
        }
        //------------------

        if(res.result!=''){
            $.fancybox({
                hideOnOverlayClick:false,
                content:res.result
            });
        }else{
            $.fancybox.close();
        }
    }else if(res!=''){
        $.fancybox({
            hideOnOverlayClick:false,
            content:res
        });
    }else{
        $.fancybox.close();
    }
}

var loadAjaxScripts=function(){
    $(".fancy_form").submit(function(){
        var result=$(this).serialize();
        var action=$(this).attr('action');
        var method=$(this).attr('method');
        $.ajax({
                   type:method,
                   url:action,
                   data:result,
                   success:function(res){
                        processAjaxResult(res);
                        loadAjaxScripts();
                   }
        });
        return false;
    });

    $("a.fancyimg").fancybox();
    initAddThis();
}

