﻿/*
*  Ajax Autocomplete for jQuery, version 1.1.3
*  (c) 2010 Tomas Kirda
*
*  Ajax Autocomplete for jQuery is freely distributable under the terms of an MIT-style license.
*  For details, see the web site: http://www.devbridge.com/projects/autocomplete/jquery/
*
*  Last Review: 04/19/2010
*/

(function (d) { function l(b, a, c) { a = "(" + c.replace(m, "\\$1") + ")"; return b.replace(new RegExp(a, "gi"), "<strong>$1</strong>") } function i(b, a) { this.el = d(b); this.el.attr("autocomplete", "off"); this.suggestions = []; this.data = []; this.badQueries = []; this.selectedIndex = -1; this.currentValue = this.el.val(); this.intervalId = 0; this.cachedResponse = []; this.onChangeInterval = null; this.ignoreValueChange = false; this.serviceUrl = a.serviceUrl; this.isLocal = false; this.options = { autoSubmit: false, minChars: 1, maxHeight: 300, deferRequestBy: 0, width: 0, highlight: true, params: {}, fnFormatResult: l, delimiter: null, zIndex: 9999 }; this.initialize(); this.setOptions(a) } var m = new RegExp("(\\/|\\.|\\*|\\+|\\?|\\||\\(|\\)|\\[|\\]|\\{|\\}|\\\\)", "g"); d.fn.autocomplete = function (b) { return new i(this.get(0) || d("<input />"), b) }; i.prototype = { killerFn: null, initialize: function () { var b, a, c; b = this; a = Math.floor(Math.random() * 1048576).toString(16); c = "Autocomplete_" + a; this.killerFn = function (e) { if (d(e.target).parents(".autocomplete").size() === 0) { b.killSuggestions(); b.disableKillerFn() } }; if (!this.options.width) this.options.width = this.el.width(); this.mainContainerId = "AutocompleteContainter_" + a; d('<div id="' + this.mainContainerId + '" style="position:absolute;z-index:9999;"><div class="autocomplete-w1"><div class="autocomplete" id="' + c + '" style="display:none; width:300px;"></div></div></div>').appendTo("body"); this.container = d("#" + c); this.fixPosition(); window.opera ? this.el.keypress(function (e) { b.onKeyPress(e) }) : this.el.keydown(function (e) { b.onKeyPress(e) }); this.el.keyup(function (e) { b.onKeyUp(e) }); this.el.blur(function () { b.enableKillerFn() }); this.el.focus(function () { b.fixPosition() }) }, setOptions: function (b) { var a = this.options; d.extend(a, b); if (a.lookup) { this.isLocal = true; if (d.isArray(a.lookup)) a.lookup = { suggestions: a.lookup, data: []} } d("#" + this.mainContainerId).css({ zIndex: a.zIndex }); this.container.css({ maxHeight: a.maxHeight + "px", width: a.width }) }, clearCache: function () { this.cachedResponse = []; this.badQueries = [] }, disable: function () { this.disabled = true }, enable: function () { this.disabled = false }, fixPosition: function () { var b = this.el.offset(); d("#" + this.mainContainerId).css({ top: b.top + this.el.innerHeight() + "px", left: b.left + "px" }) }, enableKillerFn: function () { d(document).bind("click", this.killerFn) }, disableKillerFn: function () { d(document).unbind("click", this.killerFn) }, killSuggestions: function () { var b = this; this.stopKillSuggestions(); this.intervalId = window.setInterval(function () { b.hide(); b.stopKillSuggestions() }, 300) }, stopKillSuggestions: function () { window.clearInterval(this.intervalId) }, onKeyPress: function (b) { if (!(this.disabled || !this.enabled)) { switch (b.keyCode) { case 27: this.el.val(this.currentValue); this.hide(); break; case 9: case 13: if (this.selectedIndex === -1) { this.hide(); return } this.select(this.selectedIndex); if (b.keyCode === 9) return; break; case 38: this.moveUp(); break; case 40: this.moveDown(); break; default: return } b.stopImmediatePropagation(); b.preventDefault() } }, onKeyUp: function (b) { if (!this.disabled) { switch (b.keyCode) { case 38: case 40: return } clearInterval(this.onChangeInterval); if (this.currentValue !== this.el.val()) if (this.options.deferRequestBy > 0) { var a = this; this.onChangeInterval = setInterval(function () { a.onValueChange() }, this.options.deferRequestBy) } else this.onValueChange() } }, onValueChange: function () { clearInterval(this.onChangeInterval); this.currentValue = this.el.val(); var b = this.getQuery(this.currentValue); this.selectedIndex = -1; if (this.ignoreValueChange) this.ignoreValueChange = false; else b === "" || b.length < this.options.minChars ? this.hide() : this.getSuggestions(b) }, getQuery: function (b) { var a; a = this.options.delimiter; if (!a) return d.trim(b); b = b.split(a); return d.trim(b[b.length - 1]) }, getSuggestionsLocal: function (b) { var a, c, e, g, f; c = this.options.lookup; e = c.suggestions.length; a = { suggestions: [], data: [] }; b = b.toLowerCase(); for (f = 0; f < e; f++) { g = c.suggestions[f]; if (g.toLowerCase().indexOf(b) === 0) { a.suggestions.push(g); a.data.push(c.data[f]) } } return a }, getSuggestions: function (b) { var a, c; if ((a = this.isLocal ? this.getSuggestionsLocal(b) : this.cachedResponse[b]) && d.isArray(a.suggestions)) { this.suggestions = a.suggestions; this.data = a.data; this.suggest() } else if (!this.isBadQuery(b)) { c = this; c.options.params.query = b; d.get(this.serviceUrl, c.options.params, function (e) { c.processResponse(e) }, "text") } }, isBadQuery: function (b) { for (var a = this.badQueries.length; a--; ) if (b.indexOf(this.badQueries[a]) === 0) return true; return false }, hide: function () { this.enabled = false; this.selectedIndex = -1; this.container.hide() }, suggest: function () { if (this.suggestions.length === 0) this.hide(); else { var b, a, c, e, g, f, j, k; b = this; a = this.suggestions.length; e = this.options.fnFormatResult; g = this.getQuery(this.currentValue); j = function (h) { return function () { b.activate(h) } }; k = function (h) { return function () { b.select(h) } }; this.container.hide().empty(); for (f = 0; f < a; f++) { c = this.suggestions[f]; c = d((b.selectedIndex === f ? '<div class="selected"' : "<div") + ' title="' + c + '">' + e(c, this.data[f], g) + "</div>"); c.mouseover(j(f)); c.click(k(f)); this.container.append(c) } this.enabled = true; this.container.show() } }, processResponse: function (b) { var a; try { a = eval("(" + b + ")") } catch (c) { return } if (!d.isArray(a.data)) a.data = []; if (!this.options.noCache) { this.cachedResponse[a.query] = a; a.suggestions.length === 0 && this.badQueries.push(a.query) } if (a.query === this.getQuery(this.currentValue)) { this.suggestions = a.suggestions; this.data = a.data; this.suggest() } }, activate: function (b) { var a, c; a = this.container.children(); this.selectedIndex !== -1 && a.length > this.selectedIndex && d(a.get(this.selectedIndex)).removeClass(); this.selectedIndex = b; if (this.selectedIndex !== -1 && a.length > this.selectedIndex) { c = a.get(this.selectedIndex); d(c).addClass("selected") } return c }, deactivate: function (b, a) { b.className = ""; if (this.selectedIndex === a) this.selectedIndex = -1 }, select: function (b) { var a; if (a = this.suggestions[b]) { this.el.val(a); if (this.options.autoSubmit) { a = this.el.parents("form"); a.length > 0 && a.get(0).submit() } this.ignoreValueChange = true; this.hide(); this.onSelect(b) } }, moveUp: function () { if (this.selectedIndex !== -1) if (this.selectedIndex === 0) { this.container.children().get(0).className = ""; this.selectedIndex = -1; this.el.val(this.currentValue) } else this.adjustScroll(this.selectedIndex - 1) }, moveDown: function () { this.selectedIndex !== this.suggestions.length - 1 && this.adjustScroll(this.selectedIndex + 1) }, adjustScroll: function (b) { var a, c, e; a = this.activate(b).offsetTop; c = this.container.scrollTop(); e = c + this.options.maxHeight - 25; if (a < c) this.container.scrollTop(a); else a > e && this.container.scrollTop(a - this.options.maxHeight + 25); this.el.val(this.getValue(this.suggestions[b])) }, onSelect: function (b) { var a, c; a = this.options.onSelect; c = this.suggestions[b]; b = this.data[b]; this.el.val(this.getValue(c)); d.isFunction(a) && a(c, b, this.el) }, getValue: function (b) { var a, c; a = this.options.delimiter; if (!a) return b; c = this.currentValue; a = c.split(a); if (a.length === 1) return b; return c.substr(0, c.length - a[a.length - 1].length) + b } } })(jQuery);

