$(document).ready(function() {

	$("#tabmenu > ul > li").each(function(){
        var r = $(this);
        r.hover(function(){
          $(this).children("ul").removeClass('closed');
        },function(){
          $(this).children("ul").addClass('closed');
        });
    });

	$("#mijntelfort").createTabset();

	if (typeof $.prototype.cycle == "function") {
    $("#cycle").cycle({
      slideDelay: 4000,
      slideAnimationStyle: $.cycle.slideAnimationStyles.fade,
      selectorAnimateSpeed: 150,
      selectorHoverAnimate: false
    });
  }

	$("#shops li, #slideshow div").click(function(){
    	window.location=$(this).find("a").attr("href");
		return false;
	});

	if(document.location.protocol=="http:") {
    	$("#mijntelfort #username, #mijntelfort #password").focus(function() {
    		$("#mijntelfort_mobiel").hide();
    		$("#loading").show();
    		window.location = $("#mijnLogin").attr("action");
        });
    } else {
    	$("form[name=mijnLogin]").bind("submit", function() {
    		window.doCookie();
    		window.validate_login( 'mijnLogin' );
    		return false;
    	});
    }

});

$.fn.createTabset = function() {
	var tabset = this, tabs = this.children("div.step");
	this.children("ul").children("li").click(function() {
		$(this).parents("ul").children("li").removeClass("current").end().end().addClass("current");
		$(tabs.hide().get($(this).prevAll().length)).show();
		return false;
	});
	this.find("ul > li.current").click();
};

function setCookie (name, value) {
	var today = new Date();
    var expire = new Date();
    expire.setTime(today.getTime() + 3600000*24*31*12);

    var argv = arguments;
    var argc = arguments.length;

    var path = (argc > 2) ? argv[2] : null;
    var domain = (argc > 3) ? argv[3] : null;
    var secure = (argc > 4) ? argv[4] : false;
    document.cookie = name + "=" + escape (value) +
        ((expire === null) ? "" : ("; expires=" + expire.toGMTString())) +
        ((path === null) ? "" : ("; path=" + path)) +
        ((domain === null) ? "" : ("; domain=" + domain)) +
        ((secure === true) ? "; secure" : "");
}

function calc_auth_hash( form, field_username, field_password ) {
	var username	= form.elements[field_username].value.toLowerCase();
    var password    = form.elements[field_password].value;
	var challenge	= '';

	// make MD5 of username + password + optionally challenge
	if ( 'undefined' != typeof hex_md5 ) {
		// check whether challenge is set
		if ( 'undefined' != typeof form.challenge ) {
			challenge = form.challenge.value;
		}
		form.elements[field_password].value = '';
		// empty verify field, if any
		if ( 'undefined' != typeof form.verify ) {
			form.verify.value = '';
		}
		// if challenge is empty
		if ( -1 != challenge.search( /^\s*$/ ) ) {
			form.md5.value = $.md5(username + password);
		} else {
			form.md5.value = $.md5( challenge + hex_md5( username + password ) );
		}
	}
}

var doCookie = function() {
    setCookie( 'HC', '1', '/', '.telfort.nl' );
};

var validate_login = function( form_name ) {
	var form		= document.getElementById(form_name);
	var password	= form.password.value;
    calc_auth_hash( form, 'username', 'password' );
	form.submit();
	return false;
};