/**
 * @author avergara
 */

var closedSrc = "/graphics/buttons/plus.gif";
var openSrc = "/graphics/buttons/minus.gif";

$(function() {
	//$('div.tree div:has(div)').addClass('parent').prepend('<img class="expand" src="'+closedSrc+'">');
	//$('div.parent').prepend('<img class="expand" src="'+closedSrc+'">');
	//$('div.tree .parent img.expand').click(function() {
	var speed = (jQuery.browser.msie ? 0 : 600);
	var visible = true;
	var cookies = document.cookie.split(';');
	for (var i=0; i<cookies.length; i++)
	{
	    var c = cookies[i];
	    while (c.charAt(0)==' ') c = c.substring(1, c.length);
	    if (c.indexOf('sidebar=')==0)
	    {
	        visible = c.substring(8,c.length) != 'hidden';
	        break;
	    }
	}
//	$('#sc-collapsable')
//	    .mouseover(function(){
//            $(this).addClass('hover');
//        })
//        .mouseout(function(){
//            $(this).removeClass('hover');    
//        })
//        .mousemove(function(){
//            var sidebar = document.getElementById('mainDash');
//            if (sidebar)
//            {
//                if (sidebar.style.display == 'none')
//                {
//                    
//                }
//            }
//        });
	$('.toggleSidebar, #sc-collapsable').click(function() {
	    var button = this;
	    var func = function(){
	        visible = (visible ? false : true);
	        if (visible)
	        {
	            $(button)
	                .find('img').attr('src', openSrc)
	            .end()
	                .find('.text').text('hide sidebar');
	            document.cookie = 'sidebar=visible; path=/';
	        }
	        else
	        {
	            $(button)
	                .find('img').attr('src', closedSrc)
	            .end()
	                .find('.text').text('show sidebar');
	            document.cookie = 'sidebar=hidden; path=/';
	        }
	    };
	    if (speed)
	        $('#mainDash').toggle(speed, func);
	    else
	    {
	        $('#mainDash').toggle();
	        func();
	    }
	});
	$('div.parent img.expand').click(function() {
		$(this).parent().children('div').toggleClass('open');
		$(this).parent().toggleClass('expanded');
		if ($(this).parent().hasClass('expanded')) {
			this.src = openSrc;
		}
		else {
			this.src = closedSrc;
		}
		//return false;
	});
});
