$(document).ready(function(){ 
	$(document).pngFix(); 
}); 

$(function() {
	$('#se-film').click(function() {
  		$('#webshop-intro').slideUp('slow');
  		$('#webshop-film').slideDown('slow');
  		mpmetrics.track("Watch intro movie");
	});
	
	$('#luk').click(function() {
	    $('#webshop-film').hide('0');
  		$('#webshop-intro').fadeIn('slow');
	});
}); 
$(function() {		
	$("#medarbejdere").tooltip({  
   	 // place tooltip on the right edge  
   	 position: ['center', 'right']
	});
	$("#telefoner").tooltip({  
   	 // place tooltip on the right edge  
   	 position: ['center', 'right']
	}); 
	$("#minutter").tooltip({  
   	 // place tooltip on the right edge  
   	 position: ['center', 'right']
	});
	$("#api").tooltip({  
   	 // place tooltip on the right edge  
   	 position: ['center', 'right']
	});
	$("#gratis").tooltip({  
   	 // place tooltip on the right edge  
   	 position: ['center', 'left']
	});
}); 

$(function() {
    getAvailableNumbers();

    $("#get-new-numbers").live('click', function() {
      getAvailableNumbers();
      mpmetrics.track("Get new numbers");
    })

    var numbers = "#available-numbers td";

    $(numbers).live("click", function() {
      if (!$(this).hasClass('fetch-numbers')) {
        $("#account-number").val($(this).html());
        $(numbers).removeClass("active-number");
        $(this).addClass("active-number");
      }
    })
})

function getAvailableNumbers() {
  $.getJSON('http://system.firmafon.dk/phonenumbers/unused?callback=?', populateNumberSelector);
}

// Should do something about this method...
function populateNumberSelector(data, textStatus) {
  var link = "<a id='get-new-numbers'>Hent 8 nye numre</a>";
  var table = $("#available-numbers");
  table.empty();

  var i = 0;
  for(irow=0; irow < 2; irow++) {
    var row= $("<tr></tr>");

    for(icell=0; icell < 5; icell++) {
      if (i < 8) {
        if (data[i]) {
          var cell= $("<td>" + data[i]["number"] + "</td>");
          row.append(cell);
        } else {
          var cell= $("<td></td>");
        }
      } else if (i == 9) {
        var cell = $("<td class='fetch-numbers' colspan='2'>" + link + "</td>");
        row.append(cell);
      }
      i++;
    }

    table.append(row);

    table.find("td:first").click();
  }
}

$(function() {
  var form = $("#signup-form");
  form.validate({
    rules: {
      "account[contact_name]": "required",
      "account[contact_surname]": "required",
      "account[mail]": {
        required: true,
        email: true
      },
      "account[phone]": {
        required: true,
        digits: true,
        rangelength: [8,8]
        },
      "account[name]": "required",
      "account[cvr]": {
        required: true,
        digits: true
        },
      "account[address]": "required",
      "account[zip]": {
        required: true,
        rangelength: [4,4]
        },
      "account[terms]": "required"
    },
    messages: {
      "account[contact_name]": "&nbsp;",
      "account[contact_surname]": "&nbsp;",
      "account[mail]": "&nbsp;",
      "account[phone]": "&nbsp;",
      "account[name]": "&nbsp;",
      "account[cvr]": "&nbsp;",
      "account[address]": "&nbsp;",
      "account[zip]": "&nbsp;",
      "account[terms]": "&nbsp;"
    },
    success: function(label) {
      label.html("&nbsp;").addClass("valid");
    },
    errorClass: "invalid"
  })
})

$(function() {
  $("#porting").change(function() {
        if ($(this).is(':checked'))
                $("#number-to-port").slideDown(500);
        else
                $("#number-to-port").slideUp(500);
  })
})
