// BensWidget
// to display Bens Bargains coupons on another site.
// relies on jQuery 1.3 and the xml-to-json plugin
// Authoed by J Dennis Riddle; copyright 2009 Internetbrands.com

var BensWidget = {
  storeHash : {
    "1-800 contacts, inc." : 491,
    "1-800-flowers.com" : 258,
    "1-800-petmeds" : 701,
    "123inkjets" : 587,
    "1saleaday.com" : 428,
    "1st wishes" : 791,
    "1st wishes.com" : 791,
    "1stwishes.com" : 791,
    "inkjet express" : 583,
    "39dollarglasses.com" : 588,
    "4linkcomm" : 108,
    "4 wheel parts" : 794,
    "6th ave electronics" : 462,
    "6pm.com" : 528,
    "a1books" : 795,
    "a and e - the arts and entertainment network" : 436,
    "aaa" : 467,
    "abcink" : 114,
    "abebooks" : 796,
    "abe's of maine" : 421,
    "abm communications, inc." : 418,
    "abtelectronics.com" : 2,
    "accessory geeks" : 493,
    "eforce trading co" : 574,
    "ace hardware" : 104,
    "acronis inc" : 797,
    "activa active gear for powerful women" : 534,
    "adidas america inc." : 576,
    "adobe" : 360,
    "adorama inc." : 466,
    "aeropostale" : 251,
    "alienware" : 478,
    "all battery" : 357,
    "all brands" : 798,
    "alloy" : 401,
    "allposters" : 177,
    "allsat" : 198,
    "altrec.com" : 155,
    "always for me" : 921,
    "amazon.com" : 3,
    "american eagle" : 768,
    "andysautosport.com" : 800,
    "angie s list" : 801,
    "aol" : 117,
    "aperion audio" : 802,
    "apple" : 68,
    "ashford" : 488,
    "astrology.com" : 803,
    "att" : 336,
    "at&amp;amp;t wireless services" : 354,
    "audition shoes" : 804,
    "auto barn" : 805,
    "auto parts giant" : 806,
    "autos.com" : 233,
    "avenue.com" : 591,
    "avoid traffic tickets" : 883,
    "avon" : 98,
    "b and h photo and video" : 215,
    "b.a. mason shoe" : 865,
    "babiesrus" : 211,
    "babyage.com" : 292,
    "baby to bee" : 808,
    "backcountry.com" : 280,
    "bag borrow or steal" : 530,
    "baghaus.com" : 410,
    "bagsbuy.com" : 519,
    "balsam hill" : 473,
    "banana republic" : 541,
    "bargain outfitters" : 391,
    "barnes and noble" : 36,
    "bass pro outdoors online llc" : 288,
    "bath and body works" : 769,
    "batteries.com" : 242,
    "beach camera" : 581,
    "bealls" : 809,
    "beauty" : 465,
    "belkin corporation" : 480,
    "bellacor.com" : 810,
    "bellagio hotel &amp;amp; casino, las vegas" : 348,
    "best deal magazines" : 71,
    "best buy" : 49,
    "betterworld.com" : 811,
    "biblio.com. inc" : 812,
    "biggerbooks" : 814,
    "bike nashbar" : 183,
    "birthday express" : 792,
    "birthday express.com" : 792,
    "birthdayexpress.com" : 792,
    "black forest home decor and lighting" : 815,
    "blades skating" : 239,
    "blair" : 312,
    "blinds.com" : 362,
    "blockbuster" : 111,
    "bloomdepot.com" : 275,
    "bloomingdale&#39;s" : 299,
    "blue nile" : 270,
    "bluefly" : 164,
    "bluehost.com" : 816,
    "bmg music services" : 70,
    "boca java" : 340,
    "bookcloseouts.com" : 817,
    "borders" : 194,
    "boscov's" : 113,
    "boston market" : 207,
    "breck's nurseries" : 516,
    "brooks brothers" : 431,
    "brookstone" : 230,
    "brylane home" : 495,
    "budget rent a car" : 492,
    "busted tees" : 515,
    "buy.com, inc." : 96,
    "buycostumes" : 4,
    "buydig.com" : 486,
    "cafe belmondo" : 295,
    "calvin klein" : 582,
    "cambridge soundworks" : 231,
    "camping world, inc." : 423,
    "capital one" : 435,
    "cardsdirect inc" : 542,
    "cars.com" : 819,
    "carsdirect.com" : 232,
    "car toys - a better way to go - the leading specialty retailer of mobile electr" : 440,
    "ccs" : 402,
    "celebrate express" : 790,
    "celebrate express.com" : 790,
    "celebrateexpress.com" : 790,
    "censuspc.com" : 23,
    "champs sports - where sport lives" : 471,
    "chase manhattan bank" : 93,
    "chegg.com" : 821,
    "cheryl and co." : 337,
    "chico's" : 546,
    "www.chiefvalue.com" : 311,
    "cigarrest" : 822,
    "circuit city" : 145,
    "circus circus" : 345,
    "citicards" : 260,
    "citibank" : 195,
    "www.clubit.com" : 320,
    "coastal contacts" : 496,
    "coca-cola store" : 438,
    "coffee for less.com" : 823,
    "compactappliance.com" : 509,
    "compusa" : 146,
    "computers4sure.com" : 5,
    "cooking.com" : 389,
    "corel corporation" : 523,
    "costco online" : 365,
    "costume express" : 789,
    "costume express.com" : 789,
    "costumeexpress.com" : 789,
    "costume supercenter" : 458,
    "costume craze" : 434,
    "costumes inc." : 448,
    "couturecandy.com" : 824,
    "creative" : 332,
    "crucial technology" : 58,
    "crutchfield" : 125,
    "csn stores" : 314,
    "cyberguys.com" : 457,
    "dale and thomas popcorn" : 261,
    "dan's chocolates" : 81,
    "david&#39;s cookies ? a bite above the rest." : 551,
    "dealsonic" : 8,
    "deepdiscount.com" : 315,
    "delaflowers" : 489,
    "delia's" : 400,
    "dell small business" : 43,
    "dell canada" : 156,
    "dell" : 6,
    "dentalplans.com" : 825,
    "diamond.com" : 248,
    "dicks sporting goods" : 150,
    "diet-to-go" : 826,
    "discount office supplies" : 265,
    "discountmags.com" : 160,
    "ic-mart" : 464,
    "discover bank" : 474,
    "discovery channel store" : 74,
    "dish network official website" : 525,
    "disney shopping" : 213,
    "dockers" : 566,
    "dot photo" : 269,
    "dotster" : 399,
    "drivingcomfort.com" : 355,
    "drugstore" : 37,
    "eaglebit" : 334,
    "eastbay" : 147,
    "eastern mountain sports" : 367,
    "easy cgi" : 828,
    "ebags" : 55,
    "ebay" : 9,
    "electronics boutique - ebgames" : 44,
    "ecampus.com" : 781,
    "ecost.com" : 25,
    "eddie bauer since 1920" : 342,
    "eddie bauer" : 95,
    "eforcity" : 487,
    "empirecovers.com" : 829,
    "endless" : 407,
    "enterprise rent-a-car" : 373,
    "entertainment" : 85,
    "epson store" : 128,
    "espn shop" : 386,
    "essentialapparel.com" : 830,
    "etnies shoes" : 556,
    "etoys.com" : 163,
    "etronics.com" : 298,
    "evaluezone.com" : 122,
    "evitamins" : 831,
    "www.ewiz.com" : 414,
    "expedia.com" : 159,
    "factorycard.com" : 266,
    "factorydirectdeals.com" : 374,
    "fandango" : 268,
    "faoschwarz" : 575,
    "fastweb scholarship search" : 833,
    "figleaves" : 325,
    "final score-discount shoes and apparel." : 381,
    "fingerhut" : 326,
    "finish line" : 228,
    "flipclips, inc." : 300,
    "florist" : 282,
    "fogdog sports" : 79,
    "fonts.com" : 834,
    "footlocker" : 103,
    "footsmart" : 380,
    "frames direct" : 772,
    "franklin covey" : 499,
    "fredericks of hollywood" : 450,
    "freshpair" : 317,
    "friend finder" : 835,
    "from you flowers flower delivery" : 329,
    "fry's electronics" : 13,
    "ftd florists" : 281,
    "fujitsu" : 92,
    "fye" : 445,
    "gamefly" : 398,
    "gamehouse" : 836,
    "gamestop" : 10,
    "gap" : 87,
    "gateway computers" : 60,
    "computer geeks" : 26,
    "gevalia" : 105,
    "giftcertificates.com" : 61,
    "giga golf" : 361,
    "globat" : 226,
    "general nutrition companies, inc." : 352,
    "go daddy software" : 123,
    "gogamer.com" : 67,
    "goldengadgets.com" : 144,
    "www.golfoutletsusa.com" : 837,
    "gourmet gift baskets" : 508,
    "griffin technology" : 368,
    "grower flowers" : 353,
    "gta hotels" : 838,
    "guess" : 420,
    "h&amp;amp;r block" : 372,
    "half" : 38,
    "hallmark.com" : 385,
    "handango" : 535,
    "handheld items" : 390,
    "hanes" : 476,
    "hardwarecooling.com" : 152,
    "herrington" : 358,
    "hickory farms" : 148,
    "highlights.com" : 291,
    "hilton" : 175,
    "hirekogolf" : 839,
    "the history channel" : 779,
    "home decorators collection" : 303,
    "home bistro" : 453,
    "the home depot" : 118,
    "homestead" : 209,
    "hostbrix.com" : 308,
    "hot topic" : 533,
    "hotels.com" : 313,
    "hotwire" : 256,
    "hewlett packard" : 89,
    "holabird sports" : 840,
    "home shopping network" : 59,
    "host gator" : 841,
    "hostrocket.com" : 842,
    "ibex" : 485,
    "ice.com" : 45,
    "thomson learning college textbooks" : 844,
    "ing direct" : 109,
    "ink4art" : 324,
    "inkgrabber" : 274,
    "internet florist" : 241,
    "invitation consultants" : 846,
    "iprint" : 227,
    "irobot corporation" : 262,
    "islandsurf" : 847,
    "itrimming" : 567,
    "ix web hosting" : 848,
    "j. c. whitney &amp;amp; co." : 424,
    "jcpenney" : 112,
    "j. crew" : 54,
    "jensonusa.com" : 214,
    "jetblue airways" : 351,
    "jitterbug" : 850,
    "johnston and murphy" : 371,
    "jos. a. bank clothiers" : 220,
    "j and r" : 11,
    "justdeals" : 86,
    "karmaloop" : 517,
    "kb toys" : 39,
    "kingston technology" : 115,
    "kitchenaid" : 890,
    "klinq.com" : 514,
    "k mart" : 75,
    "kodak imaging network, inc." : 100,
    "kohl's" : 130,
    "kraft" : 243,
    "lamps plus" : 190,
    "lands' end" : 369,
    "laplink" : 852,
    "laptops for less inc." : 853,
    "leapfrog toys" : 460,
    "lee jeans" : 463,
    "lenovo" : 91,
    "lenovo canada" : 503,
    "lens.com" : 442,
    "lenscrafters eyeware" : 490,
    "lessno" : 416,
    "letstalk" : 482,
    "lids" : 415,
    "lifelock identity theft prevention" : 854,
    "lighting by gregory" : 388,
    "limoges jewelry" : 501,
    "linens 'n things" : 78,
    "lingo.com" : 855,
    "logitech international s.a." : 569,
    "the london pass" : 856,
    "lord and taylor" : 237,
    "love and pride" : 857,
    "love-scent.com" : 858,
    "love toner" : 338,
    "lucky brand jeans" : 513,
    "luggage pros" : 859,
    "le travel store" : 235,
    "lumens" : 860,
    "lunarpages" : 861,
    "luxor las vegas" : 344,
    "macconnection.com" : 924,
    "macmall" : 296,
    "macspeech" : 862,
    "macy's" : 101,
    "magazines.com inc." : 309,
    "magazinecity.com" : 863,
    "magazinevalues" : 161,
    "magellan's" : 444,
    "mags for less" : 217,
    "maryland square shoes" : 864,
    "matchmaker.com" : 866,
    "max and chloe" : 867,
    "mcm electronics" : 500,
    "medicus" : 868,
    "meijer" : 555,
    "meritline.com" : 40,
    "metrokitchen" : 564,
    "mgm grand" : 346,
    "micro center" : 206,
    "microsoft adcenter" : 302,
    "microsoft small business directory" : 364,
    "microsoft corporation" : 557,
    "mikasa" : 293,
    "mister art" : 869,
    "mondera.com" : 80,
    "montgomery ward" : 395,
    "moosejaw" : 554,
    "morrell and co." : 157,
    "motorola" : 454,
    "musician's friend" : 446,
    "mvixstore.com" : 562,
    "mwave" : 141,
    "coca-cola company" : 439,
    "my wines direct" : 154,
    "nbc universal store" : 872,
    "nascar online" : 247,
    "the national geographic society" : 479,
    "the nba store" : 578,
    "netflix" : 62,
    "netmagazines" : 149,
    "network solutions" : 511,
    "new york and company" : 193,
    "newegg.com" : 53,
    "nflshop.com" : 252,
    "nike" : 382,
    "nisim international" : 873,
    "nitro-pak.com" : 874,
    "novica" : 51,
    "nuance communications, inc" : 875,
    "obopay" : 409,
    "office depot" : 29,
    "office designs" : 356,
    "officemax" : 12,
    "old navy" : 236,
    "omaha steaks" : 94,
    "one call" : 77,
    "onegreatfamily.com" : 876,
    "oneida" : 405,
    "onlineshoes.com" : 278,
    "only natural pet store" : 878,
    "onsale" : 121,
    "orbitz" : 253,
    "oregon mountain community" : 879,
    "oregon scientific" : 218,
    "the orvis company, inc." : 316,
    "overnight prints" : 531,
    "overstock" : 30,
    "packet8" : 461,
    "pacific sunwear" : 560,
    "palm" : 22,
    "paragon sports" : 272,
    "parts express" : 322,
    "paul fredrick" : 56,
    "pc connection, inc." : 124,
    "pc mall" : 14,
    "pc microstore" : 32,
    "performance bike" : 181,
    "perfume.com" : 411,
    "personal creations" : 279,
    "petco" : 459,
    "pet food direct" : 294,
    "petsmart" : 396,
    "philips" : 255,
    "philips outlet store" : 31,
    "photoworks" : 191,
    "pingo" : 370,
    "pitneyworks.com" : 331,
    "play-asia.com - video gaming &amp;amp; beyond" : 483,
    "powweb hosting" : 884,
    "prep sportswear" : 271,
    "proflowers.com" : 84,
    "progolf" : 240,
    "prostores" : 886,
    "puma" : 429,
    "punch software" : 887,
    "qwest.com" : 888,
    "radio shack" : 15,
    "rawlings sporting goods" : 394,
    "2004 masters tickets,masters golf tickets - razorgator" : 558,
    "realplayer" : 451,
    "redenvelope" : 330,
    "redoctane" : 522,
    "reebok" : 224,
    "register.com" : 536,
    "rei" : 76,
    "rei outlet" : 73,
    "restaurant.com" : 187,
    "ritz camera" : 110,
    "road runner sports" : 263,
    "rockport" : 238,
    "s&amp;amp;s worldwide" : 286,
    "safeway stores" : 778,
    "sam's club" : 413,
    "samsonite" : 328,
    "att (sbc)" : 82,
    "sears, roebuck and co." : 116,
    "second act" : 1,
    "sena technologies" : 427,
    "san francisco music box" : 484,
    "sharpcast.com" : 417,
    "invisibleshield" : 254,
    "www.shnoop.com" : 433,
    "shoe mall" : 565,
    "shoebuy" : 174,
    "shoes.com, inc." : 244,
    "shop.com" : 305,
    "shop4tech.com" : 33,
    "shopbop" : 310,
    "shopnbc" : 28,
    "shop pbs" : 273,
    "shutterfly" : 129,
    "sierra trading post" : 83,
    "signature days" : 287,
    "site sell" : 891,
    "skechers footwear" : 229,
    "skype" : 532,
    "smartbargains" : 64,
    "smartcruiser" : 245,
    "smarthome.com" : 151,
    "smugmug" : 893,
    "snapfish" : 182,
    "sony style" : 69,
    "sony style canada" : 518,
    "southwest vacations" : 577,
    "spa finder" : 404,
    "spreadshirt designer sweden" : 894,
    "stamps.com" : 397,
    "staples" : 16,
    "starbucks coffee company" : 895,
    "startec global communications" : 896,
    "start logic" : 897,
    "starwood hotels &amp;amp; resorts worldwide, inc." : 88,
    "steep" : 222,
    "steve madden" : 510,
    "sunglass hut international" : 426,
    "sunrocket" : 153,
    "stauer" : 898,
    "steven checn" : 184,
    "strawberrynet" : 899,
    "stylinonline.com" : 900,
    "supermediastore.com" : 34,
    "super warehouse" : 378,
    "surpluscomputers.com" : 17,
    "survey4profit.com" : 901,
    "switch hits" : 447,
    "symantec" : 520,
    "swimsuits for all" : 902,
    "szul online jewelry" : 107,
    "taca" : 903,
    "tafford uniforms" : 904,
    "target" : 403,
    "taxact online" : 333,
    "taxbrain" : 349,
    "taylor gifts online" : 321,
    "teamfanshop" : 788,
    "team quick cash" : 906,
    "tech depot" : 63,
    "teleflora.com," : 544,
    "teva" : 441,
    "the container store" : 580,
    "the great indoors" : 502,
    "mgm mirage" : 343,
    "the popcorn factory" : 257,
    "the sharper image" : 72,
    "sports authority" : 42,
    "sportsman's guide, inc." : 383,
    "the wine messenger" : 158,
    "thenerds.net" : 470,
    "thetechgeek.com" : 167,
    "thinkgeek" : 552,
    "thrifty car rental" : 283,
    "tigerdirect.com" : 19,
    "timberland" : 102,
    "the cute kid" : 908,
    "the sedona method" : 907,
    "the tire rack" : 90,
    "tivo" : 223,
    "t-mobile" : 403,
    "toshiba" : 212,
    "tower records" : 41,
    "toys r us" : 210,
    "tradepub" : 196,
    "travelocity" : 208,
    "treasure island" : 347,
    "treetopia trees" : 477,
    "tumi" : 910,
    "turbo tax" : 475,
    "tweeter, etc." : 20,
    "under armour athletic apparel" : 468,
    "ujena swimwear and fashion" : 926,
    "unbeatablesale.com" : 911,
    "upromise, inc." : 912,
    "urban outfitters" : 770,
    "us search" : 913,
    "vacation home rentals" : 915,
    "valuemags" : 497,
    "vann's inc." : 21,
    "vans" : 538,
    "verizon wireless" : 119,
    "internet phone service" : 250,
    "verso health and beauty direct" : 916,
    "verseo health and beauty direct" : 916,
    "vision direct" : 563,
    "vistaprint" : 189,
    "vonage" : 376,
    "wal-mart" : 35,
    "warner brothers" : 777,
    "water.com" : 917,
    "west marine" : 307,
    "westport big and tall" : 918,
    "whitesmoke text enrichment" : 919,
    "whoblinked.com" : 285,
    "willygoat.com" : 920,
    "wilsons leather" : 783,
    "windows marketplace" : 422,
    "windy city parrot" : 393,
    "wine" : 327,
    "wirefly.com" : 120,
    "woot, inc" : 168,
    "wt direct" : 505,
    "wynnlasvegas.com" : 412,
    "welcome to x10.com" : 99,
    "xheli" : 922,
    "xoxide inc" : 133,
    "extreme pc gear" : 138,
    "x-tremegeek.com" : 456,
    "yahoo!" : 197,
    "yahoo! fantasy sports" : 782,
    "yahoopersonals.com" : 793,
    "yoox" : 323,
    "york photo" : 171,
    "yugster.com" : 176,
    "yvesrocher" : 304,
    "zappos" : 169,
    "zipzoomfly.com" : 131,
    "zirh" : 377,
    "multiple zones international, inc." : 379
  },
  
  proxyUrl : "/ajax-proxy.php",
  bensMerUrl : "http://benscoupons.com/merchants_rss.xml/",
  bensCatUrl : "http://benscoupons.com/category_rss.xml/",
  bensRecUrl : "http://benscoupons.com/rss_widget.xml",
  init : function(storeName,divId) {
    BensWidget.storeName = storeName.toLowerCase();
    BensWidget.divId = divId;
    if(storeName == "mostrecent" || BensWidget.storeHash[BensWidget.storeName] == null){
      // load the most recent coupons
      jQuery.get( BensWidget.proxyUrl, { url : BensWidget.bensRecUrl },  function(xmlString){ BensWidget.processFeed(xmlString,"mostrecent"); });
    }
    else {
      // load the merchant coupons
      jQuery.get( BensWidget.proxyUrl, { url : BensWidget.bensMerUrl + BensWidget.storeHash[BensWidget.storeName] },  function(xmlString){ BensWidget.processFeed(xmlString); });
    }
  },
  
  processFeed : function(xmlString,doctype) {
    var doctype = doctype || "merchant";
    var jsonObj = jQuery.xml2json(xmlString);
    var title = jsonObj.channel.title.replace("BensCoupons.com ","").toUpperCase();
    //alert(jsonObj.toSource());
    var header_text = (doctype == "mostrecent") ? "HOT COUPONS" : "HOT " + title;
    var refer_string = "?referrer=Bodd";
    var html_table_header = "<div class=\"rightsubcoupon\"><div class=\"bens_head\"><div class=\"icon\">&nbsp; </div><h2>"+header_text+"</h2><hr class=\"head_separator\"><table class=\"bens_table_head\"><tr><th class=\"coupon_column\">Coupon</th><th class=\"expiration_column\">Expires</th><th class=\"code_column\">Coupon Code</th></tr></table></div><table class=\"bens_data\">";
    var html_table_footer = "</table></div>";
    var html_table_content = "";
    var items = [];
    if(jsonObj.channel.item instanceof Array) items = jsonObj.channel.item;
    else items[0] = jsonObj.channel.item;
    if(items[0].title.indexOf("No") > -1 && items[0].title.indexOf("Headlines") > -1) {
      if(doctype == "mostrecent"){
        BensWidget.abort();
      }
      //else if(doctype == "category"){
      //  jQuery.get( BensWidget.proxyUrl, { url : BensWidget.bensRecUrl },  function(xmlString){ BensWidget.processFeed(xmlString,"mostrecent"); });
      //}
      else {
        //doctype = "mostrecent";
        jQuery.get( BensWidget.proxyUrl, { url : BensWidget.bensRecUrl },  function(xmlString){ BensWidget.processFeed(xmlString,"mostrecent"); });
      }
      return;
    }
    var limit = (doctype == "mostrecent") ? 8 : 4;
    var displayTotal = (items.length < limit) ? items.length : limit;
    if(displayTotal == 0){}
    else {
      for(var i=0;i<displayTotal;i++){
        var code = (items[i].codestring) ? items[i].codestring : "Click Here!";
        var expDateParts = items[i].expiration.split("/");
        var expDate = new Date(expDateParts[0],expDateParts[1]-1,expDateParts[2]);
        var today = new Date();
        var expMonth = expDate.getMonth() + 1;
        var expDayofMonth = expDate.getDate();
        var expYear = expDate.getFullYear().toString().replace("20","");
        var expDateString =  expMonth + "/" + expDayofMonth + "/" + expYear;
        if(today > expDate){
          if(today.getDate() == expDate.getDate()) expDateString = "<span>Today</span>";
          else expDateString = "Expired";
        }
        html_table_content += "<tr><td class=\"coupon_column\"><a href=\""+items[i].guid+refer_string+"\" target=\"_blank\" rel=\"nofollow\">"+items[i].plaintitle+"</a></td><td class=\"expiration_column\">"+expDateString+"</td><td class=\"code_column\"><strong><a href=\""+items[i].guid+refer_string+"\" target=\"_blank\" rel=\"nofollow\">"+code+"</a></strong></td></tr>";
      }
      var html_table = html_table_header + html_table_content + html_table_footer;
      var widgetNode = document.getElementById(BensWidget.divId);
      widgetNode.innerHTML = html_table;
      widgetNode.style.display = "";
    }
  },
  
  abort : function() {
    var node = document.getElementById(BensWidget.divId);
    node.parentNode.removeChild(node);
  }
};