/*
*  fm.js
*/


function Post(form, callback) {
  form = $(form);
  $('.inactive', form).val('').removeClass('inactive');
  var url = form.attr('action');
  if (!url) { url = window.location.pathname; }
  $.post(url, form.serialize(), callback);
}

function Invoke(params, confirmation, callback) {
  if (typeof confirmation === 'string') { if (!confirm(confirmation)) { return; } }
  var url = (params.InvokeUrl ? params.InvokeUrl : '/store/ajaxhandler.aspx');
  $.post(url, params, callback);
}

var Fmp = {

  notificationIntervalId: null,
  TooltipInt: null,
  
  //  Request: function (data) {
  //    new Ajax.Request('/store/ajaxhandler.aspx', { parameters: data });
  //    return false;
  //  },

  //  SubmitForm: function (form) {
  //    return Fmp.Request(Form.serialize(form));
  //  },

  Notify: function (x, y, content) {
    var notification = $('#notification');
    notification.hide();
    $('#notification-content').html(content);
    notification.css({ top: y + 'px', left: x + 'px' }).show();
    clearInterval(Fmp.notificationIntervalId);
  },

  HideNotification: function () {
    $('#notification').bind('mouseover', function () {
      Fmp.CancelHideNotification();
    });
    Fmp.notificationIntervalId = setInterval(function () {
      var notification = $('#notification');
      notification.hide();
      Fmp.CancelHideNotification();
      $('#notification').bind('mouseover', function () {
        Fmp.CancelHideNotification();
      });
    }, 5000);
  },

  CancelHideNotification: function () {
    clearInterval(Fmp.notificationIntervalId);
    $('#notification').unbind('mouseover', function () {
      Fmp.CancelHideNotification();
    });
  },

  RemoveFromCart: function (cartProductId) {
    Invoke({ ActionType: 'RemoveFromCart', CartProductId: cartProductId });
  },

  AddToCart: function (e, data, message) {
    var x = (Event.pointerX(e) - 97);
    var y = (Event.pointerY(e) - 78 - 30);
    Fmp.Notify(x, y, message);
    Invoke(data);
    return false;
  },

  AddToCartViaForm: function (form, message) {
    var f = $(form);
    var offset = f.offset();
    var x = (offset.left);
    var y = (offset.top - 162 - 10);
    Fmp.Notify(x, y, message);
    Invoke(f.serialize());
    // Scroll to message if needed:
    var availableTop = f.position().top;
    if (availableTop < 170) { $('#notification').scrollTo(); }
    return false;
  },

  Checkout: function (form) {
    $('#SubmitButton').hide();
    $('#ProcesingMessage').show();
    $('#ProccessorErrors').hide();
    Post(form);
  },

  IncreaseValue: function (input) {
    var el = $('#' + input);
    el.val(Fmp.GetValue(el) + 1);
    Fmp.InspectValue(el);
  },

  DecreaseValue: function (input) {
    var el = $('#' + input);
    el.val(Fmp.GetValue(el) - 1);
    Fmp.InspectValue(el);
  },

  InspectValue: function (input) {
    var el = $(input);
    var i = Fmp.GetValue(el);
    if (i < 1) { i = 1; }
    el.val(i);
  },

  GetValue: function (input) {
    var el = $(input);
    var i = parseInt(el.val());
    return (isNaN(i)) ? 0 : i;
  },

  //  Debug: function (value) {
  //    var el = new Element('div', { style: 'background:#FFF; padding:10px; border:10px solid #CCC;' });
  //    el.update(value);
  //    $(document.body.appendChild(el)).scrollTo();
  //  },

  Popup: function (url) {
    window.open(url, 'win', 'status=1,width=480,height=450,resizable=1').focus();
    return false;
  },

  Tooltip: function (content, pos) {
    $('#tooltip-content').html(content);
    var t = $('#tooltip');
    t.css({ top: (pos.top - t.height()) + 'px', left: (pos.left - 128) + 'px' }).show();
    clearInterval(Fmp.TooltipInt);
  },

  _TooltipHide: function () {
    clearInterval(Fmp.TooltipInt);
    $('#tooltip').fadeOut();
  },

  TooltipHide: function () {
    Fmp.TooltipInt = setInterval(Fmp._TooltipHide, 1000);
  },

  ShowTip: function (id, imgSrc, param) {
  if ( $('#tip_' + id).css('display')==="none"){
    $('#tip_' + id).css('left', parseInt(param.x) + 30 + 'px');
    $('#tip_' + id).css('top', parseInt(param.y) - 20 + 'px');
     $('#tip_' + id).show();
  }else{
    $('#tip_' + id).css('left', parseInt(param.x) + 30 + 'px');
    $('#tip_' + id).css('top', parseInt(param.y) - 20 + 'px');
    }
  },

  HideTip: function (id, event) {
     $('.prototip').hide();
   },

   Rate: function (prodId, rating, strPrefix) {
     Invoke({ ActionType: 'RateFont', ProductId: prodId, Rating: rating, Prefix: strPrefix, InvokeUrl: '/store/ajaxhandler.aspx' });
   }

 };

