$(document).ready(function($) {
  // Sets up the facebox (lightview)
  $('a[rel*=facebox]').facebox({
    loading_image : 'facebox/loading.gif',
    close_image   : 'facebox/closelabel.gif'
  });
  $.facebox.settings.opacity = 0.5;
  
});
$(document).ready(function() {
  // Set Hidden Divs, that display if JS is not enabled
  $('#random_broadcast_count').hide();
  $('#campaign_preview').hide();
});
// Flash Notice Fade
$(document).ready(function() {
  //Flash Notice FX
  $('.flash_message').animate({opacity: 1.0}, 5000).fadeOut(1000);
});

// jQuery Tabs
// $(document).ready(function() {
//   var tabContainers = $('div#tabs_content > div');
//   tabContainers.hide().filter(':first').show();
// 
//   $('div#campaign_detail_tabs ul#campaign_detail_nav a').click(function() {
//     tabContainers.hide();
//     tabContainers.filter(this.hash).show();
//     $('div#campaign_detail_tabs ul#campaign_detail_nav li').removeClass('selected');
//     $(this).parent().addClass('selected');
//     return false;
//   }).filter(':first').click();  
// });

// Disengage the Advertiser member table when send to all is checked
$(document).ready(function() {
  var sendAll = $('#sendBroadcastToAllCheckbox');
  sendAll.click(function() {
    if ($(this).is(':checked')) {
     $('#send_advertisements_table_wrap table').css({opacity : .5});
     $('.yui-dt-paginator').hide();
     $('#send_advertisements_table_wrap :checkbox').each(function() {
       $(this).hide();
     });
    } else {
     $('#send_advertisements_table_wrap table').css({opacity : 1});
     $('.yui-dt-paginator').show();
     $('#send_advertisements_table_wrap :checkbox').each(function() {
       $(this).show();
     });
    }
  });
});


// // These are the member broadcast selector buttons
// $(document).ready(function () {
//   var all = $('#selectAll');
//   var clear = $('#clearAll');
//   var random = $('#randomSelect');
//   var elCheck = $('#send_advertisements_table_wrap :checkbox');
//   
//   all.click(function() {
//   var elCheck = $('#send_advertisements_table_wrap :checkbox');
//     elCheck.each(function() {
//       $(this).attr('checked', 'checked');
//     })
//      return false;
//   });
//   
//   clear.click(function() {
//   var elCheck = $('#send_advertisements_table_wrap :checkbox');
//     elCheck.each(function() {
//       $(this).attr('checked', '');
//     })
//      return false;
//   });
// });


// This is the Printer Friendly Link style switcher (Advertiser)
$(document).ready(function() {
  var printstyles = $("link#print_styles[rel=stylesheet]");
  var regstyles = $("link#regular_styles[rel=stylesheet]");
  $("#print").click(function() {
      regstyles = $("link#regular_styles[rel=stylesheet]").attr({href : "/public/css/print.css"});
      return false;
  });
  $("#noprint").click(function() {
      printstyles = $("link#regular_styles[rel=stylesheet]").attr({href : "/public/css/advertiser_core.css"});
      return false;
  });
});
// This is the Printer Friendly Link style switcher (Reseller)
$(document).ready(function() {
  var resellerprintstyles = $("link#print_styles[rel=stylesheet]");
  var resellerstyles = $("link#reseller_styles[rel=stylesheet]");
  $("#resellerprint").click(function() {
      resellerstyles = $("link#reseller_styles[rel=stylesheet]").attr({href : "/public/css/print.css"});
      //resellerstyles = $("link#reseller_styles[rel=stylesheet]").attr({href : "../css/print.css"});
      return false;
  });
  $("#noresellerprint").click(function() {
      resellerprintstyles = $("link#reseller_styles[rel=stylesheet]").attr({href : "/public/css/reseller_core.css"});
      return false;
  });
});
// Import Details hover 
$(document).ready(function() {
  $('.import_title a').click(function() {
    $('#import_details').toggle('slow');
    return false;
  });
});

//This is the script to set the color of the nav current page link (Public)
$(document).ready(function() {
  var url = location.pathname.substring(1); 
  var current_link = $('.sub_nav ul li a[@href$="' + url + '"]');
  
  if (url == "") {
    current_link.parent().removeClass('active');
    $('.home').addClass('active');
  } else {
  current_link.parent().addClass('active');   
  }
});
//This is the script to set the color of the nav current page link (Advertiser)
$(document).ready(function() {
  var url = location.pathname.substring(1);
  var host = jQuery.url.segment(0);
  var root = jQuery.url.segment(1);
  var controller = jQuery.url.segment(2);
  var action = jQuery.url.segment(3);
  var current_link = $('#main_nav ul li a[@href$="' + url + '"]');
  
  if (url == "") {
    current_link.parent().removeClass('active');
    $('.home').addClass('active');
  } else {
  current_link.parent().addClass('active');
  }
});

