var clickedButton = null;
var currentOpenSite = "dj";
var currentOpenTabID = 'aba-home';
var currentOpenTab = null;
var currentSelectNews = null;

var animationDelay = 500;

function openSite(siteID, callback) { 

    $(this).parent().addClass('active');

    setLoading();   
    
    $('#site-piloto').fadeOut(animationDelay, function () {
         $('#site-piloto').html("");
        });
    
    
    $('#site-deejay').fadeOut(animationDelay, function () {
        
        $('#site-deejay').html("");
        
    });

    var siteContainer = {
        'dj': '#site-deejay',
        'piloto': '#site-piloto'
    };

    // Carrega o site desejado
    $.post(
        ('/home/site/'+siteID),
        {},
        function (data) {
            $(siteContainer[siteID]).html(data);
            
            $(siteContainer[siteID]).fadeIn(animationDelay);

            currentOpenSite = siteID;

            if(!hasURL()) {
                setupURL();
            }

            setIdle();

            if(callback != null && typeof callback == "function") {
                callback();
            } else {
                if(siteID == 'dj'){
                    openTab('aba-home');              
                }
            }
        }
        );
}

function hasURL() {    
    return (window.location.hash != null && window.location.hash.length > 0);
}

function openTab(tabID) {

    $('.tabDiv').slideUp(animationDelay);
    $('.tabLink').removeClass('active');
    
    //Verifica se a ba news foi acionada se sim troca o texto da aba-home para HOME
    checkHomeNews(tabID);

    $('#'+tabID).slideDown(animationDelay);
    $('.tabLink[tabID="'+tabID+'"]').addClass('active');

    currentOpenTab = $('#'+tabID);
    currentOpenTabID = tabID;
    
    setupURL();  
    buscaLive();
    
    if(tabID == 'aba-home'){
	getTweets();
    }
    
    if(tabID == 'aba-news'){

        if(currentSelectNews){
            openNews(currentSelectNews);  
        }else{
            selectNews(0);    
        }
        
        buscaLive();
    }
    
    if(tabID == 'aba-agenda'){
        $('.flyer').fancybox();
    }
    
    if(tabID == 'aba-booking'){
        validaForm();
    }
    if(tabID == 'aba-presskit'){
        validaFormPressKit();
    }
    
    if(tabID ==  'aba-biografia'){
        activeBio();
    }
    
    if(tabID == 'aba-video-fotos'){
        loadVideoPhotos();
        verifyBrowser();
    }

    var analytics_trackPage = currentOpenSite+'/'+tabID;

    _gaq.push(['_trackEvent', analytics_trackPage]);
    
    if(tabID == 'aba-carreira'){
        
        setTimeout(function () {
            LoadCarreira();
            carroselFotos();
            habilitaScrool();
            linkFoto();
        }, 10);
        
    }
    
    
}

function setupURL() {

    if(currentSelectNews){
        var hash = "!/"+currentOpenSite+"/"+currentOpenTabID+"/"+currentSelectNews;    
    }else{
        var hash = "!/"+currentOpenSite+"/"+currentOpenTabID;
    }
    
    window.location.hash = hash;
}

function readURL() {
    
    var hash = window.location.hash.split("/");
    currentOpenSite = hash[1];
    currentOpenTabID = hash[2];

    currentSelectNews = hash[3];    

    openSite(currentOpenSite, function() {
        if(currentOpenSite == "piloto") {
            fxPerfilPiloto();            
        }
        openTab(currentOpenTabID);
    })
}

function siteLanguage(lang,url){    
    $.post(url, {
        lang: lang
    }, function(data){       
        if(data == 'OK'){            
            document.location.reload();
        }
    })
}

function openNews(id){


    if(currentOpenSite != 'dj'){
        openSite('dj', function () {
            fxPerfilDJ();
            openTab('aba-news');
            fetchNews(id);
        });
        
        return;
    }   
        
    
    if(currentOpenTabID != 'aba-news'){
        openTab('aba-news');
    }
    
    var newHash = '#!/dj/aba-news';
    window.location.hash = newHash+'/'+id;

    fetchNews(id);
    
    var offset = $('.anchor-aba-news').offset().top; 
    $("html:not(:animated),body:not(:animated)").animate({
                scrollTop: offset
            }, 1000);  
            return false;  

  
    
}

function fetchNews(id) {
    $.post(
        '/Home/openNews?id='+id,
        {},
        function (data) {
            try {
                var d = $.parseJSON(data);
                var share = "<br/><div class=\"addthis_toolbox\"> <a href=\"#\" class=\"addthis_button_compact addthis-news\" style=\"float:left;\" addthis:url=\"\" addthis:title=\"\" addthis:description=\"\"> Compartilhe </a> <a class=\"addthis_button_facebook_like\" fb:like:layout=\"button_count\"></a></div>";
                $('#image_small').attr('src', d.small_image);
                $('#legend').html(d.legenda);
                $('#date').html(d.date);
                $('#title').html(stripslashes(stripslashes(d.title)));
                $('#content').html(stripslashes(stripslashes(d.content))+share);

                var url = 'http://www.raulboesel.com.br/home/senderFB/'+id;                
                var title = d.title;
                var caption = d.caption;

                $('.addthis-news').attr('addthis:url',url);
                $('.addthis-news').attr('addthis:title', title);
                $('.addthis-news').attr('addthis:description', caption);
                
            } catch (e) {
                alert(e);
            }
        }
    )
}

function getTweets(){
	
	$(".tweet").html('');
	
	$(".tweet").tweet({
        join_text: "auto",
        username: "raulboesel",
        avatar_size: 48,
        count: 4,   
        loading_text: "Carregando tweets..."
      });

	var list = $('.tweet_list ul').length;	
	
	if(list == 0){
		$('.tweet_list').append('<li> Erro ao carregar os tweets...</li>');
	}
}

function stripslashes(str) {
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Ates Goral (http://magnetiq.com)
    // +      fixed by: Mick@el
    // +   improved by: marrtins
    // +   bugfixed by: Onno Marsman
    // +   improved by: rezna
    // +   input by: Rick Waldron
    // +   reimplemented by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Brant Messenger (http://www.brantmessenger.com/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: stripslashes('Kevin\'s code');
    // *     returns 1: "Kevin's code"
    // *     example 2: stripslashes('Kevin\\\'s code');
    // *     returns 2: "Kevin\'s code"
    return (str + '').replace(/\\(.?)/g, function(s, n1) {
        switch (n1) {
        case '\\':
            return '\\';
        case '0':
            return '\u0000';
        case '':
            return '';
        default:
            return n1;
        }
    });
}
