/**
 * IRIS.Widget 0.41
 * (c) 2008 Iris Experience S.L.
 * 
 * For details, see http://www.irisexperience.com/
 */
var IRIS = window.IRIS || {};

IRIS.Widget = {
	loadJsCSS: function(filename, filetype) {
	    if (filetype == "js") { //if filename is a external JavaScript file
	        var fileref = document.createElement('script')
	        fileref.setAttribute("type", "text/javascript")
	        fileref.setAttribute("src", filename)
	    }
	    else 
	        if (filetype == "css") { //if filename is an external CSS file
	            var fileref = document.createElement("link")
	            fileref.setAttribute("rel", "stylesheet")
	            fileref.setAttribute("type", "text/css")
	            fileref.setAttribute("href", filename)
	        }
	    if (typeof fileref != "undefined") 
	        document.getElementsByTagName("head")[0].appendChild(fileref)
	},
	
	makeDoubleDelegate: function(function1, function2) {
	   return function() {
	       if (function1)
	           function1();
	       if (function2)
	           function2();
	  }
	},
    
    loadLibs: function() {
		// Load Google API
		document.write("<"+"script language=\"javascript\" src=\"http://www.google.com/jsapi\" type=\"text/javascript\"><"+"/script>\n");
		if (window['google']) {
            google.load("jquery", "1");
		}
    },
	
	/*
	 * JavaScript Pretty Date
	 * Copyright (c) 2008 John Resig (jquery.com) with modifications by victorcoder (victorcoder@gmail.com)
	 * Licensed under the MIT license.
	 */
	
	// Takes an ISO time and returns a string representing how
	// long ago the date represents.
	prettyDate: function(time) {
	    
	    //Hack for Safari javascript Date class that is not interpreting well the GMT predicate
	    //that Jaiku api returns for each post.
	    time = time.replace(" GMT", "Z");
	    
	    var date = new Date((time || "").replace(/-/g,"/").replace(/[TZ]/g," ")),
	        diff = (((new Date()).getTime() - date.getTime()) / 1000),
	        day_diff = Math.floor(diff / 86400); 
	    
	    if ( isNaN(day_diff) || day_diff < 0 )
	        return;
	            
	    return day_diff == 0 && (
	            diff < 60 && "just now" ||
	            diff < 120 && "1 minute ago" ||
	            diff < 3600 && Math.floor( diff / 60 ) + " minutes ago" ||
	            diff < 7200 && "1 hour ago" ||
	            diff < 86400 && Math.floor( diff / 3600 ) + " hours ago") ||
	        day_diff == 1 && "Yesterday" ||
	        day_diff < 7 && day_diff + " days ago" ||
	        day_diff < 31 && Math.ceil( day_diff / 7 ) + " weeks ago" ||
	        day_diff > 31 && Math.ceil( day_diff / 31 ) + " months ago";
	}
};

IRIS.Widget.loadLibs();

String.prototype.stripTags = function () {
    return this.replace(/<([^>]+)>/g,"");
}

function BlogPreview(container){
    this.container_ = container;
	this.feed = "";
	this.result = null;
	this.options = null;
	this.offset = 0;
}

BlogPreview.prototype.show = function(url, options){
  this.feed = new google.feeds.Feed(url);
  this.options = options;
  
  this.feed.setResultFormat(google.feeds.Feed.MIXED_FORMAT);
  if (options.numEntries != null) { 
      this.feed.setNumEntries(options.numEntries);
  }
  var preview = this;
  this.feed.load(function(result){
  	  //Store the result in the object
      preview.result = result;
	  
      preview.render_(result, options);
  });
}

BlogPreview.prototype.render_ = function(result, options){
    if (!result.feed || !result.feed.entries) 
        return;
    while (this.container_.firstChild) {
        this.container_.removeChild(this.container_.firstChild);
    }
    
    var headerDiv = this.createDiv_(this.container_, "gfc-resultsHeader");
    if (!options.noTitle) {
        var header = this.createElement_("div", headerDiv, "gfc-title");
        if (options.title != "") {
          this.createLink_(header, result.feed.link, options.title);
        } else {
          this.createLink_(header, result.feed.link, result.feed.title);
        }
    }
    
	var blog = this.createDiv_(this.container_, "gfc-results");
	
	//Execute the pager if it's set, else return all entries.
	var entries = this.renderPager_(blog);
	
    for (var i = 0; i < entries.length; i++) {
        var entry = entries[i];
        var gfcResult = this.createDiv_(blog, "gfc-result");
		var gfResult = this.createDiv_(gfcResult, "gf-result");
        var linkDiv = this.createDiv_(gfResult, "gf-title");
        var entryLink = this.createLink_(linkDiv, entry.link, entry.title);
		
		entryLink.onclick = function() {
			piwik_track(this.href, stats_idsite, stats_url, "link");
		}
		
        //if (entry.author) {
        //    this.createDiv_(gfResult, "gf-author", " " + entry.author);
        //}
		if (entry.publishedDate) {
            this.createDiv_(gfResult, "gf-relativePublishedDate", " " + entry.publishedDate.substring(0, 17));
        }
        this.createDiv_(gfResult, "gf-snippet", entry.contentSnippet.stripTags());
    }
	
	if (options.callback != null && typeof(options.callback) == "function") {
        options.callback();
    }
}

