function launchModal(title,text,w,h,dclass) {
  w = (w) ? w : 400;
  h = (h) ? h : 'auto';
  dclass = (dclass) ? dclass : 'modal';

  $('#modalWin').dialog({position: ['top',50], dialogClass: dclass, closeText: 'x Close', autoOpen: false, modal: true, draggable: false, resizable: false, close: function(ev, ui) { $(this).dialog('destroy'); }});
  $('#modalWin').dialog('option',{'title': title, width: w, height: h});   
  $('#modalWin').html(text).dialog('open');
}

function closeModal() {
  $('#modalWin').dialog('close');
}

function modalError(text) {
  $('#modalWin').dialog({position: ['top',100], dialogClass: 'modal_error', closeText: 'x Close', autoOpen: false, modal: false, draggable: false, resizable: false, close: function(ev, ui) { $(this).dialog('destroy'); }});
  $('#modalWin').dialog('option',{'title': 'Error', width: 300, height: 134});   
  $('#modalWin').html('<div>' + text + '</div><input type="submit" value="CLOSE" onclick="closeModal(); return false;" />').dialog('open');
}

function processCart(data,modalanim) {
  if (typeof data.error != "undefined") {
    $('#errormsg').html(data.error).slideDown('fast');
    $(':input','#purchase').removeClass('errormsg');
    $('span.ERRORMSG','#purchase').remove();

    $.each(data.fields,function(k,v){
      $(':input[name=' + k + ']','#purchase').addClass('errormsg').after(v);
    });
  } else {
    if ($('#errormsg').is(":visible") == true) {    
      $('#errormsg').slideUp('fast');
      $(':input','#purchase').removeClass('errormsg');
      $('span.ERRORMSG','#purchase').remove();
    }

    if (modalanim) {
      window.parent.$('#header_units').text(data.units);
      window.parent.$('#header_total').text(data.total);

      var value = data.cart + "<script>function closeModal() { $('*').dialog('close'); }</script>";
      var mleft = ((($(window.parent).width() - 402) / 2) - window.parent.$('.modalWindow').offset().left);

      window.parent.$('#ui-dialog-title-modalWindow').text('Shopping Cart');
      window.parent.$('.modalWindow').html(value).animate({height:'440px'}).parent().animate({width:'400px',left:'+=' + mleft + 'px'});
    } else {
      $('#header_units').text(data.units);
      $('#header_total').text(data.total);

      launchModal('Shopping Cart',data.cart,400);
    }
  }
}

function modal(title,id,url,w,h,dclass) { 
  if (id.substring(0,1) == '.') {
    id = id;
  } else {
    id = '#' + id;
  }

  $(id).click(function() { 
    launchModalFrame(title,url,w,h,dclass);
    return false;
  }).addClass('pointer'); 
} 

function launchModalFrame(title,url,w,h,dclass) {
  w = (w) ? w : 400;
  h = (h) ? h : 'auto';
  dclass = (dclass) ? dclass : 'modal';

  var ts = new Date().getTime();
  window.parent.$('#webpage').after('<div id="modalWindow_'+ts+'" class="modalWindow"></div>');
  window.parent.$('#modalWindow_'+ts).dialog({position: ['top',40], dialogClass: dclass, closeText: 'x Close', stack: true, autoOpen: false, modal: true, draggable: false, resizable: false, close: function(){$(this).parent().remove(); $(this).remove();} });
  window.parent.$('#modalWindow_'+ts).dialog('option',{'title': title, width: w, height: h});   
  window.parent.$('#modalWindow_'+ts).html('<iframe id="modalWindow_'+ts+'_if" class="modalWindow_if" width="100%" height="100%" frameborder=0 scrolling="auto"></iframe>').dialog("open");
  window.parent.$('#modalWindow_'+ts+'_if').attr('src',url);
}

function modal_close() { window.parent.$('*').dialog('close'); } 

function modalAutoSize() {
  var www = $(window.parent).width() - 100;
  var mdw = window.parent.$('.ui-dialog:last').outerWidth();
  var mcw = $('#modal_content div:eq(0)').outerWidth();

  var wwh = $(window.parent).height() - 80;
  var mwh = window.parent.$('.modalWindow:last').height();
  var mch = $('#modal_content').height();

  if (mcw) {
    if (mcw > www) {
      var nw = www;
    } else {
      var nw = mcw;
    }
  }

  if (mch > wwh) {
    var nh = wwh;
    var scroll = 1;
    if (nw) { nw = nw + 20; }
  } else {
    var nh = mch;
    var scroll = 0;
  }

  if (scroll) {
    window.parent.$('.modalWindow_if:last').attr('scrolling','yes').css('overflow-y','scroll').data('scroll',1);
    $('html, body').css('overflow','visible');
  } else {
    window.parent.$('.modalWindow_if:last').attr('scrolling','no').css('overflow-x','hidden').css('overflow-y','hidden').data('scroll',0);
    $('html, body').css('overflow','hidden');
  }

  if (nw) {
    if (nw > mdw) {
      window.parent.$('.modalWindow:last').parent().animate({'width':'+=' + (nw-mdw),'left':'-=' + ((nw-mdw)/2)},'fast');
    } else {
      window.parent.$('.modalWindow:last').parent().animate({'width':'-=' + (mdw-nw),'left':'+=' + ((mdw-nw)/2)},'fast');
    }
  }

  if (nh > mwh) {
    window.parent.$('.modalWindow:last').animate({'height':'+=' + (nh-mwh)},'fast');
  } else {
    window.parent.$('.modalWindow:last').animate({'height':'-=' + (mwh-nh)},'fast');
  }
}

$(document).ready(function() {
  $('#messagebox').delay(10000).slideUp('slow');
  $('#jsnotice').hide();
  if ($('#q').is('*')) {
    var ac_cache = {}; 
    $('#q').autocomplete({source: function(request,response) { var ac_term=request.term.toLowerCase().replace(/[^0-9a-z]+/g,''); if (ac_cache[ac_term]) { response(ac_cache[ac_term]); return; } $.getJSON('/js/ac.html',{term: encodeURI(request.term)},function(data){ if (data && data.length > 0) { ac_cache[ac_term]=data; response(data); }});}, delay: 300, minLength: 3, open: function(event, ui) { $('.ui-autocomplete').css('z-index',9999); }, focus: function() { return false; }, select: function(event, ui) { window.location=ui.item.value; return false; }}); 
    $('#qform').submit(function() { window.location='/search/' + encodeURI($('#q').val()); return false; });
  }

  $('li ul:last-child','#navigation').css({'background':'none','margin-right':'0'});
  $('li ul:last-child a','#navigation').css({'width':'176px','margin-right':'0'});

  $('li div','#navigation').show().hide().parent().hoverIntent(function(){
    $('div',this).css('z-index','9999');
    $('div',this).stop(true,true).slideDown('fast');   
  },function(){
    $('div',this).slideUp('fast');   
  });
});

function characterLimit(id,limit,infodiv) {
  limitChars(id,limit,infodiv);
  $('#' + id).keyup(function(){
      limitChars(id,limit,infodiv);
  });
}

function limitChars(id,limit,infodiv) {
  var text = $('#' + id).val();
  var textlength = text.length;
  if(textlength > limit){
    $('#' + id).val(text.substr(0,limit));
    return false;
  } else {
    $('#' + infodiv).html(limit + ' character limit' + (((limit - textlength) != limit) ? ', ' + (limit - textlength) + ' left' : ''));
    return true;
  }
}
