/* 
    Te permito ver y analizar el codigo, mientras no lo copies descaradamente D:<
    Rodrigo D. Cuervo (rod750)
*/

$(document).ready(function(){
    $(window).bind('hashchange', function(){
        if(window.location.hash != ''){
            switch(window.location.hash){
                case '#!/sobremi':
                    septimumAjaxCall('Sobre Mi');
                break;
                case '#!/proyectos':
                    septimumAjaxCall('Proyectos');
                break;
                case '#!/experimentos':
                    septimumAjaxCall('Experimentos');
                break;
                case '#!/contacto':
                    septimumAjaxCall('Contacto');
                break;
                case '#index':
                    $('#hidden-page').hide();
                    $('#main-column, #side-column').show();
                break
            }
        }
    });
    $(window).trigger('hashchange');
    //CONTACT FORM
    contactformCallback = function(data){
        if(data.error == "true"){
            $('#contactform-response').html(data.message);
        }
        else{
            $('#name, #email, #message').val('');
            $('#contactform-response').html(data.message);
        }
    }
    $('#contactform').live("submit", function(e){
        e.preventDefault();
        iname = $('#name').val();
        iemail = $('#email').val();
        imessage = $('#message').val();
        $.ajax({
            url:'http://septimum.com/send.php',
            dataType:'jsonp',
            type:'post',
            timeout:15000,
            data:{
                name: iname,
                email: iemail,
                message: imessage
            }
        });
    });
    //PAGES
    function septimumAjaxCall(name){
        $.ajax({
            url:'http://septimum.com/pages.php',
            dataType:'jsonp',
            timeout:15000,
            data:{
                callback:'septimumCallback',
                name:name
            }
        });
    }
    septimumCallback = function(data){
        $('#hidden-page').html('<header><h3>'+data.name+'</h3></header><div id="content">'+data.content+'</div>');
        if($('#hidden-page').is(':visible') != true){
            $('#hidden-page').show();
            $('#main-column, #side-column').hide();
        }
    }
    $('#top-bar ul li a').click(function(e){
        window.location.hash = $(this).attr('href');
        if(window.location.hash === '#index'){
            $('#hidden-page').hide();
            $('#main-column, #side-column').show();
        }
        e.preventDefault();
    });
    //TABS
    $('#timelines section').hide();
    $('#timelines #twitter-tl').show();
    $('.twitter-tab').addClass('on');
    $('#timelines header nav ul li a').click(function(e){
        $('#timelines section').hide();
        $('#timelines header nav ul li a').removeClass('on');
        $(this).addClass('on');
        var tab_clicked = $(this).attr('rel');
        $('#'+tab_clicked).show();
        e.preventDefault();
    });

    //TWITTER

    $.ajax({
        url:"http://search.twitter.com/search.json?q=from:rod750",
        dataType:"jsonp",
        timeout:15000,
        success:function(data){
            var tweets = [];
            var alt = false;
            var cssclass = '';
            $.each(data.results, function(i, tweet){
                if(alt == false){
                    cssclass = '';
                    alt = true;
                }
                else{
                    cssclass = ' class="alt"';
                    alt = false;
                }
                var tweet_date = new Date(tweet.created_at);
                var now_date = new Date();
                var date_diff = now_date - tweet_date;
                var tweet_seconds = Math.round(date_diff/1000);
                var created_at = undefined;
                if(tweet_seconds < 60){
                    created_at = tweet_seconds;
                    if(created_at < 2){
                        created_at = created_at+' segundo';
                    }
                    else{
                        created_at = created_at+' segundos';
                    }
                }
                else if(tweet_seconds < 3600){
                    created_at = Math.round(tweet_seconds/60);
                    if(created_at < 2){
                        created_at = created_at+' minuto';
                    }
                    else{
                        created_at = created_at+' minutos';
                    }
                }
                else if(tweet_seconds < 86400){
                    created_at = Math.round(tweet_seconds/3600);
                    if(created_at < 2){
                        created_at = created_at+' hora';
                    }
                    else{
                        created_at = created_at+' horas';
                    }
                }
                else{
                    created_at = Math.round(tweet_seconds/86400);
                    if(created_at < 2){
                        created_at = created_at+' d&iacute;a';
                    }
                    else{
                        created_at = created_at+' d&iacute;as';
                    }
                }
                var tweet_text = tweet.text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$1" target="_blank">$1</a>').replace(/[@]+([A-Za-z0-9-_]+)/ig, '<a href="http://twitter.com/#!/$1" target="_blank">@$1</a>').replace(/[#]+([A-Za-z0-9-_ñáéíóú]+)/ig, '<a href="http://twitter.com/#!/search?q=%23$1" target="_blank">#$1</a>');
                var source = $("<span/>").html(tweet.source).text();
                tweets.push('<article'+cssclass+'><img class="profile-image" src="'+tweet.profile_image_url+'"  /><p class="tweet">'+tweet_text+'</p><footer><a href="https://twitter.com/#!/'+tweet.from_user+'" target="_blank">@'+tweet.from_user+'</a> Hace <a href="https://twitter.com/#!/'+tweet.from_user+'/status/'+tweet.id_str+'" target="_blank">'+created_at+'</a> via '+source+'</footer><br style="clear:both" /></article>');
            });
            $('#twitter-tl').html(tweets.join(''));
        },
        error:function(){
            console.log('Twitter feed load error');
        }
    });

    // TUMBLR

    tumblrCallback = function(data){
        var posts = [];
        var cssclass = '';
        var alt = false;
        $.each(data.response.posts, function(i, post){
            var post_type = post.type;
            if(alt == false){
                cssclass = '';
                alt = true;
            }
            else{
                cssclass = ' class="alt"';
                alt = false;
            }
            var post_date = new Date(post.date);
            var now_date = new Date();
            var date_diff = now_date - post_date;
            var post_seconds = Math.round(date_diff/1000);
            var created_at = undefined;
            if(post_seconds < 60){
                created_at = post_seconds;
                if(created_at < 2){
                    created_at = created_at+' segundo';
                }
                else{
                    created_at = created_at+' segundos';
                }
            }
            else if(post_seconds < 3600){
                created_at = Math.round(post_seconds/60);
                if(created_at < 2){
                    created_at = created_at+' minuto';
                }
                else{
                    created_at = created_at+' minutos';
                }
            }
            else if(post_seconds < 86400){
                created_at = Math.round(post_seconds/3600);
                if(created_at < 2){
                    created_at = created_at+' hora';
                }
                else{
                    created_at = created_at+' horas';
                }
            }
            else{
                created_at = Math.round(post_seconds/86400);
                if(created_at < 2){
                    created_at = created_at+' d&iacute;a';
                }
                else{
                    created_at = created_at+' d&iacute;as';
                }
            }
            switch(post_type){
                case 'text':
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><h3>'+post.title+'</h3><div class="tumblr-post">'+post.body+'</div><footer>Publicado por <a href="http://rod750.tumblr.com" target="_blank">'+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a></footer><br style="clear:both" /></article>');
                break;
                case 'photo':
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><a href="'+post.photos[0].alt_sizes[1].url+'"><img class="post-image" src="'+post.photos[0].alt_sizes[2].url+'" /></a><footer><a href="http://rod750.tumblr.com" target="_blank">Publicado por '+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a></footer><br style="clear:both" /></article>');
                break;
                case 'quote':
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><div class="tumblr-post quote"><p>"'+post.text+'" &mdash;'+post.source+'</p></div><footer>Publicado por <a href="http://rod750.tumblr.com" target="_blank">'+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a></footer><br style="clear:both" /></article>');
                break;
                case 'link':
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><div class="tumblr-post link"><a href="'+post.url+'" target="_blank">'+post.title+' &raquo;</a>'+post.description+'</div><footer>Publicado por <a href="http://rod750.tumblr.com" target="_blank">'+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a></footer><br style="clear:both" /></article>');
                break;
                case 'audio':
                var player = post.player.replace('color=FFFFFF', 'color=e0e0e0');
                if(post.source_title != undefined & post.source_url != undefined){
                    source = ' vía <a href="'+post.source_url+'" target="_blank">'+post.source_title+'</a>';
                }
                else{
                    source = '';
                }
                if(post.caption != undefined){
                    caption = post.caption;
                }
                else{
                    caption = '';
                }
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><div class="tumblr-post audio">'+player+caption+'</div><footer>Publicado por <a href="http://rod750.tumblr.com" target="_blank">'+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a>'+source+'</footer><br style="clear:both" /></article>');
                break;
                case 'video':
                if(post.source_title != undefined & post.source_url != undefined){
                    source = ' vía <a href="'+post.source_url+'">'+post.source_title+'</a>';
                }
                else{
                    source = '';
                }
                posts.push('<article'+cssclass+'><img class="profile-image" src="http://api.tumblr.com/v2/blog/rod750.tumblr.com/avatar/48" /><div class="tumblr-post video">'+post.player[1].embed_code+'</div><footer>Publicado por <a href="http://rod750.tumblr.com" target="_blank">'+post.blog_name+'</a> hace <a href="'+post.post_url+'" target="_blank">'+created_at+'</a>'+source+'</footer><br style="clear:both" /></article>');
                break;
            }
        });
        $('#tumblr-tl').html(posts.join(''));
    }

    $.ajax({
        url:"http://api.tumblr.com/v2/blog/rod750.tumblr.com/posts",
        dataType:"jsonp",
        timeout:15000,
        data:{
            api_key:'aqq2btY6l4P9eNzdne5kzOyRph0rEWUWNBgTFa4BofQUQQWM1i',
            jsonp:'tumblrCallback'
        }
    });

    // GOOGLE +

    googleplusCallback = function(data){
        var posts = [];
        var cssclass = '';
        var alt = false;
        $.each(data.items, function(i, post){
            if(alt == false){
                cssclass = '';
                alt = true;
            }
            else{
                cssclass = ' class="alt"';
                alt = false;
            }
            var post_date = new Date(post.published);
            var now_date = new Date();
            var date_diff = now_date - post_date;
            var post_seconds = Math.round(date_diff/1000);
            var created_at = undefined;
            if(post_seconds < 60){
                created_at = post_seconds;
                if(created_at < 2){
                    created_at = created_at+' segundo';
                }
                else{
                    created_at = created_at+' segundos';
                }
            }
            else if(post_seconds < 3600){
                created_at = Math.round(post_seconds/60);
                if(created_at < 2){
                    created_at = created_at+' minuto';
                }
                else{
                    created_at = created_at+' minutos';
                }
            }
            else if(post_seconds < 86400){
                created_at = Math.round(post_seconds/3600);
                if(created_at < 2){
                    created_at = created_at+' hora';
                }
                else{
                    created_at = created_at+' horas';
                }
            }
            else{
                created_at = Math.round(post_seconds/86400);
                if(created_at < 2){
                    created_at = created_at+' d&iacute;a';
                }
                else{
                    created_at = created_at+' d&iacute;as';
                }
            }
            var avatar = post.actor.image.url.replace('sz=50', 'sz=48');
            var post_verb = post.verb;
            var attachments = post.object.attachments;
            var post_attachments = [];
            if(attachments != undefined){
                $.each(post.object.attachments, function(i, attachment){
                    switch(attachment.objectType){
                        case 'video':
                        post_attachments.push('<p><a href="'+attachment.url+'" target="_blank"><img src="'+attachment.image.url+'" /></a></p>');
                        break;
                        case 'article':
                        post_attachments.push('<p><a href="'+attachment.url+'" target="_blank">'+attachment.displayName+'</a></p><p>'+attachment.content+'</p>');
                        break;
                        case 'photo':
                        post_attachments.push('<p><a href="'+attachment.fullImage.url+'" target="_blank"><img src="'+attachment.image.url+'" /></a></p>');
                        break;
                    }
                });
                post_attachments.join('');
            }
            switch(post_verb){
                case 'post':
                posts.push('<article'+cssclass+'><img class="profile-image" src="'+avatar+'" /><div class="google-plus-post"><p>'+post.object.content+'</p><div class="attachments">'+post_attachments+'</div></div><footer>Publicado por <a href="'+post.actor.url+'" target="_blank">'+post.actor.displayName+'</a> hace <a href="'+post.url+'" target="_blank">'+created_at+'</a></footer><br style="clear:both" /></article>');
                break;
                case 'share':
                var source = '';
                if(post.object.actor.displayName != undefined ){
                    source = '  vía <a href="'+post.object.actor.url+'" target="_blank">'+post.object.actor.displayName+'</a>';
                }
                posts.push('<article'+cssclass+'><img class="profile-image" src="'+avatar+'" /><div class="google-plus-post"><p>'+post.object.content+'</p><div class="attachments">'+post_attachments+'</div></div><footer>Publicado por <a href="'+post.actor.url+'" target="_blank">'+post.actor.displayName+'</a> hace <a href="'+post.url+'" target="_blank">'+created_at+'</a>'+source+'</footer><br style="clear:both" /></article>');
                break;
            }
        });
        $('#google-plus-tl').html(posts.join(''));
    }

    $.ajax({
        url:"https://www.googleapis.com/plus/v1/people/103024556228323331978/activities/public?key=AIzaSyBlr8Ul3R2U4pyLpcQgrNbbB-4HTrx-mnE&callback=googleplusCallback",
        dataType:"jsonp",
        timeout:15000,
        data:{
            callback:'googleplusCallback',
            key:'AIzaSyBlr8Ul3R2U4pyLpcQgrNbbB-4HTrx-mnE'
        }
    });

    // TWITPIC
    
    $.ajax({
        url:"http://api.twitpic.com/2/users/show.jsonp?username=rod750",
        dataType:"jsonp",
        timeout:15000,
        success:function(data){
            var photos = [];
            $('body').css('background-image', 'url('+data.profile_background_image_url+')');
            $.each(data.images, function(i, photo){
                if(i<5){
                    photos.push('<article><a href="http://twitpic.com/'+photo.short_id+'" class="image" style="background:#000000 url(http://twitpic.com/show/large/'+photo.short_id+') center center" target="_blank"></a></article>');
                }
            });
            $('#images').html(photos.join(''));
        },
        error:function(){
            console.log('Twitpic feed error');
        }
    });
});
