﻿
gUrl = 'http://www.gisborneherald.co.nz';

///////////////////////////////////////////////////////////
// nsTraverse
///////////////////////////////////////////////////////////

$.nsTraverse=function(node,xml)
{    
    var center = node.indexOf(':');
    var ns = node.substr(0,center);
    var tagName = node.substr(center+1);

    return $(getElementsByTagName(tagName,ns,ns,xml));

    function getElementsByTagName(tagName, ns, prefix, scope){
	    var elementListForReturn = scope.getElementsByTagName(prefix+':'+tagName);
	    if(elementListForReturn.length == 0){
		    elementListForReturn = scope.getElementsByTagName(tagName);
		    if(elementListForReturn.length == 0){
			    elementListForReturn = scope.getElementsByTagName('ns:'+tagName);
			    if(elementListForReturn.length == 0 && document.getElementsByTagNameNS){
				    elementListForReturn = scope.getElementsByTagNameNS(ns, tagName);
			    } // end if
		    } // end if
	    } // end if                 
	    return elementListForReturn;
	    
    } // end function    
   
} // end nsTraverse 

///////////////////////////////////////////////////////////
// QueryString
///////////////////////////////////////////////////////////

QueryString=function(qStr)
{
    this.items={};
    
    if(qStr.length>0)
    {    
        var args = qStr.split('&');
        for(var i=0;i<args.length;i++)
        {
            var pair=args[i].split('=');
            var name=decodeURIComponent(pair[0]);
		    var value = (pair.length==2)
			    ? decodeURIComponent(pair[1])
			    : name;
		    this.items[name]=value;
    		        
        } // end 'for'    
    }
    
    // get the value of a given key name
    this.getVal=function(name)
    {
        return this.items[name];
        
    } // end 'get'
    
    // set the value of a given key name
    this.setVal=function(name,value)
    {
        this.items[name]=value;
        
    } // end 'set'  
    
    // convert query object to a string
    this.toString=function()
    {
        var str='';
        
        var s=false;
        for(prop in this.items)
        {
            if(s)
            {
                str+='&';                
            }else{
                s=true;
            }
        
            str+=prop+'='+this.items[prop];
        }
                
        return str;
        
    } // 'toString'

} // end QueryString

///////////////////////////////////////////////////////////
// PopUpWindow
///////////////////////////////////////////////////////////

function PopUpWindow(url,id,width,height)
{    
    return window.open(url,id,'width='+width+',height='+height+',left=0,top=0,scrollbars=1');
}

///////////////////////////////////////////////////////////
// Validation
///////////////////////////////////////////////////////////

function IsValueEmpty(id){
    return ($(id).val()=='');
}

///////////////////////////////////////////////////////////
// Twitter
///////////////////////////////////////////////////////////
// Example url: http://www.twitter.com/statuses/user_timeline/12345.xml

$.fn.tweets=function(o){
  
    o=o==undefined?new Object():o;
    
    return this.each(function(){
        var cnt=$(this);
        var maxResults=o.maxResults==undefined?5:o.maxResults;
        $.ajax({
            url:'http://www.twitter.com/statuses/user_timeline/12345.json',
            cache:false,
            dataType:'jsonp',
            success:function(j){
                var count=0;
                $.each(j,function(){
                    if(count<maxResults){
                        var d=new Date(Date.parse(this.created_at));
                        cnt.append('<div class="Tweet"><div class="CreatedAt"></div><div class="Text">'+this.text+'</div></div>');
                    }
                    count++;
                }); // end 'each'
                if(o.success!=undefined){
                    o.success();
                }
            } // end 'success'
        }); // end 'ajax'        
    }); // end 'each'

} // end 'twitter'

///////////////////////////////////////////////////////////
// ShortenText
///////////////////////////////////////////////////////////

function ShortenText(text,maxChars){
    var shorten=text;
    if(shorten.length>maxChars){
        shorten=shorten.substr(0,maxChars);
    }
    var parts=shorten.split(' ');
    shorten='';
    for(var i=0;i<parts.length-1;i++){
        shorten+=parts[i]+' ';
    }    
    shorten+='...';    
    return shorten;    
} // end 'ShortenText'

///////////////////////////////////////////////////////////
// fitImage
///////////////////////////////////////////////////////////

$.fn.fitImage=function(options){

    settings=$.extend({}, {width:100,height:100}, options);

    return this.each(function(){
    
        var img = $(this);
        var wrap=$('<div class="fit-image"></div>');
        
        // setup the wrap div
        wrap.width(settings.width);
        wrap.height(settings.height);
        wrap.css('overflow','hidden');            
        img.wrap(wrap);
                        
        // setup the image
        img.width(settings.width);
        if(img.height()<settings.height){   
            img.width('auto');                 
            img.height(settings.height);        
        }    
        img.css('position','relative');
        img.css('top',(settings.height/2)-(img.height()/2));                               
        img.css('left',(settings.width/2)-(img.width()/2));
     
    });

}; // end 'fitImage'
        
