var resourcePath;

function sizeOverlay () {
   $('#overlay').each(function () {
      var popinWidth, popinHeight, popinTop, popinLeft;
      popinWidth = 600;

      $('#overlay')
         // reset width/height so document size is window size
         .css('width', 'auto').css('height', 'auto')
         // set overlay width/height to window
         .width($(document).width()).height($(document).height())
         // get the message element and position
         .parent('body').find('.overlayMessage')
         .width(popinWidth)
         .css('top', '50px').css('left', (($(document).width() - popinWidth)/2) + 'px');
   });
}
function iResize(iFrame)
{
   // add document.domain = "videoecards.com" to /1 popups on vectk sites
   //alert($(iFrame).contents().find('html').height());
   $('#loading').remove();
   $(iFrame).show().height($(iFrame).contents().find('html').height());
   sizeOverlay();
}
function enableEl(el, enabled)
{
   if (enabled)
   {
      el.removeAttr("disabled").removeClass("disabled");
   }
   else
   {
      el.attr("disabled", true).addClass("disabled");
   }
}
function setError(el, message)
{
   //alert(el.attr('id') + ' ' + message);
   if (message === undefined || message === '')
   { // no error
      el.parent('li').removeClass('error').end().find('+ label.error').remove();
   }
   else
   { // show error
      var errorLabel = el.find('+ label.error').length > 0 ? el.find('+ label.error') : $('<label class="error" for="' + el.attr('id') + '"></label>').insertAfter(el);
      errorLabel.text(message);
      el.parent('li').addClass('error');
   }
}
function validateRequired(el, setMessage, valid)
{
   valid = el.val().match(/^.+$/i) !== null && (valid === undefined || valid);
   if (setMessage === undefined || setMessage)
   {
      var imgSrc = 'http://qa-resources.videoecards.com/images/' + (valid ? 'accept.png' : 'asterisk_red.png');
      el.parent('li.required').find('> label img.required').attr('src', imgSrc);
   }
   return valid;
}
function validateEmail(el, setMessage, valid)
{
   var valid = el.val().match(/^[A-Z0-9._%+\-]+@[A-Z0-9.\-]+\.[A-Z]{2,4}$/i) !== null && (valid === undefined || valid)
   if (setMessage === undefined || setMessage)
   {
      setError(el, ((el.val().length === 0 || valid) ? '' : 'A valid email address is require'));
      validateRequired(el, true, valid);
   }
   return valid;
}
$(function () {
   resourcePath = 'http://' + window.location.hostname.replace('www', 'resources').replace(/(local|qa)/, 'qa-resources');
   /* jquery bug fix 1.2.6 */
   jQuery.browser.version = jQuery.browser.msie && /msie 7\.0/i.test(navigator.userAgent) ? "7.0" : jQuery.browser.version;

   //$('ul#sites ul li a.external').addClass('popin').removeClass('external');
   $('#body, .sidebar').corner('top');
$('.shadow').corner('tl 6px').each( function () {
      if(!($.browser.msie && $.browser.version < 7.0))
      {
         $(this).wrap('<div class="shadowpair1"></div>')
            .parent('div.shadowpair1')
               .css('margin-top', $(this).css('margin-top').replace('px','')/$(this).css('font-size').replace('px','') + 'em')
               .css('margin-right', $(this).css('margin-right').replace('px','')/$(this).css('font-size').replace('px','') + 'em')
               .css('margin-bottom', $(this).css('margin-bottom').replace('px','')/$(this).css('font-size').replace('px','') + 'em')
               .css('margin-left', $(this).css('margin-left').replace('px','')/$(this).css('font-size').replace('px','') + 'em')
            .end().css('margin', '0').wrap('<div class="shadowpair2"><div class="shadowbox"></div></div>');
      }
   });
   // todo resize overlay on screen size change
  	$(window).resize( function() {
  	   sizeOverlay();
   });

   $('a.popin').click(function () {
      $('body').prepend('<div id="overlay"></div><div class="overlayMessage"><img id="loading" alt="loading" src="http://qa-resources.videoecards.com/images/loading.gif" width="104" height="68" /></div>');
      $('<iframe src ="' + this.href + '" scrolling="no" frameborder="0"></iframe>').load(function () {
         setTimeout(iResize(this), 0);
      }).appendTo('.overlayMessage').hide();
      sizeOverlay();
      return false;
   });

    $('a.external').click(function () {
        window.open(this.href);
        return false;
    });
    $('a.external > img').each( function () {
       $(this).parent('a').addClass('noIcon');
    });
    $("div.button a, div.button input:not(.disabled), input.button:not(.disabled)").hover(
        function () {
            $(this).toggleClass("hover").parent('span').toggleClass("hover");
        },
        function () {
            $(this).toggleClass("hover").parent('span').toggleClass("hover");
        }
    );
    $('a.close').click(function () {
        window.close();
    });
   $('body.popup a.popback').click(function () {
      if (window.opener !== null)
      {
         window.opener.location = this.href;
         self.close();
      }
   });
    $('div.thumbnail .popup').click(function () {
        var features = 'menubar=no,location=no,toolbar=no,scrollbars=no,width=710,height=500';
        window.open(this.href + '/1', 'popup', features);
        return false;
    });
   $('fieldset li > input:not(:submit, :radio, :checkbox), fieldset li > textarea').focus(function () {
      $(this).parent('li').addClass('focus');
   });
   $('fieldset li > input:not(:submit, :radio, :checkbox), fieldset li > textarea').blur(function () {
      $(this).parent('li').removeClass('focus');
   });
   $('fieldset li > input').filter(':radio, :checkbox').focus(function () {
      $(this).parent('li').parent('ol').parent('fieldset').parent('li').addClass('focus');
   });
   $('fieldset li > input').filter(':radio, :checkbox').blur(function () {
      $(this).parent('li').parent('ol').parent('fieldset').parent('li').removeClass('focus');
   });
    $('.newWindow').click(function () {
        window.open(this.href, 'newWindow');
        return false;
    });
    $('.vcard').each(function () {
        var hCardId = $(this).attr('id') === null ? '%hcard' : $(this).attr('id');
        $(this).after('<p><a href="http://feeds.technorati.com/contacts/' + window.location + '/%23' + hCardId + '">Add to Address Book</a></p>');
    });
   $('li.required').find('> label:not(.error), > fieldset legend span').append('<img alt="required" class="required" src="http://qa-resources.videoecards.com/images/asterisk_red.png" />');
   $('#formKeepMeInformed').each(function () {
      $(this).find('input[type="submit"]').attr("disabled", true).addClass("disabled").click(function () {
         var rpc_url = $('#formKeepMeInformed #rpc_url').val() + '?email_address=' + $('#formKeepMeInformed #email_address').val();
         $.getJSON(rpc_url,
            function (data) {
               if (data.status === 1)
               {
                  $('#formKeepMeInformed #email_address').val('');
                  $('<p></p>').text(data.message).insertAfter($('#formKeepMeInformed ol'));
               }
               else // error
               {
                  $('#formKeepMeInformed #email_address').after('<label class="error" for="email_address">' + data.message + '</label>');
               }
            } // end funtion
         ); // end getJSON
         return false;
      });
      $(this).find('#email_address').bind('keyup mouseup change', function() {
         enableEl($('#formKeepMeInformed input[type="submit"]'), validateEmail($(this)));
      });
   });
   // hack; add back to help
   $('#contentPrim > ul.jumpLinks li.last').append('&nbsp;<a id="allHelp" href="/help-with-video-' + window.location.hostname.match(/e-?cards/i) +'">&lt; Back to All Help Topics</a>');
   //Add This
      addthis_options = 'email, favorites, facebook, twitter, myspace, digg, stumbleupon, delicious, google, more';
   $('.navGlobal li#linkAddThis a')
      .mouseover(function () {
         return addthis_open(this, '', addthisUrl, addthisTitle);
      })
      .mouseout(function () {
         addthis_close
      })
      .click(function () {
         return addthis_sendto();
      });

});
