/* ONLOAD */
jQuery(function(){
	/* VIDEO */
	jQuery('.video-call').each(function(){
		jQuery(this).attr('href','javascript:void(0)');
		jQuery(this).click(function(){
			var vid = jQuery(this).attr('id');
			var vname = jQuery(this).attr('title');
			youtube(vid,425,344);
			jQuery('#video-name').html(vname);
		});
	});
	
	/* DATE RICERCA ARTICOLI */
	Utils.fromDate = jQuery('#search-article-from'),
	Utils.toDate = jQuery('#search-article-to');
		
	jQuery('.search-article-date-select').find('select').change(function(){
		Utils.fromDate.val(Utils.dateValueBuilder('#from-date'));
		Utils.toDate.val(Utils.dateValueBuilder('#to-date'));
	});
	
	jQuery('#search-article-submit').click(function(e){
		e.preventDefault();
		
		Utils.checkForm(this.form);
	});
});

/* UTILITY */
var Utils = {
	fromDate: null,
	toDate: null,
	dateValueBuilder: function(container) {
		var dateValue = '';
		
		jQuery(container).find('select').each(function(i){
			var option = this.options[this.selectedIndex];
			if(option.value != '') dateValue += this.options[this.selectedIndex].value + (i != 2 ? '/' : '');
		});
		
		return dateValue;
	},
	checkForm: function(form) {
		var utils = this,
			fromSelectDate = parseInt(utils.revertString(utils.fromDate.val(), '/')),
			toSelectDate = parseInt(utils.revertString(utils.toDate.val(), '/'));
		
		window.console.log(fromSelectDate);
		window.console.log(toSelectDate);
		
		if(fromSelectDate > toSelectDate) {
			alert('Attenzione: la data di partenza deve essere inferiore a quella di arrivo');
		} else {
			form.submit();
		}
	},
	revertString: function(string, separator, defaultValue) {
		var defaultValue = defaultValue != null ? defaultValue : '0',
			string = string != '' ? string : defaultValue,
			stringArray = string.split(separator),
			totalElement = stringArray.length,
			newString = [];
		
		for(var i = (totalElement - 1); i >= 0; i--) {
			newString.push(stringArray[i]);
		}
		
		return newString.join('');
	}	
}

/* YOUTUBE */
function youtube(id,W,H) {
	var params = {
		allowScriptAcess: 'always',
		allowFullScreen: 'true',
		wmode: 'transparent'
	};
	var attributes = {
		id: 'youtube',
		name: 'youtube'
	};
	swfobject.embedSWF('http://www.youtube.com/v/'+id+'&fmt=22&showinfo=0&fs=1', 'youtube', W, H, '9.0.0', false, false, params, attributes);
}

/* APRI/CHIUDI */
function apri(m,n) {
	var node = document.getElementById(m);
	if (node) node.style.display = 'block';
		
	document.getElementById(n).style.color = '#ffd739';
}

function chiudi(m,n) {
	var node = document.getElementById(m);
	if (node) node.style.display = 'none';
		
	document.getElementById(n).style.color = '#fff';
}

jQuery(function(){

	if (jQuery('#scroller').length>0) {
		var fnews = jQuery('.news-flash:first');
		setInterval(function(){
			var nheight = fnews.height() + 28;
			var delta = jQuery('#scroller').position().top - nheight +"px";
			fnews = fnews.nextAll('.news-flash:first');
			if (fnews.length<1) {
				fnews = jQuery('.news-flash:first');
				jQuery('#scroller').css("top",0);
			} else {
				selectNews(delta);
			}
		}, 5000);
		
		function selectNews(delta){
			jQuery('#scroller').animate({
				top: delta
			}, 1000);
		}
	}
	
	jQuery('.add-journal').click(function() {
		var idarticle= jQuery(this).attr("id").split('-')[1];
		if (idarticle) {
			jQuery.getJSON('/acm-on-line/anas/addArticle.jsp?id='+idarticle, function(data) {
				jQuery('#msg-journal').html(data.status);
			});
		}
	});
	
	jQuery('.journal-remove').click(function() {
		var idarticle= jQuery(this).parent().attr("id").split('-')[1];
		if (idarticle) {
			jQuery.getJSON('/acm-on-line/anas/addArticle.jsp?action=r&id='+idarticle, function(data) {
				jQuery('#obj-'+idarticle).remove();
				if (jQuery('.journal-remove').length==0) {
					jQuery('#no-article').html('Nessun articolo selezionato');
					jQuery('#journal-pdf').hide();
				}
			});
		}
	});
	
	if (jQuery('#journal').length>0) {
		jQuery( "#journal" ).sortable({
			stop: function(event, ui) { 
				var previd = ui.item.prevAll("li").attr("id");
				var nextid = ui.item.nextAll("li").attr("id");
			
				if (previd) {
					qs = "?action=m&cid="+ui.item.attr("id").split('-')[1]+"&pid="+previd.split('-')[1];
					jQuery.getJSON('/acm-on-line/anas/addArticle.jsp'+qs, function(data) {
						return true;
					});
				} else if (nextid) {
					qs = "?action=m&cid="+ui.item.attr("id").split('-')[1]+"&pid=0";
					jQuery.getJSON('/acm-on-line/anas/addArticle.jsp'+qs, function(data) {
						return true;
					});
				}
			}
			},{ placeholder: 'ui-state-highlight' });
	}
});
