
JWD.Homepage = function(){
    this.twitterUser = twitterUser;
    
    this.getLastFmStream = function(){
        var ctxt = this;
        // random user
        $.getJSON(siteUrl + 'blog/frontgate.php?req=peoplelist', function(data){
            var emps = data;
            //var emp = Math.floor(Math.random()*data.length);
            //emp = emps[emp];
            
            if(emps.length > 0){
            //$('div#lastfmBox').html(' ');
                $('div#lastfmBox').randomFM({
                    //username: 'ARP1999',
                    //apikey: 'b1aea35caf8cefa43a4630ff1a0bd005',
                    users:emps,
                    number: 3,
                    artSize: 'large',
                    linkArt:true,
                    noart: '/images/noartwork.png',
                    onComplete: function(){
                        //return ctxt.load();
                    }
                });
            }
        });

    };
    
    this.getTwitterStream = function(){
        //get twitter
        var ctxt = this;
        $('#twitterBox').html(' ');
        var url = "http://twitter.com/status/user_timeline/"
			+ this.twitterUser + ".json?count="+6+"&callback=?";
        //var url = siteUrl + 'blog/frontgate.php?req=peoplelist';
        
		$.getJSON( url, function( data ){
		     
		    if(data.length > 0)
		    for(i=0;i< 1; i++){
		        $('#twitterBox').append('' + data[i].text + '');
		    }
		    //return ctxt.getExternalStreams();
		    //return ctxt.load();
		});
    };
    
    this.getLatestProjects = function(posts){
        var ctxt = this;
        
        var workCategories = new Array();
        //get categories for correct links in the URL
        $.getJSON(siteUrl + 'blog/get_categories.php' , function(data){
            workCategories = data;
            
            $.getJSON(siteUrl + 'blog/frontgate.php?req=projlist&all=1', function(data){
            //$.getJSON(siteUrl + 'projectsJSON.php', function(data){
                var featured = new Array();
                var moreFeatured = new Array();
                var bgProjects = new Array();
                var projects = new Array();
                $.each(data, function(i, item){
                    
                    if(item.featuredSection == '1' && item.section == 'work') {
                        featured.push(item); 
                    }
                    if(item.featured == '1') {
                        bgProjects.push(item);
                    } 
                    if(item.featured != '1' && item.featuredSection != '1'){
                        projects.push(item)
                    }
                });
                
                var randIndex = Math.floor(Math.random()*featured.length);
                var rezFeatured = featured;
                
                randFeatured1 = rezFeatured.splice(randIndex,1)[0];
                randIndex2 = Math.floor(Math.random()*rezFeatured.length);
                randFeatured2 = rezFeatured.splice(randIndex2,1)[0];

                
                // add featured blog posts too so we can pic the background featured out of work, play AND posts
                $.each(posts, function(i, item){
                    if(item.homepage_featured == 1) bgProjects.push(item);
                });
               
                randFeaturedBackground = bgProjects[Math.floor(Math.random() * bgProjects.length)];
                
                p =1;
                
                if(randFeatured1.section != 'post'){

                    $($('#workBox .subbox')[0]).html('<a href="' + randFeatured1.section + '.php#/featured/'+p+'/' + randFeatured1.friendlyUrl + '/1"><img title="<h2>' + randFeatured1.name.replace('|', '<br/>') + '</h2><br/>' + randFeatured1.shortDesc + '" width="105" height="71" src="' + randFeatured1.thumb + '"></a>');
                } 
                
                var catIndex;
                $.each(workCategories, function(i, item){
                    if(item.id == projects[0].tags[0]) catIndex = item;
                });
                //catIndex.name
                $($('#workBox .subbox')[1]).html('<a href="' + randFeatured2.section + '.php#/featured/'+p+'/' + randFeatured2.friendlyUrl +'/1"><img title="<h2>'+ randFeatured2.name.replace('|', '<br/>') + '</h2><br/>' + randFeatured2.shortDesc + '" width="105" height="71" src="'+randFeatured2.thumb+'"></a>');
                
                $('#workBox .subbox a img').tooltip({ 
                    track: false, 
                    delay: 0, 
                    showURL: false, 
                    showBody: " - ", 
                    fade: 250 
                });
                 
                
                //add first project's first image
                var id = 'img-homepage';
                var style = 'display:none;width: 100%; height: 100%;z-index:20;margin-top:0px;position:absolute;'
                
                $('#bgWrapper').image(randFeaturedBackground.images[0],id,style, function(){
                    var FullscreenrOptions = {  width: 1920, height: 1200, bgID: '#' + id };
                    $.fn.fullscreenr(FullscreenrOptions);
                    $('#' + id).fadeIn('fast', function(){
                        $('#' + id).css('z-index', '19');
                        $('#featuredDescription h1 a.hp-featured').html(randFeaturedBackground.name != undefined ? randFeaturedBackground.name.replace('|', '<br/>') : randFeaturedBackground.title.replace('|', '<br/>'));
                        $('#featuredDescription #descCont a.hp-featured').html(randFeaturedBackground.shortDesc != undefined ? randFeaturedBackground.shortDesc : randFeaturedBackground.excerpt);

                        if(randFeaturedBackground.section != 'post'){
                            
                            $('.hp-featured').attr('href', randFeaturedBackground.section +'.php#/featured/'+p+'/'+ randFeaturedBackground.friendlyUrl +'/1');
                        } else {
                            
                            $('.hp-featured').attr('href', 'read.php#/featured' + '/1/' + randFeaturedBackground.id + '/1');
                        }
                    });
                 });
            });
            
        });
        
    }
    
    this.getLatestPosts = function(){
        var ctxt = this;
        var d = new Date();
        var year = d.getFullYear();
        $.getJSON(siteUrl + 'blog/get_months.php?y=' + year, function(data){
            
            var posts = new Array();
            $.each(data, function(month, post){

                post.month = month;
                posts.push(post);
            });
            
            if(posts.length > 0){
                if(posts.length >= 2) var howManyPosts = 2;
                if(posts.length < 2) var howManyPosts = posts.length;
                for(i=0; i < howManyPosts ; i++){
                    var p = Math.ceil(i / 8);
                    if(p < 1) p =1;
                    if(posts[i].featured) var ptype = 'featured';
                    else ptype = 'archived';
                    
                    $($('#blogBox .subbox')[i]).html('<a href="read.php#/'+ ptype +'/'+p+'/'+ posts[i].id +'/1"><img title="<h2>'+ posts[i].title.replace('|', '<br/>') + '</h2><br/>' + posts[i].excerpt + '" width="105" height="71" src="'+posts[i].thumb+'"></a>');
                }
            }
            $('#blogBox .subbox a img').tooltip({ 
                track: false, 
                delay: 0, 
                showURL: false, 
                showBody: " - ", 
                fade: 250 
            });
            
            ctxt.getLatestProjects(posts);
            
        });
    };
    
    this.init = function(){
        this.getLastFmStream();
        this.getTwitterStream();
        this.getLatestPosts();
        
        //add rolovers for the widgets
        $('#workBox a').live('mouseenter',
            function(){
                //rolover
                $('#workBox .header a').html('RECENT PROJECTS');
                
            });
        $('#workBox a').live('mouseleave',function(){
                //rollout
                $('#workBox .header a').html('WORK.');
            }
        );
        
        $('#blogBox a').live('mouseenter',
            function(){
                //rolover
                $('#blogBox .header a').html('BLOG POSTS');
            });
        $('#blogBox a').live('mouseleave',function(){
                //rollout
                $('#blogBox .header a').html('READ.');
            }
        );
        
        $('#lastfmCont a').live('mouseenter',
            function(){
                //rolover
                $('#lastfmCont .header').html('LAST.FM');
            });
        $('#lastfmCont a').live('mouseleave',function(){
                //rollout
                $('#lastfmCont .header').html('MUSIC.');
            }
        );
        
        $('#tweetBox a').live('mouseenter',
            function(){
                //rolover
                $('#tweetBox .header').html('@JWD_CREATIVE');
            });
        $('#tweetBox a').live('mouseleave',function(){
                //rollout
                $('#tweetBox .header').html('FOLLOW.');
            }
        );
    };
    
    this.init();
};

