$().ready(function() {
    initSlideMenu('CategoriesSlideMenu', 'slow', true);

$('#curr_button').click(function(event){
$('ul#currencies li ul').slideToggle('slow');
event.preventDefault();
});

});


function initSlideMenu(id, speed, accordion) {
   mustBeOpenedTabs = ($.cookie('opened' + id)) ? $.cookie('opened' + id).split('=') : new Array();
   /*mustBeOpenedTabs.sort();
   if (mustBeOpenedTabs.length > 0) {
	   for (i=0; i < mustBeOpenedTabs.length; i=i+1) {
		   setTimeout('$("#' + id + mustBeOpenedTabs[i] + '").next().slideDown("' + speed + '")', 500 * i);
	   }
   }*/
   $('ul#' + id + ' > li:has(ul) a').click(
      function() {         
         alreadyOpenedTabs = ($.cookie('opened' + id)) ? $.cookie('opened' + id).split('=') : new Array();
         totalOpenedTabs   = (alreadyOpenedTabs.length > 0) ? alreadyOpenedTabs.length : 1;
         
         curElem = $('#' + $(this).attr('id'));
      	 realId = $(this).attr('id').substr(strlen(id));
     	  	 
         if (curElem.next().css('display') == 'none') {
        	 
        	 /*
        	  * Opening
        	  */
        	if (true == accordion) {
               $('ul#' + id + ' li ul:visible').slideUp(speed);
               accordionOpenedItem = realId;
        	} else {
               alreadyOpenedTabs[alreadyOpenedTabs.length] = realId;
        	}
         	$(this).next().slideDown(speed);
         	
         } else {
        	 
        	 /*
        	  * Closing
        	  */
        	if (true == accordion) {
        	   $(this).next().slideUp(speed);
        	   accordionOpenedItem = '';
        	} else {
               $(this).next().slideUp(speed);         
               for (i=0; i < totalOpenedTabs; i = i+1)
               {
                  if (alreadyOpenedTabs[i] == realId) {
                     delete alreadyOpenedTabs[i];
                  }
               }
            }
         }        	

         toCookie = '';
         if (true == accordion) {
             toCookie = accordionOpenedItem;
      	 } else {
            for (i=0; i < alreadyOpenedTabs.length; i=i+1)
            {
               if (typeof(alreadyOpenedTabs[i]) != 'undefined') {
                  toCookie = (toCookie != '') ? toCookie + '=' + alreadyOpenedTabs[i] : alreadyOpenedTabs[i]; 
               }
            }
      	 }
         $.cookie('opened' + id, toCookie, { path: '/', expires: 365 });
         return false;
      }
   );
   $('ul#' + id + ' li ul a').unbind();
}


jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') {
        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();
        }
        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 {
        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]);
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function strlen (string) {
    var str = string+'';
    var i = 0, chr = '', lgth = 0;

    var getWholeChar = function (str, i) {
        var code = str.charCodeAt(i);
        var next = '', prev = '';
        if (0xD800 <= code && code <= 0xDBFF) {
            if (str.length <= (i+1))  {
                throw 'High surrogate without following low surrogate';
            }
            next = str.charCodeAt(i+1);
            if (0xDC00 > next || next > 0xDFFF) {
                throw 'High surrogate without following low surrogate';
            }
            return str.charAt(i)+str.charAt(i+1);
        } else if (0xDC00 <= code && code <= 0xDFFF) {
            if (i === 0) {
                throw 'Low surrogate without preceding high surrogate';
            }
            prev = str.charCodeAt(i-1);
            if (0xD800 > prev || prev > 0xDBFF) {
                throw 'Low surrogate without preceding high surrogate';
            }
            return false;
        }
        return str.charAt(i);
    };

    for (i=0, lgth=0; i < str.length; i++) {
        if ((chr = getWholeChar(str, i)) === false) {
            continue;
        }
        lgth++;
    }
    return lgth;
}




