var _prefix = '';
var maxSteps = 0;
var curStep = 0;

$(document).ready(function (e) {

	$('input').focus(function(){$(this).removeClass('error-input error-same')});

	$('.swapImage').rollover(true);
	
  $('#btnSave').formValidator({
    onSuccess: function () {
      $("#frmRegister").submit();
    },
    scope: '#frmRegister'
  });

  $('#btnSavePass').formValidator({
    onSuccess: function () {
      $("#frmUserPass").submit();
    },
    scope: '#frmUserPass'
  });
	
  $('#btnSend').formValidator({
    onSuccess: function () {
      $("#frmContact").submit();
    },
    scope: '#frmContact'
  });

  $('#btnSaveDetails').formValidator({
    onSuccess: function () {
      $("#frmUserDetails").submit();
    },
    scope: '#frmUserDetails'
  });
	
  $('#newsletterBtn').click(function(e)
	{
		e.preventDefault();
     $.post(_prefix + "/ajax.server.php", { act: "addNewsletter", email: $("#newsletterEmail").attr('value') }, function (data) {
		 		$("#newsletterMessage").html(data);
		 	});		
	});

  $("input#loginUsername").DefaultValue("utilizator (e-mail)");
  $("input#loginPassword").DefaultValue("parola");
	$("input#newsletterEmail").DefaultValue("adresa de e-mail");
	$("input#search").DefaultValue("cauta produs in site");

  $("#loginButton").click(function (e) {
    e.preventDefault();
    $("#frmLogin").submit();
  });

  $("#frmRegister *").focus(function () {
    $(this).next("div").css('color', '#000000');
  });

  $("#frmUserDetails *").focus(function () {
    $(this).next("div").css('color', '#000000');
  });

  $("#frmUserPass *").focus(function () {
    $(this).next("div").css('color', '#000000');
  });

  $("#frmRegister *").blur(function () {
    $(this).next("div").css('color', '#bdbdbd');
  });

  $("#frmUserDetails *").blur(function () {
    $(this).next("div").css('color', '#bdbdbd');
  });

  $("#frmUserPass *").blur(function () {
    $(this).next("div").css('color', '#bdbdbd');
  });
  
	$("#frmOrder *").focus(function () {
    $(this).next("div").css('color', '#000000');
  });
	$("#frmOrder *").blur(function () {
    $(this).next("div").css('color', '#bdbdbd');
  });	
	
  $("#imgSec").click(function () {
    $(this).attr('src', 'include/script_generate_code.php?id=' + Math.random());
  });

	$("#searchBtn").click(function(e){
		e.preventDefault();
		window.location.href = _prefix + '/cauta/' + $("#search").val();
	});

  $("select#judet").change(function () {
    var i = 0; var options = '';
    $.getJSON(_prefix + "/ajax.server.php", { act: "getCities", id: $(this).attr('value') }, function (data) {
      if (data != null)
        for (i = 0; i < data.length; i++) {
          options += '<option value="' + data[i]['id'] + '">' + data[i]['name'] + '</option>';
        }
      else {
        options = '<option value="">alege mai intai un judet</option>';
      }
      $('#localitate').html(options);
    });
  });

  $('.productHome').each(function () { maxSteps++ });
	
  //maxSteps = Math.ceil(maxSteps / 3);	
	maxSteps = maxSteps - 3;
	
  $("#arrowRight").click(function (e) {
    e.preventDefault();
    if (curStep < maxSteps) {
      $("#productScroll").animate({ "left": "-=212px" }, "slow", "easeOutBack");
			curStep++;
    }
  });
  $("#arrowLeft").click(function (e) {
    e.preventDefault();
    if (curStep > 0) {
      $("#productScroll").animate({ "left": "+=212px" }, "slow", "easeOutBack");
      curStep--;
    }
  });
	
	$('#sizes').change(function(){changeSize();});	
	$('#colour').change(function(){
		window.location.href = _prefix + '/produs/' + $("#productID").val() + "/" + $("#productName").val() + "/" + $("#colour").val();
	});
	
	$("#addToCart").click(function(e){
		e.preventDefault();
		$("#frmProductDetails").submit();
	});
	
	if($('#sizes').length != 0)
		changeSize();

  $('a.finishOrder').formValidator({
    onSuccess: function () {
      $("#frmFinish").submit();
    },
    scope: '#frmFinish'
  });

	$("input#loginPassword").keypress(function(e){
		if (e.keyCode == "13") 
		{
			$("#frmLogin").submit();
		} 
	});
	
	$("input#search").keypress(function(e){
		if (e.keyCode == "13") 
		{
			window.location.href = _prefix + '/cauta/' + $("#search").val();
		} 
	});
	$("form#frmSearch").submit(function(){
		return false;
	});

});

function changeSize()
{
	$('#stoc').html(size[$('#sizes').val()]);	
}

function removeFromCart(id)
{
	if(confirm('Esti sigur de stergere?'))
		window.location.href = _prefix + '/cos/stergere/' + id;	
}
