var $j = jQuery.noConflict();

$j(document).ready(function(){
	$j('#actionBarToggleLink').bind('click', function(){
		//since it is the action bar being clicked, it is collapsing
		var exDate = new Date();
		exDate.setDate(exDate.getDate() + 7);
		document.cookie = 'actionBarDisplay=none;expires=' + exDate.toUTCString();
		document.cookie = 'actionBarToolsDisplay=block;expires=' + exDate.toUTCString();
		//figure out the states
		$j('#actionBar').slideToggle(500);
		$j('#actionBarTools').slideToggle(500);
	});

	$j('#actionBarTools').bind('click', function(){
		//since it is the tools being clicked, the action bar is expanding
		var exDate = new Date();
                exDate.setDate(exDate.getDate() + 7);
		document.cookie = 'actionBarDisplay=block;expires=' + exDate.toUTCString();
                document.cookie = 'actionBarToolsDisplay=none;expires=' + exDate.toUTCString();
		$j('#actionBarTools').slideToggle(500);
		$j('#actionBar').slideToggle(500);
	});

	$j('.actionBarLabelIcon').bind('click', function(){
		var targetId = $j(this).parents('div').children('div.actionBarPanel').attr('id');
		$j('.actionBarPanel').each(function(){
			if($j(this).css('display') == 'block' && $j(this).attr('id') != targetId){
				$j(this).slideToggle('slow');
			}
		});
		//loop over the images to set the state
		$j('.actionBarLabelIcon').each(function(){
			var img = $j(this).children('img').attr('src');
			if(img.substring(img.length - 10, img.length-4) == '_hover'){
				img = img.substring(0, img.length - 10) + '.png';
		                $j(this).children('img').attr('src', img);
			}
		});

		var img = $j(this).children('img').attr('src');

		//see if this is a close or a expand
		if(img.substring(img.length - 10, img.length-4) == '_hover'){	
			//it is already hovered so remove it
			img = img.substring(0, img.length - 10) + '.png';
                        $j(this).children('img').attr('src', img);
		}else{
			//expanding
			img = img.substring(0, img.length - 4) + '_hover.png';
	                $j(this).children('img').attr('src', img);

		}
		//slide up the panel
		$j(this).parents('div').children('div.actionBarPanel').slideToggle('slow');
		
		//hide the label popup
		$j(this).parents('div').children('div.actionBarLabel').css('display', 'none');
	});

	$j('.actionBarLabelIcon').bind('mouseover', function(){
		//show the popup label
		$j(this).parents('div').children('div.actionBarLabel').css('display', 'block');

		//now check to see what the state of the image is
		var img = $j(this).children('img').attr('src');
		if(img.substring(img.length - 10, img.length-4) != '_hover'){       
			img = img.substring(0, img.length - 4) + '_hover.png';
			$j(this).children('img').attr('src', img);
		}
	});
	
	$j('.actionBarLabelIcon').bind('mouseout', function(){
		//hide the popup
                $j(this).parents('div').children('div.actionBarLabel').css('display', 'none');

		//make sure the img isn't already cleared
		var img = $j(this).children('img').attr('src');
		if(img.substring(img.length-10, img.length-4) != "_hover"){
			return;
		}

		//now check if the panel has been shown
		if($j(this).parent('div').children('div.actionBarPanel').css('display') == 'none' || $j(this).parent('div').children('div.actionBarPanel').css('display') === undefined) {
        	        img = img.substring(0, img.length - 10) + '.png';
                	$j(this).children('img').attr('src', img);
		}
        });

	$j('.actionBarItemClose').bind('click', function(){
		//slide down the panel
		$j(this).parents('div.actionBarPanel').slideToggle('slow');
		//swap out the img
		var imgEl = $j(this).parent('div').parent('div').parent('div').parent('div').children('a').children('img');
		var img = imgEl.attr('src');
		img = img.substring(0, img.length - 10) + '.png';
                $j(imgEl).attr('src', img);
		
	});
		
	$j('.actionBarPanelUl').children('li').not('.actionBarLCPCategoryDisabled').bind('mouseover', function(){
		$j(this).css('background-image', 'url("/modules/ActionBar/css/img/panel_selection_bg.png")').css('background-repeat', 'repeat-x').css('color', 'white');
		$j(this).children('a').css('color', 'white');
		$j(this).children('div.actionBarLCPArrow').each(function(){
			$j(this).children('img').attr('src', '/modules/ActionBar/css/img/lcp_arrow_right_hover.png');
		});
	});
	$j('.actionBarPanelUl').children('li').not('.actionBarLCPCategoryDisabled').bind('mouseout', function(){
                $j(this).css('background-image', 'none').css('color', 'black');
		$j(this).children('a').css('color', 'black');
		$j(this).children('div.actionBarLCPArrow').each(function(){
                        $j(this).children('img').attr('src', '/modules/ActionBar/css/img/lcp_arrow_right.png');
                });
        });

	$j('.actionBarItemClose').bind('mouseover', function(){
		var img = $j(this).children('img').attr('src');
		img = img.substring(0, img.length -4) + '_hover.png';
		$j(this).children('img').attr('src', img);
	});

	$j('.actionBarItemClose').bind('mouseout', function(){
		var img = $j(this).children('img').attr('src');
		img = img.substring(0, img.length - 10) + '.png';
		$j(this).children('img').attr('src', img);
	});

	$j('.actionBarLabel').corner('5px');
});



