// JavaScript Document
var slidein_menu = function() {
	var mySlidein = new Fx.Slide('slidein_content').hide();
	
	$('c_toggle').addEvent('click', function(e){
		var content_status = $('slidein_content').getStyle('display');
		if (content_status == 'none') $('slidein_content').setStyle('display', 'block');
		e.stop();
		mySlidein.toggle();
	});
	
	//update the menu control - when menu is opened, show "close". when
	mySlidein.addEvent('complete', function() {
		if (mySlidein.open) {
			$('c_toggle').set('html', 'Close Menu');
		} else {
			$('c_toggle').set('html', 'Open Menu');
		}
	});	
};


var show_content = function () {
	this.getParent().getElements('.hidden').setStyle('display', 'none');
	this.setStyle('display', 'block');
}

//add shadow to the images
var show_img_shadow = function (elem) {
	//create 4 corner elements
	var myTopLeft_shadow  = new Element('div');
		myTopLeft_shadow.setStyles({
		'margin' : '0px',
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/img_bg_topleft.gif) no-repeat top left',
		'width': elem.width + 25,
		'height': elem.height + 25
	});
	var myTopRight_shadow = new Element('div');
	myTopRight_shadow.setStyles({
		'margin' : '0px',
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/img_bg_topright.gif) no-repeat top right'
	});
	var myBottomLeft_shadow = new Element('div');
	myBottomLeft_shadow.setStyles({
		'margin' : '0px',
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/img_bg_bottomleft.gif) no-repeat bottom left'
	});
	var myBottomRight_shadow = new Element('div');
	myBottomRight_shadow.setStyles({
		'margin' : '0px',
		'padding' : '10px 15px 15px 10px',
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/img_bg_bottomright.gif) no-repeat bottom right'
	});
	
	myBottomRight_shadow.wraps(elem);
	myBottomLeft_shadow.wraps(myBottomRight_shadow);
	myTopRight_shadow.wraps(myBottomLeft_shadow);
	myTopLeft_shadow.wraps(myTopRight_shadow);
};

//get the height of window
var get_window_height = function() {
  var windowHeight = 0;
	
  if (typeof(window.innerHeight) == 'number')
    windowHeight = window.innerHeight;
	
  else {
		
    if (document.documentElement && document.documentElement.clientHeight)
      windowHeight = document.documentElement.clientHeight;
		
    else {
      if (document.body && document.body.clientHeight)
        windowHeight = document.body.clientHeight; }; };
				
  return windowHeight;
};

//get the height of main_content
var get_content_height = function() {
	var content_height = $('main_content').getSize().y;
	
	return content_height;
}


//adjust the content height if needed
var adjust_height = function () {
	var main_content_height = get_content_height();
	var window_height = get_window_height();
	
	var real_height = 20 + 90 + 30 + 31 + main_content_height + 180 + 20;
	
	if (real_height < window_height) {
		var new_height = window_height - 20 - 90 - 30 - 31 - 180 - 20;
		var new_height_px = new_height + 'px';
		$('main_content').setStyle('height', new_height_px);
	}
}

//round corner with background color
var rounded_corner_bg = function(elem) {
	//create 4 corner elements
	var myTopLeft  = new Element('div');
	myTopLeft.setStyles({
		'margin': '0px', 
		'background': 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/top_left_bg.jpg) no-repeat top left', 
		'margin-top': '10px'
	});
	var myTopRight = new Element('div');
	myTopRight.setStyles({
		'margin': '0px',	
		'background': 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/top_right_bg.jpg) no-repeat top right'
	});
	var myBottomLeft = new Element('div');
	myBottomLeft.setStyles({
		'margin': '0px', 
		'background': 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/bottom_left_bg.jpg) no-repeat bottom left'
	});
	var myBottomRight = new Element('div');
	myBottomRight.setStyles({
		'margin': '0', 
		'background': 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/bottom_right_bg.jpg) no-repeat bottom right', 
		'padding' : '15px'
	});
	
	//copy element content
	var content_copy = elem.get('html');
	
	elem.empty();
	elem.grab(myTopLeft);
	myTopLeft.grab(myTopRight);
	myTopRight.grab(myBottomLeft);
	myBottomLeft.grab(myBottomRight);
	myBottomRight.set('html',content_copy);
};

