var adfonic_isIE6 = false;

function bindHighlight() {
  jQuery('.first,.second').live('mouseover', function(){
    jQuery(this).css('background-color','#ffffc9');
  }).live('mouseout', function(){
    jQuery(this).css('background-color','');
  });
}

function showAlertDiv(selector,divSelector) {
    var alertOpts = {
    className: "alert",
    cornerRadius: 0,
    orient: "above",
    width: 280,
    arrowHeight: 0,
    arrowInset: 0,
    nudgeHorizontal:-120,
    nudgeVertical:-13,
    region:'.adHome',
    content: divSelector
    }
    jQuery(selector).callout(alertOpts);
}

function showAlert(selector,header,text) {
   var str = '<table cellpadding="0" cellspacing="0" border="0">'
       + '<tr><td class="popupDiv1">' + header + '</td></tr>'
    + '<tr><td class="popupDiv2">' + text
    + '</td></tr>'
    + '<tr><td class="popupDiv3"></td></tr></table>';

    var alertOpts = {
    className: "alert",
    cornerRadius: 0,
    orient: "above",
    width: 280,
    arrowHeight: 0,
    arrowInset: 0,
    nudgeHorizontal:-120,
    nudgeVertical:-13,
    region:'.adHome',
    text: str
    }
    jQuery(selector).callout(alertOpts);
}

function bindCallout(selector,suffix,x,y,orientation) {
    if (!suffix) {
      suffix = '';
      x = -8;
      y = -3;
      orientation = "above";
    }

    var tooltipDefaults = {
    className: "tooltip",
    cornerRadius: 0,
    orient: orientation,
    width: 280,
    arrowHeight: 0,
    arrowInset: 0,
    nudgeHorizontal:x,
    nudgeVertical:y,
    region:'.adHome'
    }
  jQuery(selector).hover(function(){
    var header = jQuery('#'+this.id + '>.calloutHeader').text();
    var text = jQuery('#'+this.id + '>.calloutText').html();

    var str = '<table cellpadding="0" cellspacing="0" border="0">'
       + '<tr><td class="callout' + suffix + 'Div1">' + header + '</td></tr>'
    + '<tr><td class="calloutDiv2">' + text + '</td></tr>'
    + '<tr><td class="callout' + suffix + 'Div3"></td></tr></table>';
    jQuery(this).callout(jQuery.extend({}, tooltipDefaults, {
      text: str,
      align: "left"
    }));
    if (adfonic_isIE6) { jQuery('.tooltip').each(function() { ie6SelectHide(this, true); }); }
  }, function(){
    if (adfonic_isIE6) { jQuery('.tooltip').each(function() { ie6SelectHide(this, false); }); }
    jQuery(this).closeCallout();
  });
}

jQuery(document).ready(function(){
    bindCallout('.callout', '',-8,-3,"above");
    bindCallout('.calloutFlip', 'Flip',-246,6,"below");
  bindHighlight();
});

function parseQuery ( query ) {
   var Params = new Object ();
   if ( ! query ) return Params;
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) continue;
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

// This function doesn't work if checkize is enabled.
function selectAll(id, value) {
  jQuery("input[id$='" + id + "']").attr("checked", value);
}

// Invokes the button with a (local) id matching 'action'.
function doAction(obj, action) {
  jQuery(obj.form).prepend('<input type="hidden" name="' + obj.form.id + ':' + action + '" value="" />');
  obj.form.submit();
}

// This has to go outside the function
var jsfAjaxNativeSubmit = new Object();
var jsfAjaxNativeSubmitAny = null;

function jsfAjaxInit(divId, url) {
  jQuery('#' + divId + ' form').each(function(i) {
    if (this.submit) {
        jsfAjaxNativeSubmit[this.id] = this.submit;
        jsfAjaxNativeSubmitAny = this.submit;
    } else {
    // Work around a strange issue in Chrome where this.submit is undefined on some forms, but not all... not sure why?
    jsfAjaxNativeSubmit[this.id] = jsfAjaxNativeSubmitAny;
    }
    this.submit = function() {
      if (jQuery('#' + divId + ' form input[type="hidden"][name$="_NO_AJAX"]').length > 0) {
      this.submit = jsfAjaxNativeSubmit[this.id];
      this.submit();
        return true;
      }

      jQuery('#' + divId).css('position','relative').append('<div class="loading"></div>');

      if (this.method == 'post') {
      jQuery('#' + divId).load(url + '?__ts=' + new Date().getTime(), jQuery(this).serializeArray(), function(data, textStatus) {
          jsfAjaxInit(divId, url);
        }); // load
      } else {
      jQuery.get(url + '?' + jQuery(this).serialize() + '&__ts=' + new Date().getTime(), null, function(data, textStatus) {
          jQuery('#' + divId).html(data);
          jsfAjaxInit(divId, url);
        }); // get
      }
      return false;
    }; // function
    jQuery(this).submit(this.submit);
  }); // each form
}

function jsfAjaxLoad(divId, url) {
    var useUrl = url + '?__ts=' + new Date().getTime();
    jQuery('#' + divId).load(useUrl, null, function() {
      jsfAjaxInit(divId, url);
    });
}

function jsfAjaxImageOver(idSuffix, url) {
    SI_MM_swapImage(jQuery('[id$="' + idSuffix + '"]')[0].id, '', url, 1);
}
function jsfAjaxImageOut() {
    SI_MM_swapImgRestore();
}

var countdown = {
    init: function() {
    countdown.remaining = countdown.max - jQuery(countdown.obj).val().length;
    if (countdown.remaining > countdown.max) {
        jQuery(countdown.obj).val(jQuery(countdown.obj).val().substring(0,countdown.max));
    }
    jQuery(countdown.obj).closest(".remainingBlock").find(".remaining").html(countdown.remaining + " characters remaining");
    if (countdown.remaining != countdown.max) {
        jQuery('#previewText').html('<a href="#">' + jQuery(countdown.obj).val() + '</a>');
        jQuery('#previewTextWithAppIcon').html('<a href="#">' + jQuery(countdown.obj).val() + '</a>');
    } else {
        jQuery('#previewText').text('');
        jQuery('#previewTextWithAppIcon').text('');
    }
    },
    max: null,
    remaining: null,
    obj: null
};

jQuery(function() {
jQuery(".countdown").each(function() {
        jQuery(this).focus(function() {
        var c = jQuery(this).attr("class");
        countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
        countdown.obj = this;
        iCount = setInterval(countdown.init,200);
        }).blur(function() {
            countdown.init();
            clearInterval(iCount);
        });
    // Set initial value
    var c = jQuery(this).attr("class");
    countdown.max = parseInt(c.match(/limit_[0-9]{1,}_/)[0].match(/[0-9]{1,}/)[0]);
    countdown.obj = this;
    countdown.init();
    });//each
});//jQuery

