
$(document).ready(function() {
	
	//------------------
	// INPUT fields default values
	//------------------

	$('INPUT[@onempty]').focus(
		function() {
			var obj = $(this).get(0);
			if(obj.value == $(this).attr('onempty')) { obj.value = ''; }
		}
	)

	$('INPUT[@onempty]').blur(
		function() {
			var obj = $(this).get(0);
			if(obj.value == '') { obj.value = $(this).attr('onempty'); }
		}
	)
	
	$.scrollTo.defaults.axis='xy';
	$('div.pane').scrollTo(0);
	$.scrollTo(0);

	var $paneOptions=$('#pane-options');

	$('.MenuLink').each(function() {
		$(this).attr('menuheight', $(this).next().height());
	});
	$('.MenuLinkSub').each(function() {
		var sXpathContent = '#item_' + $(this).attr('key') + '_' + $(this).attr('keysub');
		var offset = $(sXpathContent).offset();
		var offsetRel =  $('.ContentScroller_'+ $(this).attr('key')).offset();
		var iScrollTop = -(offset.top-offsetRel.top);
		$(this).attr('contenttop', iScrollTop);
		
	});
	$('.MenuLink').next().find('.Padding').hide();

	
	
});


function moveMain(obj, objSub){
	//MENU SCRUCTURE

	var objLink = obj;
	var $paneOptions=$('#pane-options');

	//redefine soundFlash
	switch($(objLink).attr('key')){
		case 'home' : isHome = 1 ; break;
		default : isHome = 0; break;
	}

	//trigger the first subMenu
	if(!objSub){$(objLink).parent().find('.MenuLinkSub:first').trigger('click');}

	//sel menu
	$('.MenuLink.Sel').removeClass('Sel');
	$('.MenuLink#'+$(objLink).attr('id')).addClass('Sel');
	
	fixDottedLinksOutline('.Logo a');
	fixDottedLinksOutline('.MainMenu a');
	
	fixDottedLinksOutline('.MenuLink#'+$(objLink).attr('id')+' a');
	
	//PLAY SOUND
	swfobject.embedSWF("flash/swf/menu_sound.swf", "menu_sound", "10", "10", "8.0.0", "inc/swfobject/expressInstall.swf");
	
	$paneOptions.stop();

	//animate Sub Menus on roll out
	$('.MenuLink').next().animate({ height:'0px' }, 200);
	$('.MenuLink').next().find('.Padding').hide();
	

	switch($(objLink).attr('key')){
		case 'search' :  ajxLoadSearchContent(_menu_scructure['search'][0],$('#search_keywords').val()); break;
		case 'contactos' :  ajxLoad('#item_contactos',_menu_scructure['contactos'][0],'#item_contactos'); break;
	}

	setTimeout(function() {
		$paneOptions.scrollTo('#page_' + $(objLink).attr('key'), 1500, { onAfter:function() {
			$(objLink).next().find('.Padding').show();
			if($(objLink).attr('menuheight')) {
				$(objLink).next().animate({ height:$(objLink).attr('menuheight') + 'px' }, 200 , function(){
					if(objSub) { moveSub(objSub); }
				});
			}
		} });
	}, 200);

	return false;

}


function moveSub(obj){
	
	var objLink = obj;
	var iTop = $(objLink).attr('contenttop');
	var myKey = $(objLink).attr('key');
	var myKeySub = $(objLink).attr('keysub');
	var sXpathContent = '#item_' + myKey + '_' + myKeySub;
	
	//alert('KEY : '+myKey + '\nSUBKEY : ' + myKeySub + '\nPATH CONTENT : ' + sXpathContent + '\niTop : ' +iTop );
	//$('.MenuLink#'+myKey).trigger('click');
	
	$('.MenuLinkSub.Sel').removeClass('Sel');
	$('.MenuLinkSub#'+$(objLink).attr('id')).addClass('Sel');
	
	fixDottedLinksOutline('.MenuLinkSub#'+$(objLink).attr('id')+' a');
	
	$('.ContentScroller_' + $(objLink).attr('key')).stop();
	$('.ContentScroller_' + $(objLink).attr('key')).animate({ top: iTop + 'px' }, 200, function() {

		if(_menu_scructure[myKeySub][0]){
			ajxLoad(sXpathContent,_menu_scructure[myKeySub][0],myKeySub); 
		}
		
		if(_menu_scructure[myKeySub][1] != ""){
			photoLoad(_menu_scructure[myKeySub][1],myKey);
		}

	});	
		
}


