// TinyMCE Functions
function writeRealMedia(p){writeEmbed('CFCDAA03-8BE4-11cf-B84B-0020AFBBCCFA','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0','audio/x-pn-realaudio-plugin',p)}function writeWindowsMedia(p){p.url=p.src;writeEmbed('6BF52A52-394A-11D3-B153-00C04F79FAA6','http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701','application/x-mplayer2',p)}function writeEmbed(cls,cb,mt,p){var h='',n;h+='<object classid="clsid:'+cls+'" codebase="'+cb+'"';h+=typeof(p.id)!="undefined"?'id="'+p.id+'"':'';h+=typeof(p.name)!="undefined"?'name="'+p.name+'"':'';h+=typeof(p.width)!="undefined"?'width="'+p.width+'"':'';h+=typeof(p.height)!="undefined"?'height="'+p.height+'"':'';h+=typeof(p.align)!="undefined"?'align="'+p.align+'"':'';h+='>';for(n in p)h+='<param name="'+n+'" value="'+p[n]+'">';h+='<embed type="'+mt+'"';for(n in p)h+=n+'="'+p[n]+'" ';h+='></embed></object>';document.write(h)}function writeFlash(p){writeEmbed('D27CDB6E-AE6D-11cf-96B8-444553540000','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0','application/x-shockwave-flash',p)}function writeShockWave(p){writeEmbed('166B1BCA-3F9C-11CF-8075-444553540000','http://download.macromedia.com/pub/shockwave/cabs/director/sw.cab#version=8,5,1,0','application/x-director',p)}function writeQuickTime(p){writeEmbed('02BF25D5-8C17-4B23-BC80-D3488ABDDC6B','http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0','video/quicktime',p)}

// jquery cookie
jQuery.cookie=function(name,value,options){if(typeof value!='undefined'){options=options||{};if(value===null){value='';options.expires=-1}var expires='';if(options.expires&&(typeof options.expires=='number'||options.expires.toUTCString)){var date;if(typeof options.expires=='number'){date=new Date();date.setTime(date.getTime()+(options.expires*24*60*60*1000))}else{date=options.expires}expires='; expires='+date.toUTCString()}var path=options.path?'; path='+(options.path):'';var domain=options.domain?'; domain='+(options.domain):'';var secure=options.secure?'; secure':'';document.cookie=[name,'=',encodeURIComponent(value),expires,path,domain,secure].join('')}else{var cookieValue=null;if(document.cookie&&document.cookie!=''){var cookies=document.cookie.split(';');for(var i=0;i<cookies.length;i++){var cookie=jQuery.trim(cookies[i]);if(cookie.substring(0,name.length+1)==(name+'=')){cookieValue=decodeURIComponent(cookie.substring(name.length+1));break}}}return cookieValue}};

// author Marco "DWJ" Solazzi - hello@dwightjack.com
// license  Dual licensed under the MIT licenses.
// copyright Copyright (c) 2008 Marco Solazzi
(function(jQuery){jQuery.fn.inputLabel=function(text,opts){o=jQuery.extend({color:"#DEDEDE",e:"focus",force:false,keep:true},opts||{});var clearInput=function(e){var target=jQuery(e.target);var value=jQuery.trim(target.val());if(e.type==e.data.obj.e&&value==e.data.obj.innerText){jQuery(target).css("color","").val("");if(!e.data.obj.keep){jQuery(target).unbind(e.data.obj.e+" blur",clearInput)}}else if(e.type=="blur"&&value==""&&e.data.obj.keep){jQuery(this).css("color",e.data.obj.color).val(e.data.obj.innerText)}};return this.each(function(){o.innerText=(text||false);if(!o.innerText){var id=jQuery(this).attr("id");o.innerText=jQuery(this).parents("form").find("label[for="+id+"]").hide().text()}else if(typeof o.innerText!="string"){o.innerText=jQuery(o.innerText).text()}o.innerText=jQuery.trim(o.innerText);if(o.force||jQuery(this).val()==""){jQuery(this).css("color",o.color).val(o.innerText)}jQuery(this).bind(o.e+" blur",{obj:o},clearInput)})}})(jQuery);

// lists paginations 
jQuery.fn.morePages = function() {
	document.createElement('abbr');	
	if ($(this).length > 0){
		$(this).css('cursor','pointer').find('select').hide();
		$(this).append('<acronym title="ir para outras p&aacute;ginas">&hellip;</acronym>')
		
		$(this).each(function(){ 			   			  
			$(this).click(function() {
				$(this).find('acronym').hide();
				$(this).find('select').show();			  
			});
			
			$(this).find('select').blur(function () {										  	
				$(this).hide();
				$(this).next().show();
		   });			
		});
		
	}
};

