﻿// global namespace
var DMG = {};



/****** On dom ready ******/
$(document).ready( function() {
    
    //embedded fonts
    _typeface_js.renderDocument();
       
    //tweetfeed
    
    DMG.tweetfeed = {};
    $('.tweetFeed').each(function(e)
    {
        DMG.fetchTweets(this);
    });
    
    //auto rolling.
    var interval = setInterval(function()
    {
       $('.jFlowNext').trigger('click')
    },15000);
    
    
});

/****** On Window loaded ******/
$(window).bind('load', function () {
//once window is fully loaded including all images etc
    
    //image rolling
    $(".imgover")
    .hover(
        function () {
            this.src = this.src.replace(".png","x.png").replace(".gif","x.gif");
        },
        function () {
            this.src = this.src.replace("x.png",".png").replace("x.gif",".gif");
        }
    );
    
    //image preloading
    $(".imgover").each( 
        function () {
            $('<img />')
            .attr('src', this.src.replace(".png","x.png").replace(".gif","x.gif"))
            .load(function(){
                $('#hidden').append( $(this) );
            });
        });
        

});

/****** 
 * get twitter feed
 * alex 16.6.2009
 * based on monitter.com widget
 *******/

jQuery.fn.reverse=Array.prototype.reverse;

String.prototype.linkify=function()
{
return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g, function(m)
    {
        return m.link(m);
    });
};

String.prototype.linkuser=function()
{
    return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u)
    {
        var username=u.replace("@","")
        return u.link("http://twitter.com/"+username);
    });
};

String.prototype.linktag=function()
{
    return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t)
    {
        var tag=t.replace("#","%23")
        return t.link("http://search.twitter.com/search.json?q="+tag);
    });
};

DMG.fetchTweets = function (elem)
{
    
    if(typeof jQuery.fn.corners == "undefined") { //we need the rounded corners here

        $.ajaxSetup({async: false});
        $.getScript('/Scripts/jquery.corners.min.js');
        $.ajaxSetup({async: true});
        
    }
     
    elem=$(elem);
    q=elem.attr('id').split("~")[1];
    
    var rpp = elem.attr('id').split("~")[0]; 
    if(q!=DMG.tweetfeed['last_id'+q])
    {
        DMG.tweetfeed['last_id'+q]=0;     
    }

    var url="http://search.twitter.com/search.json?q="+q+"&rpp="+rpp+"&since_id="+DMG.tweetfeed['last_id'+q]+"&callback=?";
    $.getJSON(url,function(json)
    {
        $('div.tweet:gt('+rpp+')',elem).each(function(){$(this).fadeOut('slow')});
        $(json.results).reverse().each(function()
        {
            if($('#tw'+this.id,elem).length==0)
            {
                
                var thedate=new Date(Date.parse(this.created_at));
                var thedatestr=thedate.getDate()+'.'+(thedate.getMonth() + 1)+'.'+thedate.getFullYear();
                
                var divstr='<div id="tw'+this.id+'" class="tweet"><a href="http://twitter.com/'+this.from_user+'" target="_blank"><img src="'+this.profile_image_url.replace("normal","bigger")+'" ></a><div class="tweettext">'+thedatestr+' - '+this.text.linkify().linkuser().linktag()+'</div><div class="clear">&nbsp;</div></div>';
                DMG.tweetfeed['last_id'+q]=this.id;
                elem.prepend(divstr);
                
                $('#tw'+this.id,elem).hide();
                $('#tw'+this.id+' img',elem).hide();
                $('#tw'+this.id+' img',elem).fadeIn(4000);
                $('#tw'+this.id,elem).fadeIn('slow').corners("10px");
                
            }
        });
        
        setTimeout(function()
        {
           DMG.fetchTweets(elem,q)
        },10000);
    });
    return(false);
}
