// 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 rounded_corner = function(elem) {
	//create 4 corner elements
	var myTopLeft  = new Element('div');
	myTopLeft.setStyles({
		'margin' : '10px 0 0 0', 
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/top_left_dept.jpg) no-repeat top left'
	});
	var myTopRight = new Element('div');
	myTopRight.setStyles({
		'margin' : '0', 
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/top_right_dept.jpg) no-repeat top right'
	});
	var myBottomLeft = new Element('div');
	myBottomLeft.setStyles({
		'margin' : '0', 
		'background' : 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/bottom_left_dept.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_dept.jpg) no-repeat bottom right',
		'padding' : '10px 15px 20px 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);
};

var rounded_corner_bg = function(elem) {
	//create 4 corner elements
	var myTopLeft  = new Element('div');
	myTopLeft.setStyles({
		'margin' : '0', 
		'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' : '0', 
		'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' : '0', 
		'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' : '5px 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);
};

var scroll_bar = function(content, control_up, control_down) {
	var scroll = new Fx.Scroll(content, {
		wait: false,
		duration: 4000,
		offset: {'x': 0, 'y': 0},
		wheelStops: true,
		transition: Fx.Transitions.Quad.easeInOut
	});
	 
	$(control_up).addEvent('mouseover', function(event) {
		event = new Event(event).stop();
		scroll.toTop();
	});
	
	$(control_up).addEvent('mouseleave', function(event) {
		event = new Event(event).stop();
		scroll.pause();
	});
	 
	$(control_down).addEvent('mouseover', function(event) {
		event = new Event(event).stop();
		scroll.toBottom();
	});
	
	$(control_down).addEvent('mouseleave', function(event) {
		event = new Event(event).stop();
		scroll.pause();
	});	
}

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);
};

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();
	
	/* ======== links with round corner ======== */
	$$('.links_roundcorner_bg').each(function(item){
		rounded_corner(item);
	});	
	
	/*	======== rounded corner with background ======== */
	$$('.quick_links_section').each(function(item) {
		rounded_corner_bg(item);
	});
	
	if($('quicklink2_content') != null) {
		rounded_corner_bg($('quicklink2_content'));
	}
	
	/* ======== 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(http://www.dental.umaryland.edu/dentalimages/template09/background_img/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(http://www.dental.umaryland.edu/dentalimages/template09/background_img/plus_short.jpg) no-repeat');
		}
	});
	
	/* ======== accordion main ======== */
	if($('dental_dept_name') != null){
		if($('dental_dept_name').get('html') == 'OIT') {
			var opened_accordion_main = 1;
		} else {
			var opened_accordion_main = 0;
		}
	}else {
		var opened_accordion_main = 0;
	}
	if($('accordion_main') != null) {
		var myAccordion = new Accordion($('accordion_main'), 'div.am_toggler_handle', 'div.am_toggler_content', {
			opacity: false,
			show: opened_accordion_main,
			onActive: function(toggler, element){
				// tab function
				if (($chk($(element).getChildren()[1]) != 0) && $chk($(element).id)) {
					$(element).getChildren().setStyle('display', 'none');
					$(element).getChildren()[0].setStyle('display', 'block');
					$(element).getChildren()[1].setStyle('display', 'block');			
					$(element).getElements('.tab_handle').each(function(item) {
						var content_name = $(item).id + '_content';		
						$(item).addEvent('click',show_content.bind($(content_name)));
					});
				}
				toggler.setStyle('background', 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/minus_long.jpg) no-repeat');
			},
			onBackground: function(toggler, element){
				toggler.setStyle('background', 'url(http://www.dental.umaryland.edu/dentalimages/template09/background_img/plus_long.jpg) no-repeat');
			}
		});
	}	
	
	/* ======== tooltip ======== */
	var customTips = $$('.tooltip');
	var toolTips = new Tips(customTips, {
		className: 'custom_tip'
	});

	
	/* ======== news bar ========	*/
	if($('news_headlines_wrapper') != null) scroll_bar($('news_headlines_wrapper'), $('news_up'), $('news_down'));
	
	/* ======== events bar ======== */
	if($('events_wrapper') != null) scroll_bar($('events_wrapper'), $('events_up'), $('events_down'));

		
	/* ======== slim gallery========
	credit to: slimbox - http://www.digitalia.be/software/slimbox
	======== slim gallery========*/
	if($('slimbox_gallery') != null) { 
		$('slimbox_gallery').addEvent('click',function(){
				Slimbox.open(imgs, 0);
		});	
	}
	
	/* ======== highlights ========
	credit to: noobSlide - http://www.efectorelativo.net/laboratory/noobSlide/
	======== highlights======== */
	
	var info = $('hl_box').getNext().set('opacity',0.6);
	var ns = new noobSlide({
		mode: 'vertical',
		box: $('hl_box'),
		items: sampleObjectItems,
		size: 200,
		handles: $$('#hl_handles div'),
		handle_event: 'click',
		fxOptions: {
			duration: 1000,
			wait: false
		},
		onWalk: function(currentItem,currentHandle){
			info.empty();
			new Element('h4').set('html','<a href="'+currentItem.link+'">link</a>'+currentItem.title + ' - ' + currentItem.description).inject(info);
			//new Element('p').set('html',''+ currentItem.description).inject(info);
			this.handles.set('opacity',0.6);
			currentHandle.set('opacity',1);
		}
		});
	ns.walk(0);
});


