
/* ============================= */
/* ===== JavaScript Document === */
/* ============================= */


$(document).ready(function(){
	$('ul.sf-menu').superfish();
	$("ul.sf-menu li ul li:first-child").addClass("first");
	$("ul.sf-menu li ul li:last-child").addClass("last");

	if ($('.subMenu').length) {
		$("ul.sf-vertical").superfish({ 
			/*animation: {height:'show'},   // slide-down effect without fade-in */
			pathClass:  'current' ,
			delay:1200               // 1.2 second delay on mouseout 
		});
	}

	initInputValue();
	
	$(window).resize(function(){
	resizeBar();
	});
	

	
	if($('.userPannelTabs').length){
		tabsFunc('userPannelTabs');
	}
	
	if($('.tabs').length){
		tabsFuncHome('tabs');
	}
	
//	if($('.contentTabs').length){
//		var $tabs = $('.contentTabs').tabs();
//	}
	
//	if($('.contentInsideTabs').length){
//		$('.contentInsideTabs').tabs();
//	}
	
	if($('.infoTabs').length){
		$('.infoTabs').tabs();
	}
	
	$('#control').click(function () { 
		if($('.userPannel').height() == 31){
			
			$('.userPannel').animate({
				height: '192'
			  }, 700, function() {
				$('.userPannelControl').css("background-position","top");
			  });
			fixIe6ScrollPosition(192); 			
		} else {
			
			$('.userPannel').animate({
			height: '31'
			  }, 700, function() {
				$('.userPannelControl').css("background-position","bottom");
			  });
			fixIe6ScrollPosition(31);			
		}
	});//.filter(':first').click();

	if($('.makeColumns').length){
		$('.makeColumns').columnize({width:290});	
	}

	$(window).bind("resize", resizeWindow);

	 var newWindowHeight = $(window).height();
	 footerHeight(newWindowHeight);

	 function resizeWindow( e ) {
		 var newWindowHeight = $(window).height();
		 footerHeight(newWindowHeight);
		 //alert(newWindowHeight);
	 }

	 if($.browser.msie) {
		 $(window).scroll(function () {
		 fixIe6ScrollPosition();
		 }); 
	 }

	
	onDocReady();
});

function fixIe6ScrollPosition(height) {

	if($.browser.msie) {

	if (ie6) {

	if (height == null) {

	height = $(".userPannel").height();

	} 


	var scrollPosition = $(window).height() + $(window).scrollTop() - height; 

	$(".userPannel").css("top", scrollPosition);

	} else {

	$(".userPannel").css('PositionBottom',0);

	} 

	} 

}


function footerHeight(browsePosition){

	var position = $('#footer').position();
	var y = browsePosition - position.top;

	if( y < 132){ var y = 132 };
	
	$('#footer').css( 'height' , y);

};

function tabsFunc(cssClass){
	var tabContainers = $('div.' + cssClass + ' > div ');
	tabContainers.hide().filter(':first').show();
	firstException = false;
	$('div.' + cssClass + ' .tabNavigation ul a').click(function () {                                
		tabContainers.hide();
		tabContainers.filter(this.hash).show();                                
		$('div.' + cssClass + ' .tabNavigation ul a').removeClass('selected');
		$('div.' + cssClass + ' .tabNavigation ul li').removeClass('selected');
		$(this).addClass('selected');
		$(this).parents('li').addClass('selected');
		if( firstException && $('.userPannel').height() == 31){
			$('.userPannel').animate({
				height: '192'
			  }, 700, function() {
				$('.userPannelControl').css("background-position","top");
			  });
			fixIe6ScrollPosition(192);			
		}
		return false;                        
	}).filter(':first').click();
	firstException=true;
};

function tabsFuncHome(cssClass){
	var tabContainers = $('div.' + cssClass + ' > div ');                        
	tabContainers.hide().filter(':first').show(); 
	$('div.' + cssClass + ' ul.tabNavigation a').click(function () {  
		tabContainers.hide();
		tabContainers.filter(this.hash).show();                                
		$('div.' + cssClass + ' ul.tabNavigation a').removeClass('selected');
		$('div.' + cssClass + ' ul.tabNavigation li').removeClass('selected');
		$(this).addClass('selected');
		$(this).parents('li').addClass('selected');
		return false;                        
	}).filter(':first').click();                
};


