//crasist menu.js

//*-FOR DEBUG * JQUERY_COOKIE---------------------------------------------------*//

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;
    }
};

//*-/FOR DEBUG * JQUERY_COOKIE---------------------------------------------------*//

var target;
var writer = [];

function init()
{
	target = $('div#contLeft h5');
	target.each(function(){
		this.defSrc = $(this).children('a').children('img').attr('src');
	});
	
	var checker = checkCookie();
	
	if(checker == null)
	{
		target.each(function(){
			this.flag = true;
			setInitialState(this);
		});
	}
	else
	{
		var cnt = 0;
		target.each(function(){
			this.flag = eval(checker[cnt]);
			var curPath = stateViewSet(this.flag, this);
			$(this).children('a').children('img').attr('src', curPath);
			setInitialState(this);
			cnt++;
		});
	}
	
	target.each(function(){
		writer.push(this);
		$(this).click(handleAccordion);
	});
	
	$.cookie('state', getWriter(), { path:"/" });
}

function setInitialState($target)
{
	var executer = $($target).next().children('ul');
	if(!$target.flag)
	{
		executer.hide();
	} 
}

function getWriter()
{
	var arr = [];
	for(var i = 0; i < writer.length; i++)
	{
		//alert(writer[i].flag);
		arr.push(writer[i].flag);
	}
	
	return arr;
}

function checkCookie()
{
	var ck = $.cookie('state');
	
	if(ck != null)
	{
		return cookieToObject(ck);
	}
	else
	{
		return null;
	}
}

function cookieToObject($src)
{
	var info = $src.split(","); 
	return info;
}

function stateViewSet($flag, $scope)
{
	var addPhrase = '_f2';
	var imgPath = $($scope).children('a').children('img').attr('src');
	var type = imgPath.substring(imgPath.lastIndexOf('.'), imgPath.length);
	var filename = getFileName(imgPath);
	//var filename = imgPath.substring(0, imgPath.indexOf('.'));
	
	if(!$flag)
	{
		var overPath = filename + addPhrase + type;
		return overPath;
	}
	else
	{
		return $scope.defSrc;
	}
}

function getFileName($imgPath)
{
	var path = $imgPath;
	var position = path.indexOf("i");
	var fileName = path.substring(position, path.lastIndexOf('.'));
	var dir = path.substring(0, position);
	return dir + fileName;
}

function handleAccordion()
{
	var executer = $(this).next().children('ul');
	executer.slideToggle();
	this.flag = !this.flag;
	
	var curPath = stateViewSet(this.flag, this);
	$(this).children('a').children('img').attr('src', curPath);
	
	var writer = getWriter();
	$.cookie('state', writer, { path:"/" });
	
	return false;
}

$(document).ready(function(){
	
	init();
		
});