(function($){
    
    //Default user settings
    var numMSG = 20, account = 'pixelplus', placeHolder = 'scroll-con', loadingMsg = 'Loading tweets...';
            
    //System settings
    var msgNo = 1, feedID = 0, cFeedID =0;
    
    $.pixeltwitter = {
        launch: function(opt) {	
            
            numMSG=opt.numMSG?opt.numMSG:numMSG;
            account = opt.account?opt.account:account;
            placeHolder=opt.placeHolder?opt.placeHolder:placeHolder;
            loadingMsg=opt.loadingMsg?opt.loadingMsg:loadingMsg;  
            
            if($("#"+placeHolder)){	
                // show the load message
                this.loading();

                // query the twitter API and create the tweets list
                this.connect(1);		
            }   
        },
        loading: function(){
            $("#"+placeHolder).html(loadingMsg);
        },
        connect: function(x){
            $.ajax({
                type: "GET",
                url: "https://api.twitter.com/1/statuses/user_timeline.json?include_entities=true&include_rts=true&screen_name="+account+"&count="+numMSG,
                async: false,
                dataType: "jsonp",
                timeout: 1000,
                success: function(json){
    
                    if(x==1){
                        $("#"+placeHolder).html(""); 
                    } 
        
                    $.each(json,function(i,item) {
                        
                        if(x==1 || (i<numMSG && item.id>feedID)){
                            
                            if(i==0){
                                
                                cFeedID = item.id; 
                            }
                            
                            if (item.text != "undefined") {
                                
                                var name, screenName;
                                
                                var tweet = item.text;
                                var d = new Date(item.created_at.replace('+','UTC+'));
                                var created_at = ('0' + d.getDate()).slice(-2)+"."+('0' + d.getMonth()).slice(-2)+"."+d.getFullYear();
								var created_hour = ('0' + d.getHours()).slice(-2)+":"+('0' + d.getMinutes()).slice(-2);  
                                var link =  "http://twitter.com/"+item.user.screen_name+"/status/"+item.id_str;  
                                
                                if(!isEmpty(item.retweeted_status)){
       
                                    name = item.retweeted_status.user.name;
                                    screenName = item.retweeted_status.user.screen_name;
                                    tweet = item.retweeted_status.text;
                                    
                                    mHTML='<div id="tweet'+ msgNo +'" class="news"><dl class="tarih"><dt class="tarih-gun">' + created_at.split(".")[0] + '</dt><dd class="tarih-ay">' + (Number(created_at.split(".")[1])+1) + '</dd><dd class="tarih-yil">' + created_at.split(".")[2] + '</dd></dl><p class="twitMessage">' + $.pixeltwitter.parseLinks(tweet) + '</p></div>';
                  
                                }else{
                                    
                                    name = item.user.name;
                                    screenName = item.user.screen_name;
                                    
                                    mHTML='<div id="tweet'+ msgNo +'" class="news"><dl class="tarih"><dt class="tarih-gun">' + created_at.split(".")[0] + '</dt><dd class="tarih-ay">' + (Number(created_at.split(".")[1])+1) + '</dd><dd class="tarih-yil">' + created_at.split(".")[2] + '</dd></dl><p class="twitMessage">' + $.pixeltwitter.parseLinks(tweet) + '</p></div>';
                                
                                }

                                $("div#tweets").append(mHTML);

                                $('#tweet'+msgNo).hide();
                                $('#tweet'+msgNo).show("slow");
								
                                msgNo++;								
                            }
                            
                        }
						//$('div#tweets').next('div.rightBoxShadow').css('height','100%');
                    });	
                    feedID=cFeedID;
        
                    //JScrollbar specific settings 
                    $('div#'+placeHolder+' ul li').hover(function(){
                        $(this).addClass('selected').siblings().removeClass('selected');
                    },function(){
                        $(this).removeClass('selected');
                    });
                    
                },
                error: function(){
                    $("#"+placeHolder).html("Service temporarily unavailable. Try again later or contact the administrator for more information.");
                }
            });
        },
        parseLinks: function(str){
            
            var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
            str = str.replace(exp,"<a href='$1' class='extLink' target='_blank'>$1</a>"); 
            var exp = /[\@]+([A-Za-z0-9-_]+)/ig;
            str = str.replace(exp,"<a href='http://twitter.com/$1' class='profileLink'>@$1</a>"); 
            var exp = /[\#]+([A-Za-z0-9-_]+)/ig;
            str = str.replace(exp,"<a href='http://twitter.com/#!/search?q=$1' class='hashLink'>#$1</a>"); 
            
            return str;
                        
        }
    };
    
})(jQuery);

function isEmpty(obj) {
    if (typeof obj == 'undefined' || obj === null || obj === '') return true;
    if (typeof obj == 'number' && isNaN(obj)) return true;
    if (obj instanceof Date && isNaN(Number(obj))) return true;
    return false;
}