function initInputValue(){
//	$('input[type="text"]').each(function(){
//		$(this).data('defaultValue',$(this).val());					 
//	});
//	$('input[type="text"]').focus(function() {
//		if($(this).val() == $(this).data('defaultValue')){
//			$(this).val("");
//		}
//	});
//	$('input[type="text"]').blur(function() {
//		if($(this).val()== "") {
//			$(this).val($(this).data('defaultValue'));
//		}
//	});
}

function resizeBar(){
	var wWidthSize = $(document).width();
	if(wWidthSize < 968) {
		$('body').css("background-position","-360px top");
	} else{
		$('body').css("background-position","center top");
	}
	//$(cssClass).width(wWidthSize);
	//alert(wWidthSize);
};
/* ============================= */
/* ===== Header Login/Logout === */
/* ============================= */
function getIsneCookie(cookie_name){
      var cookieData = new Array(5); 
      cookieData[0] = '';
      cookieData[1] = '';
      cookieData[2] = '';
      cookieData[3] = '';
      cookieData[4] = '';

      var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');
      var repl = /\$|,|~|`|\^|\(|\)|\=|\[|\_|\]|\[|\}|\{|\;|\'|\"|\<|\>|\||\\|\./g;  
      if (results) {
    	if(results[1] != null){
    		results[1] = $.base64Decode(results[1]);
		}
	    if ((results[1] != null) && (results[1].split("|")[0] != null )) {
            	cookieData[0] = unescape(results[1].split("|")[0].replace(repl,""));
            }
	    if ((results[1] != null) && (results[1].split("|")[1] != null )) {
            	cookieData[1] = unescape(results[1].split("|")[1].replace(repl,""));  
            }
	    if ((results[1] != null) && (results[1].split("|")[2] != null )) {
	        cookieData[2] = unescape(results[1].split("|")[2].replace(repl,""));  
            }
	    if ((results[1] != null) && (results[1].split("|")[3] != null )) {
            	cookieData[3] = unescape(results[1].split("|")[3].replace(repl,""));  
            }
	    if ((results[1] != null) && (results[1].split("|")[4] != null )) {
            	cookieData[4] = unescape(results[1].split("|")[4].replace(repl,""));  
            }
      }
      
      return cookieData;
}

function logInOut(cookie_name)
{
      var cookie = readCookie(cookie_name);

      if (cookie){//cookie available
      	$(".userLinks .logout").removeClass("off");
      	$(".userLinks .logout").addClass("on");
      	$(".userLinks .login").removeClass("on");
      	$(".userLinks .login").addClass("off");
      	$("#welcomeUser").html(readCookieParam('ISNE_COOKIE', 1));
      	$(".welcomeMessage").css("display", "block"); 
      }
      else{//no cookie
      	$(".userLinks .logout").removeClass("on");
      	$(".userLinks .logout").addClass("off");
      	$(".userLinks .login").removeClass("off");
      	$(".userLinks .login").addClass("on");
      	$("#welcomeUser").html("&nbsp;");
      	$(".welcomeMessage").css('display','none');
      } 
}

function readCookie(name) {
	try{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') 
				c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0){
				var content = c.substring(nameEQ.length,c.length);
	    		return $.base64Decode(content);
			}
		}
	}
	catch(erro){
		var c = erro;
	}
	return null;
}

function readCookieParam(name, index) {

	try{
		var cookieValue = readCookie(name);
		return cookieValue.split('|')[index];
	}catch(erro){
		alert(erro);
	}
	return null;

}

function toggleLayer( whichLayer ){ 
	var elem, vis;  
	if( document.getElementById ) // this is the way the standards work    
		elem = document.getElementById( whichLayer );  
	else if( document.all ) // this is the way old msie versions work      
		elem = document.all[whichLayer];  
	else if( document.layers ) // this is the way nn4 works    
		elem = document.layers[whichLayer];  
	
	vis = elem.style;  
	
	// if the style.display value is blank we try to figure it out here  
	if(vis.display == '' && elem.offsetWidth != undefined && elem.offsetHeight != undefined)    
		vis.display = (elem.offsetWidth != 0 && elem.offsetHeight != 0)?'block':'none';  
		vis.display = (vis.display == '' || vis.display =='block')?'none':'block';
}

function toggle(obj) {
	var el = document.getElementById(obj);
	if ( el.style.display != 'none' ) {
		el.style.display = 'none';

	}
	else {
		el.style.display = 'block';

	}
}