// fadein/fadeout information
var fadein_info = function() {
	var menuStatus = 0;
	var menuOpen = 'fadein_menu0';
						
	//click to show
	$('fadein_menu').getElements('.fadein_handle').each(function(item){
		$(item).addEvent('click', function() {
		//alert(menuStatus);
		//alert(menuOpen);								
	
		var handle_x = $(item).getPosition().x;
		var handle_y = $(item).getPosition().y;
								
		var window_x = window.getSize().x;
		var window_y = window.getSize().y;
								
		//alert(handle_x);
		//alert(handle_y);
								
		//alert(window_x);
		//alert(window_y);
								
		if (handle_x > window_x/2 + 20) {
			var temp_x = handle_x - 470;
			var temp_y = handle_y + 25;
		}else{
			var temp_x = handle_x;
			var temp_y = handle_y + 25;
		}
							
		//hide using opacity on page load
		$$('.sub_fadein_menu').setStyles({
			'opacity': '0',
			'display': 'block',
			'top': temp_y + 'px',
			'left': temp_x + 'px'
		});
																
		if (menuStatus == 0) {
			var sub_menu_id = 'sub_' + item.id;								
			$(sub_menu_id).fade('in');				
			menuStatus = 1;
			
			//alert(item.id);
								
			menuOpen = item.id;
		}else{
			var need_close = 'sub_' + menuOpen;
			$(need_close).fade('out');
			menuStatus = 0;
									
			if (item.id != menuOpen) {
				var need_open = 'sub_' + item.id;
				$(need_open).fade('in');
				menuStatus = 1;
				menuOpen = item.id;
			}  else {
				menuOpen = 'fadein_menu0';
			}
		}
		});
	});
						
	//hiders
	$('fadein_menu').getElements('.close_fadein_menu').each(function(item){
		$(item).addEvent('click', function() {
			var closeElement = item.id.replace('close', 'sub');			
			//var closeElement = this.getParent().id;				
			$(closeElement).fade('out');
						
			menuStatus = 0;
			menuOpen = 'fadein_menu0';
		});			
	});
};

var show_tab_content = function() {
	$$('.tab_handle').each(function(item, index) {
		if(index == 0) {
			var initial_content = item.id + '_content';
			$(initial_content).setStyle('display', 'block');
		};
		
		$(item).addEvent('click', function() { 
			$$('.tab_handle').each(function(item) {
				var hidden_content = item.id + '_content';
				$(hidden_content).setStyle('display', 'none');
			});
			var show_content = this.id + '_content';
			$(show_content).setStyle('display', 'block');
		});
	})
}

window.addEvent('domready', function() {
	var opened_accordion = 0;
	var curent_site_name = $('dental_dept_name').get('html');
		
	if (Cookie.read('dental_site_name') != null && Cookie.read('opened_accordion_index') != null && Cookie.read('dental_site_name') == curent_site_name) {
		var opened_accordion = Cookie.read('opened_accordion_index');
	}

	/*	======== slidein menu ========	*/
	slidein_menu();
	
	/* ======== round corner with bg ======== */
	$$('.menu_item').each(function(item) {
		rounded_corner_bg($(item).getFirst('div'));
	});
	
	/* ======== fadein info ======== */
	if($('fadein_menu') != null) {
		fadein_info();
	}

	
	/* ======== put shadow on the imgs ======== */
	$$('.img_with_shadow').each(function(item){
		show_img_shadow(item);
	});
	
	/* ======== accordion links ======== */
	var myAccordion = new Accordion($('accordion_nav'), 'div.an_toggler_handle', 'div.an_toggler_content', {
		opacity: false,
		display: opened_accordion,
		onActive: function(toggler, element){
			toggler.setStyle('background', 'url(/bin/x/c/minus_short.jpg) no-repeat');
			var opened_index = $$('.an_toggler_handle').indexOf(toggler);
			var dental_site = $('dental_dept_name').get('html');
			var myCookie = Cookie.write('dental_site_name',dental_site);
			var myCookie = Cookie.write('opened_accordion_index',opened_index);
		},
		onBackground: function(toggler, element){
			toggler.setStyle('background', 'url(/bin/t/w/plus_short.jpg) no-repeat');
		}
	});
	
	if($('accordion_part') != null) {
		var myAccordionPart = new Accordion($('accordion_part'), 'div.accordion_part_handle', 'div.accordion_part_content', {
			opacity: false,
			display: -1,
			onActive: function(toggler, element){
				toggler.setStyle('background', 'url(/bin/r/b/opened.jpg) no-repeat');						
			},
			onBackground: function(toggler, element){
				toggler.setStyle('background', 'url(/bin/v/n/closed.jpg) no-repeat');
			}
		});
	};
	
	show_tab_content();
	
	/* ======== adjust content height ======== */
	adjust_height();
	
	/* ======== tooltip ======== */
	var customTips = $$('.tooltip');
	var toolTips = new Tips(customTips, {
		className: 'custom_tip'
	});

});


