//Global navigation
var timeout = 500;
var closetimer = 0;
var ddmenuitem = 0;

Array.prototype.unique = function () {
	var r = new Array();
	o: for (var i = 0, n = this.length; i < n; i++) {
		for (var x = 0, y = r.length; x < y; x++) {
			if (r[x] == this[i]) {
				continue o;
			}
		}
		r[r.length] = this[i];
	}
	return r;
}

function numbersonly(myfield, e, dec) {
  var key;
  var keychar;
  if (window.event) {
     key = window.event.keyCode;
  } else if (e) {
     key = e.which;
  } else {
     return true;
  }
  keychar = String.fromCharCode(key);
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) ) {
     return true;
  } else if ((("0123456789").indexOf(keychar) > -1)) {
     return true;
  } else if (dec && (keychar == ".")) {
     myfield.form.elements[dec].focus();
     return false;
  } else {
    return false;
  }
}

function jsddm_open(){
  jsddm_canceltimer();
	jsddm_close();
	ddmenuitem = $(this).find('ul').eq(0).css('visibility', 'visible');
}

function jsddm_close(){
  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');
}

function jsddm_timer(){
  closetimer = window.setTimeout(jsddm_close, timeout);
}

function jsddm_canceltimer(){
  if (closetimer){
    window.clearTimeout(closetimer);
		closetimer = null;
	}
}

function update_cart() {
	$.ajax({
	  data:  'product_type=' + $('#product_type').val()
	      + '&texture='      + $('#texture').val()
	      + '&color='        + $('#color_select').val()
	      + '&shape='        + $('#shape_select').val(),
	  dataType: 'script',
	  type: 'post',
	  url: '/cart/update_display'
	});
}

function jquery_exec() {
	$('#advanced_button, #search_button').each(function(){
		if ($(this).attr('src').match('_off')){
			$(this).hover(
				function(){
					$(this).attr('src', $(this).attr('src').replace('_off', '_on'));
				},
				function(){
					$(this).attr('src', $(this).attr('src').replace('_on', '_off'));
				}
			);
		}
	});

  // Global navigation search feature
  $('#navigation li').each(function(){
		if ($(this).find('img').size() > 0 && $(this).find('img').attr('src').match('_off')){
			$(this).hover(
				function(){
					$(this).find('img').attr('src', $(this).find('img').attr('src').replace('_off', '_on'));
				},
				function(){
					$(this).find('img').attr('src', $(this).find('img').attr('src').replace('_on', '_off'));
				}
			);
		}
	});

	// Global navigation hovers
	$('#jsddm > li').bind('mouseover', jsddm_open);
	$('#jsddm > li').bind('mouseout',  jsddm_timer);
	$(document).click(function(){
	  jsddm_close();
	});


  //Search
  var showing = false;
  $('#search').hover(
    function(){
      $(this).attr('src', $(this).attr('src').replace('_off', '_on'));
    },
    function(){
      if ($('.search_field').css('display') != 'block'){
        $(this).attr('src', $(this).attr('src').replace('_on', '_off'));
      }
    }
  );

  $('#search').click(function(){
    if (!showing){
      $(this).attr('src', $(this).attr('src').replace('_off', '_on'));
      $('.search_field').show();
      showing = true;
    } else {
      $(this).attr('src', $(this).attr('src').replace('_on', '_off'));
      $('.search_field').hide();
      showing = false;
    }
  });


	$("#tabletops, #bags, #floormats, #hospitality, #utility").hover(
    function (){
      $(this).find(".home_links").fadeIn("slow");
    },
    function (){
      $(this).find(".home_links").fadeOut("slow");
    }
  );

  $(".home_link").hover(
    function () {
			$(this).addClass('highlighted');
    },
    function (){
			$(this).removeClass('highlighted');
    }
  );

  $(".trade_links").hover(
    function (){
      $(this).find('img').attr('src', $(this).find('img').attr('src').replace('_off', '_on'));
    },
    function () {
      $(this).find('img').attr('src', $(this).find('img').attr('src').replace('_on', '_off'));
    }
  );

  $(".question").click(function() {
    $('.answer[rel=' + $(this).attr('rel') + ']').toggle();
  });

//  Scriptbox.overlay.init();

    parseProductUrl = function(){
        var product_groups1 = $(document).url().param('c').toString();
        var product_type = product_groups1.split(',')[0];
        var texture = product_groups1.split(',')[1];
        var product_groups2 = $(document).url().attr("hash");
        var color = product_groups2.split('/')[1];
        var shape = product_groups2.split('/')[2];
        return "product_type="+product_type+"&texture="+texture+"&color="+color+"&shape="+shape;
    }

    printContractProduct = function(){
        if ($(document).url().attr("hash").split('/')[1] == "-1"){
            alert("Please select a product color");
            return;
        }
        var params = parseProductUrl();
        var protocol = $(document).url().attr("protocol");
        var host = $(document).url().attr("host");
        window.open(protocol+"://"+ host+"/contract/print?"+params,'print','');
    }
}

$(jquery_exec);