$(function() {

    // Cookie setter function
	setCookie = function(c_name,value,exdays) {
	    var exdate = new Date();
		exdate.setDate(exdate.getDate() + exdays);
		var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
		document.cookie=c_name + "=" + c_value;
	}
	
	// Cookie getter function
	getCookie = function(c_name) {
	    var i,x,y,ARRcookies=document.cookie.split(";");
		for (i=0;i<ARRcookies.length;i++) {
		    x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
			y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
			x=x.replace(/^\s+|\s+$/g,"");
			if (x==c_name) {
			    return unescape(y);
			}
		}
	}
	
    // Add the cycle plugin to the top content
	if ( $('body').hasClass('page-1') ) {
        $("#zone-top").cycle({
		    speed: 1500,
			delay: -2500,
			timeout: 8000
		});
	}
        
    // Manipulate the notes
    $(".footer-notes").hover(
	    function() {     
            $(this).find("a").css('text-decoration', 'underline');
		}, function() {    
            $(this).find("a").css('text-decoration', 'none');
        }
	);
	
    // Check if there are enough token to display the navigation arrows
    $(".footer-notes").each(function() {
	    if ( ! $(this).find(".token-Asiantuntijaverkosto").length ) {
            if ( $(this).find(".token").length < 2 ) {
				$(this).addClass('no-cycle').find(".cycle-up, .cycle-down").remove();
			}
		}
    });
    
	// Unwrap Asiantuntijaverkosto-tokens (this is required in order to make cycle work)
	$(".footer-notes .token-Asiantuntijaverkosto .anchor").unwrap().remove();
    
    $(".footer-notes .token").each(function() {
        var href = $(this).find(".content-container").find("a").attr("href");
        if (href) {
            $(this).css('cursor', 'pointer').click(function() {
                window.location = href;
            });
        }
    });
    
    $(".cycle-down, .cycle-up").hover(
	    function() {
            $(this).closest(".footer-notes").find(".token").find("a").css('text-decoration', 'none');     
		}, function() {
            $(this).closest(".footer-notes").find(".token").find("a").css('text-decoration', 'underline');
		}
	);
    
    $.fn.vAlign = function() {
        return this.each(function(i){
            var h = $(this).height();
            var oh = $(this).outerHeight();
            var mt = (h + (oh - h)) / 2;
            $(this).css("margin-top", "-" + mt + "px");
            $(this).css("top", "50%");
            $(this).css("position", "absolute");
        });
    };    
    
    $(".footer-notes .content-container").each(function() {
        $(this).vAlign(this);
    });
    
    // Manipulate the notes
    $(".footer-notes .note-contents .zone").each(function() {
	    var parentNoteID = $(this).parents('.footer-notes:first').attr('id');
	    $(this).cycle({
            next: '.cycle-down-' + parentNoteID,
            prev: '.cycle-up-' + parentNoteID,
            timeout: 0,
			fx: 'none'
		});
    });
	
	// Set autocomplete for email field
	$('#email').attr('autocomplete', 'off');
    
});