/* FORM HIDE FIELDS */
jQuery.fn.showHideFields = function() {
	if ($(this).length > 0){
		
		$(this).each( function(){
								   
			var $this = $(this).children('input');
			var elementContainer = $this.attr("id")+"_container";
			
			if ($this.is(":checked")) 
				{ $("#"+elementContainer).show("fast"); }
			else 
				{ $("#"+elementContainer).hide("fast"); }									
			
			$this.click(function(){
			
				if ($this.is(":checked"))
					{
				    var validador = $("#" + $("#"+elementContainer).attr("reqvalid"))[0]; 
				    ValidatorEnable(validador, true);
                    $("#"+elementContainer).slideDown();
					}
					
				else
					{ 
					var validador = $("#" + $("#"+elementContainer).attr("reqvalid"))[0]; 
					ValidatorEnable(validador, false);
					$("#"+elementContainer).slideUp();
					}
			})
			
		});
	}
};	

// Gallery pagination
function pageselectCallback(page_index, jq){
	var new_content = jQuery('#gallery-list span.item:eq('+page_index+') a').clone().attr("rel",page_index);
	new_content.click(function(e){
		e.preventDefault();
		jQuery('#gallery-list span.item:eq('+$(this).attr("rel")+') a').click();
		return false;
	});
	$('#gallery-placeholder').empty().append(new_content);
	return false;
}

function initPagination() {
	// count entries inside the hidden content
	var num_entries = jQuery('#gallery-list span.item').length;
	// Create content inside pagination element
	$("#gallery-page").pagination(num_entries, {
		callback: pageselectCallback,
		items_per_page:1 // Show only one item per page
	});
 }

$(document).ready(function() {
		
	//RESIZE TEXT
	if($.cookie('TEXT_SIZE')) {
		var actualSize = $.cookie('TEXT_SIZE');
		$('.tsize').addClass(actualSize);
		$('.text_size .'+actualSize).addClass('active');	
	}
	$('.text_size a').click(function() {
		$('.text_size>ul>li').removeClass('active');
		$(this).parent('li').addClass('active');
		var textSize = $(this).parent().attr('class');
		$('.tsize').removeClass('small medium large').addClass(textSize);
		$.cookie('TEXT_SIZE',textSize, { path: '/', expires: 10000 });
		return false;
	});
	

	
	// list expand article "Associated Documents"
	//if ( $('.activeHide').length > 0){
				
	//};
	
	$('.morePages').morePages();
	
	//search label
	$(".search input:text").inputLabel();
	
	/* MENU NIVEL 2 - Animacao*/
	$("#nav>li").hover(
	  function () {
		$(this).addClass("menuOn");
	  }, 
	  function () {
		$(this).removeClass("menuOn");
	  }
	);
	
	$(".sub-nav").each(function(){
		
		var $this = $(this);
		var elRelative = $this.parents(".center");
		var elParent =	$this.prev();
		var elRelativeMin = elRelative.offset().left;
		var elRelativeMax = elRelative.offset().left+elRelative.outerWidth();
 		
		//localizar o menu pai
		var parentWidth = elParent.outerWidth();
		var parentPosition = elParent.position().left;
		var parentOffset = elParent.offset().left;
		
		//meio do menu pai
		var parentMiddleoffSet = parentOffset+parentWidth/2;
		
		// comprimento do sub menu
		var menuWidth = $this.find("ul").width() + 10;
		
		//posicionar o menu em relacao ao relative
		var containerPosition = parentPosition+parentWidth/2;
		var containerPositionFix = containerPosition-menuWidth/2;	
		
		//comprimento certo ao sub menu
		$this.width(menuWidth);
		$this.find('>ul').width(menuWidth);
		
		var menuOffSetLeft = parentMiddleoffSet-menuWidth/2;
		var menuOffSetRight = parentMiddleoffSet+menuWidth/2;
		
		//Verifica se fica dentro do container e posiciona
		if(elRelativeMin > menuOffSetLeft){
			$this.css("left",0);
		}
		else{
			if(menuOffSetRight>elRelativeMax){
				$this.css({'left': 'auto', 'right': "1px"})
			}
			else{
				$this.css("left",containerPositionFix);		
			}
		}
			
	});
	
	if ($('.login').hasClass('dadosIncorrectos') == false)
	{
	    $('.login').slideUp(100);
	}
	else
	{
	    $('.login').show();
	}
	$('#login_handler').click(function(event) {
		$('.login').slideToggle();
		event.stopPropagation();
	});
	$('.login').click(function(event) {
		event.stopPropagation();
	});	
	$('body').click(function(event) {
	 	$('.login').slideUp();
		event.stopPropagation();
	 });	
	
});