BlogPreview.prototype.createDiv_ = function(parent, className, opt_text){
    return this.createElement_("div", parent, className, opt_text);
}

BlogPreview.prototype.createLink_ = function(parent, href, text){
    var link = this.createElement_("a", parent, "gf-title", text);
    link.href = href;
	link.target = "_blank";
    return link;
}

BlogPreview.prototype.createElement_ = function(tagName, parent, className, opt_text){
    var div = document.createElement(tagName);
    div.className = className;
    parent.appendChild(div);
    if (opt_text) {
        div.appendChild(document.createTextNode(opt_text));
    }
    return div;
}

BlogPreview.prototype.paginate_ = function(direcction){
	if(direcction == "next") {
	    if(this.offset + this.options.itemspp >= this.result.feed.entries.length) return;
	    this.offset += this.options.itemspp;	
	} else if(direcction == "prev") {
		if(this.offset <= 0) return;
		this.offset -= this.options.itemspp;
	}
	
    this.render_(this.result, this.options);
}

BlogPreview.prototype.renderPager_ = function(blog) {
	var entries = new Array();
	
	if(this.options.pager == true) {
        for (var i = 0; i < this.options.itemspp; i++) {
			if(this.offset + i >= this.result.feed.entries.length) break;
			entries.push(this.result.feed.entries[this.offset + i]);
		}
		
		var pager = this.createDiv_(blog, "gfc-pager");
		
		var prevDiv = this.createDiv_(pager, "gfc-pager-prev");
        var prev = this.createLink_(prevDiv, "#", "Prev");
		
		var preview = this;
		prev.onclick = function() {
		    preview.paginate_("prev");	
			return false;
		}
		
		var nextDiv = this.createDiv_(pager, "gfc-pager-next");
		var next = this.createLink_(nextDiv, "#", "Next");
		next.onclick = function() {
            preview.paginate_("next");  
            return false;
        }
    } else {
        entries = this.result.feed.entries;
    }
		
	return entries; 
}

/**
 * RUZEE.ShadedBorder 0.6.1
 * (c) 2006 Steffen Rusitschka
 *
 * RUZEE.ShadedBorder is freely distributable under the terms of an MIT-style license.
 * For details, see http://www.ruzee.com/
 */
var RUZEE = window.RUZEE ||
{};

