var dialog = new function(){
  var IDs = {
    video:   0,
    generic: 0
  };

  this.generic = function(title, name, width, height, cleanup) {
    var dlg, options;
    options = { content: { id: '#dialog-' + name }, dialog: {} };
    
    // Build up the options from any parameters passed in
    if( title !== undefined  ){ options.content.title = title; }
    if( name !== undefined   ){ options.content.URL = '/include/dialogs/' + name + '.html'; }
    if( width !== undefined  ){ options.dialog.width = width; }
    if( height !== undefined ){ options.dialog.height = height; }
    
    // Create the dialog
    dlg = $.DialogEx(options);
    
    // If cleanup is set to true, go to town on the dialog when it is closed.
    if( cleanup !== undefined && cleanup === true )
    {
      dlg.bind('dialogclose', function(event, ui){
        // Destroy the dialog
        dlg.dialog( 'destroy' );
        // Remove the content
        $(options.content.id).remove();
      });
    }
    
    // Open the dialog
    dlg.dialog('open');
    
    // Just a counter for the number of dialogs loaded
    IDs.generic++;
  };
  
  this.coupon = function(code) { dialog.generic('<span style="color: #f00;">' + code + '</span> &ndash; Coupon Code Disclaimer', code); };
  this.error  = function(title, file, width, height) { dialog.generic('<span style="color: #f00;">' + title + '</span>', file, width, height); };

  this.AMEX              = function(){ dialog.generic('AMEX Subscriptions Info',                 'AMEX'               ); };
  this.AMEXTERMS1        = function(){ dialog.generic('Save 15%',                                'AMEXTERMS1'         ); };
  this.AMEXTERMS2        = function(){ dialog.generic('Earn Double Membership Rewards Points',   'AMEXTERMS2'         ); };
  this.FestiveWineCovers = function(){ dialog.generic('Festive Wine Covers',                     'festive-wine-covers'); };
  this.FOREIGN           = function(){ dialog.generic('Foreign Countries We Ship To',            'FOREIGN',   368, 400); };
  this.FOMCLIST          = function(){ dialog.generic('Fruit of the Month Club&reg; Collection', 'FOMCLIST',  700, 450); };
  this.GC_Terms          = function(){ dialog.generic('Gift Card Terms &amp; Conditions',        'GC-Terms'           ); };
  this.HolidayCutOffs    = function(){ dialog.generic('Holiday Shipping Cut-Off Dates',          'holidaycutoffs'     ); };
  this.STORY             = function(){ dialog.generic('Story of Harry &amp; David Disclaimer',   'STORY'              ); };
  this.STJUDE            = function(){ dialog.generic('St. Jude Disclaimer',                     'STJUDE'             ); };
  this.TPO               = function(){ dialog.generic('Discount Disclaimer',                     'TPO'                ); };
  this.FEUG              = function(){ dialog.generic('Free Express Upgrade Terms &amp; Conditions','FEUG'            ); };
  this.RIPENING          = function(){ dialog.generic('Ripening and Storage',                    'RIPENING'           ); };
  this.VALOFFER          = function(){ dialog.generic('Valentine&rsquo;s Day Delivery Deals',    'VALOFFER'           ); };
  this.FRIENDS           = function(){ dialog.generic('3 Ways to Save*',                         'FRIENDS'            ); };
  this.ENTERTAINMENT     = function(){ dialog.generic('Take 10% Off Your Entire Product Purchase','ENTERTAINMENT'      ); };

  
  this.FLAG     = function(){ dialog.coupon('FLAG');     };
  this.H66      = function(){ dialog.coupon('H66');      };
  this.SHIP     = function(){ dialog.coupon('SHIP');     };
  this.LABOR    = function(){ dialog.coupon('LABOR');    };
  this.SAVE     = function(){ dialog.coupon('SAVE');     };
  this.BIRTHDAY = function(){ dialog.coupon('BIRTHDAY'); };
  this.DISCOVER = function(){ dialog.coupon('DISCOVER'); };
  this.PEARDEAL = function(){ dialog.coupon('PEARDEAL'); };
  this.GIVE     = function(){ dialog.coupon('GIVE');     };
  this.JOLLY    = function(){ dialog.coupon('JOLLY');    };
  this.JOY      = function(){ dialog.coupon('JOY');      };
  this.DEAL     = function(){ dialog.coupon('DEAL');     };
  this.SAVENOW  = function(){ dialog.coupon('SAVENOW');  };
  this.STAR     = function(){ dialog.coupon('STAR');     };
  this.HOLLY    = function(){ dialog.coupon('HOLLY');    };
  this.JOLLY    = function(){ dialog.coupon('JOLLY');    };
  this.HAPPY    = function(){ dialog.coupon('HAPPY');    };
  this.MERRY    = function(){ dialog.coupon('MERRY');    };
  this.PRANCER  = function(){ dialog.coupon('PRANCER');  };
  this.DASHER   = function(){ dialog.coupon('DASHER');   };
  this.FREESHIP = function(){ dialog.coupon('FREESHIP'); };
  this.SAVETODAY= function(){ dialog.coupon('SAVETODAY');};
  this.CHEERS   = function(){ dialog.coupon('CHEERS');   };
  this.SANTA    = function(){ dialog.coupon('SANTA');    };
  this.SNOW     = function(){ dialog.coupon('SNOW');     };
  this.TINSEL   = function(){ dialog.coupon('TINSEL');   };
  this.YIPPEE   = function(){ dialog.coupon('YIPPEE');   };
  this.LOVE     = function(){ dialog.coupon('LOVE');     };
  this.CLOVER   = function(){ dialog.coupon('CLOVER');   };
  this.SAVE20   = function(){ dialog.coupon('SAVE20');   };
  this.FREE50   = function(){ dialog.coupon('FREE50');   };
  this.SENDFREE = function(){ dialog.coupon('SENDFREE'); };


  
  this.RIOJA     = function(){ dialog.generic('Rioja Video',    'RIOJA',     550, undefined, true); };
  this.STJUDEVID = function(){ dialog.generic('St. Jude Video', 'STJUDEVID', 880, undefined, true); };

  this.checkoutInvalidAddress = function(){ dialog.error('Attention', 'checkout-invalid-address', 400, 100); };
  this.checkoutDeliveryError  = function(){ dialog.error('Attention', 'checkout-delivery-error',  400, 100); };
  this.checkoutSaveAddress    = function(){ dialog.error('Attention', 'checkout-save-address',    400, 100); };
  this.checkoutSaveDelivery   = function(){ dialog.error('Attention', 'checkout-save-delivery',   400, 100); };
};

