$(function(){	
	News.setup();
	
	/* FAQS */
	$('a.toggle').click(function(e){
		e.preventDefault();
		$(this).next().slideToggle();
	});
	
	Centers.setup();
	Comments.setup();
	
	// Match all <A/> links with a title tag and use it as the content (default).
	$('a.tip').qtip();
	
	/* Home rotator */
	$('#header .collage').cycle({ 
	    fx:     'fade', 
	    speed:  1500, 
	    timeout: 10000,
	 	next: '.pager .next',
		prev: '.pager .prev'
	});
	
	/* footer select pages menu */
	/*
	$('form#footerPages select').change(function(e) {
		if ($(this).val()) {
			if ($(this).val()=='preguntas_frecuentes') {
				window.location = APP.baseurl + $(this).val();
			} else {
				window.location = APP.baseurl + 'white_pages/view/' + $(this).val();
			}
		}		
	});
	*/
	
	
	function show() {
		var menu = $(this);
		menu.children(".actions").slideDown();
	}
	function hide() { 
		var menu = $(this);
		menu.children(".actions").slideUp();
	}
	$(".hover-menu").hoverIntent({
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
		interval: 50,   // number = milliseconds for onMouseOver polling interval
		over: show,     // function = onMouseOver callback (required)
		timeout: 500,   // number = milliseconds delay before onMouseOut
		out: hide       // function = onMouseOut callback (required)
	});
	
	// Compartir Beca
	$('a.btnShareBeca').click(function(e) {
		e.preventDefault();			
		ShareItem.openForm('scholarships', $(this).attr('data-id'));
	});
});

var Comments = {
	setup:function() {
		/* Form de agregar nuevo comment */
		$('div#comments form').live('submit', function(e) {
			e.preventDefault();
			var options = { 
				type: 'POST',
		        target: 'div#comments', // target element(s) to be updated with server response
		        success: function() {
		        	/* hide preloader */
		        	$('div#comments div.preloader').hide();
		        }
		    }; 		
			$('div#comments form').ajaxSubmit(options);
			/* show preloader */
			$('div#comments div.preloader').show();
		});
		/* boton de eliminar un comment en particular */
		$('div#comments div.row div.actions a').live('click', function(e){
			e.preventDefault();
			var el = $(this);
			if (confirm('¿Estas seguro?')) {
				$.ajax({
					url:APP.baseurl+'admin/comments/delete/'+$(this).attr('data-id')+'/model:'+$(this).attr('data-model')+'/pk:'+$(this).attr('data-pk'),
					success:function(data) {
						if (data == '1') {
							el.parent().parent().remove();
						} else {
							alert('Ha ocurrido un error, no se ha podido eliminar el comentario.');
						}
					}
				});
			}			
		});
	}	
};

var Centers = {
	setup:function() {	
		/* Compartir con un amig@ */
		$('a.btnShareCenter').click(function(e) {
			e.preventDefault();			
			ShareItem.openForm('centers', $(this).attr('data-id'));
		});
	}	
};

var News = {
	setup:function() {
		/* SELECT de meses (autojump) */
		/*
		$('select#NewsMonth').change(function(e) {
			var url = ($(this).val()) ? APP.baseurl + 'news/index/month:' + $(this).val() : APP.baseurl + 'news/';
			document.location.href = url;
		});
		*/
	
		/* Compartir con un amig@ */
		$('a.btnShare').click(function(e) {
			e.preventDefault();			
			ShareItem.openForm('news', $(this).attr('data-id'));
		});
	}	
};

var ShareItem = {
	openForm: function(controller, item_id) {
		var url = APP.baseurl + controller + '/share/' + item_id;
		//alert(url);
		//return false;
		$('div#popup').load(
			url,
			{},
			function(){				
				/* UI dialog */
				$('div#popup').dialog({
					'modal':true,
					'minWidth':500,
					'minHeight':500,
					'title':'Compartir con un amig@',
					'buttons':{
						'Enviar': function() { ShareItem.submit(); }
					}
				});
			}
		);
	},
	submit: function() {
		var options = { 
			type: 'POST',
	        target: 'div#popup' // target element(s) to be updated with server response 
	    }; 		
		$('div#popup form').ajaxSubmit(options);
	}
};

var Tiny = {
	setup:function(){
		if (jQuery().tinymce) {
			/* plugin loaded */
			$('textarea.tinymce').tinymce({
				// Location of TinyMCE script
				script_url : APP.baseurl+'js/tinymce/jscripts/tiny_mce/tiny_mce.js',
				language: 'es',
				content_css : APP.baseurl+'css/public.css',
				
				/* Seteo el root para que las rutas sean absolutas y se vean bien en el site publico */
				/*document_base_url : "http://"+APP.domain+"/",*/
				relative_urls : false,
        		remove_script_host : false,
	
				// General options
				theme : "advanced",
				plugins : "pagebreak,style,layer,table,save,advhr,advimage,advlink,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist,imagemanager",
				
				// Theme options
				theme_advanced_buttons1 : "formatselect, bold, italic, underline, strikethrough, separator, justifyleft, justifycenter, justifyright, justifyfull, |, bullist, numlist, indent",
				theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,link,unlink,anchor,cleanup,code,image",
				theme_advanced_buttons3 :"",
				theme_advanced_buttons4 : "",
				theme_advanced_toolbar_location : "top",
				theme_advanced_toolbar_align : "left",
				theme_advanced_resizing : true
			});
		}
	}
};