RUZEE.ShadedBorder = {

    create: function(opts){
        var isie = /msie/i.test(navigator.userAgent) && !window.opera;
        var isie6 = isie && !window.XMLHttpRequest;
        function sty(el, h){
            for (k in h) {
                if (/ie_/.test(k)) {
                    if (isie) 
                        el.style[k.substr(3)] = h[k];
                }
                else 
                    el.style[k] = h[k];
            }
        }
        function crdiv(h){
            var el = document.createElement("div");
            el.className = "sb-gen";
            sty(el, h);
            return el;
        }
        function op(v){
            v = v < 0 ? 0 : v;
            if (v > 0.99999) 
                return "";
            return isie ? " filter:alpha(opacity=" + (v * 100) + ");" : " opacity:" + v + ';';
        }
        
        var sr = opts.shadow || 0;
        var r = opts.corner || 0;
        var bor = 0;
        var bow = opts.border || 0;
        var boo = opts.borderOpacity || 1;
        var shadow = sr != 0;
        var lw = r > sr ? r : sr;
        var rw = lw;
        var th = lw;
        var bh = lw;
        if (bow > 0) {
            bor = r;
            r = r - bow;
        }
        var cx = r != 0 && shadow ? Math.round(lw / 3) : 0;
        var cy = cx;
        var cs = Math.round(cx / 2);
        var iclass = r > 0 ? "sb-inner" : "sb-shadow";
        var sclass = "sb-shadow";
        var bclass = "sb-border";
        var edges = opts.edges || "trlb";
        if (!/t/i.test(edges)) 
            th = 0;
        if (!/b/i.test(edges)) 
            bh = 0;
        if (!/l/i.test(edges)) 
            lw = 0;
        if (!/r/i.test(edges)) 
            rw = 0;
        
        var p = {
            position: "absolute",
            left: "0",
            top: "0",
            width: lw + "px",
            height: th + "px",
            ie_fontSize: "1px",
            overflow: "hidden",
            margin: "0",
            padding: "0"
        };
        var tl = crdiv(p);
        delete p.left;
        p.right = "0";
        p.width = rw + "px";
        var tr = crdiv(p);
        delete p.top;
        p.bottom = "0";
        p.height = bh + "px";
        var br = crdiv(p);
        delete p.right;
        p.left = "0";
        p.width = lw + "px";
        var bl = crdiv(p);
        
        var tw = crdiv({
            position: "absolute",
            width: "100%",
            height: th + "px",
            ie_fontSize: "1px",
            top: "0",
            left: "0",
            overflow: "hidden",
            margin: "0",
            padding: "0"
        });
        var t = crdiv({
            position: "relative",
            height: th + "px",
            ie_fontSize: "1px",
            margin: "0 " + rw + "px 0 " + lw + "px",
            overflow: "hidden",
            padding: "0"
        });
        tw.appendChild(t);
        
        var bw = crdiv({
            position: "absolute",
            left: "0",
            bottom: "0",
            width: "100%",
            height: bh + "px",
            ie_fontSize: "1px",
            overflow: "hidden",
            margin: "0",
            padding: "0"
        });
        
        var b = crdiv({
            position: "relative",
            height: bh + "px",
            ie_fontSize: "1px",
            margin: "0 " + rw + "px 0 " + lw + "px",
            overflow: "hidden",
            padding: "0"
        });
        
        bw.appendChild(b);
        
        var mw = crdiv({
            position: "absolute",
            top: (-bh) + "px",
            left: "0",
            width: "100%",
            height: "100%",
            overflow: "hidden",
            ie_fontSize: "1px",
            padding: "0",
            margin: "0"
        });
        
        function corner(el, t, l){
            var w = l ? lw : rw;
            var h = t ? th : bh;
            var s = t ? cs : -cs;
            var dsb = [];
            var dsi = [];
            var dss = [];
            
            var xp = 0;
            var xd = 1;
            if (l) {
                xp = w - 1;
                xd = -1;
            }
            for (var x = 0; x < w; ++x) {
                var yp = h - 1;
                var yd = -1;
                if (t) {
                    yp = 0;
                    yd = 1;
                }
                var finished = false;
                for (var y = h - 1; y >= 0 && !finished; --y) {
                    var div = '<div style="position:absolute; top:' + yp + 'px; left:' + xp + 'px; ' +
                    'width:1px; height:1px; overflow:hidden; margin:0; padding:0;';
                    
                    var xc = x - cx;
                    var yc = y - cy - s;
                    var d = Math.sqrt(xc * xc + yc * yc);
                    var doShadow = false;
                    
                    if (r > 0) {
                        // draw border
                        if (xc < 0 && yc < bor && yc >= r || yc < 0 && xc < bor && xc >= r) {
                            dsb.push(div + op(boo) + '" class="' + bclass + '"></div>');
                        }
                        else 
                            if (d < bor && d >= r - 1 && xc >= 0 && yc >= 0) {
                                var dd = div;
                                if (d >= bor - 1) {
                                    dd += op((bor - d) * boo);
                                    doShadow = true;
                                }
                                else 
                                    dd += op(boo);
                                dsb.push(dd + '" class="' + bclass + '"></div>');
                            }
                        
                        // draw inner
                        var dd = div + ' z-index:2;' + (t ? 'background-position:0 -' + (r - yc - 1) + 'px;' : 'background-image:none;');
                        var finish = function(){
                            if (!t) 
                                dd = dd.replace(/top\:\d+px/, "top:0px");
                            dd = dd.replace(/height\:1px/, "height:" + (y + 1) + "px");
                            dsi.push(dd + '" class="' + iclass + '"></div>');
                            finished = true;
                        };
                        if (xc < 0 && yc < r || yc < 0 && xc < r) {
                            finish();
                        }
                        else 
                            if (d < r && xc >= 0 && yc >= 0) {
                                if (d >= r - 1) {
                                    dd += op(r - d);
                                    doShadow = true;
                                    dsi.push(dd + '" class="' + iclass + '"></div>');
                                }
                                else {
                                    finish();
                                }
                            }
                            else 
                                doShadow = true;
                    }
                    else 
                        doShadow = true;
                    
                    // draw shadow
                    if (sr > 0 && doShadow) {
                        d = Math.sqrt(x * x + y * y);
                        if (d < sr) {
                            dss.push(div + ' z-index:0; ' + op(1 - (d / sr)) + '" class="' + sclass + '"></div>');
                        }
                    }
                    yp += yd;
                }
                xp += xd;
            }
            el.innerHTML = dss.concat(dsb.concat(dsi)).join('');
        }
        
        function mid(mw){
            var ds = [];
            
            ds.push('<div style="position:relative; top:' + (th + bh) + 'px; height:2048px; ' +
            ' margin:0 ' +
            (rw - r - cx) +
            'px 0 ' +
            (lw - r - cx) +
            'px; ' +
            ' padding:0; overflow:hidden;' +
            ' background-position:0 ' +
            (th > 0 ? -(r + cy + cs) : '0') +
            'px;"' +
            ' class="' +
            iclass +
            '"></div>');
            
            var dd = '<div style="position:absolute; width:1px;' +
            ' top:' +
            (th + bh) +
            'px; height:2048px; padding:0; margin:0;';
            if (sr > 0) {
                for (var x = 0; x < lw - r - cx; ++x) {
                    ds.push(dd + ' left:' + x + 'px;' + op((x + 1.0) / lw) +
                    '" class="' +
                    sclass +
                    '"></div>');
                }
                
                for (var x = 0; x < rw - r - cx; ++x) {
                    ds.push(dd + ' right:' + x + 'px;' + op((x + 1.0) / rw) +
                    '" class="' +
                    sclass +
                    '"></div>');
                }
            }
            
            if (bow > 0) {
                var su = ' width:' + bow + 'px;' + op(boo) + '" class="' + bclass + '"></div>';
                ds.push(dd + ' left:' + (lw - bor - cx) + 'px;' + su);
                ds.push(dd + ' right:' + (rw - bor - cx) + 'px;' + su);
            }
            
            mw.innerHTML = ds.join('');
        }
        
        function tb(el, t){
            var ds = [];
            var h = t ? th : bh;
            var dd = '<div style="height:1px; overflow:hidden; position:absolute; margin:0; padding:0;' +
            ' width:100%; left:0px; ';
            var s = t ? cs : -cs;
            for (var y = 0; y < h - s - cy - r; ++y) {
                if (sr > 0) 
                    ds.push(dd + (t ? 'top:' : 'bottom:') + y + 'px;' + op((y + 1) * 1.0 / h) +
                    '" class="' +
                    sclass +
                    '"></div>');
            }
            if (y >= bow) {
                ds.push(dd + (t ? 'top:' : 'bottom:') + (y - bow) + 'px;' + op(boo) +
                ' height:' +
                bow +
                'px;" class="' +
                bclass +
                '"></div>');
            }
            
            ds.push(dd +
            (t ? 'background-position-y:0; top:' : 'background-image:none; bottom:') +
            y +
            'px;' +
            ' height:' +
            (r + cy + s) +
            'px;" class="' +
            iclass +
            '"></div>');
            
            el.innerHTML = ds.join('');
        }
        
        corner(tl, true, true);
        corner(tr, true, false);
        corner(bl, false, true);
        corner(br, false, false);
        mid(mw);
        tb(t, true);
        tb(b, false);
        
        return {
            render: function(el){
                if (typeof el == 'string') 
                    el = document.getElementById(el);
                if (el.length != undefined) {
                    for (var i = 0; i < el.length; ++i) 
                        this.render(el[i]);
                    return;
                }
                el.className += " sb";
                sty(el, {
                    position: "relative",
                    background: "transparent"
                });
                
                // remove generated children
                var node = el.firstChild;
                while (node) {
                    var nextNode = node.nextSibling;
                    if (node.nodeType == 1 && node.className == 'sb-gen') 
                        el.removeChild(node);
                    node = nextNode;
                }
                
                var iel = el.firstChild;
                
                var twc = tw.cloneNode(true);
                var mwc = mw.cloneNode(true);
                var bwc = bw.cloneNode(true);
                
                el.insertBefore(tl.cloneNode(true), iel);
                el.insertBefore(tr.cloneNode(true), iel);
                el.insertBefore(bl.cloneNode(true), iel);
                el.insertBefore(br.cloneNode(true), iel);
                el.insertBefore(twc, iel);
                el.insertBefore(mwc, iel);
                el.insertBefore(bwc, iel);
                
                if (isie6) {
                    el.onmouseover = function(){
                        this.className += " hover";
                    }
                    el.onmouseout = function(){
                        this.className = this.className.replace(/ hover/, "");
                    }
                }
                if (isie) {
                    function resize(){
                        twc.style.width = bwc.style.width = mwc.style.width = el.offsetWidth + "px";
                        mwc.firstChild.style.height = el.offsetHeight + "px";
                    }
                    el.onresize = resize;
                    resize();
                }
            }
        };
    }
}

// add our styles to the document
document.write('\
  <style type="text/css">\
  .sb, .sbi, .sb *, .sbi * { position:relative; z-index:1; }\
  * html .sb, * html .sbi { height:1%; }\
  .sbi { display:inline-block; }\
  .sb-inner { background:#ddd; }\
  .sb-shadow { background:#000; }\
  .sb-border { background:#bbb; }\
  </style>\
');

