var addthis_pub				= 'International University College'; 
var addthis_logo			= 'http://test3.pixeldepo.com/img/logo-addthis.jpg';
var addthis_logo_background	= 'ffffff';
var addthis_logo_color		= '201910';
var addthis_brand			= 'IUC';
var addthis_options			= 'favorites, email, delicious, google, digg, reddit, facebook, live, more';

CD3.Behaviors.assign({
	'input[type=text],textarea': CD3.Behaviors.TitleAsDefaultValue,
	'#nav-tools': function(){
		new CD3.FontSwitcher(this.down('.text-resize').show(), 'content', {
			plus:	'.bigger',
			minus:	'.smaller'
		});
	},
	'#nav-common li': {
		mouseenter: function(){
			var ul = this.down('ul');
			
			ul && ul.show();
		},
		mouseleave: function(){
			var ul = this.down('ul');
			
			ul && ul.hide();
		}
	},
	'#nav-tools2': function(){
		this.show();
	},
	'#nav-tools2 .nav-tools-share': {
		mouseover: function(){
			addthis_open(this, '', location.href, 'International University College')
		},
		mouseout: addthis_close,
		click: addthis_sendto
	},
	'#nav-tools2 .nav-tools-print:click': function(e){
		e.stop();
		print();
	},
	'select[multiple=multiple]': CD3.MultiSelect,
	'#polls_list:click': {
		'dt': function(e){
			var visible = this.next('dd').visible();
			this.up('dl').select('dd').invoke('blind' + (visible ? 'Up' : 'Down'), {duration: .5});
		} 
	},
	'#poll_form:submit': function(e){
		e.stop();
		this.down('input[type=submit]').writeAttribute('value', ' ').setStyle({
			border:		'0px',
			background:	'transparent url(/img/preloader1.gif) no-repeat scroll'
		});
		
		this.request({
			onSuccess: function(t){
				try {
					var response = t.responseText.evalJSON(true);
					for(var aid in response){
						var percent = response[aid], 
							answer  = $('poll_answer_' + aid);
						
						answer.down('.bar span').morph({width: Math.round(percent) + 'px'});
						answer.down('.percent').innerHTML = percent + '%';
						
						$('poll_answer_' + aid + '_radio').removeWithEffect('fade', {duration: .3});
					}
					$('poll_form').down('input[type=submit]').removeWithEffect('blind-up', {duration: .3});
					$('poll_form').stopObserving('submit').observe('submit', function(e){ e.stop(); });
				} catch(e){
					$('poll_form').submit();
				}
			}
		});
	}
});

CD3.Behaviors.assignIf('#gallery-pics', function(div){
	var picSpan = div.down('span.gallery-pic');
	
	if (!picSpan) return {};
	
	var images  = picSpan.getAttribute('images').split(' ').compact(),
		current = 0;
		
	function showImage(src){
		picSpan.innerHTML = '<span style="background-image: url(/img/gallery_loading.gif)"></span>';
			CD3.ImageLoader(src, function(){
			picSpan.innerHTML = '<span style="background-image: url(' + src + ')"></span>';
		});
	};
	
	function nextImage(dir, e){
		e.stop();
		
		current += dir;
		
		if (current >= images.length)	current = 0;
		else if (current < 0)			current = images.length -1;
		
		$('current_pic').innerHTML = current + 1;
		showImage(images[current]);
	};
	
	div.down('ul.pics').show();
	return {
		'ul.pics:click': {
			'.prev': nextImage.curry(-1),
			'.next': nextImage.curry(1)
		}
	};
});
CD3.Behaviors.assignIf('#gallery-box', function(box){
	var list = box.down('ul.paging');
	
	if (!list) return {};
	
	var items	= list.show().select('li.image a'),
		current	= 0;
	
	function markSelected(){
		items.invoke('removeClassName', 'selected');
		items[current].addClassName('selected');
	}
	
	function showSelected(callback){
		var src = items[current].getAttribute('src');

		CD3.ImageLoader(src, function(){
			if (callback) callback();
			
			$('gallery_image').innerHTML = '<img src="' + src + '" />';
			
			timeout.reset();
		});
	}
	
	function changeSelectedWith(dir){
		timeout.stop();

		current = current + dir;
		
		if (current >= items.length){
			current = 0;
		} else if (current < 0){
			current = items.length -1;
		}
	}
	
	function nextImage(direction){
		changeSelectedWith(direction);
		markSelected();
		showSelected();
	}
	
	var timeout = new PeriodicalExecuter(function(){
		changeSelectedWith(1);
		showSelected(markSelected);
		markSelected();
	}, 4);
	
	timeout.reset = function(){
		this.stop();
		this.registerCallback();
	};

	return {
		'ul.paging:click': {
			'.prev':  	function(e){ e.stop(); nextImage(-1); },
			'.next':  	function(e){ e.stop(); nextImage( 1); },
			'.image a': function(e){ e.stop();
				current = (parseInt(this.innerHTML) || 1) - 1;
				markSelected();
				showSelected();
			}
		}
	};
});
