jQuery(document).ready(function() {

	var cookie_name = 'MOREID';

	var path = location.pathname;
	var my_path = '';
	if (path.match(/\/([^\/]+)\/index\.html$/)) {
		my_path = RegExp.$1;

		if($.cookie("PATHID")) {
			if($.cookie("PATHID") != my_path) {
				$.cookie(cookie_name, '-1', {expires: 365, path: '/'});
				$.cookie("PATHID", my_path, {expires: 365, path: '/'});
			}
		} else {
			$.cookie("PATHID", my_path, {expires: 365, path: '/'});
		}

	}


	$('.footNote').css('display', 'block');
	$('.morearea').css('display', 'none');

	$('a').not('[href!="#readmore"]').each(function(i) {
		$(this).parent().attr("id", "readmore_" + i);
		$(this).click(function() {
			$('#morearea_' + i).css('display', 'block');
			$('#readmore_' + i).css('display', 'none');

			var cookie_value = $.cookie(cookie_name);

			if (cookie_value) {

				var array = cookie_value.split('_');
				var flg = false;

				for (var j = 0; j < array.length; j++) {
					if (array[j] == i) {
						flg = true;
					}
				}

				if (!flg) {
					array.push(i);
					$.cookie(cookie_name, array.join('_'), {expires: 365, path: '/'});
				}

			} else {
				$.cookie(cookie_name, i, {expires: 365, path: '/'});
			}

			return false;
		});
	});
	$('a').not('[href!="#closemore"]').each(function(i) {
		$(this).click(function() {
			$('#morearea_' + i).css('display', 'none');
			$('#readmore_' + i).css('display', 'block');

			var cookie_value = $.cookie(cookie_name);

			if (cookie_value) {
				var array = cookie_value.split('_');
				var tmp_array = new Array();

				for (var j = 0; j < array.length; j++) {
					if (array[j] != i) {
						tmp_array.push(array[j]);
					}
				}

				$.cookie(cookie_name, tmp_array.join('_'), {expires: 365, path: '/'});

			}

			return false;
		});
	});

	$(".morearea").attr("id", function(i) {
		return "morearea_" + i;
	})
	.each(function(i){
		$('#morearea_' + i).css('display', 'none');
	});

	if($.cookie(cookie_name)) {
		if($.cookie(cookie_name) != -1) {
			var cookie_value = $.cookie(cookie_name);
			var array = cookie_value.split('_');

			for (var j = 0; j < array.length; j++) {
				$('#morearea_' + array[j]).css('display', 'block');
				$('#readmore_' + array[j]).css('display', 'none');
			}
		}
	}

});


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
