﻿function SetCookie(name, value, expires, path, domain, secure) {
    var today = new Date();
    today.setTime(today.getTime());

    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
            ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
            ((path) ? ";path=" + path : "") +
            ((domain) ? ";domain=" + domain : "") +
            ((secure) ? ";secure" : "");
}
function GetCookie(check_name) {
    var a_all_cookies = document.cookie.split(';');
    var a_temp_cookie = '';
    var cookie_name = '';
    var cookie_value = '';
    var b_cookie_found = false; // set boolean t/f default f

    for (i = 0; i < a_all_cookies.length; i++) {
        a_temp_cookie = a_all_cookies[i].split('=');

        cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

        if (cookie_name == check_name) {
            b_cookie_found = true;
            if (a_temp_cookie.length > 1) {
                cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
            }
            return cookie_value;
            break;
        }
        a_temp_cookie = null;
        cookie_name = '';
    }
    if (!b_cookie_found) {
        return null;
    }
}
function ToggleState(cat, brand, toplist, cookieDomain) {
    var strValue = GetCookie('leftMenuState');
    var strOldValue = strValue;
    if (strValue == null) {
        strValue = '100';
    }
    if (cat != null) {
        strValue = cat + strValue.charAt(1) + strValue.charAt(2);
    }
    if (brand != null) {
        strValue = strValue.charAt(0) + brand + strValue.charAt(2);
    }
    if (toplist != null) {
        strValue = strValue.charAt(0) + strValue.charAt(1) + toplist;
    }

    SetCookie('leftMenuState', strValue, 30, '/', cookieDomain, '');
}
function MenuState(shop, used, imageUrl, cookieDomain) {
    var strArrow1 = imageUrl + '/images/leftlink_pil1.gif';
    var strArrow2 = imageUrl + '/images/leftlink_pil2.gif';
    window.addEvent('domready', function() {
        var strValue = GetCookie('leftMenuState');
        if (strValue == null) {
            strValue = '100';
        }
        var status = {
            'false': 'close',
            'true': 'open'
        };

        if (shop == true || used == true) {
            var divCat = $('divCat');
            var myVerticalSlide = new Fx.Slide(divCat, {
                mode: 'vertical',
                transition: 'cubic:out',
                onStart: function() {
                    var strValue = GetCookie('leftMenuState');
                    if (strValue == null) {
                        strValue = '100';
                    }
                    if ($('catArrow').src == strArrow2)
                        $('catArrow').src = strArrow1;
                    else
                        $('catArrow').src = strArrow2;
                    ToggleState((strValue.charAt(0) == '1' ? '0' : '1'), null, null, cookieDomain);
                }
            });
            var divBrand = $('divBrand');
            var myVerticalSlide2 = new Fx.Slide(divBrand, {
                mode: 'vertical',
                transition: 'cubic:out',
                onStart: function() {
                    var strValue = GetCookie('leftMenuState');
                    if (strValue == null) {
                        strValue = '100';
                    }
                    if ($('brandArrow').src == strArrow2)
                        $('brandArrow').src = strArrow1;
                    else
                        $('brandArrow').src = strArrow2;
                    ToggleState(null, (strValue.charAt(1) == '1' ? '0' : '1'), null, cookieDomain);
                }
            });

            if (strValue.charAt(0) == '1') {
                divCat.style.display = 'block';
                myVerticalSlide.show();
                document.getElementById('catArrow').src = strArrow2;
            }
            else {
                divCat.style.display = 'block';
                myVerticalSlide.hide();
                document.getElementById('catArrow').src = strArrow1;
            }

            if (strValue.charAt(1) == '1') {
                divBrand.style.display = 'block';
                myVerticalSlide2.show();
                document.getElementById('brandArrow').src = strArrow2;
            }
            else {
                divBrand.style.display = 'block';
                myVerticalSlide2.hide();
                document.getElementById('brandArrow').src = strArrow1;
            }

            $('v_toggleCat').addEvent('click', function(e) {
                e.stop();
                myVerticalSlide.toggle();
            });

            $('v_toggleBrand').addEvent('click', function(e) {
                e.stop();
                myVerticalSlide2.toggle();
            });
        }

        if (shop == true) {
            var divToplist = $('divToplist');
            var myVerticalSlide3 = new Fx.Slide(divToplist, {
                mode: 'vertical',
                transition: 'cubic:out',
                onStart: function() {
                    var strValue = GetCookie('leftMenuState');
                    if (strValue == null) {
                        strValue = '100';
                    }
                    if ($('toplistArrow').src == strArrow2)
                        $('toplistArrow').src = strArrow1;
                    else
                        $('toplistArrow').src = strArrow2;
                    ToggleState(null, null, (strValue.charAt(2) == '1' ? '0' : '1'), cookieDomain);
                }
            });

            if (strValue.charAt(2) == '1') {
                divToplist.style.display = 'block';
                myVerticalSlide3.show();
                document.getElementById('toplistArrow').src = strArrow2;
            }
            else {
                divToplist.style.display = 'block';
                myVerticalSlide3.hide();
                document.getElementById('toplistArrow').src = strArrow1;
            }

            $('v_toggleToplist').addEvent('click', function(e) {
                e.stop();
                myVerticalSlide3.toggle();
            });
        }
    });
}