
/*
 * HAL
 * Author: Greg Sabia
 * Support: http://gregsabia.com
 *
 * Computer extends and initializes HAL
*/ 

var HAL = function(document,window){
    
    var self = this;
    var deploy = {
        openThePodBayDoors : function(){
            return console.log('I\'m sorry Greg, I\'m afraid I can\'t do that.');
        },
        _extended : false,
        _getTimeout : 300,
        please : {
            extend : function(obj){
                return $.extend(HAL._extended,obj);
            },
            clearText : function(theField){
                if(thefield.defaultValue==thefield.value){ thefield.value = "" }
            },
            replaceText : function(thefield){
                if(thefield.value==""){ thefield.value = thefield.defaultValue }
            },
            preloadImages : function(){
                var d=document;
                if(d.images){
                    if(!d.p) d.p=new Array();
                    var i,j=d.p.length,a=this.preloadImages.arguments; for(i=0; i<a.length;i++)
                    if(a[i].indexOf("#") != 0){
                        d.p[j]=new Image; d.p[j++].src=a[i];
                    }
                }
            },
            getAnchor : function(){
                var url_anchor = document.location.toString();
                if(url_anchor.match('#')){
                    return url_anchor.split('#')[1];
                }else{
                    return false;
                }
            },
            setAnchor : function(anchor){
                var url_anchor = document.location.toString();

                document.location = url_anchor.split('#')[0]+'#'+anchor;
            }
        },
        init : function(){
            // DOM Ready
            HAL.flickr.init();
            HAL.check_css.init();
            
            // Post-load higher resolution image
            var preloaded_image = $.extend(new Image,{ src : 'assets/images/bg_man_simonpais.jpg', id : 'preload', width : 1, height: 1, onload : function(){ $('body').css('background-image','url("'+this.src+'")'); } });
            
            return this;
        },
        flickr : {
            // Flickr stuff
            feed : false,
            imglist : '#flickr-images',
            init : function(){
                return false; // For now...

                HAL.flickr.getFeed(function(data){
                    // Let's do something with the photos
                    var max_photos = 11;
                    var html = '';

                    $.each(data.items,function(i,photo){
                        if((i+1) >= max_photos){
                            html += HAL.flickr.wrapPhoto(photo,true);
                            return false;
                        }else{
                            html += HAL.flickr.wrapPhoto(photo);
                        }
                    });

                    $(HAL.flickr.imglist).append(html+'<br class="clear" />');
                });
            },
            getFeed : function(callback){
                $.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=11099253@N07&lang=en-us&format=json&jsoncallback=?",function(data){
                    if(typeof data.generator !== 'undefined'){
                        HAL.flickr.feed = {
                            photos : data.items
                        };

                        if(callback) callback(data);
                    }else{
                        return false;
                    }
                });
            },
            wrapPhoto : function(photo,last){
                var tag = (last) ? '<li class="item last">' : '<li class="item">';

                return tag+'<a href="'+photo.link+'" target="_blank" class="thumbmask"><img src="'+photo.media.m.replace(/_m.jpg/,'_s.jpg')+'" width="75" height="75" alt="'+photo.title+'" border="0" /></a></li>';
            }
        },
        analytics : {
            executable : function(){
                if(typeof _gaq !== 'undefined'){
                    return true;
                }else{
                    this.log('Couldn\'t track event because analytics is not executable');
                    return false;
                }
            },
            log : function(s){
                if(typeof console !== 'undefined') console.log(s)
            },
            track : {
                pageview : function(url,callback){
                    // Track the pageview
                    if(HAL.analytics.executable()){
                        var c = _gaq.push(['_trackPageview',url]);
                        HAL.analytics.log('Tracked '+url+' using _trackPageview');
                    }

                    if(callback) callback();

                    return c;
                },
                event : function(category,action,label,value){
                    if(HAL.analytics.executable()){
                        var c = _gaq.push(['_trackEvent',category,action,label,value]);
                        HAL.analytics.log('Tracked '+category+':'+action+' using _trackEvent');
                    }

                    return c;
                }
            }
        },
        check_css : {
            init : function(){
                this.checkWindowSize();
                $(window).resize(this.checkWindowSize);
            },
            checkWindowSize : function(){
                if($(window).width() < 1350){
                    $('body').addClass('compact')
                }else{
                    $('body').removeClass('compact')
                }
            }
        },
        check : {
            init : function(){
                return this.parentNode;
            }
        }
    }
    
    return deploy;
    
}(document,window)
