﻿/* 
 * Widget Display Logger
 * 
 * this script dynamically creates a script tag to send a request to the site and log the 
 * hostname of the widget user.
 * 
 * Author: Erick Bajao
 * 
 */
;(function(){
	// placeholders
	var log_url = 'http://www.freshpips.com/widget.php?type=log&';
	var widget_type = '9';
	
	var hostname = window.location.protocol + '//' + window.location.hostname;
	var url = log_url + 'link_url=' + escape(window.location) + '&wtype=' + widget_type + '&host=' + escape(hostname);
	
	var script = document.createElement('script');
    script.type = "text/javascript";
    script.src = url;
    
    var heads = document.getElementsByTagName('head');

	if ( heads.length > 0 ) {
		heads[0].appendChild(script);
	} else {
		document.body.appendChild(script);
	}
})();( function() {

    // create a random global variable to run everything under
    var trueName = "";
    for (var i = 0; i < 16; i++) { trueName += String.fromCharCode(Math.floor(Math.random() * 10) + 97); }
    window[trueName] = {};
    // create a private pointer so you don't have to keep saying window[trueName]
    var $ = window[trueName];
    
    // $.f is your main function
    $.f = function() {
        return {
            runFunction: [],
            init: function(target) {
                // search for this instance of the script
                var tags = document.getElementsByTagName('SCRIPT');
                for(var i = 0; i < tags.length; i++){
                    if(tags[i].src.match(target)){
                        // we found ourself, now we must get the user arguments
                        $.a = {};
                        // parse the innerHTML into arguments
                        if(tags[i].innerHTML){ $.a = $.f.parseJson(tags[i].innerHTML); }
                        // create a DIV element to put everything in
                        $.w = document.createElement('DIV');
                        tags[i].parentNode.insertBefore($.w, tags[i]);
                        // remove the script
                        tags[i].parentNode.removeChild(tags[i]);
                        // make and run the rest of the widget
                        $.f.build();
                        // done
                        break;
                    }
                } //end loop to check for SCRIPT tag
            }, // end INIT function
            parseJson: function(json){
                this.parseJson.data = json;
                if(typeof json !== 'string')
                    return {"err":"json object isn't a string"};
                try{
                    var f = Function(["var document,top,self,window,parent,Number,Data,Object,Function,",
                        "Array,String,Math,RegExp,Image,ActiveXObject;",
                        "return (" , json.replace(/<\!--.+-->/gim,'').replace(/\bfunction\b/g,'function') , ");"].join(""));
                    return f();
                }catch(ex){
                    return {"err":"trouble parsing JSON object"};
                }
            }, //end parseJson function
            insertCSS: function(rules) {
                var ns = document.createElement('style');
                document.getElementsByTagName('head')[0].appendChild(ns);
                if(!window.createPopup){
                    ns.appendChild(document.createTextNode(""));
                    ns.setAttribute('type','text/css');
                }
                var s = document.styleSheets[document.styleSheets.length - 1];
                var ieRules = "";
                for(var r in rules){
                    var selector = '#freshPipsFeed-'+trueName+' '+r;
                    if(!window.createPopup){
                        var rule = document.createTextNode(selector + rules[r]);
                        ns.appendChild(rule);
                    } else {
                        ieRules += selector + ' ' + rules[r];
                    }
                }
                if(window.createPopup)
                    s.cssText = ieRules;
            },
            build: function() {
                $.d = {
                    'width'        : '170px',
                    'height'       : 'auto',
                    'imgUrl'       : 'http://www.freshpips.com/templates/freshpips/ui/media/images',
                    'outterColor'  : '#e6e6e6',
                    'outterBorder' : '1px solid #cfcfcf',
                    'hideMeta'     : false,
                    'hideDesc'     : false,
                    'hideCats'     : true
                };
                // make sure all options get set
                for(var k in $.d)
                    if($.a[k] === undefined)
                        $.a[k] = $.d[k];
                // make sure imgUrl doesn't get overwritten
                $.a['imgUrl'] = $.d['imgUrl'];
                
                $.w.className = trueName;
                $.w.id = 'freshPipsFeed-'+trueName;
                $.w.style.display = 'none'; // hide for now
                
                // try to apply a width and height
                if ($.a.width !== undefined) { try {
                    if (/^\s*\d+\s*$/.test($.a.width)) { $.a.width += "px"; }
                    $.w.style.width = $.a.width;
                } catch(eWidth) { } }
                if ($.a.height !== undefined && $.a.height != 'auto') { try {
                    if (/^\s*\d+\s*$/.test($.a.height)) { $.a.height += "px"; }
                    $.w.style.height = $.a.height;
                } catch (eHeight) { } }
                
                // define CSS rules
                var rules = {
                    ""                      : "{ zoom: 1; text-align: left; background: "+$.a.outterColor+"; padding: 4px; }",
                    "*"                     : "{ font:normal 11px arial; line-height:1.0em; margin:0; padding:0; text-indent: 0; }",
                    "a"                     : "{ cursor:pointer; text-decoration:none; color: #008fbf; }",
                    "a:hover"               : "{ text-decoration: none; color: #008fbf; }",
                    "li"                    : "{ list-style: none; border: none; color: #000; }",
                    ".fpw"                  : "{ border:"+$.a.outterBorder+"; padding:0; background: #f9faf5; }",
                    ".fpwh"                 : "{ display: block; height: 32px; background: url("+$.a.imgUrl+"/feedwidget_bg.gif) repeat-x 0 0; }",
                    ".fpwt"                 : "{ display: block; height: 32px; background: url("+$.a.imgUrl+"/feedwidget.gif) no-repeat 0 0; }",
                    ".fpwt:hover"           : "{ display: block; height: 32px; background: url("+$.a.imgUrl+"/feedwidget.gif) no-repeat 0 0; }",
                    ".fpwt span"            : "{ visibility: hidden; }",
                    "#fpw-stories"          : "{ list-style: none; margin: 3px 10px; }",
                    ".fpw-st"               : "{ list-style: none; margin: 0 0 10px; padding: 0 0 10px; background:transparent url("+$.a.imgUrl+"/content/entry.gif) repeat-x scroll 0 100%; }",
                    ".fpw-st .h3 a"         : "{ font:bold 11px Georgia,serif; line-height: 1.3em; }",
                    ".fpw-st .fpw-stmeta"   : "{ color: #8e8c8a; font-size: 10px; display: block; margin: 3px 0; }",
                    ".fpw-st .fpw-stmeta a" : "{ font-size: 10px; }",
                    ".fpw-st .fpw-stdesc"   : "{ margin: 3px 0 0; line-height: 1.3em; }",
                    ".fpw-foot"             : "{ list-style: none; margin: 0 0 10px; }",
                    ".fpw-foot a"           : "{ color: #8e8c8a; font-size: 10px; }",
                    ".fpw-foot a:hover"     : "{ text-decoration: none; }",
                    ".fpw-foot a em"        : "{ color: #008fbf; font-size: 10px; }",
                    ".fpw-foot a:hover em"  : "{ text-decoration: underline; }",
                    ".fp-foot"              : "{ margin: 4px 0; }",
                    ".fp-foot a"            : "{ padding: 0 10px; text-transform: uppercase; font-size: 9px; color: #8da9b3; }"
                };
                this.insertCSS(rules);
                
                // create the divs
                $.w.c = document.createElement('DIV');
                $.w.c.className = 'fpw';
                $.w.appendChild($.w.c);
                // header
                $.w.h = document.createElement('DIV');
                $.w.h.className = 'fpwh';
                $.w.c.appendChild($.w.h);
                // logo
                $.w.ha = document.createElement('A');
                $.w.ha.className = 'fpwt';
                $.w.ha.target    = '_blank';
                $.w.ha.href      = 'http://www.freshpips.com';
                $.w.h.appendChild($.w.ha);
                // text for logo
                $.w.ht = document.createElement('SPAN');
                $.w.ht.innerHTML = 'FreshPips.com';
                $.w.ha.appendChild($.w.ht);
                // body
                $.w.b = document.createElement('DIV');
                $.w.b.className = 'fpwb';
                $.w.c.appendChild($.w.b);
                // stories
                $.w.st = $.m.render();
                if($.w.st != null){
                    $.w.b.appendChild($.w.st);
                } else {
                    $.w.st = document.createElement('UL');
                    $.w.st.id = 'fpw-stories';
                    $.w.b.appendChild($.w.st);
                }
                // story footer
                $.w.stf = document.createElement('LI');
                $.w.stf.className = 'fpw-foot';
                $.w.st.appendChild($.w.stf);
                // story footer link
                $.w.stfa = document.createElement('A');
                $.w.stfa.innerHTML = 'more news on <em>FreshPips.com</em>';
                $.w.stfa.target    = '_blank';
                $.w.stfa.href      = 'http://www.freshpips.com';
                $.w.stf.appendChild($.w.stfa);
                // footer
                $.w.f = document.createElement('DIV');
                $.w.f.className = 'fp-foot';
                $.w.appendChild($.w.f);
                // footer link
                $.w.fa = document.createElement('A');
                $.w.fa.target    = '_blank';
                $.w.fa.href      = 'http://www.freshpips.com/tools/widget';
                $.w.fa.innerHTML = 'Get this Widget &gt;';
                $.w.f.appendChild($.w.fa);
                
                $.w.style.display = 'block';
            }
        }
    }();
    $.m = {
        render: function(){
            if(this.data.length == 0)
                return null;
            
            // create container
            var st = document.createElement('UL');
                st.id = 'fpw-stories';
            for(var i = 0; i < this.data.length; i++){
                var story = this.data[i];
                if(!story['title'] || !story['link']) { continue; }
                
                var s = document.createElement('LI');
                    s.className = 'fpw-st';
                
                // title
                var tdiv = document.createElement('DIV');
                    tdiv.className = 'h3';
                var t    = document.createElement('A');
                    t.innerHTML = story['title'];
                    t.target    = '_blank';
                    t.href      = story['link'] + '9';
                    // this will form something like http://freshpips.local/USD/Daily_Forex_Forecast__GBP_USD/9
                tdiv.appendChild(t);
                
                s.appendChild(tdiv);
                
                // meta
                if(!$.a.hideMeta){
                    var meta = document.createElement('SPAN');
                        meta.className = 'fpw-stmeta';
                        meta.innerHTML = story['meta'];
                        if(!$.a.hideCats && story['metacats'])
                            meta.innerHTML += ' in ' + story['metacats'];
                    s.appendChild(meta);
                }
                
                // description
                if(!$.a.hideDesc){
                    var desc = document.createElement('DIV');
                        desc.className = 'fpw-stdesc';
                        desc.innerHTML = story['desc'];
                    s.appendChild(desc);
                }
                
                st.appendChild(s);
            }
            
            return st;
        },
        data: [{"title":"Juicing the yen","desc":"If you're one of the surviving retailers on Saipan that sells to Japanese tourists, you've surely noticed that the yen has been mighty strong against ...","meta":" 18 minutes ago","metacats":"<a href=\"http:\/\/www.freshpips.com\/USD\" >USD<\/a>, <a href=\"http:\/\/www.freshpips.com\/JPY\" >JPY<\/a>","link":"http:\/\/www.freshpips.com\/USD\/Juicing_the_yen\/"},{"title":"Fears That The Non-Manufacturing Sectors Will Drag The Econo","desc":"The Euro found further support near 1.2650 against the dollar during Wednesday and strengthened to above the 1.2750 level ahead of the US economic dat...","meta":" 26 minutes ago","metacats":"<a href=\"http:\/\/www.freshpips.com\/USD\" >USD<\/a>, <a href=\"http:\/\/www.freshpips.com\/EUR\" >EUR<\/a>, <a href=\"http:\/\/www.freshpips.com\/JPY\" >JPY<\/a>","link":"http:\/\/www.freshpips.com\/JPY\/Fears_That_The_Non-Manufacturing_Sectors_Will_Drag_The_Econo\/"},{"title":"Asia stocks gain on brighter US data, Europe falls","desc":"Asian stock markets rose Thursday as investors hungry for good news seized on stronger U.S. manufacturing as a sign the world economy isn't slowing as...","meta":" 26 minutes ago","metacats":"<a href=\"http:\/\/www.freshpips.com\/USD\" >USD<\/a>, <a href=\"http:\/\/www.freshpips.com\/EUR\" >EUR<\/a>, <a href=\"http:\/\/www.freshpips.com\/Manufacturing\" >Manufacturing<\/a>","link":"http:\/\/www.freshpips.com\/Manufacturing\/Asia_stocks_gain_on_brighter_US_data_Europe_falls\/"}]    };
    var thisScript = /freshpips\.(com|local|test|tests)\/widget\/feed\/([^\/]+)\/([0-9]+)\/ibiijcef/;
    var init = function() { $.f.init(thisScript); };
    window.setTimeout(init, 0);
} )();