jQuery.noConflict();
jQuery(window).load(function() {
	j_main_content = jQuery('.main_content')
	j_sidebar = jQuery('#sidebar')
	if (j_sidebar.size()!=0) {
		height_main_content = j_main_content.height()
		height_sidebar = j_sidebar.height();
		
		if (height_sidebar<211) {
			j_sidebar.height(211)
			height_sidebar = 211;
		}
		if (height_main_content<(height_sidebar+10)) 
			j_main_content.height(height_sidebar+10)
	}
});
(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
        var div = $(this).children('a');
        var ph = $(this).innerHeight();
        var dh = div.height();
		var th = $(this).height();
        var mh = (ph - dh) / 2;
        div.css('padding-top', mh);
		div.height(th-mh)
	});
};
})(jQuery);

jQuery(function() {
	jQuery('.accordion_news_text').hide();
	jQuery('.accordion_news_title').click(function(){
		if(jQuery(this).hasClass('accordion_visible')) {
			jQuery(this).parent().parent().find('.accordion_news_text').slideUp('fast',function(){ jQuery(this).removeClass('accordion_visible'); });
			jQuery(this).parent().parent().find('.accordion_news_title').removeClass('accordion_visible');
			return false;
		} else {
			jQuery(this).parent().parent().find('.accordion_news_text').slideUp('fast',function(){ jQuery(this).removeClass('accordion_visible'); });
			jQuery(this).parent().parent().find('.accordion_news_title').removeClass('accordion_visible');
			jQuery(this).addClass('accordion_visible').parent().find('.accordion_news_text').slideDown('fast',function(){ jQuery(this).addClass('accordion_visible') });
			return false;
		}
	});
	
	var ajax_river = false,
		ajax_nav = jQuery( '.ajax_nav' );
		
	ajax_nav.live({
		ajax_nav_request: function ( e, nav_action ) { 
			if(ajax_river)
				return;
			
			ajax_river = true;
			
			var this_nav = jQuery(e.target),
				response_place = jQuery(this_nav.find( '.ajax_nav_html_selector' ).val());
						
			this_nav.find('.river-waiting').show().css({'visibility':'visible'});
			response_place.parent().find('.river-waiting').show().css({'visibility':'visible'});

			query = this_nav.find( '.ajax_query' ).val();
			actual_page = parseInt(this_nav.find( '.ajax_actual_page' ).val());
			total_pages = parseInt(this_nav.find( '.ajax_total_pages' ).val());
			nav_ID = this_nav.find( '.ajax_nav_ID' ).val();
			data = {
				action: 'nav_request',
				query: JSON.parse(query),
				nav_action: nav_action,
				actual_page: actual_page,
				total_pages: total_pages,
				nav_ID: nav_ID
			};
			jQuery.ajax({
				type: 'POST',
				url: CreaAjax.ajaxurl,
				data: data,
				success: function(answer){
					if(answer!=""){
						response = JSON.parse(answer);
						if (response['html']) 
							response_place.html(stripslashes(response['html']));
						if (response['page']) {
							this_nav.find( '.ajax_nav_page' ).val(response['page']);
							this_nav.find( '.ajax_actual_page' ).val(response['page']);
						}
						if (response['total_pages']) {
							this_nav.find( '.ajax_total_pages' ).val(response['total_pages']);
							this_nav.find( '.ajax_nav_total' ).text(response['total_pages']);
						}
					}
					this_nav.css({'display':'inline-block','width':'auto'}).width((this_nav.width()+1)).css({'display':'block'})
					response_place.parent().find('.river-waiting').hide().css({'visibility':'hidden'});
					this_nav.find('.river-waiting').hide().css({'visibility':'hidden'});
					ajax_river = false;
				}
			});
		},
		init: function(e) {
			it = jQuery(e.target)
			it.width((it.width()+1)).css({'display':'block'})
			query = JSON.parse(it.find( '.ajax_query' ).val());
			query['category__not_in'] = [14,13];
			it.find( '.ajax_query' ).val(JSON.stringify(query));
			it.find( '.ajax_nav_page' ).live({
				keypress: function ( e ) {
					code = (e.keyCode ? e.keyCode : e.which);
					if (code == 13) {
						it = jQuery(e.target)
						new_actual_page = parseInt(it.val());
						ajax_actual_page = it.parent().parent().find('.ajax_actual_page');
						ajax_total_pages = parseInt(it.parent().parent().find('.ajax_total_pages').val());
						if(new_actual_page>ajax_total_pages)
							new_actual_page = ajax_total_pages;
						else if(new_actual_page<=0) 
							new_actual_page = 1;
							
						ajax_actual_page.val(new_actual_page);
						ajax_nav.trigger( 'ajax_nav_request', 'update_page' );
						e.preventDefault();
					}
				}
			})
		}
	});
	ajax_nav.trigger('init');
	
	jQuery('.check_category input').bind({
		change: function( e ) {
			it = jQuery(e.target)
			cat_ids = [];
			query = JSON.parse(ajax_nav.find( '.ajax_query' ).val());
			jQuery('.check_category input:checked').each(function() {
				cat_ids.push(jQuery(this).parent().attr('category'));
			});
			delete(query['category_name']);
			if(cat_ids)
				query['category__in'] = cat_ids;
			ajax_nav.find( '.ajax_query' ).val(JSON.stringify(query));
			ajax_nav.trigger( 'ajax_nav_request', 'change_query' );
		}
	})
	ajax_nav.find( '.ajax_nav_action' ).live({
		click: function ( e ) {
			action = jQuery(this).attr('action');
			ajax_nav.trigger('ajax_nav_request', action);
		}
	})
		
	jQuery('.comments_box').hide();
	jQuery('#comment_toggle').click( function() { 
		j_comments_box = jQuery('.comments_box');
		if(!j_comments_box.is(':animated')) {
			if(j_comments_box.hasClass('visible')) {
				jQuery(this).parent().fadeTo(300,0.3,function() { jQuery(this).toggleClass('visible').fadeTo(300,1) })
				j_comments_box.fadeTo(600,0,function() { jQuery(this).hide().toggleClass('visible') })
			} else {
				jQuery(this).parent().fadeTo(300,0.3,function() { jQuery(this).toggleClass('visible').fadeTo(300,1) })
				j_comments_box.show().fadeTo(600,1,function() { jQuery(this).toggleClass('visible') })
			}
		}
	});

	months_t = jQuery('#all_months_translation').val();
	if(months_t) {
		months_t = months_t.split(',');
	}

	var month,year;
	jQuery('#next_month').bind({
		click: function(event) {
			element_may_iGo = jQuery('#may_iGo');
			may_iGo = parseInt(element_may_iGo.val());
			if(may_iGo==1) {
				element_may_iGo.val(0)
				jQuery(this).parent().find('.river-waiting').css({'visibility':'visible'}).show();
				element_month = jQuery('#b_actual_month');
				element_year = jQuery('#b_actual_year');
				month = parseInt( element_month.val() );
				year = parseInt( element_year.val() );
				if(month==12) { 
					month=0;
					year++;
				}
				month++;
				element_month.val(month);
				element_year.val(year);
				
				jQuery.ajax({
					type: 'POST',
					url: CreaAjax.ajaxurl,
					data: ['action=calendar_resquest&c_year=',year,'&c_month=',month].join(''),
					success: function(html){
						jQuery('.eventos_content .river-waiting').css({'visibility':'hidden'}).hide();
						jQuery('#servedata').html(html);
						tbody_html = jQuery('#servedata').find('#ajax_get_calendar')
						jQuery('#wp-calendar tbody').remove()
						jQuery('#wp-calendar').append(tbody_html);
						jQuery('#may_iGo').val(1)
						jQuery('#actual_month').text(CreaAjax[['months_',(month)].join('')]);
					}
				});
			}
		}
	});
	
	jQuery('#prev_month').bind({
		click: function(event) {
			element_may_iGo = jQuery('#may_iGo');
			may_iGo = parseInt(element_may_iGo.val());
			if(may_iGo==1) {
				element_may_iGo.val(0)
				jQuery(this).parent().find('.river-waiting').css({'visibility':'visible'}).show();
				element_month = jQuery('#b_actual_month');
				element_year = jQuery('#b_actual_year');
				month = parseInt(element_month.val());
				year = parseInt(element_year.val());
				if(month==1) { 
					month=13;
					month--;
				}
				month--;
				element_month.val(month);
				element_year.val(year);
				
				jQuery.ajax({
					type: 'POST',
					url: CreaAjax.ajaxurl,
					data: ['action=calendar_resquest&c_year=',year,'&c_month=',month].join(''),
					success: function(html){
						jQuery('.eventos_content .river-waiting').css({'visibility':'hidden'}).hide();
						jQuery('#servedata').html(html);
						tbody_html = jQuery('#servedata').find('#ajax_get_calendar')
						jQuery('#wp-calendar tbody').remove()
						jQuery('#wp-calendar').append(tbody_html);
						jQuery('#may_iGo').val(1)
						jQuery('#actual_month').text(CreaAjax[['months_',(month)].join('')]);
					}
				});
			}
		}
	});
	
	jQuery( '.term-licitacoes .post_item' ).each(function() {
		it = jQuery(this);
		var form = it.find( '.licitacoes_download_form .wpcf7' ),
			a = it.find( 'a' ),
			link = a.attr( 'href' );
		a.attr( 'href', '' ).css({ 'cursor':'pointer' });
		form.find('.attachment').val(link);
		
		a.bind({
			click: function(e){
				it = jQuery(this);
				download_form = form.parent();
				
				if (!download_form.is(':visible')) {
					download_form.removeClass('hidden').show();
				} else {
					download_form.addClass('hidden').hide();
				}
				return false;
			}
		});
	});
	
	jQuery("#slider").easySlider({
		prevId: 		'prevBtn',
		prevText: 		'Previous',
		nextId: 		'nextBtn',	
		nextText: 		'Next',
		controlsShow:	true,
		controlsBefore:	'',
		controlsAfter:	'',	
		controlsFade:	true,
		firstId: 		'firstBtn',
		firstText: 		'First',
		firstShow:		false,
		lastId: 		'lastBtn',	
		lastText: 		'Last',
		lastShow:		false,				
		vertical:		false,
		speed: 			500,
		auto:			true,
		pause:			6000,
		continuous:		true, 
		numeric: 		true,
		numericId: 		'controls',
		atSetCounter:	function(j) { 
			href_link = j.find('a.featured_link_recover').attr('href')
			jQuery('#destaque_link_pointer').attr('href',href_link)
		}
	});
	jQuery("#crearj_webtv_slider").easySlider({
		prevId: 		'webtv_prev',
		prevText: 		'',
		nextId: 		'webtv_next',	
		nextText: 		'',
		controlsShow:	true,
		controlsBefore:	'',
		controlsAfter:	'',	
		controlsFade:	false,
		firstShow:		false,
		lastShow:		false,				
		vertical:		false,
		speed: 			500,
		auto:			true,
		pause:			8000,
		continuous:		true, 
		numeric: 		false,
		numericId: 		'controls',
		atSetCounter:	function(j) { 
			title_text = j.find('img').attr('alt')
			jQuery('#crearj_webtv_title').fadeTo(250, 0.00001,function() {wrapper_height = jQuery(this).parent().height(); jQuery(this).text(title_text).css({'padding-top': (wrapper_height-jQuery(this).height())/2}).fadeTo(250, 1)})
		}
	});
	
	var href = "";
	if (jQuery('.real_link').val()!=""){ 
		var href = jQuery('.real_link').val();
	}
	var bind = href.concat(jQuery('.s').val());
	jQuery('.search_link').attr('href', bind);
	
	jQuery('.s').keyup(function () {
		href = jQuery('.real_link').val();
		bind = href.concat(jQuery(this).val());
		jQuery('.search_link').attr('href', bind);
	});
	
	jQuery('.s').keyup(function(e) {
		if(e.keyCode == 13) {
			window.location.replace(jQuery('.search_link').attr('href'));
		}
	});
	
});

function stripslashes (str) {
    return (str + '').replace(/\\(.?)/g, function (s, n1) {
        switch (n1) {
        case '\\':
            return '\\';
        case '0':
            return '\u0000';
        case '':
            return '';
        default:
            return n1;
        }
    });
}
