$(document).ready(function(){


  $(".search-active").mousedown(function(){
    $(this).css("background-position","0 -58px");
  }).mouseup(function(){
    $(this).css("background-position","0 0");
  });
  
  $("#search-submit").mousedown(function(){
    var templateBase = $("#template-directory").attr("href");
    $(this).attr("src",''+templateBase+'/images/find-active.gif');
  });
  
  $("#search-submit").click(function(){
    $(this).attr("src",''+templateBase+'/images/find-spinner.gif');
  });

  
  $(".get-rss").mousedown(function(){
    $(this).css("background-position","0 -45px");
  }).mouseup(function(){
    $(this).css("background-position","0 0");
  });

// trigger lightbox on exit
//$('.click').livequeryquery('click', function(event) {
  
if(!$('body').hasClass('trial')){
  $('.click').livequery('click', function() {
    $(this).parent().parent().parent().parent().parent().parent().find('.button.click').hide();
  });
}


$('.activate').livequery('click', function() {
});

//if($('.center').attr('id') != 'adsense_page'){
$('.click').livequery('click', function() {
//var subscribe_html = $(".subscribe").html();
//$("#test-html").html('<div class="MailPress">' + subscribe_html + '</div>');

//var bitlyUrl = $(this).parent().parent().parent().find(".bitly-url").attr("href");
//var tweetUrl = $(this).parent().parent().parent().find(".tweet-this").attr("href");
//var facebookUrl = $(this).parent().parent().parent().find(".fb-share").attr("href");
var couponTitle = $(this).parent().parent().parent().find(".coupon-title").html();
var couponCat = $(this).parent().parent().parent().find(".post-cat img").attr("title");
var catID = $(this).parent().parent().parent().find(".merchid").attr("title");

var templateBase = $("#template-directory").attr("href");
//$("#share-html").html('<h2 style="text-align:right;float:left;width:70px;color:#999999;font-size:1em;font-weight:lighter;">Share This<br />Coupon:</h2><a id="show-link" rel="nofollow" href="#" onclick="javascript: pageTracker._trackPageview(\'/link/'+couponCat+'/lightbox\');" ><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_link.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/fbshare/'+couponCat+'/lightbox\');"rel="nofollow" target="_blank" href="'+facebookUrl+'"> <img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_facebook.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/tweet/'+couponCat+'/lightbox\');" href="'+tweetUrl+'"rel="nofollow" target="_blank" ><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_twitter.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/emailshare/'+couponCat+'/lightbox\');" rel="nofollow" href="mailto:?subject='+couponTitle+' '+couponCat+' Coupon &body=Check out this coupon from SaveBrite.com '+bitlyUrl+'"><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_email.gif" /></a>');
$("#share-html").html('');

$.fx.speeds._default = 1000;

lightboxExit = $('#lightbox-exit').html();
// grab 
var thisClass = $('#wrap').attr('class');

var dialog = $('#lightbox-exit')
.load(lightboxExit)
  .dialog({
    autoOpen: false,
    height: 340,
    width: 600,
    modal: true,
    show: 'blind',
    dialogClass: thisClass,
    hide: 'explode',
    buttons: { "No thanks. Just show me the coupon code.": function() { $(this).dialog("close"); } } 
});

//dialog.appendTo($("form:first"));

var vote = $(this).parent().parent().parent().find(".vote").html();
if(vote == undefined){//if we are on product page
var vote = $(this).parent().parent().find(".click span").attr("title");
}

$("#lightbox-exit .vote").html(vote);
$("#lightbox-exit .cat-title").html(couponCat); //update coupon cat title in lightbox
$("#lightbox-exit #categ").val(catID); // update coupon cat id in lightbox

//var Code = $(this).parent().parent().find(".click span").attr('title');
//if(Code != undefined){
//var hiddenCode = 'Coupon Code:<br /><strong>'+Code+'</strong>';
//$("#lightbox-exit .show-code").html(hiddenCode);
//}

// end email lightbox

/*
$(".post-cat").each(function(){
  var Code = $(this).parent().parent().find(".click span").attr('title');
  if(Code != undefined){
  $(this).html('Coupon Code:<br /><span>'+Code+'</span>');
  }
}
);
*/

//$('#cat').bgiframe(); // fix ie selec from poping above dialog

//initiate modal dialog
dialog.dialog('open');

});
//} end if adsense page

/* Old Facebook Fan Lightbox
//trigger facebook dialog 20% of the time
var rand_no = Math.ceil(100*Math.random());

if(rand_no > 80){
var template_base = $("#template-directory").attr("href");
var dialog = $('#facebook-lightbox')
.html('<div class="facebook-module"><script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script><script type="text/javascript">FB.init("6d912ef1a774f6e709cfa18f077d77f7");</script><fb:fan profile_id="112130948829116" stream="" connections="10" width="300" height"250"></fb:fan></div><a href="http://www.facebook.com/pages/SaveBrite/112130948829116"><img align="left" src="'+template_base+'/images/iPad.gif" /></a>')
.dialog({
autoOpen: false,
height: 320,
width: 650,
modal: true,
show: 'blind',
hide: 'explode'
});

dialog.dialog('open');
}
*/

$('.submit-coupon').livequery('click', function() {
  submitHtml = $('#coupon-submit').html();
  var dialog = $('#coupon-submit')
  .load(submitHtml)
  .dialog({
    autoOpen: false,
    height: 720,
    width: 650,
    modal: true,
    show: 'blind',
    hide: 'explode'
  });

  dialog.dialog('open');
});


//$('.post-ratings img').click(function(){ // on success

/*$('.post-ratings').find('img:first').click(function(){ // on failure

var bitlyUrl = $(this).parent().parent().parent().find(".bitly-url").attr("href");
var tweetUrl = $(this).parent().parent().parent().find(".tweet-this").attr("href");
var facebookUrl = $(this).parent().parent().parent().find(".fb-share").attr("href");
var couponTitle = $(this).parent().parent().parent().find(".coupon-title").html();
var couponCat = $(this).parent().parent().parent().find(".post-cat img").attr("title");
var templateBase = $("#template-directory").attr("href");
//$("#share-html").html('<h2 style="text-align:right;float:left;width:70px;color:#999999;font-size:1em;font-weight:lighter;">Share This<br />Coupon:</h2><a id="show-link" rel="nofollow" href="#" onclick="javascript: pageTracker._trackPageview(\'/link/'+couponCat+'/lightbox\');" ><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_link.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/fbshare/'+couponCat+'/lightbox\');"rel="nofollow" target="_blank" href="'+facebookUrl+'"> <img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_facebook.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/tweet/'+couponCat+'/lightbox\');" href="'+tweetUrl+'"rel="nofollow" target="_blank" ><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_twitter.gif" /></a><a onclick="javascript: pageTracker._trackPageview(\'/emailshare/'+couponCat+'/lightbox\');" rel="nofollow" href="mailto:?subject='+couponTitle+' '+couponCat+' Coupon &body=Check out this coupon from SaveBrite.com '+bitlyUrl+'"><img align="right" width="96" class="share-block" src="'+templateBase+'/images/button_email.gif" /></a>');
$.fx.speeds._default = 1000;
$("#pop-email").html('');
$("#lightbox-headline").html('');

//lightboxExit = $('#test-html').html('<img align="right" style="margin:0 50px 10px 10px;" width="100" src="'+templateBase+'/images/ninja.png" /><h2>Want an even sweeter deal? Get an extra <strong>5% Cash-Back</strong> on you\'re purchase for each friend you refer to SaveBrite.com*</h2><br /><p>*In order to qualify for you\'re cash back, your friend must use a coupon on a purchase through SaveBrite.com. You can share this coupon with the following services:</p>');
lightboxExit = $('#test-html').html();

var dialog = $('#lightbox-exit')
.load(lightboxExit)
.dialog({
autoOpen: false,
height: 400,
width: 600,
modal: true,
show: 'blind',
hide: 'explode'
});

dialog.dialog('open');

$("#lightbox-exit .vote").html('');
});
*/


//Larger thumbnail preview

$(".shopping_offerimage").hover(function(){
  $(this).css({'z-index' : '100'});
  $(this).addClass("hover");
  $(this).find('img').addClass("hover").stop()
  .animate({
    marginTop: '-110px',
    marginLeft: '-110px',
    top: '50%',
    left: '50%',
    width: '200px',
    padding: '20px'
    }, 200);

  } , function() {
    $(this).css({'z-index' : '0'});
    $(this).removeClass("hover");
    $(this).find('img').removeClass("hover").stop()
    .animate({
      marginTop: '0',
      marginLeft: '0',
      top: '0',
      left: '0',
      width: '110px',
      padding: '5px'
      }, 400);
    });



    // load howto video into a lightbox
    $('#howto-trigger').livequery('click', function() {
      submitHtml = $('#howto-video').html();
      var dialog = $('#howto-video')
      .load(submitHtml)
      .dialog({
        autoOpen: false,
        height: 450,
        width: 675,
        modal: true,
        show: 'blind',
        hide: 'explode'
      });

      dialog.dialog('open');
    });


    // $('#rating_' + post_id).click(function(){
      // $(this).parent().parent().parent().hide();
      // });

      // initialize scrollable together with the circular plugin
      //$("#infinite").scrollable();


      // initialize vertical scrollable
      /*$("div.scrollable.vertical").scrollable({
      vertical:true,
      size: 3
      }).circular().autoscroll({autopause:true,autoplay: true,interval:3000});
      */
//if (!$('body').hasClass('.trial')) {

      // trigger hidden link that submits outbout url
      $('.click').livequery('click', function() {
        //$(this).nextAll("form:first").submit();

        if($('body').is('.products') && $(this).hasClass("cat")){
          window.blur(); // popup window on products pages
        }else if ($('body').is('.products') ) {
          var href = $(this).closest(".shopping_offer").find(".code").attr('href');
          window.open(href);
          //window.open(href,'NewWin','status=yes,scrollbars=yes');
          //window.blur(); // popup window on products pages
        }else{
          var href = $(this).closest(".coupon").find(".code").attr('href');
          window.open(href,'','status=yes,scrollbars=yes');
          //window.open(href,'NewWin','status=yes,scrollbars=yes');
          //window.focus(); // hide window on coupons
        }

      });
 //}

/* Old Popup Window
// trigger popup window to show coupon
$('.click').popupWindow({
  height:500,
  width:540,
  top:50,
  left:50,
  scrollbars:1
});
*/

// show popup window w/featured coupons
//if (!$('body').hasClass('.trial')) {
  $(".click").livequery(function(){
    $(this).popupwindow();
  });
//}


// show more sidebar categories
$('.show-more').livequery('click', function() {
   $(this).parent().find("ul.wp-tag-cloud").addClass('expanded');
   $(this).text('Show Less');
   $(this).addClass('show-less').removeClass('show-more');

  // show less sidebar categories
  $('.show-less').livequery('click', function() {
    $(this).parent().find("ul.wp-tag-cloud").removeClass('expanded');
    $(this).text('Show More');
    $(this).removeClass('show-less').addClass('show-more');
  });
});



    //sign in popup
    /* $(".signin").click(function(e) {
    e.preventDefault();
    $("fieldset#signin_menu").toggle();
    $(".signin").toggleClass("menu-open");
    });*/

    $("fieldset#signin_menu").livequery('mouseup', function() {
      return false
    });

    $(document).livequery('mouseup', function(e) {
      if($(e.target).parent("a.signin").length==0) {
        $(".signin").removeClass("menu-open");
        $("fieldset#signin_menu").hide();
      }
    });

    // tooltip
    $('.q_mark').tipsy({gravity: 's'}); // nw | n | ne | w | e | sw | s | se
    /*$('.tooltip').livequery('mouseover', function(){
    $(this).tipsy({gravity: 's'});
    });
    */

    //toggle
    $('#t2').livequery('click', function() {
      $("#query").removeClass("multi");
    });

    $('#t1').livequery('click', function() {
      $("#query").addClass("multi");
    });


    // Check the radio button associated with the label
    $('span.reason').click(function(e){
      $(this).prev('input:first').attr('checked',true);
    });

    // Bind function to the click events of the navigation links
    /* $('#menu li a').click(function(e) {
    // Prevent the browser from loading the page the user tried to visit
    e.preventDefault();

// Save the URL of where the user wanted to go
destination = $(this).attr('href');

// Show the exit survey
showExitSurvey();
});

*/

function PopIt() {
  showExitSurvey();
  window.onbeforeunload = UnPopIt;
  // Save the URL of where the user wanted to go
  return "Before you go...Would you mind taking a brief survey?";
}

function UnPopIt() { /* nothing to return */ }

//window.onbeforeunload = PopIt;

// prevent interal/external links from triggering exit poll
// $("a[id!=trigger]").click(function(){ window.onbeforeunload = UnPopIt; });
// $("form").submit(function(){ window.onbeforeunload = UnPopIt; });

function showExitSurvey()
{
  $('#survey').dialog({
    width: 500,
    bgiframe: true,
    modal: true,
    close: function() { $('#survey').dialog('destroy'); }, // Destroy the dialog. Allows it to appear again if closed.
    buttons: {"Continue" : log_answer } // Add a continue button that logs the answer when clicked
  });
}

function log_answer() {
  var templateBase = $("#template-directory").attr("href");
  //var destination = window.location.href;
  $.ajax({
    type : "GET",
    cache : false,
    url: ""+templateBase+"/survey-ajax",
    data: $("#survey-form").serialize(),
    dataType: "text",
    beforeSend: function() { $('#survey').dialog('destroy'); },
    complete: function() { /*window.location.replace(destination);*/ }
});
return;
}

function mail_rating(maildata) {
  var templateBase = $("#template-directory").attr("href");
  //var destination = window.location.href;
  $.ajax({
    type : "GET",
    cache : false,
    url: ""+templateBase+"/downvote",
    data: maildata,
    dataType: "text"
});
return;
}

// downvote send an email
$('.post-ratings').find('img:first').click(function(){
  maildata = new Object();
  maildata.id = $(this).parent().attr('id');
  mail_rating(maildata);
  return;
});


// upvote trigger popup
$('.post-ratings').find('img:second').click(function(){
  var dialog = $('#like-savebrite')
  .load()
  .dialog({
    autoOpen: false,
    height: 350,
    width: 645,
    modal: true,
    show: 'blind',
    hide: 'explode'
  });

  dialog.dialog('open');
});

// track email subscriptions
$('.subscribe2-subscribe').livequery('click', function() {
  pageTracker._trackPageview('/subscription/');
  $(".ui-dialog-titlebar-close").show();
});

// track cs subscriptions
$('#cs-submit').livequery('click', function() {
  pageTracker._trackPageview('/subscription/cs');
});

// track cs conversions
if($('div.center,div#content').hasClass('hide-buttons')){
  pageTracker._trackPageview('/subscription/cs/success');
};
  
// trigger dialog for cs clicks
/*$('#adsense_page .click').livequery('click', function() {
$('.coolsavings')
.dialog({
  autoOpen: true,
  height: 320,
  width: 495,
  modal: true,
  show: 'blind',
  hide: 'explode',
  open: function(event, ui)
     {
         $(this).find('#clicksource').val('cclick');
     },
  close:function() { $('.coolsavings').dialog('destroy'); }
  
  });
});
*/

// payment form processing
$('#payment-form input:radio').livequery('click', function() {
  
   var value = $(this).attr('value');
  
   $('#total_payment_ammount').val(value);
   $(".div_option").removeClass('selected');
   $(this).parent().addClass('selected');
   if($(this).attr('id') == 'onetime'){
     $('#item_name_1').val('One Time Plan');
     $('#trialpay_button').attr('href','http://www.trialpay.com/productpage/?c=ce2d0c6&tid=A7DefnW');
   }
   if($(this).attr('id') == 'monthly'){
     $('#item_name_1').val('Monthly Plan');
     $('#trialpay_button').attr('href','http://www.trialpay.com/productpage/?c=cb54e36&tid=RHyTqyu');
   }
   if($(this).attr('id') == 'annual'){
     $('#item_name_1').val('Annual Plan');
     $('#trialpay_button').attr('href','http://www.trialpay.com/productpage/?c=2567791&tid=6rGxYG1');
   }
   
});


if($('body').hasClass('trial')){
  
 $('.click,.activate').livequery('click', function(event) {
 
 // prevent link from firing

 lightboxPay = $('#payment').html();
 var couponCat = $(this).parent().parent().parent().find(".post-cat img").attr("title");
  
$("#payment .cat-title").html(couponCat); //update coupon cat in lightbox

var dialog2 = $('#payment')
.load(lightboxPay)
.dialog({
  autoOpen: false,
  height: 400,
  width: 600,
  modal: true,
  show: 'blind',
  hide: 'explode',
  open: function(event, ui)
     {
          $(this).parent().children().children('.ui-dialog-titlebar-close').hide();
     }
});

dialog2.dialog('open');


event.preventDefault();

}); // endclick

} // endif

$('#savebar-closebox').livequery('click', function() {
  $(this).parent().hide();
});

$('#page-savebar .button').click(function(e){
  if($("#eula").is(':checked')) {
    return true;
  }else{
    e.preventDefault();
    alert('Please agree to the Terms and Conditions below');
  }
});

});

