var aSection = [];
var sectionIndex = [];
var sectionTotal = [];

function noSel(){return false;}
function noSelect(o){
	$(o).mousedown(noSel).mouseup(noSel).mousemove(noSel).mouseout(noSel).mouseover(noSel);
}
function jPreloadImages(img,callback,i){
	if(i==null) i=0;
	if(i>=img.length){ if(callback!=null) callback(); return; }
	$('<img>').attr('src',img[i]).bind('load',function(){
		jPreloadImages(img,callback,i+1);
	});
}

function _load(type,section, div){
	var _url = "";
	var _content = "";
	switch(type){
		case "section":
			_url = "xml-section.php";
			break;
		case "page":
			_url = "xml-page.php";
			_content = "-page";
			break;
	}
	if( _url == "" ) return;
	
	$.ajax({
		type: "GET",
		dataType: "xml",
		url: wp_url + '/' +_url,
		data: "section="+section+"&numPost=5",
		complete: function(data) {
			aSection[div] = { json:null, index:0 };
			aSection[div].json = $.xmlToJSON(data.responseXML);
		
			if( aSection[div].json.item != null && aSection[div].json.item.length > 0 )
			{
				$('#'+div+' #s-title').html(aSection[div].json.item[0].title[0].Text );
				$('#'+div+' #s-content'+_content).html(aSection[div].json.item[0].content[0].Text);
				switch(type)
				{
					case 'section':
						$('#'+div+' #s-more a')
							.attr('href',aSection[div].json.item[0].link[0].Text)
							.css('visibility','visible');
						$('#'+div+' #s-pagebar .go-section a').attr('href',aSection[div].json.url);
						break;
					case 'page':
						$('#'+div+' #s-pagebar .go-section-page a')
							.attr('href',aSection[div].json.item[0].link[0].Text)
							.css('visibility','visible');
						break;
				}
				
				sectionTotal[div] = aSection[div].json.item.length;
				sectionIndex[div] = 0;
				sectionInit(div);
				
			}else{
				$('#'+div+' #s-title').html('');
				$('#'+div+' #s-content'+_content).html('');
			}
		}
	});
}

//--

function sectionNext(sectionId){
	if( sectionIndex[sectionId] >= sectionTotal[sectionId] - 1 ) return;
	sectionIndex[sectionId]++;
	sectionShow(sectionId);
	if( sectionIndex[sectionId] >= sectionTotal[sectionId] - 1 ) {
		$('#'+sectionId+' #next').addClass('disabled');
	}
	$('#'+sectionId+' #prev').removeClass('disabled');
}
function sectionPrev(sectionId){
	if( sectionIndex[sectionId] <= 0 ) return;
	sectionIndex[sectionId]--;
	sectionShow(sectionId);
	alert("pause");
	
	if( sectionIndex[sectionId] <= 0 ) {
		$('#'+sectionId+' #prev').addClass('disabled');
	}
	$('#'+sectionId+' #next').removeClass('disabled');
	
}
function sectionShow(sectionId){
	$('#'+sectionId+' #s-title').html(aSection[sectionId].json.item[sectionIndex[sectionId]].title[0].Text );
	$('#'+sectionId+' #s-content').html(aSection[sectionId].json.item[sectionIndex[sectionId]].content[0].Text);
	
	$('#'+sectionId+' #s-more a')
		.attr('href',aSection[sectionId].json.item[sectionIndex[sectionId]].link[0].Text)
		.css('visibility','visible');
}
function sectionInit(sectionId){
	
	if( sectionTotal[sectionId] > 0 ){
		$('#'+sectionId+' #next').click(function(){
			sectionNext( $(this).parent().parent().attr('id') );
		});
		$('#'+sectionId+' #prev').click(function(){
			sectionPrev( $(this).parent().parent().attr('id') );
		});
		
		if( sectionTotal[sectionId] > 1 )
			$('#'+sectionId+' #next').removeClass('disabled');
		
		sectionShow(sectionId);
	}else{
		$('#'+sectionId).html('Aun no se han ingresado Noticias.');
	}
}
//--

$(document).ready(function(){
	
	jPreloadImages([
		wp_url + '/img/menu_bg.png',
		wp_url + '/img/previous.png',
		wp_url + '/img/next.png',
		wp_url + '/img/previous_disabled.png',
		wp_url + '/img/next_disabled.png',
		wp_url + '/img/ajax-loader.gif'
	]);
	
	$("#block-1 > ul").tabs();
	$("#block-1 > ul").tabs();
	$("#block-3 > ul").tabs();
	
	_load('section','noticias','news');
	_load('section','eventos','events');
	_load('section','testimonios','reviews');
	_load('page','centro-de-investigacion','cid');
	_load('page','celula-academica','cell');
	
	$("#s-pagebar img[@src$=png]").pngfix();
	
	
	var flashvars = {};
	var params = {wmode:"opaque",quality:'high'};
	var attributes = {id:"myFlashBannerMain"};
	swfobject.embedSWF(wp_url + "/img/anima.swf", "myAlternativeBannerMain", "904", "150", "9.0.28", wp_url + "/js/swfobject/expressInstall.swf", flashvars, params, attributes);
	
	attributes = {id:"myFlashBlock2"};
	swfobject.embedSWF(wp_url + "/img/convenios.swf", "myAlternativeBlock2", "280", "136", "9.0.28", wp_url + "/js/swfobject/expressInstall.swf", flashvars, params, attributes);
	
	 /*
	$('#myFlashBannerMain').flash(
		{ src: wp_url + '/img/anima.swf',
		//wmode:'opaque',quality:'high',
			width: 904,
			height: 150 },
		{ version: 9 }
	);
	$('#myFlashBlock2').flash(
		{ src: wp_url + '/img/convenios.swf',
		//wmode:'opaque',quality:'high',
			width: 280,
			height: 136 },
		{ version: 9 }
	);
	*/ 
});