var ajax_page = 28240;
var ajax_page = 29597;


var AjaxBasket = null;
var WineSearch = null;
var GlobalBuy = null;
var LoginBox = null;
var ProductOrder = null;
var ProductFilter = null;

// Inspired by base2 and Prototype
// It is here, so that it is easyer to create a new CLASS
(function(){
  var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;

  // The base Class implementation (does nothing)
  this.Class = function(){};

  // Create a new Class that inherits from this class
  Class.extend = function(prop) {
    var _super = this.prototype;

    // Instantiate a base class (but only create the instance,
    // don't run the init constructor)
    initializing = true;
    var prototype = new this();
    initializing = false;

    // Copy the properties over onto the new prototype
    for (var name in prop) {
      // Check if we're overwriting an existing function
      prototype[name] = typeof prop[name] == "function" &&
        typeof _super[name] == "function" && fnTest.test(prop[name]) ?
        (function(name, fn){
          return function() {
            var tmp = this._super;

            // Add a new ._super() method that is the same method
            // but on the super-class
            this._super = _super[name];

            // The method only need to be bound temporarily, so we
            // remove it when we're done executing
            var ret = fn.apply(this, arguments);
            this._super = tmp;

            return ret;
          };
        })(name, prop[name]) :
        prop[name];
    }

    // The dummy class constructor
    function Class() {
      // All construction is actually done in the init method
      if ( !initializing && this.init )
        this.init.apply(this, arguments);
    }

    // Populate our constructed prototype object
    Class.prototype = prototype;

    // Enforce the constructor to be what we expect
    Class.constructor = Class;

    // And make this class extendable
    Class.extend = arguments.callee;

    return Class;
  };
})();

function IsNumeric(sText) {
  var ValidChars = "0123456789.";
  var IsNumber=true;
  var Char;

  for (i = 0; i < sText.length && IsNumber == true; i++) {
    Char = sText.charAt(i);
    if (ValidChars.indexOf(Char) == -1) {
      IsNumber = false;
    }
  }
  
  return IsNumber;
}


function updateGridListView () {
  var forms = jQuery("#grid_list > form.webshop-product-loop");

  var addIt = true;
  var row = null;
  var rowID = 0;

  for (var i = 0; i < forms.length; i++) {
    if ( (i+1) % 3 == 1 ) {
      row = jQuery('<div class="box-holder"><div class="holder"><div class="frame" id="frame' + i + '"></div></div></div>');
      row.appendTo(jQuery("#grid_list"));
      rowID = 'frame' + i;
    }
    jQuery(forms[i]).appendTo(jQuery("#" + rowID));
  }

}


var ab = null; // Ajax basket
var ws = null; // Wine search
var gb = null; // Global buy
var lb = null; // Global buy
var po = null; // Product order
var pf = null; // Product filter

var runables = new Array();

