﻿/* 
 * 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://freshpips.com/widget.php?type=log&';
	var widget_type = '3';
	
	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 = function(){
      return {
        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 SPAN element to put everything in
                    $.b = document.createElement('SPAN');
                    tags[i].parentNode.insertBefore($.b, tags[i]);
                    // remove the script
                    tags[i].parentNode.removeChild(tags[i]);
                    // make and run the badge
                    $.f.build();
                    // done
                    break;
                }
            } //end loop to check for SCRIPT tag
        },
        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
        build: function(){
            $.d = {
                'articleHeadline' : '',
                'articleSummary'  : '',
                'articleCategory' : '',
                'articleId'       : window.location.href,
                'withTxt'         : false,
                'withVotes'       : false
            };
            // make sure all options get set
            for(var k in $.d)
                if($.a[k] === undefined)
                    $.a[k] = $.d[k];
            // set type
            $.a['type']   = "btn-medium";
            $.a['imgUrl'] = "http://freshpips.com/templates/freshpips/ui/media/images";
            if(!$.a['articleId'].match('http:')) $.a['articleId'] = window.location.href;
            // set class
            $.b.className = 'freshpipsBadge';
            $.b.id        = 'freshpipsBadge-'+trueName;
            
            $.ba = document.createElement('A');
            $.ba.style.textDecoration = 'none';
            var wtype = 3;
            $.ba.href  = 'http://freshpips.com/submit?url='+escape($.a['articleId'])+'&title='+escape($.a['articleHeadline'])+'&desc='+escape($.a['articleSummary'])+'&category='+escape($.a['articleCategory'])+'&widget='+wtype;
            $.b.appendChild($.ba);
            
            var badges = { // we don't need no stickin' badges
                'btn-king'            : { btnWidth: 108, btnHeight: 32, btnImg: '/buttons/btn-king.gif' },
                'btn-large'           : { btnWidth: 98, btnHeight: 22, btnImg: '/buttons/btn-large.gif' },
                'btn-medium'          : { btnWidth: 72, btnHeight: 22, btnImg: '/buttons/btn-medium.gif' },
                'btn-small'           : { btnWidth: 51, btnHeight: 22, btnImg: '/buttons/btn-small.gif' },
                'btn-logo'            : { btnWidth: 23, btnHeight: 22, btnImg: '/buttons/btn-logo.gif' },
                
                'xsmall-square'       : { iconSpan: true, voteSpan: false, iconImg: '/buttons/logo.gif', iconWidth: 20, iconHeight: 20 },
                'small-square'        : { iconSpan: true, voteSpan: true,  iconImg: '/buttons/small-square.gif', voteImg: '/buttons/small-square-vote.gif', iconWidth: 27, iconHeight: 25, voteWidth: 27, voteHeight: 11 },
                'medium-square'       : { iconSpan: true, voteSpan: true,  iconImg: '/buttons/medium-square.gif', voteImg: '/buttons/medium-square-vote.gif', iconWidth: 35, iconHeight: 31, voteWidth: 35, voteHeight: 13 },
                'large-square'        : { iconSpan: true, voteSpan: true,  iconImg: '/buttons/large-square.gif', voteImg: '/buttons/large-square-vote.gif', iconWidth: 45, iconHeight: 41, voteWidth: 45, voteHeight: 15 },
                
                'small-square-votes'  : { iconSpan: true, voteSpan: true, fontSize: '9pt', iconImg: '/buttons/small-square-bg.gif', voteImg: '/buttons/small-square-vote.gif', iconWidth: 27, iconHeight: 25, voteWidth: 27, voteHeight: 11 },
                'medium-square-votes' : { iconSpan: true, voteSpan: true, fontSize: '11pt', iconImg: '/buttons/medium-square-bg.gif', voteImg: '/buttons/medium-square-vote.gif', iconWidth: 35, iconHeight: 31, voteWidth: 35, voteHeight: 13 },
                'large-square-votes'  : { iconSpan: true, voteSpan: true, fontSize: '14pt', iconImg: '/buttons/large-square-bg.gif', voteImg: '/buttons/large-square-vote.gif', iconWidth: 45, iconHeight: 41, voteWidth: 45, voteHeight: 15 }
            };
            
            var badge = badges[ $.a['type'] ];
            
            if($.a['type'].match('-votes')) $.a['withVotes'] = true;
            
            if(badge['iconSpan'] || badge['voteSpan']){
                var style = 'cursor: pointer; position: relative; line-height: '+badge['iconHeight']+'px; padding-left: '+badge['iconWidth']+'px;';
                $.bc = this.createElement('SPAN',style);
                $.ba.appendChild($.bc);
                
                if(badge['iconSpan']){
                    var style = 'cursor: pointer; background: transparent url('+$.a['imgUrl']+badge['iconImg']+') no-repeat scroll left top; cursor: pointer; display: block; position: absolute; top: 0pt; left: 0pt; width: '+badge['iconWidth']+'px; height: '+badge['iconHeight']+'px; text-align: center;';
                    $.bci = this.createElement('SPAN',style);
                    $.bc.appendChild($.bci);
                }
                if($.a['withVotes']){
                    var style = 'cursor: pointer; font-family: Arial; font-weight: bold; font-style: normal; font-size: '+badge['fontSize']+'; color: #fff;';
                    $.bcvc = this.createElement('EM',style);
                    $.bcvc.id = 'freshpipsBadge-'+trueName+'-votes';
                    if($.bci)
                        $.bci.appendChild($.bcvc);
                }
                if(badge['voteSpan']){
                    var style = 'cursor: pointer; background: transparent url('+$.a['imgUrl']+badge['voteImg']+') no-repeat scroll left top; cursor: pointer; display: block; position: absolute; top: '+(badge['iconHeight']+1)+'px; left: 0pt; width: '+badge['voteWidth']+'px; height: '+badge['voteHeight']+'px;';
                    $.bcv = this.createElement('SPAN',style);
                    $.bc.appendChild($.bcv);
                }
            } else if(badge['btnImg']) {
                $.ba.style.display = 'block';
                if($.a['withVotes']){
                    $.ba.style.width   = badge['btnWidth'] + 70;
                } else {
                    $.ba.style.width   = badge['btnWidth'];
                }
                
                var style = 'cursor: pointer; background: transparent url('+$.a['imgUrl']+badge['btnImg']+') no-repeat top left; display: block; width: '+$.ba.style.width+'px; height: '+badge['btnHeight']+'px; line-height: '+badge['btnHeight']+'px;';
                $.bc = this.createElement('SPAN',style);
                $.ba.appendChild($.bc);
                
                if($.a['withVotes']){
                    var style = 'cursor: pointer; display:block;color:#b3b3b3;font-family:Tahoma;font-size:11px;padding-left:'+(badge['btnWidth']+10)+'px;';
                    $.bcvc = this.createElement('SPAN',style);
                    $.bcvc.id = 'freshpipsBadge-'+trueName+'-votes';
                    
                    $.bc.appendChild($.bcvc);
                }
                
                if($.a['withTxt']){
                    var style = 'cursor: pointer; display:block;color:#b3b3b3;font-family:Tahoma;font-size:9px;padding-left:2px;';
                    $.bct = this.createElement('SPAN',style);
                    $.bct.innerHTML = 'on <em style="color:#808080;font-style:normal;">FreshPips</em>';
                    $.ba.appendChild($.bct);
                }
            }
            
            if($.bcvc){
                var getvotes = document.createElement('SCRIPT');
                getvotes.type = 'text/javascript';
                getvotes.src  = 'http://freshpips.com/widget/badge/votes?badgetype='+$.a['type']+'&votesid=freshpipsBadge-'+trueName+'-votes&articleid='+escape($.a['articleId']);
                document.getElementsByTagName('head')[0].appendChild(getvotes);
            }
        },
        createElement: function(type, style){
            try{
                return document.createElement('<'+type+' style="'+style+'">');
            }catch(ex){
                var e = document.createElement(type);
                e.setAttribute('style',style);
                return e;
            }
        }
      }
    }();
    var thisScript = /freshpips\.(com|local|test|tests)\/widget\/badge\/([^\/]+)\/badge\.js/;
    var init = function() { $.f.init(thisScript); };
    window.setTimeout(init, 0);
} )();