(function(f){function D(){this.regional=[];this.regional['']={labels:['Years','Months','Weeks','Days','Hours','Minutes','Seconds'],labels1:['Year','Month','Week','Day','Hour','Minute','Second'],compactLabels:['y','m','w','d'],timeSeparator:':',isRTL:false};this._9={until:null,since:null,timezone:null,serverSync:null,format:'dHMS',layout:'',compact:false,description:'',expiryUrl:'',expiryText:'',alwaysExpire:false,onExpiry:null,onTick:null};f.extend(this._9,this.regional[''])}var v='countdown';var m=0;var n=1;var t=2;var u=3;var o=4;var s=5;var p=6;f.extend(D.prototype,{markerClassName:'hasCountdown',_r:setInterval(function(){f.countdown._j()},980),_7:[],setDefaults:function(b){this._f(this._9,b);E(this._9,b||{})},UTCDate:function(b,a,c,d,g,h,k,i){if(typeof a=='object'&&a.constructor==Date){i=a.getMilliseconds();k=a.getSeconds();h=a.getMinutes();g=a.getHours();d=a.getDate();c=a.getMonth();a=a.getFullYear()}var e=new Date();e.setUTCFullYear(a);e.setUTCDate(1);e.setUTCMonth(c||0);e.setUTCDate(d||1);e.setUTCHours(g||0);e.setUTCMinutes((h||0)-(Math.abs(b)<30?b*60:b));e.setUTCSeconds(k||0);e.setUTCMilliseconds(i||0);return e},_s:function(b,a){if(!a){return f.countdown._9}var c=f.data(b,v);return(a=='all'?c.options:c.options[a])},_k:function(b,a){var c=f(b);if(c.hasClass(this.markerClassName)){return}c.addClass(this.markerClassName);var d={options:f.extend({},a),_0:[0,0,0,0,0,0,0]};f.data(b,v,d);this._l(b)},_g:function(b){if(!this._h(b)){this._7.push(b)}},_h:function(b){return(f.inArray(b,this._7)>-1)},_c:function(a){this._7=f.map(this._7,function(b){return(b==a?null:b)})},_j:function(){for(var b=0;b<this._7.length;b++){this._b(this._7[b])}},_b:function(b,a){var c=f(b);a=a||f.data(b,v);if(!a){return}c.html(this._m(a));c[(this._1(a,'isRTL')?'add':'remove')+'Class']('countdown_rtl');var d=this._1(a,'onTick');if(d){d.apply(b,[a._5!='lap'?a._0:this._d(a,a._2,new Date())])}var g=a._5!='pause'&&(a._3?a._8.getTime()<=a._3.getTime():a._8.getTime()>=a._6.getTime());if(g&&!a._i){a._i=true;if(this._h(b)||this._1(a,'alwaysExpire')){this._c(b);var h=this._1(a,'onExpiry');if(h){h.apply(b,[])}var k=this._1(a,'expiryText');if(k){var i=this._1(a,'layout');a.options.layout=k;this._b(b,a);a.options.layout=i}var e=this._1(a,'expiryUrl');if(e){window.location=e}}a._i=false}else if(a._5=='pause'){this._c(b)}f.data(b,v,a)},_l:function(b,a,c){a=a||{};if(typeof a=='string'){var d=a;a={};a[d]=c}var g=f.data(b,v);if(g){this._f(g.options,a);E(g.options,a);this._n(b,g);f.data(b,v,g);var h=new Date();if((g._3&&g._3<h)||(g._6&&g._6>h)){this._g(b)}this._b(b,g)}},_f:function(b,a){var c=false;for(var d in a){if(d.match(/[Ll]abels/)){c=true;break}}if(c){for(var d in b){if(d.match(/[Ll]abels[0-9]/)){b[d]=null}}}},_n:function(b,a){var c=this._1(a,'serverSync');c=(c?c.apply(b,[]):null);var d=new Date();var g=this._1(a,'timezone');g=(g==null?-d.getTimezoneOffset():g);a._3=this._1(a,'since');if(a._3){a._3=this.UTCDate(g,this._e(a._3,null));if(a._3&&c){a._3.setMilliseconds(a._3.getMilliseconds()+d.getTime()-c.getTime())}}a._6=this.UTCDate(g,this._e(this._1(a,'until'),d));if(c){a._6.setMilliseconds(a._6.getMilliseconds()+d.getTime()-c.getTime())}a._2=this._o(a)},_t:function(b){var a=f(b);if(!a.hasClass(this.markerClassName)){return}this._c(b);a.removeClass(this.markerClassName).empty();f.removeData(b,v)},_u:function(b){this._5(b,'pause')},_v:function(b){this._5(b,'lap')},_w:function(b){this._5(b,null)},_5:function(b,a){var c=f.data(b,v);if(c){if(c._5=='pause'&&!a){c._0=c._p;var d=(c._3?'-':'+');c[c._3?'_3':'_6']=this._e(d+c._0[0]+'y'+d+c._0[1]+'o'+d+c._0[2]+'w'+d+c._0[3]+'d'+d+c._0[4]+'h'+d+c._0[5]+'m'+d+c._0[6]+'s');this._g(b)}c._5=a;c._p=(a=='pause'?c._0:null);f.data(b,v,c);this._b(b,c)}},_x:function(b){var a=f.data(b,v);return(!a?null:(!a._5?a._0:this._d(a,a._2,new Date())))},_1:function(b,a){return(b.options[a]!=null?b.options[a]:f.countdown._9[a])},_e:function(l,q){var r=function(b){var a=new Date();a.setTime(a.getTime()+b*1000);return a};var w=function(b){b=b.toLowerCase();var a=new Date();var c=a.getFullYear();var d=a.getMonth();var g=a.getDate();var h=a.getHours();var k=a.getMinutes();var i=a.getSeconds();var e=/([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g;var j=e.exec(b);while(j){switch(j[2]||'s'){case's':i+=parseInt(j[1],10);break;case'm':k+=parseInt(j[1],10);break;case'h':h+=parseInt(j[1],10);break;case'd':g+=parseInt(j[1],10);break;case'w':g+=parseInt(j[1],10)*7;break;case'o':d+=parseInt(j[1],10);g=Math.min(g,f.countdown._a(c,d));break;case'y':c+=parseInt(j[1],10);g=Math.min(g,f.countdown._a(c,d));break}j=e.exec(b)}return new Date(c,d,g,h,k,i,0)};var x=(l==null?q:(typeof l=='string'?w(l):(typeof l=='number'?r(l):l)));if(x)x.setMilliseconds(0);return x},_a:function(b,a){return 32-new Date(b,a,32).getDate()},_m:function(c){c._0=periods=(c._5?c._0:this._d(c,c._2,new Date()));var d=false;var g=0;for(var h=0;h<c._2.length;h++){d|=(c._2[h]=='?'&&periods[h]>0);c._2[h]=(c._2[h]=='?'&&!d?null:c._2[h]);g+=(c._2[h]?1:0)}var k=this._1(c,'compact');var i=this._1(c,'layout');var e=(k?this._1(c,'compactLabels'):this._1(c,'labels'));var j=this._1(c,'timeSeparator');var l=this._1(c,'description')||'';var q=function(b){var a=f.countdown._1(c,'compactLabels'+periods[b]);return(c._2[b]?periods[b]+(a?a[b]:e[b])+' ':'')};var r=function(b){var a=f.countdown._1(c,'labels'+periods[b]);return(c._2[b]?'<span class="countdown_section"><span class="countdown_amount">'+periods[b]+'</span><br/>'+(a?a[b]:e[b])+'</span>':'')};return(i?this._q(c,i,k):((k?'<span class="countdown_row countdown_amount'+(c._5?' countdown_holding':'')+'">'+q(m)+q(n)+q(t)+q(u)+(c._2[o]?this._4(periods[o],2):'')+(c._2[s]?(c._2[o]?j:'')+this._4(periods[s],2):'')+(c._2[p]?(c._2[o]||c._2[s]?j:'')+this._4(periods[p],2):''):'<span class="countdown_row countdown_show'+g+(c._5?' countdown_holding':'')+'">'+r(m)+r(n)+r(t)+r(u)+r(o)+r(s)+r(p))+'</span>'+(l?'<span class="countdown_row countdown_descr">'+l+'</span>':'')))},_q:function(d,g,h){var k=this._1(d,(h?'compactLabels':'labels'));var i=function(b){return(f.countdown._1(d,(h?'compactLabels':'labels')+d._0[b])||k)[b]};var e=function(b,a){return Math.floor(b/a)%10};var j={desc:this._1(d,'description'),sep:this._1(d,'timeSeparator'),yl:i(m),yn:d._0[m],ynn:this._4(d._0[m],2),ynnn:this._4(d._0[m],3),y1:e(d._0[m],1),y10:e(d._0[m],10),y100:e(d._0[m],100),y1000:e(d._0[m],1000),ol:i(n),on:d._0[n],onn:this._4(d._0[n],2),onnn:this._4(d._0[n],3),o1:e(d._0[n],1),o10:e(d._0[n],10),o100:e(d._0[n],100),o1000:e(d._0[n],1000),wl:i(t),wn:d._0[t],wnn:this._4(d._0[t],2),wnnn:this._4(d._0[t],3),w1:e(d._0[t],1),w10:e(d._0[t],10),w100:e(d._0[t],100),w1000:e(d._0[t],1000),dl:i(u),dn:d._0[u],dnn:this._4(d._0[u],2),dnnn:this._4(d._0[u],3),d1:e(d._0[u],1),d10:e(d._0[u],10),d100:e(d._0[u],100),d1000:e(d._0[u],1000),hl:i(o),hn:d._0[o],hnn:this._4(d._0[o],2),hnnn:this._4(d._0[o],3),h1:e(d._0[o],1),h10:e(d._0[o],10),h100:e(d._0[o],100),h1000:e(d._0[o],1000),ml:i(s),mn:d._0[s],mnn:this._4(d._0[s],2),mnnn:this._4(d._0[s],3),m1:e(d._0[s],1),m10:e(d._0[s],10),m100:e(d._0[s],100),m1000:e(d._0[s],1000),sl:i(p),sn:d._0[p],snn:this._4(d._0[p],2),snnn:this._4(d._0[p],3),s1:e(d._0[p],1),s10:e(d._0[p],10),s100:e(d._0[p],100),s1000:e(d._0[p],1000)};var l=g;for(var q=0;q<7;q++){var r='yowdhms'.charAt(q);var w=new RegExp('\\{'+r+'<\\}(.*)\\{'+r+'>\\}','g');l=l.replace(w,(d._2[q]?'$1':''))}f.each(j,function(b,a){var c=new RegExp('\\{'+b+'\\}','g');l=l.replace(c,a)});return l},_4:function(b,a){b=''+b;if(b.length>=a){return b}b='0000000000'+b;return b.substr(b.length-a)},_o:function(b){var a=this._1(b,'format');var c=[];c[m]=(a.match('y')?'?':(a.match('Y')?'!':null));c[n]=(a.match('o')?'?':(a.match('O')?'!':null));c[t]=(a.match('w')?'?':(a.match('W')?'!':null));c[u]=(a.match('d')?'?':(a.match('D')?'!':null));c[o]=(a.match('h')?'?':(a.match('H')?'!':null));c[s]=(a.match('m')?'?':(a.match('M')?'!':null));c[p]=(a.match('s')?'?':(a.match('S')?'!':null));return c},_d:function(h,k,i){h._8=i;h._8.setMilliseconds(0);var e=new Date(h._8.getTime());if(h._3){if(i.getTime()<h._3.getTime()){h._8=i=e}else{i=h._3}}else{e.setTime(h._6.getTime());if(i.getTime()>h._6.getTime()){h._8=i=e}}var j=[0,0,0,0,0,0,0];if(k[m]||k[n]){var l=f.countdown._a(i.getFullYear(),i.getMonth());var q=f.countdown._a(e.getFullYear(),e.getMonth());var r=(e.getDate()==i.getDate()||(e.getDate()>=Math.min(l,q)&&i.getDate()>=Math.min(l,q)));var w=function(b){return(b.getHours()*60+b.getMinutes())*60+b.getSeconds()};var x=Math.max(0,(e.getFullYear()-i.getFullYear())*12+e.getMonth()-i.getMonth()+((e.getDate()<i.getDate()&&!r)||(r&&w(e)<w(i))?-1:0));j[m]=(k[m]?Math.floor(x/12):0);j[n]=(k[n]?x-j[m]*12:0);var F=function(b,a,c){var d=(b.getDate()==c);var g=f.countdown._a(b.getFullYear()+a*j[m],b.getMonth()+a*j[n]);if(b.getDate()>g){b.setDate(g)}b.setFullYear(b.getFullYear()+a*j[m]);b.setMonth(b.getMonth()+a*j[n]);if(d){b.setDate(g)}return b};if(h._3){e=F(e,-1,q)}else{i=F(new Date(i.getTime()),+1,l)}}var y=Math.floor((e.getTime()-i.getTime())/1000);var A=function(b,a){j[b]=(k[b]?Math.floor(y/a):0);y-=j[b]*a};A(t,604800);A(u,86400);A(o,3600);A(s,60);A(p,1);if(y>0&&!h._3){var G=[1,12,4.3482,7,24,60,60];var B=p;var C=1;for(var z=p;z>=m;z--){if(k[z]){if(j[B]>=C){j[B]=0;y=1}if(y>0){j[z]++;y=0;B=z;C=1}}C*=G[z]}}return j}});function E(b,a){f.extend(b,a);for(var c in a){if(a[c]==null){b[c]=null}}return b}f.fn.countdown=function(b){var a=Array.prototype.slice.call(arguments,1);if(b=='getTimes'||b=='settings'){return f.countdown['_'+b+'Countdown'].apply(f.countdown,[this[0]].concat(a))}return this.each(function(){if(typeof b=='string'){f.countdown['_'+b+'Countdown'].apply(f.countdown,[this].concat(a))}else{f.countdown._k(this,b)}})};f.countdown=new D()})(jQuery);
(function(){
  function getServerTime()
  {
    var time = null;
    $.ajax({
      url:        '/gifts/store/CommonAssets/Common/include/time.jsp',
      async:      false, 
      dataType:   'html',
      dataFilter: function(data) { return $(data).text(); },
      success:    function(text) { time = new Date(text); },
      error:      function(http, message, exc) { time = new Date(); }
    });
    return time;
  }

  function getTonight()
  {
    var result;
    result = new Date();
    
    result.setHours(0);
    result.setMinutes(0);
    result.setSeconds(0);
    result.setTime(result.getTime() + 86400 * 1000);
    
    return result;
  }

  function startCountdown()
  {
    var target = getTonight();
    $('#content .espot .countdown').countdown({
      until: target,
      format: 'HMS',
      serverSync: getServerTime
    });
  }
  
  $(function(){ if( $.Exists('#content .espot .countdown') ){ startCountdown(); } });
  
})();