//$(window).ready(function(){
$(window).load(function(){


  var forms = jQuery("div#grid_list > div.box-holder > div.holder > div.frame > form");
  var lastWasLast = false;
  for (var i = 0; i < forms.length; i++) {

    var f = forms[i];
    //console.info((i % 3));
    if ( !(i % 3) ) {
      f.className += ' first-child';
    } else if ((i % 3) == 2) {
      f.className += ' last-child';
      if (i == forms.length-1) {
        lastWasLast = true;
      } else {
        lastWasLast = false;
      }
    } else {
      f.className += ' middle-child';
    }

  }
  /*if (!lastWasLast) {
    var f = forms[forms.length-1];
    if (f) {
      f.className += ' last-child';
    }
  }*/




  if (AjaxBasket) {
    //ab = new AjaxBasket(28236);
    ab = new AjaxBasket(29539);
    ab.addbasketOrderListener($('ajax_basket_order'));
    //ab.showCurrentPage();
  }

  if (WineSearch) {
    //ws = new WineSearch(28235, 28232);
    ws = new WineSearch(29669, 29538);
  }

  if (GlobalBuy) {
    gb = new GlobalBuy();
  }

  if (LoginBox) {
    lb = new LoginBox();
  }

  if (ProductOrder) {
    po = new ProductOrder();
  }

  if (ProductFilter) {
    pf = new ProductFilter();
  }

  if ($('grid_list')) {
    updateGridListView();
  }

  shop_updated_basket();

  var listOfReviews = $('listOfReviews');
  if (listOfReviews) {
    var els = listOfReviews.getElementsByTagName('*');
    if (els.length === 0) {
      var product_reviews = $('product_review_header');
      product_reviews.parentNode.removeChild(product_reviews);
    }
  }

  if (runables.length > 0) {
    for (var i = 0 ; i < runables.length; i++) {
      var ru = runables[i];
      ru();
    }
  }


  var tev = jQuery('table.tipenven');
  if (tev.length > 0) {
    //var t = tev[0];
    var btns = jQuery('table.tipenven > tbody > tr > td > input');
    for (var i = 0; i < btns.length; i++) {
      var btn = btns[i];
      if (btn.type == 'submit') {
        var v = btn.value;
        var p = btn.parentNode;
        p.removeChild(btn);
//console.info(p);
        //var nBtn = jQuery('<div class="button-green"><div class="btn-holder"><a id="tip_submit" href="javascript:void(0);" onclick="this.form.submit();">Send</a></div></div>');
        var nBtn = jQuery('<div class="btn-bestil"><div class="btn-holder"><a id="tip_submit" href="javascript:void(0);" onclick="document.form1.submit();">Send</a></div></div>');
        //p.appendChild(nBtn);
jQuery(p).append(nBtn );
Cufon.replace('table.tipenven tbody tr td .btn-bestil', { fontFamily: 'ScalaSans Bold'});
      }
    }
    
    //console.info(t);
  }

  if (document.getElementById("zip_code")) {
    document.getElementById("zip_code").onblur = function(){
      findCity();
      if ($("gls_zipcode")) {
        $("gls_zipcode").value = this.value;
      }
    }
  }


  var rightSide = document.getElementById('rightSide');
  var leftSide = document.getElementById('leftSide');
  if (rightSide) {
    var wrapper = document.getElementById('wrapper');
    var wX = wrapper.offsetLeft;
    var wW = wrapper.offsetWidth;
    var x = wX + wW;
    var x = wX + 978 + 6;
    var y = 100;
    
    if (is_window_large_enough_for_banners()) {
      rightSide.style.left = x + 'px';
      rightSide.style.top = y + 'px';
        rightSide.style.display = '';
    } else {
      rightSide.style.display = 'none';
    }
  }
  if (leftSide) {
    var wrapper = document.getElementById('wrapper');
    var wX = wrapper.offsetLeft;
    var wW = wrapper.offsetWidth;
    var x = wX + wW;
    var x = wX + 6 - leftSide.offsetWidth;
    var y = 100;
    
    if (is_window_large_enough_for_banners()) {
      leftSide.style.left = x + 'px';
      leftSide.style.top = y + 'px';
        leftSide.style.display = '';
    } else {
      leftSide.style.display = 'none';
    }
  }
});

$(window).resize(function(){
  var rightSide = document.getElementById('rightSide');
  if (rightSide) {
    var wrapper = document.getElementById('wrapper');
    var wX = wrapper.offsetLeft;
    var wW = wrapper.offsetWidth;
    var x = wX + wW;
    var x = wX + 978 + 6;
    var y = 100;
  
    if (is_window_large_enough_for_banners()) {
      rightSide.style.left = x + 'px';
      rightSide.style.top = y + 'px';
      rightSide.style.display = '';
    } else {
      rightSide.style.display = 'none';
    }
  }
  var leftSide = document.getElementById('leftSide');
  if (leftSide) {
    var wrapper = document.getElementById('wrapper');
    var wX = wrapper.offsetLeft;
    var wW = wrapper.offsetWidth;
    var x = wX + wW;
    var x = wX + 6 - leftSide.offsetWidth;
    var y = 100;
  
    if (is_window_large_enough_for_banners()) {
      leftSide.style.left = x + 'px';
      leftSide.style.top = y + 'px';
      leftSide.style.display = '';
    } else {
      leftSide.style.display = 'none';
    }
  }
});

var banner_width = 126;

function is_window_large_enough_for_banners () {
  var rightSide = document.getElementById('rightSide');
  var rWidth = banner_width
  
  var wrapper = document.getElementById('wrapper');
  var wX = wrapper.offsetLeft;
  var wW = wrapper.offsetWidth;
  var x = wX + 978 + 6;
  
  var x2 = x + rWidth;
  
  return x2 <= document.getElementById("widthTester").offsetWidth;
}

