// JavaScript Document for Aussie SE global elements.

$(document).ready( function()
      {
         PEPS.rollover.init();
      });
      
      PEPS = {};
      
      PEPS.rollover =
      {
         init: function()
         {
            this.preload();
            
            $(".ro").hover(
               function () { $(this).attr( 'src', PEPS.rollover.newimage($(this).attr('src')) ); },
               function () { $(this).attr( 'src', PEPS.rollover.oldimage($(this).attr('src')) ); }
            );
         },
         
         preload: function()
         {
            $(window).bind('load', function() {
               $('.ro').each( function( key, elm ) { $('<img>').attr( 'src', PEPS.rollover.newimage( $(this).attr('src') ) ); });
            });
         },

         newimage: function(src) { src = src.replace(/-on/, ''); return src.substring(0, src.search(/(\.[a-z]+)/)) + '-on' + src.match(/(\.[a-z]+)/)[0]; },
         oldimage: function( src ){ return src.replace(/-on/, ''); }
      };
	  
	  
	  
	  
$(document).ready( function(){
	if($('.firstMenu').length){
		initSubMenu();
	}
	if ($('#showHelp1, #showHelp2').length) {
	    initblockFactura($('#showHelp1'));
	    initblockFactura($('#showHelp2'));
	}
	if ($('.menu ul li').length) {
	    initHover($('.menu ul li'));
	}						
	if ($('.menu ul li img').length) {
	    initHideMenu($('.menu ul li img'));
	}
	if ($('a.fancyBox').length) {
	    $('a.fancyBox').fancybox({'overlayOpacity': 0.75,'overlayColor': '#000'});
	}
	if ($('.concursoNao, .concursoSim').length) {
	  initPassatempoDisney($('.concursoNao, .concursoSim').find('input'));
	}
});

function initPassatempoDisney (self) {
  if (!$('input[value="rbSim"]').attr('checked')) {
    $('input[value="rbLisboa"], input[value="rbPorto"]').attr('disabled','disabled');
  }
  self.click(function(){
    if ($(this).val() == "rbNao") {
      $('input[value="rbLisboa"], input[value="rbPorto"]').removeAttr('checked').attr('disabled','disabled');
    }
    if ($(this).val() == "rbSim") {
      $('input[value="rbLisboa"], input[value="rbPorto"]').removeAttr('disabled');
    }  
  });
}

function initSubMenu(){
    $('.linkTopMenu').click(function() {
        var mainMenu = $('ul.menuComm').height();
        var subMenu = $(this).next('ul').height();
        var total = mainMenu + subMenu;
        var lesstotal = mainMenu - subMenu;

        if ($(this).next('ul').css('display') == 'none') {
            $(this).next('ul').slideDown('slow');
            //mainMenu.heigth(subMenuHeigth);
            $('ul.menuComm').height(total)
        } else if ($(this).next('ul').css('display') == 'block') {
            //mainMenu.heigth(mainMenuHeigth);


            $(this).next('ul').slideUp('slow', function() {
                $('ul.menuComm').height(lesstotal)
            });
        }
        return false;
    });
}


function initblockFactura(self) {
    var block = self.closest('div').parent().parent();
    block.find('input.showHelp').focus(function(){block.find('.imgContainer:first').show()});
    block.find('input.showHelp').blur(function(){block.find('.imgContainer:first').hide()});
    self.children(':first').hover(function() { self.find('.imgContainer').toggle(); }, function() { self.find('.imgContainer').toggle(); });
}

function initHover(self) {
    self.find('ul').css('display', 'none');
    self.find('img').hover(function() { }, function() {
        var global = $(this).parent();
        if (global.find('ul').is(':visible')) {
            var img = global.find('img').attr('src').replace(/-on/, '');
            var newimg = img.substring(0, img.search(/(\.[a-z]+)/)) + '-on' + img.match(/(\.[a-z]+)/)[0];
            global.find('img').attr('src',newimg);
        }
    });
}

function initHideMenu (self){
    $('.menu ul li img').click(function(){
        var par = $(this).parent();
        var elem = par.find('ul');
        var hideMenu = function(){
            elem.css({'display': 'none','visibility':'hidden'});
        };
        var showMenu = function(){
            elem.css({'display': 'block','visibility':'visible'});
        };
        if (elem.is(':visible')) {
            setTimeout(hideMenu,10);
        } else {
            par.parent().parent().find('img').each(function(){
                if (elem.parent().find('img').attr('src') != $(this).attr('src')) {
                    var img = $(this).attr('src').replace(/-on/, '');
                    $(this).attr('src',img);            
                    $(this).next().css({'display': 'none','visibility':'hidden'});
                }
            });
            setTimeout(showMenu,10);
        }
   }); 
   $('body').click(function(event){
        if (!$(event.target).hasClass('ro')) {
             $('.menu ul li ul').css({'display': 'none','visibility':'hidden'});
             $('.menu ul li').find('img').each(function(){
                var img = $(this).attr('src').replace(/-on/, '');
                $(this).attr('src',img);            
                $(this).next().css({'display': 'none','visibility':'hidden'});
             });
       }
   });
}