//This is the fx for the public panels to slide in the request for account box 
$(document).ready(function() {
    $("li.request_link a").click(function () {
       if (this.text == "Request Info") {
        $('div.terms').animate({width: '632px', opacity: .5}, 800);
        $(this).text("Back");
        if ($('div.signup_block').hasClass('image_swap')) {
          $('.image_swap').toggle(500);
          $('.signup_swap').toggle(1000);
        } else {
          $('.signup_block').toggle(1000);
        }
        
      } else {
        $('div.terms').animate({width: '939px', opacity: 1}, 1000);
        $(this).text("Request Info");
        $('.signup_block').toggle(800); 
      }
    });
  });
 
//This is the script for data table color bgs to change based on time. 
$(document).ready(function() {
  var $lastActivityDataCell = $('td.yui-dt0-col-LastActivity div');
  $lastActivityDataCell.each(function() {
    var dateparts = $(this).text();
    var d = new Date();
    var today = d.getTime();
    var day = (24*60*60*1000);
    var twoWeeksAgo = (day * 14);
    var oneMonthAgo = (day * 31);
    var divdate = new Date(dateparts);
    var lastActivity = (divdate.getTime());
    var twoWeeksAgo = (today - twoWeeksAgo);
    var oneMonthAgo = (today - oneMonthAgo);
    
    if (lastActivity < twoWeeksAgo && lastActivity > oneMonthAgo) {
            $(this).addClass('data_table_warning');
          } else if (lastActivity < oneMonthAgo) {
            $(this).addClass('data_table_extreme_warning');
          }
  });
  
  var $lowConvDataCell = $('td.yui-dt0-col-LowConv div');
  $lowConvDataCell.each(function() {
    var lowConvRate = $(this).text();
    var lowConvRateValue = parseFloat(lowConvRate);
    var lowThreshold = (0.20);
    
    if (lowConvRateValue <= lowThreshold) {
      $(this).addClass('data_table_low_threshold');
    }
  });
  
  var $highConvDataCell = $('td.yui-dt0-col-HighConv div');
  $highConvDataCell.each(function() {
    var highConvRate = $(this).text();
    var highConvRateValue = parseFloat(highConvRate);
    var highThreshold = (0.65);
    
    if (highConvRateValue >= highThreshold) {
      $(this).addClass('data_table_high_threshold');
    }
  });
  
});
// This is the Update Campaign (newscast) char counter
$(document).ready(function() { 
  updateButton = $('#updateButton');
  campaignTabs = $('#campaign_detail_tabs ul li');
  newsCastDiv = $('#newsCast');
  boilerPlateKeyword = $('.newsBoiler');
  availableChars = 140;
  kwrd = $('#campaign_detail_keyword').text();
  boilerPlateKeyword.text(kwrd);
  charCount = $('.char_count');
  boilerPlateText = $('#boiler_plate').text();
  boilerPlateTextLength = boilerPlateText.length;
  previewContentWindow = $('#create_campaign_deliverable_content_preview_content');
  previewContentWindowText = previewContentWindow.text();
  previewContentWindowTextLength = previewContentWindowText.length;

  charCount.text(availableChars - (boilerPlateTextLength + previewContentWindowTextLength));
  
  newsCastDiv.bind('keyup', function() {
    newsCastValue = $(this).val();
    breakCount = $('.newscastChar');
    previewContentWindow.text(newsCastValue);
    previewContentWindowTextLength = newsCastValue.length;
    keywordText = $('#boiler_plate_keyword').text().replace(/ /g,'');
    boilerCount = $.trim(keywordText).length;
    boilerPlateTextLength = boilerPlateText.length;
    charCount.text(availableChars - (boilerCount + previewContentWindowTextLength) - 22);
    count = parseFloat(breakCount.text());
  }); 
  // Update Campaign action 
  updateButton.bind('click', function() {
    if (count < 0 ) {
      jAlert('You are attempting to add too many characters to your campaign. If you send your message with too many characters, it will not be delivered.', 'Exceeded Character Count');
      return false
    } else {
      jConfirm('Are you sure you want to run this Campaign?', 'Campaign Confirmation', function(r) {
        if (r == true) {
          //jAlert('Your Broadcast has been scheduled.', title+' Broadcast Confirmed');
          if ($("#campaignDetails").valid()) {
            $("form#campaignDetails").submit();
          } else {
            return false;
          }
        } else {
          return false;
        }
      });
    }
  });
  
  
  campaignTabs.bind('click', function() {
    if ($(this).hasClass('details')) {
    $('#campaign_preview').show();
  } else {
   $('#campaign_preview').hide(); 
  }
  });
});

