// jQuery Functions

$(function(){
	var path = location.pathname.substring(1);

	if ( path )
		$('#nav a[href$="' + path + '"], #leftCol ul a[href$="' + path + '"]').attr('class', 'active');
});


$(document).ready(function() {

	var link = "/index.php/"; // Url to your application (including index.php/)

	$("#basketAdd").submit(function() {

		// Get the product ID and the quantity
		var id = $(this).find('input[name=product_id]').val();
		var qty = $(this).find('input[name=qty]').val();
		var art = $(this).find('select[name=art]').val();

		$.post(link + "site/add_to_cart", { product_id: id, qty: qty, art: art, ajax: '1' },
			function(data){

				if(data == 'true'){
					$.post(link + "site/total_products", function(cart){ // Get the contents of the url cart/show_cart  
						$("#basket h3 strong").html(cart); // Replace the information in the div #cart_content with the retrieved data
					});
					
					alert("Product added to cart");
				}
				else {
					alert("Product does not exist");
				}
		});

		return false; // Stop the browser of loading the page defined in the form "action" parameter.
	});
});





// Accessible search form

function initOverLabels () {
  if (!document.getElementById) return;  	

  var labels, id, field;

  // Set focus and blur handlers to hide and show 
  // LABELs with 'overlabel' class names.
  labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
	
    if (labels[i].className == 'overlabel') {

      // Skip labels that do not have a named association
      // with another field.
      id = labels[i].htmlFor || labels[i].getAttribute('for');
      if (!id || !(field = document.getElementById(id))) {
        continue;
      }

      // Change the applied class to hover the label 
      // over the form field.
      labels[i].className = 'overlabel-apply';

      // Hide any fields having an initial value.
      if (field.value !== '') {
        hideLabel(field.getAttribute('id'), true);
      }

      // Set handlers to show and hide labels.
      field.onfocus = function () {
        hideLabel(this.getAttribute('id'), true);
      };
      field.onblur = function () {
        if (this.value === '') {
          hideLabel(this.getAttribute('id'), false);
        }
      };

      // Handle clicks to LABEL elements (for Safari).
      labels[i].onclick = function () {
        var id, field;
        id = this.getAttribute('for');
        if (id && (field = document.getElementById(id))) {
          field.focus();
        }
      };

    }
  }
};


function hideLabel (field_id, hide) {
  var field_for;
  var labels = document.getElementsByTagName('label');
  for (var i = 0; i < labels.length; i++) {
    field_for = labels[i].htmlFor || labels[i].getAttribute('for');
    if (field_for == field_id) {
      labels[i].style.textIndent = (hide) ? '-3000px' : '0px';
      return true;
    }
  }
}


window.onload = function () {
  setTimeout(initOverLabels, 50);
};


