$(document).ready(function() {

  // fix png transparency for IE6:
  $('.pngfix, #top_menu ul, #top_menu .top, #top_menu .bottom, #side_menu .top, #side_menu .bottom, #side_menu .fill, .pre-sign_block .top, .pre-sign_block .bottom, .pre-sign_block .middle, .dsublist .top, .dsublist .bottom, #diplomas ul ul, .map li span, .jqmWindow h3, .jqmWindow .content, .jqmWindow .bottom').pngFix();     
         
  //dropdown menus for IE6:
  dropdownMenus();   

  //opening and closing right diploma menu
  rightMenuClick();
  
    
  //Forcing rollover to cover active option of menu
  leftMenuActive();
  
  //fix IE z-index bug
  $('div.diploma_link:first').addClass('first');
  fixLocations();  
  
  //jobx/diplomas listing tabs
  toggleTabs();  
  
  //diploma page toggle blocks
  toggleDiplomaBlock(); 

  //modal window with form
  $('.jqmWindow').jqm(
    {
      trigger: 'a.open_form', 
      overlay: 70, 
      closeClass: 'close',
      target: '#modal_content',
      onShow: openInFrame
    });
    
    
  //modal gallery   
  $('.galleryWindow').jqm(
    {
      trigger: 'a.open_gallery', 
      overlay: 70,
      closeClass: 'close',
      onShow: startGallery,
      onHide: endGallery
    })
    .find('li img').click(function(){$('.galleryWindow').jqmHide();});

  //modal clip   
  $('.clipWindow').jqm(
    {
      trigger: 'a.open_clip', 
      overlay: 70,
      closeClass: 'close',
      onShow: startClip
    });

});


// display 'merci' image after posting contact form
function showMerci() {
  $("#merci", parent.document.body).addClass('merci pngfix');
}


/*
 * --------------------------------------------------------------------
 * dropdown menus for IE6
 * --------------------------------------------------------------------
 */
dropdownMenus = function() {
    if (document.all) {
        $('#top_menu > li, #diplomas li, #diplomas .diploma_link, .map li').hover(
          function() { 
            if($(this).hasClass('has_children'))
              $(this).addClass('hc_over');
            else
              $(this).addClass('over');
          },
          function() {
            $(this).removeClass('over hc_over');
          }
        );
    }  
}


/*
 * --------------------------------------------------------------------
 * jobx/diplomas listing tabs
 * --------------------------------------------------------------------
 */
toggleTabs = function() {
  $('.tab_body').hide().filter(':first').show();
  $(".tabs li").each(function(index){ 
      $(this).find('a').click(function(){
         $(".tabs li a.active").removeClass('active') ;
         $(this).addClass('active');
         $('.tab_body').hide();
         $('.tab_body').eq(index).show() ;
         return false;
      });
  });
} 

/*
 * --------------------------------------------------------------------
 * diploma page toggle blocks
 * --------------------------------------------------------------------
 */
toggleDiplomaBlock = function() {
  $('ul.diploma_info h4').click(function() {
    $(this).toggleClass('active').next('.body').toggle();
  });
} 


/*
 * --------------------------------------------------------------------
 * fix ie z-index bug on map page
 * --------------------------------------------------------------------
 */
fixLocations = function() {
  if(!(jQuery.support.style)) {
    $('.map li').hover(
      function() { 
        $('.map li').css('z-index', '-1');
        $(this).css('z-index', '49');
      },
      function() {
        $('.map li').css('z-index', 'auto');      
      }
    ); 
  }
}




/*
 * --------------------------------------------------------------------
 * modal window with form 
 * --------------------------------------------------------------------
 */

var openInFrame = function(hash)
{
    var $trigger = $(hash.t);
    var $modalWindow = $(hash.w);
    var $modalContainer = $('iframe', $modalWindow);
    var myUrl = $trigger.attr('href');
//    var imgSrc = $trigger.attr('rel');
//    var imgAlt = $trigger.text();
    $modalContainer.html('').attr('src', myUrl);
//    $('.jqmWindow h3 img').attr('src', imgSrc).attr('alt', imgAlt);

    $("#merci", parent.document.body).removeClass('merci pngfix');
    $modalWindow.show();

}