//This is the script for the create campaign preview panes
$(document).ready(function() {
  
  keywordActivator = $('#keywordAssignSelect');
  keyword = $('#campaign_detail_keyword').text();
  if (keywordActivator.val()) {
    keywordActivatorDiv = $('#create_campaign_keyword_activator_preview');
    boilerPlateKeyword = $('#boiler_plate_keyword');
    keywordActivatorDiv.text(keywordActivator.val().toUpperCase());
    boilerPlateKeyword.text(keywordActivator.val().toUpperCase());
    offerText = $('#offer');
    offerTextValue = offerText.val();
    availableChars = 140;
    charCount = $('.char_count');
    boilerPlateText = $('#boiler_plate').text();
    boilerPlateTextLength = boilerPlateText.length;
    previewContentWindow = $('#create_campaign_deliverable_content_preview_content');
    previewContentWindowText = previewContentWindow.text();
    previewContentWindowTextLength = previewContentWindowText.length;
  
    charCount.text(availableChars - (boilerPlateTextLength + previewContentWindowTextLength));
  
    // Changing the Keyword Select Actions
    keywordActivator.bind('change', function() {
      var keywordActivatorCurrent = $('#keywordAssignSelect :selected');
      var keywordActivatorValue = keywordActivatorCurrent.val();
      var keywordActivatorDiv = $('#create_campaign_keyword_activator_preview');
      keywordActivatorDiv.text(keywordActivatorValue.toUpperCase());
      boilerPlateKeyword.text(keywordActivator.val().toUpperCase());
      var boilerPlateText = $('#boiler_plate').text();
    
      var boilerPlateTextLength = boilerPlateText.length;
      var previewContentWindow = $('#create_campaign_deliverable_content_preview_content');
      var previewContentWindowText = previewContentWindow.text();
      var previewContentWindowTextLength = previewContentWindowText.length;
      charCount.text(availableChars - (boilerPlateTextLength + previewContentWindowTextLength));
    });
  
    // Updating the preview with the input values
    offerText.bind('keyup', function() {
      offerTextValue = $(this).val();
      previewContentWindow.text(offerTextValue);
      previewContentWindowTextLength = offerTextValue.length;
      keywordText = $('#boiler_plate_keyword').text().replace(/ /g,'');
      boilerCount = $.trim(keywordText).length;
      boilerPlateTextLength = boilerPlateText.length;
      charCount.text(availableChars - (boilerCount + previewContentWindowTextLength) - 22);
    });
  
    newsCastDiv = $('#newsCast');
    createButton = $('#create');
    
    newsCastDiv.bind('keyup', function() {
      newsCastValue = $(this).val();
      breakCount = $('.createNews');
      previewContentWindow.text(newsCastValue);
      previewContentWindowTextLength = newsCastValue.length;
      keywordText = $('#boiler_plate_keyword').text().replace(/ /g,'');
      boilerCount = $.trim(keywordText).length;
      boilerPlateTextLength = boilerPlateText.length;
      charCount.text(availableChars - (boilerCount + previewContentWindowTextLength) - 22);
      count = parseFloat(breakCount.text());
    });
    
    // Create Campaign action 
    createButton.bind('click', function() {
      if (count < 0 ) {
        jAlert('You are attempting to add too many characters to your message. If you send your message with too many characters, it will not be delivered.', 'Exceeded Character Count');
        return false;
      } else {
        jConfirm('Are you sure you want to create this campaign?', 'Campaign Create Confirmation', function(r) {
          if (r == true) {
            if ($("#create_campaign_form").valid()) {
              $("form#create_campaign_form").submit();
            } else {
              return false;
            }
          } else {
            return false;
          }
        });
      }
    });
    
  } else {
    // This is the Send Broadcast 
    broadcastSubmit = $('#broadcastSubmitButton');
    broadCastMessageDiv = $('#broadcastMessage');
    availableChars = 140;
    charCount = $('.char_count');
    boilerPlateKeyword = $('#boiler_plate_keyword');
    boilerPlateKeyword.text(keyword.toUpperCase());
    
    broadCastMessageDiv.bind('keyup', function() {
      // breakCount = $('.broadcastChar');
      // broadCastDiv = $(this).val();
      previewContentWindowTextLength = $(this).val().length;
      // boilerPlateText = $('#boiler_plate').text().replace(/ /g,'');
      keywordText = $('#boiler_plate_keyword').text().replace(/ /g,'');
      boilerCount = $.trim(keywordText).length;
      // boilerPlateTextLength = boilerPlateText.length;
      charCount.text(availableChars - (boilerCount + previewContentWindowTextLength) - 22);
      count = parseFloat(charCount.text());
    });
    termsCheckbox = $('#broadcastTermsCheckbox');
    // Send Broadcast action 
    broadcastSubmit.bind('click', function() {
      if (count < 0 ) {
        jAlert('You are attempting to add too many characters to your message. If you send your message with too many characters, it will not be delivered.', 'Exceeded Character Count');
        return false;
      } else {
        if ($('#broadcastTermsCheckbox').is(':checked')) {
          var title = $('#broadcastTitle').text();
          jConfirm('Are you sure you want to run this broadcast?', title +' Broadcast Confirmation', function(r) {
            if (r == true) {
              //jAlert('Your Broadcast has been scheduled.', title+' Broadcast Confirmed');
              if ($("#campaignBroadcast").valid()) {
                $("form#campaignBroadcast").submit();
              } else {
                return false;
              }
            } else {
              return false;
            }
          });
        } else {
          jAlert('You must agree to the terms.', 'Terms Agreement');
          return false;
        }
      }
    });
  }
});

// This is the script for the (Reseller) Create Advertiser Form
$(document).ready(function() {
  $('#reseller_create_advertiser_campaign').hide();
});