function DocumentReady(obj) {
	
	tb_init($(obj).find('a.thickbox, area.thickbox, input.thickbox'));
	
	fixDottedLinksOutline('.Faqs a');
	
	// FADE
	$(obj).find(".FadeArticle").fadeTo("fast", 0.0);

	$(obj).find(".FadeArticle").hover(function(){		
			//$(this).stop();
			$(this).fadeTo("fast", 0.8);
		},function(){
			//$(this).stop();
			$(this).fadeTo("fast", 0.0);
	});
	
	

}


function homeSoundFlash()
{
	return isHome;
}

function photoLoad(imgSrc,myKey){
	switch(myKey){
		case 'empresa': imgWidth= 147; imgHeight= 147;	break;
		default : imgWidth= 190; imgHeight= 190;	break;
	}
	$('#roldana_back_picture_' + myKey).html('<img src="'+ imgSrc +'" width="' + imgWidth + '" height="' + imgHeight + '">').show();
}

function ajxLoad(sXpathContent,pageToLoad,myKeySub, myFilter){
	
	switch(myKeySub){
		case 'emp_noticias_list': var tbl_width = 342; var tbl_height = 250; break;
		default : var tbl_width = 750; var tbl_height = 340; break;
	}

	$(sXpathContent).html('<table width="'+tbl_width+'" height="'+tbl_height+'"><tr><td align="center" valign="center"><img src="/images/loading.gif" width="116" height="104" alt="loading" /></td></tr></table>').show();
	$.get(pageToLoad, { subKey_detail : myKeySub, filter : myFilter }, function(data){ 
		$(sXpathContent).html(data).show();
		DocumentReady($(sXpathContent).get(0));
	});


}

function ajxLoadSearchContent(pageToLoad,search_keywords_value){
	$('#item_search').html('<table width="750" height="440"><tr><td align="center" valign="center"><img src="/images/loading.gif" width="116" height="104" alt="loading" /></td></tr></table>').show();
		$.get(pageToLoad, { search_keywords : search_keywords_value }, function(data){ 
			$('#item_search').html(data).show();
			DocumentReady($('#item_search').get(0));
		});
}

function reloadPage(){
	alert('reloading');
}

function fixDottedLinksOutline(domChunk) { $(domChunk).focus(function(){this.blur();});  }

function reset_o(){
	$paneOptions.stop().stop();
	$paneOptions[0].scrollLeft=$paneOptions[0].scrollTop=0
};

function showLoading(domChunk) {
	var offset = $(domChunk).offset();
	$('#LoadingIcon').css({
		position:	'absolute',
		top : ($(domChunk).height()-40), 
		left : (($(domChunk).width()/2)+100)
	});
	$('#LoadingIcon').show();	
}

function tb_flash(aID){
	tb_show("","iframe_project_detail.php?aID=" + aID + "&KeepThis=true&TB_iframe=true&height=440&width=755","");
}

function hideLoading() {
	$('#LoadingIcon').hide('fast');	
}


/*SCROLL///////////////////////////////////*/
jQuery(function($){

	$('#contentScroll1').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll2').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll3').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll4').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll5').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll6').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll7').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll8').jScrollPane({showArrows:true, scrollbarWidth:17});
	$('#contentScroll9').jScrollPane({showArrows:true, scrollbarWidth:17});

});

//----------------------
// FAQ
//----------------------

function SwitchFAQ(iQuestion) {
	fixDottedLinksOutline('#Q' + iQuestion + ' a');
	$('.Question a').removeClass('Sel');
	$('.Answer').each(function(){
		if(this!=$('#A' + iQuestion).get(0)) { $(this).hide('fast'); }					   
	});
	$('#Q' + iQuestion + ' a').addClass('Sel');
	$('#A' + iQuestion).show('fast');
}


