var Flooring = {
  'init':function() {
    $('a[cid]').click(function(event){
      var obj = $(this)
      var cid = obj.attr('cid');
      if (obj.attr('href').indexOf(cid)==-1) {
        if (obj.attr('href').indexOf('?')==-1) {
          obj.attr('href', obj.attr('href') + '?cid=' + cid);
        } else {
          obj.attr('href', obj.attr('href') + '&cid=' + cid);
        }
      }
    });
    
    $('#optin form').submit(function(event){
    	var emailAddress = $("#emailAddress").val();
    	var dataString = 'emailAddress='+ emailAddress;
	    $.ajax({
		   type: "POST",
		   url: "/optin.asp",
		   data: dataString,
		   success: function(htmlText){
		   	  s=s_gi(s_account);
	          s.linkTrackVars='events,eVar4';
	          s.linkTrackEvents='event9';
	          s.events='event9';
	          s.eVar4='Bruce Email Footer';
	          s.tl(this,'o','Optin Footer Click');
		   	  optinPopup(htmlText);
		   }
		});
		return false;
    });
    
    $('#nav a').click(function(event){
      s=s_gi(s_account);
      s.linkTrackVars='prop33';
      s.prop33=this.innerHTML;
      s.tl(this,'o','Top Nav Click');
    });
    $('.linkbar a').click(function(event){
      s=s_gi(s_account);
      s.linkTrackVars='prop33';
      s.prop33=this.innerHTML;
      s.tl(this,'o','Quick Link Click');
    });

    $('.leftNav a').each(function() {
      if (this.href==window.location.href) {
        $(this).addClass('selected');
      }
    });

    $(document).keyup(function(event) {
      if (event.keyCode==27) {
        if($('#popover').length>0) $('#popover').css({'display':'none'});
      }
    });

    $('.enlargable div').click(function(event){
      if(event.target.tagName=='A')return true;

      var im = $(this).parent().find('img');
      im.attr('alt', im.parent().find('h1').html());
      im.attr('h2', im.parent().find('h2').html());
      if(im.parent().find('h2').length==0){
        im.attr('h2', im.parent().find('a').html());
      }
      Flooring.popover(im);
      return false;
    });

    $('.enlargable img').click(function(event){
      var a = $(this).parent().find('a');
      if (a && a.length>0) {
        window.location = a.attr('href');
      }
    });

    $('input.clear-on-focus').focus(function (event){
        if (!this.cleared){
            this.cleared=1;
            this.value="";
        }
    });

    if($('#nav a.section-' + Flooring.section).length>0) {
      $('#nav a.section-' + Flooring.section).addClass('selected');
    } else {
      $('#nav a').each(function() {
        if (this.href==window.location.href) {
          $(this).addClass('selected');
        }
      });
    }

    $('form').submit(function(event){
        if (this.action.match(/flooring-retailers.asp/)) {
          /* make sure cid set*/
          if (!this.action.match(/bruce_FindRetailer/)) {
            this.action = this.action + (this.action.match(/\?/)?"&":"?") + 'cid=bruce_FindRetailer';
          }

          s=s_gi(s_account);
          s.linkTrackVars='events';
          s.linkTrackEvents='event1';
          s.events='event1';
          if ($('.detailPage[itemId]').length>0) {
            s.products="item;i" + $('.detailPage[itemId]').attr('itemId');
            s.linkTrackVars='products,events';
          }
          s.tl(this,'o','Dealer Locator');
        }
    });



  },
  'popover':function(imgTag) {
  	if($('#popover-container').hasClass('optin')){
  		$('#popover-container').removeClass('optin');
  	}
  	if($('#popover-shim').hasClass('optin')){
  		$('#popover-shim').removeClass('optin');
  	}
    $('#popover-image').attr('src','/i/images/spinner.gif');

    imgTag = $(imgTag);
    var img = {'width':imgTag.width(),'height':imgTag.height()};
    var vp = {'width':$(window).width()-100,'height':$(window).height()-120};

    var c_img = {
      'width':img.width*vp.height/img.height,
      'height':vp.width*img.height/img.width
    }

    var qs = (c_img.width>vp.width)?("hei="+Math.floor(c_img.height)):("wid="+Math.floor(c_img.width));
    $('#popover-image').attr('src','http://s7d2.scene7.com/is/image/Armstrong/' + imgTag.attr('s7-id') + '?' + qs);

    $('#popover h1').html(imgTag.attr('alt'));
    $('#popover h2').html(imgTag.attr('h2'));
    $('#popover').css({'display':'block'});

    s=s_gi(s_account);
    s.products="item;i" + imgTag.attr('itemId');
    s.linkTrackVars='products,events';
    s.linkTrackEvents='event12';
    s.events='event12';
    s.tl(this,'o','Enlarge image');
  },
  initSections: function(section, subSection) {
    s.prop29 = Flooring.checkValue(section);
    s.prop30 = Flooring.checkValue(subSection);
    /* handle uncategorirzed pages - like site map */
    if (section==null||section=='None') {
      s.prop29 = s.prop30
      s.prop30 = null;
    }
  },
  checkValue : function(v) {
    if (v==null||v=="null"||v=="&nbsp;")return null;
    return v;
  }
};

optinPopup = function(htmlText) {
	$('#popover h1').html('');
    $('#popover h2').html(htmlText);
    $('#popover').css({'display':'block'});
    $('#popover-image').css({'display':'none'});
    $('#popover-container').addClass('optin');
    $('#popover-shim').addClass('optin');
}
