/**
 * Mass High Tech Widget
 * version 1.2
 * 
 * Copyright (c) 2009, All Rights Reserved Worldwide
 *
 * author        Fred LeBlanc <fred@suredev.com> of SureDev via Boston Web Studio
 * created for   Mass High Tech
 *
 * Find documentation, code updates and support:
 *   http://masshightech.com/tech-news-widget/
 */
MHTWidget=function(widgetId){this.intervalPointer;this.intervalCounter=0;this.loadInterval=100;this.remoteLoadTimeout=3000;this.widgetId=(typeof widgetId=='undefined')?'MHTWidget':widgetId;this.state='new';this.jQueryFile='http://www.masshightech.com/tech-news-widget/jquery.min.js';this.remoteParser='http://www.masshightech.com/tech-news-widget/parser/';this.feedLocation='http://www.masshightech.com/rss.html';this.analyticsMedium='widget';this.analyticsCampaign='widget-tracking';this.feeds={mass_high_tech:this.makeFeed('','New England Tech News'),storage:this.makeFeed('?i=61,62,63','Storage'),nanotechnology:this.makeFeed('?i=64','Nanotechnology'),personnel:this.makeFeed('?i=66','Personnel'),robotics:this.makeFeed('?i=67','Robotics'),defense:this.makeFeed('?i=68,69','Defense'),education_and_training:this.makeFeed('?i=70,71,72','Education and Training'),startups:this.makeFeed('?i=73','Startups'),legislation:this.makeFeed('?i=74','Legislation'),entrepreneurship:this.makeFeed('?i=76','Entrepreneurship'),information_technology:this.makeFeed('?i=77','Information Technology'),instrumentation:this.makeFeed('?i=1','Instrumentation'),financial_services:this.makeFeed('?i=2,52,53,54,55','Financial Services'),services:this.makeFeed('?i=3,43,44,45,46,47,48,49,50,75','Services'),envirotech:this.makeFeed('?i=4,41','Envirotech'),energy:this.makeFeed('?i=5,38,39,40,56,57,58,59','Energy'),life_sciences:this.makeFeed('?i=6,32,33,35,36','Life Sciences'),networks:this.makeFeed('?i=7,22,23,26,30','Networks'),manufacturing:this.makeFeed('?i=31','Manufacturing'),real_estate:this.makeFeed('?i=8','Real Estate'),internet:this.makeFeed('?i=9,20,21,24,25','Internet'),software:this.makeFeed('?i=17,13,14,28,60,65','Software'),hardware:this.makeFeed('?i=10,11,12,27','Hardware')};this.display={feed:this.feeds.mass_high_tech,feedName:'mass_high_tech',boxWidth:'250px',textColor:'#000',linkColor:'#0072bc',backgroundColor:'#f7f5e9',borderColor:'#cfcba9',borderSize:'2px',articleLimit:4,newWindow:false};this.maximumArticles=10;this.minimumArticles=1;this.init();};MHTWidget.prototype.init=function(){this.state='loading';document.write('<div id="'+this.widgetId+'"></div>');this.loadjQuery();};MHTWidget.prototype.setFeed=function(feed){feed=this.contractName(feed);this.validateFeed(feed);this.display.feed=eval('this.feeds.'+feed);this.display.feedName=feed;};MHTWidget.prototype.setBoxWidth=function(width){if(!this.validateSize(width)){throw('Cannot set box width, invalid width "'+width+'" given.');}
this.display.boxWidth=width;};MHTWidget.prototype.setTextColor=function(color){if(!this.validateColor(color)){throw('Cannot set text color, invalid hexidecimal color "'+color+'" given.');}
this.display.textColor=color;};MHTWidget.prototype.setLinkColor=function(color){if(!this.validateColor(color)){throw('Cannot set link color, invalid hexidecimal color "'+color+'" given.');}
this.display.linkColor=color;};MHTWidget.prototype.setBackgroundColor=function(color){if(!this.validateColor(color)){throw('Cannot set background color, invalid hexidecimal color "'+color+'" given.');}
this.display.backgroundColor=color;};MHTWidget.prototype.setBorderColor=function(color){if(!this.validateColor(color)){throw('Cannot set border color, invalid hexidecimal color "'+color+'" given.');}
this.display.borderColor=color;};MHTWidget.prototype.setBorderSize=function(size){if(!this.validateSize(size)){throw('Cannot set border size, invalid size "'+size+'" given.');}
this.display.borderSize=size;};MHTWidget.prototype.setArticleLimit=function(limit){if(!this.validateArticleLimit(limit)){throw('Cannot set article limit, invalid limit of '+size+' given.');}
this.display.articleLimit=limit;};MHTWidget.prototype.setNewWindowFlag=function(flag){this.display.newWindow=(flag)?true:false;};MHTWidget.prototype.place=function(){var me=this;var markup;switch(this.state){case'timeout':alert('Cannot place widget, remote code loading timed out.');break;case'loading':setTimeout(function(){me.place();},me.loadInterval);return;break;case'new':throw('Cannot place widget, did not load remote code.');break;}
try{jQuery(document).ready(function(){jQuery.getJSON(me.remoteParser+'?limit='+me.display.articleLimit+'&feedName='+me.display.feedName+'&feed='+me.display.feed.feedURL+'&callback=?',function(json){var nonStoryPopCode='';if(me.display.newWindow){nonStoryPopCode=' onclick="window.open(this.href); return false;"';}
markup='<div style="font-size:.9em;padding:0 0 5px;" id="MHTHeader">';markup=markup+'<p style="float:left;margin:0;line-height:1;" id="MHTFeedName"><br /><strong style="font-size: 1.2em;"><a href="'+me.makeAdLink(me.makeTrackableLink('http://masshightech.com'),json.sponsor.code,json.sponsor.channel)+'"'+nonStoryPopCode+'>MHT</a>: '+me.display.feed.title+'</strong></p>';markup=markup+'<span style="clear:both;display:block;"></span>';markup=markup+'</div>';markup=markup+'<div style="background-color:'+me.display.backgroundColor+';border:'+me.display.borderSize+' solid '+me.display.borderColor+';color:'+me.display.textColor+';padding: 0 10px;" id="MHTLinks">';jQuery.each(json.items,function(i,item){var popCode='';if(me.display.newWindow){popCode=' onclick="window.open(this.href); return false;"';}
markup=markup+'<p style="margin:.75em 0;line-height:1.4;"><a href="'+me.makeAdLink(me.makeTrackableLink(item.link),json.sponsor.code,json.sponsor.channel)+'" style="color:'+me.display.linkColor+';text-decoration:none;"'+popCode+'>'+item.title+'</a><br /><small>'+item.publishDate+'</small></p>';});markup=markup+'</div>';markup=markup+'<div style="font-size:.9em;padding:5px 0 0;" id="MHTSponsoredBy"><p style="margin:0;">';markup=markup+'Sponsored by <a href="'+json.sponsor.website+'" style="text-decoration:none;"'+nonStoryPopCode+'>'+json.sponsor.name+'</a></p></div>';jQuery('#'+me.widgetId).width(me.display.boxWidth).html(markup);});});}catch(e){throw(e.message);}};MHTWidget.prototype.contractName=function(name){name=name.replace(/ /i,'_');name=name.toLowerCase();return name;};MHTWidget.prototype.makeFeed=function(path,feedTitle){var obj={feedURL:this.feedLocation+path,title:feedTitle}
return obj;};MHTWidget.prototype.makeTrackableLink=function(link){var analyticsAddOn='utm_source='+escape(window.location.href)+'&utm_medium='+this.analyticsMedium+'&utm_campaign='+this.analyticsCampaign;return(link.indexOf('?')>-1)?link+'&'+analyticsAddOn:link+'?'+analyticsAddOn;};MHTWidget.prototype.makeAdLink=function(link,code,channel){var openXAddOn=[];if(code!=null){openXAddOn.push('sponsorcode='+code);}
if(channel!=null){openXAddOn.push('channelcode='+channel);}
return(link.indexOf('?')>-1)?link+'&'+openXAddOn.join('&'):link+'?'+openXAddOn.join('&');}
MHTWidget.prototype.validateFeed=function(feed){if((this.contractName(feed)in this.feeds)==false){throw('Cannot use feed, invalid feed "'+feed+'" given.');}};MHTWidget.prototype.validateSize=function(size){return(size.match(/^\d+(?:\.\d)?(?:em|px|%)$/i)!=null);};MHTWidget.prototype.validateColor=function(color){return(color.match(/^#[0-9a-f]{3}$/i)!=null||color.match(/^#[0-9a-f]{6}$/i)!=null);};MHTWidget.prototype.validateArticleLimit=function(limit){return(limit>=this.minimumArticles&&limit<=this.maximumArticles);};MHTWidget.prototype.loadjQuery=function(){var src,tags;var me=this;try{jQuery;this.state='loaded';}catch(e){src=document.createElement('script');src.type='text/javascript';src.src=this.jQueryFile;tags=document.getElementsByTagName('head');if(tags.length<1){throw('Cannot load widget, invalid HTML set up. Missing <head> tag.');}
tags[0].appendChild(src);this.intervalPointer=setInterval(function(){me.jQueryCheck();},this.loadInterval);}};MHTWidget.prototype.jQueryCheck=function(){try{this.tick();if(jQuery=='undefined'){throw('f');}
clearInterval(this.intervalPointer);this.state='loaded';}catch(e){return false;}};MHTWidget.prototype.tick=function(){this.intervalCounter++;if(this.intervalCounter>=this.remoteLoadTimeout/this.loadInterval){clearInterval(this.intervalPointer);this.state='timeout';throw('Could not remotely load jQuery. Loading timeout.');}};