(function () {
  Kava.Globals.StoreID = 0;
  Kava.Globals.StoreName = 'YLighting';
  Kava.Globals.StoreDomain = 'ylighting.com';
  Kava.Globals.StorePrefix = 'YL';
  Kava.Globals.LPID = '65645376';
  Kava.Globals.LP_ChatSkill = 'yl_chat';
  // Kava.Globals.GAID = 'UA-543263-7';
  Kava.Globals.GAID = 'UA-543263-1';
  Kava.Globals.OrderConf = 0;
  Kava.Globals.IsAgent = undefined;
  Kava.Globals.GoogRetargLabels = {
	sale: 'vuFDCOKBowEQldnC_wM', 
	cart: 'uDk2CIiBowEQldnC_wM',
	home: 'HK5xCLyCowEQldnC_wM',
	highval: 'rOavCJaDowEQldnC_wM',
	medval: 'ccs0CPCDowEQldnC_wM',
	lowval: 'OKz4CMqEowEQldnC_wM',

	brand: 'YE-YCIyIowEQldnC_wM',

	flos: '-FkvCKSFowEQldnC_wM',
	artemide: '_C1_CP6FowEQldnC_wM',
	wac: 'L_OdCNiGowEQldnC_wM',
	nelson: 'MDFICLKHowEQldnC_wM',

	lamps: '_kCjCOaIowEQldnC_wM',
	lightingsystems: 't8CACMCJowEQldnC_wM',
	ceiling: 'tS3uCJqKowEQldnC_wM',
	fans: '6ui0CPSKowEQldnC_wM',
	
	other: 'ad4ECM6LowEQldnC_wM'
  };
  
  Kava.Logger();
  Kava.Loader("ssl.xorox.net");
  
  Kava.Loader.css('/static/css/jquery.modal.css');
  Kava.Loader.css('/static/css/zwello.css');
	
/*
	$CVO = [[ 'trackPage' ]];
	$CVO.account = 'ylighting';
	$CVO.site = '0';
	$CVO.server = 'sp1.convertro.com';
	$CVO.atHead = new Date;
	<!--#include virtual="/static/js/cvo.js" --> 
  Kava.Loader.script({
    name: 'CVO',
    source: '//sp1.convertro.com/trax/load/ylighting/0'
//  source: '/static/js/CVO.js'
  });
*/

  Kava.Loader.script({
    name: 'google.analytics',
    source: '/static/js/google.analytics.js'
  });
  
  Kava.Loader.script({
    name: 'jquery',
    source: '/static/js/jquery.js'
  });

  Kava.Loader.script({
    name: 'jquery.ui',
    source: '/static/js/jquery.ui.js',
    needs: 'jquery'
  });

  Kava.Loader.script({
    name: 'jquery.cookie',
    source: '/static/js/jquery.cookie.js',
    needs: 'jquery'
  });
  
  do_keyword_headline();
  // do_pgo();
  
  Kava.Loader.script({
    name: 'jquery.json',
    source: '/static/js/jquery.json.js',
    needs: 'jquery'
  });
  
  Kava.Loader.script({
    name: 'jquery.dom',
    source: '/static/js/jquery.dom.js',
    needs: 'jquery'
  });
  
  Kava.Loader.script({
    name: 'jquery.modal',
    source: '/static/js/jquery.modal.js',
    needs: 'jquery'
  });
  
  Kava.Loader.script({
    name: 'jquery.validate',
    source: '/static/js/jquery.validate.js',
    needs: 'jquery'
  });
  
  Kava.Loader.script({
    name: 'scott-live',
    source: '/static/js/scott/live.js',
    needs: 'loaded'
  });

  Kava.Loader.script({
    name: 'merchant_cart',
    source: '/static/js/merchant_cart.js',
    needs: 'jquery.cookie jquery.json'
  });
  
  Kava.Loader.script({
    name: 'site.dialog',
    source: '/static/js/site.dialog.js',
    needs: 'jquery.modal jquery.json jquery.validate jquery.dom'
  });
  
  Kava.Loader.script({
    name: 'loaded',
    source: '/static/js/loaded.js', 
    needs: 'site.dialog jquery.ui google.analytics merchant_cart'
  });
  
  Kava.Loader.script({
    name: 'express_shipping',
    source: '/static/js/express_shipping.js',
    needs: 'cvo jquery.cookie'
  });
  
  Kava.Loader.script({
    name: 'trade_reg_form',
//    source: '/static/js/trade_reg_form.js',
    source: '/static/js/test_trade_reg_form.js',
    needs: 'trax'
  });
  
	var do_agent = function () {
		Kava.Loader.use('trade_reg_form');
	};

  Kava.Loader.script({
    name: 'cvo',
    source: function () {
    	if (window.$CVO) {
				$CVO.push([ 'onTrackReady', function () {
					Kava.Globals.IsAgent = $CVO.is_agent;
					Kava.Loader.loaded('cvo');
					if (Kava.Globals.IsAgent) {
					  do_agent();
					}
				}]);
			}
			else {
				Kava.Loader.loaded('cvo');
			}
    }
  });

  Kava.Loader.script({
    name: 'trax',
    source: '/trax/visit/' + Kava.Globals.StoreID + '/rev59.js',
    needs: 'jquery jquery.cookie loaded cvo express_shipping'
  });

  Kava.Loader.use('trax');

  if (window.__Item) {
    Kava.Loader.use('jquery.ui');
    Kava.Loader.use('scott-live');
  }

  function do_pgo () {
    // if (/KHTML|Safari|WebKit/.test(navigator.userAgent)) return;
    
    // don't shuffle dimmers page
    if (/\/dimmers\.html/.test(document.URL))
      return;

    // don't shuffle best sellers page with multiple product_grid id's
    if (/top10besepea\.html/.test(document.URL))
      return;
    
    // don't shuffle sorted pages
    if (/allitems-/.test(document.URL))
      return;
    
    // stylesheet DOM edits on ssl pages under FireFox flakey?
    if (/^https/.test(window.location.protocol))
      return;
    
    // pgo increasing bounce rate?
    if (window.location.pathname.length <= 1)
      return;
    
    var sid = readCookie('txi_2');
    
    var is_agent = readCookie('is_agent');
    if (is_agent || !sid)
      return;
    
    // replace 1's with 2's (we could put a "1" in sid_base but that might skew the random distribution since we're not generating 1's)
    sid = sid.replace(/1/g, 2);
    
    var sid_base = '2 3 4 5 6 7 8 9 A B C D E F G H J K M N P Q R S T U V W X Y Z'.split(' ');
    var sid_ords = {};
    var sid_digits = sid.substr(2,6).split('');
    var sid_seed = 0;
    for (var i = 0; i < sid_base.length; i++)
      sid_ords[sid_base[i]] = i;
    for (var i = 0; i < sid_digits.length; i++)
      sid_seed = sid_seed * sid_base.length + sid_ords[sid_digits[i]];

    // $.each(sid_base, function (i, v) { sid_ords[v] = i });
    // $.each(sid.substr(2,6).split(''), function (i, v) { sid_seed = sid_seed * sid_base.length + sid_ords[v] });
    Kava.Globals.SIDSeed = sid_seed;
    
    // if (sid_seed % 5 && !Kava.Globals.ClientDebug) return;
    var ss = document.styleSheets[0];
    
    try {
      if (ss.insertRule) {
        ss.insertRule("#product_grid {display: none;}", 0);
      }
      else if (ss.addRule) {
        ss.addRule("#product_grid", "display: none");
      }
      else
        return;
    }
    catch (err) {
      // if (Kava.Globals.ClientDebug) window.alert(err);
      return;
    }
  
    Kava.Loader.script({
      name: 'pgo',
      source: '/static/js/pgo.js',
      needs: 'jquery.cookie'
    });
    Kava.Loader.use('pgo');
  }

  function do_keyword_headline() {
    var m = document.URL.match(/[&?]keyword=([^&?]*)/i);
    if (m && m[1]) {
      var kw = unescape(m[1]);
      kw = kw.replace(/\b\w/g, function (c) { return c.toUpperCase() });
      var f = function () {
        var e = document.getElementById('keyword_headline');
        if (e)
          e.innerHTML = kw;
        else
          window.setTimeout(f, 50);
      };
      window.setTimeout(f, 50);
    }
  }

  function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
      var c = ca[i];
      while (c.charAt(0)==' ') c = c.substring(1,c.length);
      if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
  }
  
})();

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = "";
}

