var jQ=jQuery.noConflict();
jQ(document).ready(function(){
	if (jQ('.loader').length)
		var placeholderSrc=jQ('.loader').attr('src').replace('ajax-loader','placeholder');
	//================================================== shared-related
	var selector=window.location.pathname;
	selector=selector.replace('/','');
	if (! selector)
		selector='inicio';
	jQ('.selector.'+selector).show(0).prev().css('fontWeight','bold');
	jQ('.block.icon').addClass(selector);

	var blockLeftHeightOriginal;
	jQ('.content.second p.bullet').click(function(){
		var relatedIndex=jQ(this).attr('rel');
		jQ('.content.second .news[rel!="'+relatedIndex+'"]').hide();
		var navHeight=jQ(this).parents('.nav').height();
		jQ('.content.second .news[rel="'+relatedIndex+'"]').slideDown(0);
		var newsHeight=jQ('.content.second .news[rel="'+relatedIndex+'"]').outerHeight();
		if (newsHeight>navHeight){
			jQ(this).parents('.block.left.enlarge').animate({
				height:newsHeight+30
			},500);
			jQ(this).parents('.nav').css('height',newsHeight);
		}
		else if (newsHeight<navHeight){
			if (newsHeight<blockLeftHeightOriginal-30){
				jQ(this).parents('.block.left.enlarge').animate({
					height:blockLeftHeightOriginal
				},500);
				jQ(this).parents('.nav').css('height',blockLeftHeightOriginal-30);
			}
			else{
				jQ(this).parents('.block.left.enlarge').animate({
					height:newsHeight+30
				},500);
				jQ(this).parents('.nav').css('height',newsHeight);
			}
		}
		return false;
	});
	//================================================== /shared-related
	//================================================== positioning-related
	jQ(window).load(function(){
		var blockHeights=new Array();
		jQ('.content.second .block').each(function(){
			if (! jQ(this).hasClass('border')){
				if (jQ(this).hasClass('map'))
					blockHeights.push(jQ(this).height()-285);
				else
					blockHeights.push(jQ(this).height());
			}
		});
		maxBlockHeight=Math.max.apply(0,blockHeights);
		jQ('.content.second .nav').css({
			height:maxBlockHeight+'px'
		});

		jQ('.content.second .block.enlarge').css({
			height:jQ('.content.second .block.enlarge').height()+30+'px'
		});

		jQ('.block.left p.caption, .block.center p.caption').each(function(){
			var parentHeight=jQ(this).parent('.block').outerHeight();
			var thisHeight=jQ(this).outerHeight();
			var position=(parentHeight-thisHeight)/2;
			jQ(this).css('top',position+'px');
		});
		blockLeftHeightOriginal=jQ('.content.second .block.enlarge').height();
		jQ('.content.second p.bullet:first').click();

		jQ('#loading').fadeOut(500);
	});
	//================================================== /positioning-related
	//================================================== slider-related
	if (jQ('.block.larger img').length){
		jQ('.block.larger .wrapper').cycle({
			slideExpr:'.caption',
			fx:'blindX',
			timeout:7500,
			sync:false,
			manualTrump:false,
			pause:true,
			cleartypeNoBg:true,
			pager:jQ('.block.larger .pager'),
			pagerAnchorBuilder:function(index){
				return '.block.larger .pager a:eq('+index+')';
			}
		});
	}
	if (jQ('.block.larger.map .pics .container a img').length){
		jQ('.block.larger.map .pics .container').cycle({
			slideExpr:'a',
			fx:'fade',
			timeout:5000,
			manualTrump:false,
			pause:true,
			cleartypeNoBg:true,
			pager:jQ('.block.larger.map .pics .pager'),
			pagerAnchorBuilder:function(index){
				return '.block.larger.map .pics .pager a:eq('+index+')';
			}
		});
		jQ('.block.larger.map .pics .container a').fancybox({
			centerOnScroll:true,
			overlayShow:true,
			//transitionIn:'elastic',
			//transitionOut:'elastic',
			speedIn:300,
			speedOut:300,
			titlePosition:'over',
			titleFormat:function(title,currentArray,currentIndex){
				rel=currentArray.attr('rel');
				rel=rel.replace('fancy_group_','');
				return '<table cellspacing="0" cellpadding="0" id="fancybox-title-float-wrap"><tbody><tr><td id="fancybox-title-float-left"></td><td id="fancybox-title-float-main">'+title+'</td><td id="fancybox-title-float-right"></td></tr></tbody></table><span class="index">Foto '+(currentIndex+1)+' de '+currentArray.length+'</span>';
			}
		});
	}
	//================================================== /slider-related
	//================================================== contact-related
	jQ('form#contact').submit(function(){
		jQ.ajax({
			type:'post',
			url:paths.ajax_url,
			data:{
				action:'contact',
				mail:jQ('#contact').find('input#mail').val(),
				msg:jQ('#contact').find('textarea#msg').val()
			},
			beforeSend:function(){
				jQ('.block.left p#success').hide();
				jQ('.block.left p#failure').hide();
				jQ('.block.left p#loading').show();
			},
			success:function(fb){
				jQ('.block.left p#loading').hide();
				if (fb.response){
					jQ('.block.left p#success').show();
				}
				else{
					jQ('.block.left p#failure').show();
				}
			},
			error:function(error){
				jQ('.block.left p#loading').hide();
				jQ('.block.left p#failure').show();
			}
		});
		return false;
	});
	//================================================== /contact-related
	//================================================== corner-related
	jQ('.content.second .block.enlarge').corner('10px bottom');
	jQ('.content.second .block.larger.map .placeholder').corner('5px');
	if (! jQ.browser.msie || (jQ.browser.msie && jQ.browser.version>8)){
		jQ('.content.second .block.larger.map .placeholder iframe').corner('5px');
		jQ('.content.second .block.larger.map .placeholder .pics').corner('5px');
	}
	//================================================== /corner-related
});
//================================================== extra_functions-related
jQ.fn.clearField=function(){
	return this.focus(function(){
		if(this.value==this.defaultValue){
			this.value='';
		}
	}).blur(function(){
		if(!this.value.length){
			this.value=this.defaultValue;
		}
	});
}
//================================================== /extra_functions-related