jQuery(function () {

  var zindex = 1000;
  var body = $('body');

  var reEscape = new RegExp('(\\' + ['/', '.', '*', '+', '?', '|', '(', ')', '[', ']', '{', '}', '\\'].join('|\\') + ')', 'g');

  function fnFormatResult(value, data, currentValue) {
    var pattern = '(' + currentValue.replace(reEscape, '\\$1').replace(/\s+/g, '|') + ')';
    return value.replace(new RegExp(pattern, 'gi'), '<strong>$1<\/strong>');
  }

  jQuery('#q').autocomplete({ serviceUrl: '/services/autocomplete.ashx', width: 388, minChars: 1, fnFormatResult: fnFormatResult, onSelect: function (v, d) { if (d) { window.location = d; } else { $('#searchform').submit(); } } });

  var tooltip = [];
  tooltip.push(
  '<div id="tooltip" class="tooltip" style="display:none; top:100px; left:150px; z-index:' + (++zindex) + ';">',
    '<div id="tooltip-top"></div>',
    '<div id="tooltip-content">Tooltip</div>',
    '<div id="tooltip-bottom"></div>',
  '</div>');
  var tt = $(tooltip.join(''));
  tt.mouseover(function () { clearInterval(Store.TooltipInt); });
  tt.mouseout(function () { Store.TooltipHide(); });
  body.append(tt);

  $("#next").click(function () {

    var positionNow = $('#popfirstline').position();
    if (positionNow.left < -530) {

      $('#popular-fonts .popfonts-wrapper').each(function (i) {
        $(this).animate({ left: '0' }, 300);
      });

    } else {

      $('#popular-fonts .popfonts-wrapper').each(function (i) {
        $(this).delay(60 * i).animate({ left: '-=265px' }, 300);
      });
    };
  });

  $("#prev").click(function () {
    var positionNow = $('#popfirstline').position();
    if (positionNow.left > "-1") {

      $('#popular-fonts .popfonts-wrapper').each(function (i) {
        $(this).animate({ left: '0' }, 300);
      });

    } else {

      $('#popular-fonts .popfonts-wrapper').each(function (i) {
        $(this).delay(60 * i).animate({ left: '+=265px' }, 300);
      });

    };
  });

  $("#popular-fonts a").hover(function () {
    $(this).children("img").stop().animate({ left: '-260' }, 200);
    // $(this).prev(".pop-font-hover").hide();
  }, function () {
    $(this).children("img").stop().animate({ left: '0' }, 200);
  });

  
});


function ToggleCategoryFilter() {
  $('#category-filter').slideToggle('fast');
  $('#category-switch').toggleClass('category-switch-on category-switch-off');
}

function Rate(prodId, stars) {
  Invoke({ ActionType: 'RateFont', ProductId: prodId, Rating: stars, InvokeUrl: '/store/ajaxhandler.aspx' });
}