/*
  if (Kava.Globals.ClientDebug && 0) {
    Kava.Loader.script({
      name: 'price_match',
      source: '/static/js/price_match.js'
    });
  
  //  if (/\/(price-match.html)\?/.test(document.URL))
    Kava.Loader.use('price_match');
  }
  
*/

// 


// document.write('<scr' + 'ipt src="kava.js"></scr' + 'ipt>');
/*
var L = [];

var div_debug = document.createElement("div");

window.onload = function () {
  var body = document.getElementsByTagName("body")[0];
  body.appendChild(div_debug);
}

function log (msg) {
  L.push(msg);
  div_debug.innerHTML += '<br>' + msg
}

var r = [];
Kava.Loader();

Kava.Loader.script({
	name: 'js1',
	source: 'js.php?p=1'
});
Kava.Loader.script({
	name: 'js2',
	source: 'js.php?p=2',
	needs: 'js1 js3'
});
Kava.Loader.script({
	name: 'js3',
	source: 'js.php?p=3'
});

Kava.Loader.use('js1 js2 js3');
log('Result ' + r);

  
  Kava.Loader.script({
    name: 'feed_perfect',
    source: '/static/js/fp.js',
    needs: 'jquery.cookie'
  });

  else {
    Kava.Loader.script({
      name: 'legacy_cart',
      source: '/static/js/legacy_cart.js',
      needs: 'jquery.cookie'
    });
  }


*/