jQuery.noConflict();

jQuery(document).ready(function() {
  if (!jQuery('#flash').is(':hidden')) {
    jQuery('#flash').oneTime('5s', function() {
      jQuery('#flash').slideToggle('slow');
    });
  }

  jQuery('#search-keywords').focus(function() {
    this.value = '';
  });

  if (jQuery('#main:has(#welcome)')) {
    var counter = 0;
    jQuery('#welcome div').everyTime('3s', function() {
      if (counter == 0) {
        jQuery('#welcome div').css('background-image', 'url(../images/deals-clothes.png)');
        counter = 1;
      } else if (counter == 1) {
        jQuery('#welcome div').css('background-image', 'url(../images/deals-computers.png)');
        counter = 2;
      } else if (counter == 2) {
        jQuery('#welcome div').css('background-image', 'url(../images/deals-electronics.png)');
        counter = 3;
      } else {
        jQuery('#welcome div').css('background-image', 'url(../images/deals-toys.png)');
        counter = 0;
      }
    });
  }
  
  
  
  
  
    ZeroClipboard.setMoviePath('http://www.dealtattle.com/ZeroClipboard.swf');
    
    var tooltipText = 'Click code to copy and open site';
  
    jQuery('div.deal a.code').live('mouseover', function() {
      var code = jQuery(this);
      if (clip) {
        clip.destroy();
      }
      var clip = new ZeroClipboard.Client();
      clip.setHandCursor(true);
      clip.setCSSEffects(true);
      clip.setText(code.text());
      if (clip.div) {
        clip.receiveEvent('mouseout', null);
        clip.reposition(this);
      } else {
        clip.glue(this);
      }
  
      clip.receiveEvent('mouseover', null);
  
      var tooltip = jQuery('#deal-tooltip');
      var pos = code.parents('.deal').position();
      tooltip.text(tooltipText);
      tooltip.css({
        left: pos.left + code.width() + 110,
        top: pos.top + 16,
        opacity: 0.8,
        display: 'none'
      }).fadeIn('slow');
  
      clip.addEventListener('mouseout', function() {
        tooltip.fadeOut('slow');
        clip.destroy();
      });
  
      clip.addEventListener('complete', function() {
        tooltip.text('Copied!');
        var url = code.attr('href');
        var merchantWindow = window.open(url, '_blank');
        if (!merchantWindow) {
          top.location = url;
        }
      });
    });
  
    if (jQuery('#main:has(#deals)')) {
    /*
      jQuery('#deals').everyTime('30s', function() {
        var update = true;
        jQuery('div.comments, div.comment, div.lists, div.share').each(function() {
          if (!jQuery(this).is(':hidden')) {
            update = false;
          }
        });
        if (update) {
          jQuery('#deals div.loading').show();
          jQuery('#deals div.update-area').load(jQuery('#deals div.update a').attr('href'));
        }
      });
  */
      jQuery('#deals li a').live('click', function() {
        var active = jQuery(this).parent('li');
        jQuery('#deals div.update-area').load(this.href, null, function() {
          jQuery('#deals li').removeClass('selected');
          active.addClass('selected');
        });
        return false;
      });
  
      jQuery('#deals div.pagination a, #deals div.update a').live('click', function() {
        jQuery('#deals div.update-area').load(this.href);
        return false;
      });
  
      jQuery('div.deal a.show-comments, div.deal a.hide-comments').live('click', function() {
        var parent = jQuery(this).parents('div.deal');
        var target = parent.find('div.comments');
        if (target.is(':hidden')) {
          parent.find('div.lists').slideUp();
          parent.find('div.share').slideUp();
          target.load(this.href, null, function() { target.slideToggle(); });
        } else {
          target.slideToggle();
        }
        return false;
  		});
  
      jQuery('div.deal a.add-comment, div.deal div.comment a.cancel').live('click', function() {
        var parent = jQuery(this).parents('div.deal');
        var target = parent.find('div.comment');
        if (target.is(':hidden')) {
          parent.find('div.lists').slideUp();
          parent.find('div.share').slideUp();
          target.load(this.href, null, function() { target.slideToggle(); });
        } else {
          target.slideToggle();
        }
        return false;
  		});
  
      jQuery('div.deal div.comment a.submit').live('click', function() {
        var parent = jQuery(this).parents('div.deal');
        var form = parent.find('div.comment form');
        var action = form.attr('action');
        var target = parent.find('div.comment');
        var comments = parent.find('div.comments');
  
        jQuery.ajax({
          type: 'post',
          url: action,
          data: form.serialize(),
          success: function(html) {
            target.html(html);
            if (html.search('.error_list') == -1) {
              var commentsAction = action.replace('new_comment', 'comments');
  
              comments.load(commentsAction, null, function() {
                if (comments.is(':hidden')) { comments.slideToggle(); }
                target.slideToggle('slow');
              });
            }
          }
        });
        return false;
      });
  
      jQuery('div.deal a.add-to-list').live('click', function() {
        if (jQuery(this).hasClass('login-link')) {
          return true;
        }
        var parent = jQuery(this).parents('div.deal');
        var target = parent.find('div.lists');
        if (target.is(':hidden')) {
          parent.find('div.comments').slideUp();
          parent.find('div.comment').slideUp();
          parent.find('div.share').slideUp();
          target.load(this.href, null, function() { target.slideToggle(); });
        } else {
          target.slideToggle();
        }
        return false;
      });
  
      jQuery('div.deal a.new-list').live('click', function() {
        var target = jQuery(this).parents('div.deal').find('div.lists');
        target.slideToggle('fast');
        target.load(this.href, null, function() { target.slideToggle('fast'); });
        return false;
      });
  
      jQuery('div.deal div.lists a.cancel').live('click', function() {
        var target = jQuery(this).parents('div.deal').find('div.lists');
        target.slideToggle('slow', function() { target.text(''); });
        return false;
      });
  
      jQuery('div.deal div.lists a.submit').live('click', function() {
        var parent = jQuery(this).parents('div.deal');
        var target = parent.find('div.lists');
        var form = target.find('form');
  
        jQuery.ajax({
          type: 'post',
          url: form.attr('action'),
          data: form.serialize(),
          success: function(html) {
            target.html(html);
            if (html.search('.error_list') == -1) {
              target.oneTime('5s', function() {
                target.slideToggle('slow');
              });
            }
          }
        });
        return false;
      });
  
      jQuery('div.deal div.vote a.up').live('click', function() {
        jQuery.post(this.href, null, function() {
          if (jQuery('#deals div.update a').size()) {
            jQuery('div.update-area').load(jQuery('#deals div.update a').attr('href'));
          } else {
            location.reload(true);
          }
        });
        return false;
      });
      
      jQuery('div.deal div.vote a.down').live('click', function() {
        jQuery.post(this.href, null, function() {
          if (jQuery('#deals div.update a').size()) {
            jQuery('div.update-area').load(jQuery('#deals div.update a').attr('href'));
          } else {
            location.reload(true);
          }
        });
        return false;
      });
    }
  
  
  
  
  
  
  
  
  
    jQuery('div.deals-list a.show-comments, div.deals-list a.hide-comments').live('click', function() {
      var parent = jQuery(this).parents('div.deals-list');
      var target = parent.find('div.comments');
      if (target.is(':hidden')) {
        parent.find('div.edit').slideUp();
        parent.find('div.delete').slideUp();
        parent.find('div.share').slideUp();
        target.load(this.href, null, function() { target.slideToggle(); });
      } else {
        target.slideToggle();
      }
      return false;
    });
  
    jQuery('div.deals-list a.add-comment').live('click', function() {
      var parent = jQuery(this).parents('div.deals-list');
      var target = parent.find('div.comment');
      if (target.is(':hidden')) {
        parent.find('div.edit').slideUp();
        parent.find('div.delete').slideUp();
        parent.find('div.share').slideUp();
        target.load(this.href, null, function() { target.slideToggle(); });
      } else {
        target.slideToggle();
      }
      return false;
    });
  
    jQuery('div.deals-list div.comment a.cancel').live('click', function() {
      var target = jQuery(this).parents('div.deals-list').find('div.comment');
      target.slideToggle('slow', function() { target.text(''); });
      return false;
    });
  
    jQuery('div.deals-list div.comment a.submit').live('click', function() {
      var parent = jQuery(this).parents('div.deals-list');
      var form = parent.find('div.comment form');
      var action = form.attr('action');
      var target = parent.find('div.comment');
      var comments = parent.find('div.comments');
  
      jQuery.ajax({
        type: 'post',
        url: action,
        data: form.serialize(),
        success: function(html) {
          target.html(html);
          if (html.search('.error_list') == -1) {
            var commentsAction = action.replace('new_comment', 'comments');
  
            comments.load(commentsAction, null, function() {
              if (comments.is(':hidden')) { comments.slideToggle(); }
              target.slideToggle('slow');
            });
          }
        }
      });
      return false;
    });
  
    jQuery('div.deals-list a.share').live('click', function() {
      if (jQuery(this).hasClass('login-link')) {
        return true;
      }
      var parent = jQuery(this).parents('div.deals-list');
      var target = parent.find('div.share');
      if (target.is(':hidden')) {
        parent.find('div.comments').slideUp();
        parent.find('div.comment').slideUp();
        parent.find('div.edit').slideUp();
        parent.find('div.delete').slideUp();
        target.load(this.href, null, function() { target.slideToggle(); });
      } else {
        target.slideToggle();
      }
      return false;
    });
  
    jQuery('div.deals-list a.delete').live('click', function() {
      var target = jQuery(this).parents('div.deals-list').find('div.ajax-tools div.delete');
      target.load(this.href, null, function() { target.slideToggle(); })
      return false;
    });
  
    jQuery('div.deals-list div.deals a.delete-deal').live('click', function() {
      var target = jQuery(this).parents('div.deal-tools').find('div.deal-delete div.confirm');
      target.load(this.href, null, function() { target.slideToggle(); })
      return false;
    });
  
    jQuery('div.deals-list div.share a.cancel').live('click', function() {
      var target = jQuery(this).parents('div.deals-list').find('div.share');
      target.slideToggle('slow', function() { target.text(''); });
      return false;
    });
  
    jQuery('div.deals-list div.delete a.cancel').live('click', function() {
      var target = jQuery(this).parents('div.deals-list').find('div.delete');
      target.slideToggle('slow', function() { target.text(''); });
      return false;
    });
  
    jQuery('div.deals-list div.delete a.submit').live('click', function() {
      jQuery.post(this.href, null, function() {
        location.href = jQuery('#nav a.lists').attr('href');
      });
      return false;
    });
  
    jQuery('div.deals-list div.deal-delete a.cancel').live('click', function() {
      var target = jQuery(this).parents('div.deal-delete').find('div.confirm');
      target.slideToggle('slow', function() { target.text(''); });
      return false;
    });
  
    jQuery('div.deals-list div.deal-delete a.submit').live('click', function() {
      jQuery.post(this.href, null, function() {
        jQuery('#deals-list').load(location.href);
      });
      return false;
    });
  
    jQuery('div.deals-list div.share a.submit').live('click', function() {
      var parent = jQuery(this).parents('div.deals-list');
      var target = parent.find('div.share');
      var form = target.find('form');
  
      jQuery.ajax({
        type: 'post',
        url: form.attr('action'),
        data: form.serialize(),
        success: function(html) {
          target.html(html);
          if (html.search('.error_list') == -1) {
            target.oneTime('5s', function() {
              target.slideToggle('slow');
            });
          }
        }
      });
      return false;
    });
  
    jQuery('div.deals-list div.vote a.up').live('click', function() {
      jQuery.post(this.href, null, function() {
        jQuery('#deals-list').load(location.href);
      });
      return false;
    });
  
    jQuery('div.deals-list div.vote a.down').live('click', function() {
      jQuery.post(this.href, null, function() {
        jQuery('#deals-list').load(location.href);
      });
      return false;
    });
  
});
