GaspareStore = {

  fbLink: '',

  init: function() {
    obj = this;

    this._resize_popup_wall();
    setTimeout(function() {
      obj._resize_popup_wall();
    }, 2000);
    setTimeout(function() {
      obj._resize_popup_wall();
    }, 4000);
    setTimeout(function() {
      obj._resize_popup_wall();
    }, 6000);
    setTimeout(function() {
      obj._resize_popup_wall();
    }, 8000);
    setTimeout(function() {
      obj._resize_popup_wall();
    }, 10000);

    if(!jQuery('#_popup_index') || jQuery('#_popup_index').val() != '4') {
      return;
    }
    // add conditional to not do this if we have a bot
    if($('.checkout') !== null) {
      this._initAjaxLoading();
 
      Menu.init();
      Search.init();
      Subscribe.init();

      this._initNewsletterForm();
      this._startMainSlideshow();

      var getParams = this.getUrlVars();

      if(typeof getParams.path !== 'undefined') {

        eval('var page_object = ' + getParams.type.substr(0,9) + ';');

        this._loadPopup(page_object, getParams.path);
      }

      this._bindEvents();
    }
  },

  userLoggedIn: function() {
    if(jQuery('#gaspare_logout').length !== 0) {
      return true;
    }
    return false;
  },

  _bindEvents: function() {
    obj = this;

    $(window).resize(function() {
      obj._resize_popup_wall();
    });
  },

  _loadPopup: function(popup_object, popup_url, callback) {
    callback = callback || function() {};
    var parentObj = this;

    var new_callback = function() {
      if ($('#_popup_index').val() == $('#_min_popup_index').val()) {
        //fade in the popup wrapper
        $('#popup_wall').fadeTo('100', 0.3);
      }
      callback();
    };

    var currentDate = new Date();

    var options = {
        url: popup_url,
        callback: function(popup_page) {
          parentObj.showPopup(popup_page, popup_object);
          new_callback();
        }
    };

    this.doAjaxEncodedRequest(options);
  },

  doAjaxEncodedRequest: function(options) {

    var parentObj = this;

    if(document.location.protocol == "https:") {
      if(options.url.search('https') === -1 && options.url.search('http') === 0) {
       var insecure = options.url.substr(4);
       options.url = 'https' + insecure;
      }
    }

    var link = options.url;
    while(link.substr(0,1) === '/') {
      link = link.substr(1, link.length);
    }

    this.fbLink = link;

    $('#progress').show();

    jQuery.ajax({
        cache: false,
        dataType: 'text',
        url: options.url,
        beforeSend: function(xhr) {
          xhr.setRequestHeader("Content-type", "text/html;charset=iso-8859-1");
          if(xhr.overrideMimeType) {
            xhr.overrideMimeType('text/html; charset=iso-8859-1');
          }
        },
        success: function(popup_page) {

          $('#progress').hide();
          var pageTracker = _gat._getTracker("UA-20851144-1");
          pageTracker._trackPageview(options.url);
          options.callback(popup_page);
          parentObj.updateCartTotal($(popup_page).find('#items_in_cart').html());
        }
    });
  },

  updateCartTotal: function(value) {
    $('#items_in_cart').html(value);
  },

  showPopup: function(popup_page, popup_object) {

    var start_inventory = popup_page.search('var inventoryarray');
    if (start_inventory !== 0) {
      var end_inventory = popup_page.substr(start_inventory).search('</script>');
      var inventory = popup_page.substr(start_inventory, end_inventory);

      if($.browser.msie) {
        var new_script =  $('<script type="text/javascript">' + inventory + '</script>');
        new_script.appendTo('body');
      } else {
        $('body').append($('<script />', {
          html: inventory
        }));
      }
    }

    this.updateCartTotal($(popup_page).find('#items_in_cart').html());

    var parentObj = this;
    // this should only be called by other popups
    this.pauseSlideshow();
    var popup_index = parseInt($('#_popup_index').val(), 10);

    $('#_popup_index').val(popup_index +2);
    $('#popup_wall').css('zIndex', popup_index +1);
    var z_index = $('#_popup_index').val();
    var new_el = $(popup_page).find('.popup_container');

    $(new_el).appendTo('body');

    new_el.css('zIndex', z_index); 
    new_el.find('#popup_close a').click( function(e) {
      e.preventDefault();
      parentObj._removePopup($(this).parent().parent()); 
    });

    popup_object.init();
    new_el.hide();
    $('#popup_wall').show();
    this._resize_popup_wall();

    //scroll up
    $('html, body').animate({scrollTop:0}, '100');

    new_el.fadeIn(250);    
  },

  _resize_popup_wall: function() {
    var tallest_container = jQuery(document).height();
    jQuery('.popup_container').each(function() {
      var popup_height = jQuery(this).height() + 120;
      if(popup_height > tallest_container) {
        tallest_container = popup_height;
      }
    });
    jQuery('#popup_wall').height(tallest_container);
  },

  _removePopup: function(popup, callback) {
    callback = callback || function() {};
    var popup_index = parseInt($('#_popup_index').val(), 10);
    $('#_popup_index').val(popup_index - 2);
    var parentObj = this;
    this.resumeSlideshow();
    $(popup).fadeOut(100, function() {
        $(popup).remove();
        if ($('.popup_container').length === 0) {
          //fade out the popup wrapper
          $('#popup_wall').fadeOut(50, function() {
            Menu.disabled = false;
            callback();
          });
          $('#popup_wall').css('zIndex', 1);

          $('#_popup_index').val(4);
        } else {
          $('#popup_wall').css('zIndex', popup_index -3);
          callback();
        }
    });
  },

  _initAjaxLoading: function () {
    var parentObj = this;

    $('#user_menu a.myBag').click(function(e) {
      e.preventDefault();

      parentObj._loadPopup(Cart, $(this).attr('href'));

      return false;
    });

    $('a.popup_static').not(".myBag").each(function() {
      // set link to "loadPopup"
      $(this).click(function(e) {
        // prevent default link action
        e.preventDefault();
        // load the popup for a non-object
        parentObj._loadPopup(ExtraPage, $(this).attr('href'));

        return false;
      });
    });
    
    $('a.popup_static_category').not(".myBag").each(function() {
      // set link to "loadPopup"
      $(this).click(function(e) {
        // prevent default link action
        e.preventDefault();
        // load the popup for a non-object
        parentObj._loadPopup(Category, $(this).attr('href'));

        return false;
      });
    });

    $('.popup_static_product').not(".myBag").each(function() {

      // set link to "loadPopup"
      $(this).click(function(e) {
        // prevent default link action
        e.preventDefault();

        // load the popup for a Product
        parentObj._loadPopup(GsProduct, $(this).attr('href'));

        return false;
      });

    });

    //show and hide the lovely ajax loader
    $("#progress").ajaxStart(function() {
       $(this).show();
    });

    $("#progress").ajaxStop(function() {
       $(this).hide();
    });
  },

  _startMainSlideshow: function() {
    this.slideshow = jQuery('#slideshow_container');
    this.slideshow.crossSlide({
              sleep: 4,
              fade: 1,
              started_callback: function() {
                $('#slideshow_image_cover').fadeOut(1000, function() {
                  $('#slideshow_image_cover').css('zIndex', 4);
                  //$('#progress').fadeOut(200);
                });
              }
            },
            this._getSlidesArray()            
      );
  },
  
  _getSlidesArray: function() {
    var slides = [], root = "http://chocolat.s3.amazonaws.com/chocolate/slides/";
    for(var i = 1; i <= 22; i++) {
      var index, slide;
      index = i < 10 ? "0" + i : i;
      slide = {src: root + 'chocolate-' + index + ".jpg"};
      slides.push(slide);      
    }
    return slides;
  },
  
  stopSlideshow: function() {
    this.slideshow.crossSlideStop();
  },

  restartSlideshow: function() {
    this.slideshow.crossSlideRestart();
  },

  pauseSlideshow: function() {
     this.slideshow.crossSlidePause();   
  },

  resumeSlideshow: function() {
    this.slideshow.crossSlideResume();
  },

  showAddedToCart: function(image, title) {
    var obj = this;
    $('#added_to_cart .added_image img').attr('src', image);
    $('#added_to_cart .product_title').html(title);
    $('#added_to_cart').fadeIn(250);
  },

  hideAddedToCart: function() {
      $('#added_to_cart').delay(500).fadeOut(250);
  },

  _initNewsletterForm: function() {
    var parentObj = this;
/*
    $('#mailinglist_form').ajaxForm({
      beforeSend: function(xhr) {
        xhr.setRequestHeader("Content-type", "text/html;charset=iso-8859-1);
        if (xhr.overrideMimeType) {
          xhr.overrideMimeType('text/html; charset=iso-8859-1');
        }
      },
      success: function(popup_page) {
        parentObj.showPopup(popup_page, {init: function() {}});
      }
    });*/
  },

	getUrlVars: function() {
		var vars = {};
		var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			vars[key] = value;
		});
		return vars;
	}

};