/*
 * --------------------------------------------------------------------
 * modal gallery  
 * --------------------------------------------------------------------
 */
 
var startGallery = function(hash)
{
    var $trigger = $(hash.t);
    $(hash.w).show();
    if( $trigger.attr('rel') ) 
    {
      var imageID = parseInt($trigger.attr('rel'));
      showImg($('.galleryWindow li:nth('+imageID+')'));  
    }
    else
    {
      showImg($('.galleryWindow li:first'));  
    }
}

function showImg(imgWrapper) {
  var currentImg = imgWrapper.find('img');
  var currentImgHeight = currentImg.height();
  var currentImgWidth = currentImg.width();
  var galleryWindow = imgWrapper.parents('.galleryWindow');
  var galleryWindowLeft = galleryWindow.css('left').replace('px', ''); 

  galleryWindow.find('.next, .previous').unbind().hide();
  galleryWindow.find('.close, .img_nav').hide();
  galleryWindow.find('ul li').css('visibility','hidden');

  galleryWindow
    .animate({ 
      height: currentImgHeight + 60 + 'px'
    }, 400)
    .animate({ 
      width: currentImgWidth + 20 + 'px',
      marginLeft: (-currentImgWidth/2)+'px' 
    }, 400, 
    function() {
      imgWrapper.css('visibility','visible');
      if(imgWrapper.prev('li').length) {
        galleryWindow.find('.previous').click(function(){ 
          showImg(imgWrapper.prev('li')); 
        }).show();
      }
      if(imgWrapper.next('li').length) {
        galleryWindow.find('.next').click(function(){ 
          showImg(imgWrapper.next('li')); 
        }).show();
      }
      galleryWindow.find('.close, .img_nav').show();
    }); 
}


endGallery = function(hash) {
  $(hash.w).hide().attr('style',''); 
  $(hash.o).fadeOut('500', function() {$(this).remove();});
}

startClip = function(hash)
{
  $(hash.w).show(); 
  $(hash.w).css('marginLeft', (-1)*($('#flash_wrapper').width()/2) +'px' );
  $(hash.w).width($('#flash_wrapper').width());
  $(hash.w).height($('#flash_wrapper').height());
}



/*
 * --------------------------------------------------------------------
//Forcing rollover to cover active option of menu
 * --------------------------------------------------------------------
 */
leftMenuActive = function() {
  $('#side_menu ul a').hover(
    function() {
      var nextLink = $(this).parent().next().find('a');
      if(nextLink.hasClass('active')) {
        nextLink.removeClass('active').addClass('active-next');
      }
    },
    function() {
      var nextLink = $(this).parent().next().find('a');
      if(nextLink.hasClass('active-next')) {
        nextLink.removeClass('active-next').addClass('active');
      }    
    }
  );
}

/*
 * --------------------------------------------------------------------
//opening and closing right diploma menu
 * --------------------------------------------------------------------
 */
rightMenuClick = function() {
  $('#right_col .diploma_link a.opener').click(function(){
    $(this).next('.dlist').show();
    return false;
  });
  
  $('#right_col .diploma_link').hover(function(){}, function(){
    $(this).children('.dlist').hide();
  });
   
  $('#diplomas .dlist > ul > li:has(ul)').hover(
    function(){
      $(this).addClass('fixHeight');
    },
    function() {
      $(this).removeClass('fixHeight')      
    }
  ); 
}

/*
 * --------------------------------------------------------------------
//ToolTips
 * --------------------------------------------------------------------
 */

	$(function(){
		$("[title]").mbTooltip({ // also $([domElement])..mbTooltip  >>  in this case only children element are involved
			opacity : .97,       //opacity
			wait:200,           //before show
			cssClass:"default",  // default = default
			timePerWord:70,      //time to show in milliseconds per word
			hasArrow:true,			// if you whant a little arrow on the corner
			imgPath:"/extension/utec_design/design/utec_design/images/tooltip/",
			ancor:"mouse", //"parent"  you can ancor the tooltip to the mouse position or at the bottom of the element
			shadowColor:"black" //the color of the shadow
		});
	})


