function getXmlHttpRequestObject(){
    var a = null;
    if (window.XMLHttpRequest) 
        try {
            a = new XMLHttpRequest
        } 
        catch (c) {
        }
    else 
        if (window.ActiveXObject) 
            for (var d = 0, b; b = ["MSXML2.XMLHTTP.6.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP", "Microsoft.XMLHTTP"][d++];) 
                try {
                    a = new ActiveXObject(b);
                    break
                } 
                catch (c) {
                }
    return a
}

var searchReq = getXmlHttpRequestObject();
function searchSuggest(){
    if (searchReq.readyState == 4 || searchReq.readyState == 0) {
        var str = escape(document.getElementById('search').value);
        searchReq.open("GET", '/store/sgsearch/searchSG.php?search=' + str, true);
        searchReq.onreadystatechange = handleSearchSuggest;
        searchReq.send(null);
    }
}

function handleSearchSuggest(){
    if (searchReq.readyState == 4) {
        var ss = document.getElementById('search_suggest')
        ss.innerHTML = '';
        var str = searchReq.responseText.split("\n");
        for (i = 0; i < str.length - 1; i++) {
            //Build our element string.  This is cleaner using the DOM, but
            // IE doesn't support dynamically added attributes.
            var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
            suggest += 'onmouseout="javascript:suggestOut(this);" ';
            suggest += 'onclick="javascript:setSearch(this.innerHTML);" ';
            suggest += 'class="suggest_link">' + str[i] + '</div>';
            ss.innerHTML += suggest;
        }
    }
}

function suggestOver(div_value){
    div_value.className = 'suggest_link_over';
}

function suggestOut(div_value){
    div_value.className = 'suggest_link';
}

function setSearch(value){
    document.getElementById('search').value = value;
    document.getElementById('search_suggest').innerHTML = '';
}

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;
    }
};

var auroraMenuSpeed = 150;
$(document).ready(function(){
	$.cookie('testcookie' , 'expanded')
	var auroramenucount = 0;
	$('.auroramenu').each(function(){
		var auroramenuitemcount = 0;
		$(this).children('li').children('ul').each(function(){
			if($.cookie('arMenu_' + auroramenucount + '_arItem_' + auroramenuitemcount) == 1){
				$(this).siblings('a').attr('onClick' , 'auroraMenuItem(\'' + auroramenucount + '\' , \'' + auroramenuitemcount + '\' , \'0\'); return false;');
				$(this).parent().children('.aurorahide').css("display","inline");
				$(this).parent().children('.aurorashow').css("display","none");
			} else {
				$(this).css("display","none");
				$(this).siblings('a').attr('onClick' , 'auroraMenuItem(\'' + auroramenucount + '\' , \'' + auroramenuitemcount + '\' , \'1\'); return false;');
				$(this).parent().children('.aurorahide').css("display","none");
				$(this).parent().children('.aurorashow').css("display","inline");
			}
			auroramenuitemcount ++;
		});
		auroramenucount ++;
	});
});
function auroraMenuItem(menu , item , show){
    $.cookie('arMenu_' + menu + '_arItem_' + item , show);
	var auroramenucount = 0;
	$('.auroramenu').each(function(){
		if(menu == auroramenucount){	
			var auroramenuitemcount = 0;
			$(this).children('li').children('ul').each(function(){
				if(item == auroramenuitemcount){
					if(show == 1){
						$(this).slideDown(auroraMenuSpeed);
						//$(this).click(auroraMenuItemHide(menu , item));
						$(this).siblings('a').attr('onClick' , 'auroraMenuItem(\'' + menu + '\' , \'' + item + '\' , \'0\'); return false;');
						$(this).parent().children('.aurorahide').css("display","inline");
						$(this).parent().children('.aurorashow').css("display","none");
					} else {
						$(this).slideUp(auroraMenuSpeed);
						$(this).siblings('a').attr('onClick' , 'auroraMenuItem(\'' + menu + '\' , \'' + item + '\' , \'1\'); return false;');
						$(this).parent().children('.aurorahide').css("display","none");
						$(this).parent().children('.aurorashow').css("display","inline");
					}
				}
				auroramenuitemcount ++;
			});
		}
		auroramenucount ++;
	});
}
