var $j = jQuery.noConflict();
$j(document).ready(function() {

  // Destroy one of active classes on contact page
  if($j('#main_menu .last').prev().find('a:last').hasClass('current_page'))
  {
    $j('#main_menu li .current_page_ancestor').removeClass('current_page_ancestor');
  }
  
  // We have to find out if the page in the menu has children
  var first_ul = $j('#content_left').children('ul:first');
  
  $j('#content_left .children').each(function() {
	$j(this).parent().addClass('content_left_children');
  });
  
  // Hide show effect on children pages
  $j('.content_left_children > a').click(function() {
  
	// Store our parent element
	var ourParent = $j(this).parent();
	
	/**
	 * 197 - O spolocnosti -> Technologicky park
	 * 133 - Kariera -> Volne pracovne miesta
	 */
	if(ourParent.hasClass('page-item-197') || ourParent.hasClass('page-item-133')) {
		return;
	}
	
    ourParent.find('ul').stop(true, true).slideToggle('slow');
    return false;   
  });

  // Add some classes to the selected items in the menu on specific page
  left_menu_1 = $j('.category_8 #content_left').find('ul').find('li:last').addClass('content_left_last_li');
  // Also add this class to the other categories
  $j(first_ul).children('li:last').addClass('content_left_last_li');
  left_menu_2 = $j(left_menu_1).prev();
  left_menu_3 = $j(left_menu_2).prev();
  
  // Basic setup

  var acc_width_half = '231px';
  var acc_width_full = '446px';
  var acc_active = 0;
  
  // Accordion coffee magic...
  // Don't try to understand the logic, code can be probable more effective & shorter
  
  // Go through the slides and do the magic...
  $j('.accordion_single').each(function() {
    acc_list_height = $j(this).find('.acc_list').height();
    //acc_list_set = ((77 - acc_list_height) / 2);
    acc_list_set = ((77 - acc_list_height) / 2) + 3;
    $j(this).find('.acc_list').css({ top: acc_list_set });
  });
   
  $j('.accordion_single').hover(function() {

    // Get active object
    var acc_object = $j(this).parent().find('.active_acc')
    // Get html invalid ID :)  
    acc_actual = $j(this).attr('invalid_id');    
    // Get html invalid ID of actual element
    acc_active = $j(acc_object).attr('invalid_id');
    
    if(!($j(this).hasClass('active_acc')))
    {
    
      //
      // Hide old list and show the new one
      //
      $j(acc_object).find('.acc_list').fadeOut('slow');
      
      switch(acc_actual)
      {
        case '1':
                       
          // 2 is active
          if(acc_active == 2)
          {
            $j(this).stop().animate({
              width: acc_width_full
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half,
              left: '446px'
            }, 700);            
          }
          
          // 3 is active
          else if(acc_active == 3)
          {
            $j(this).stop().animate({
              width: acc_width_full
            }, 700);
            
            $j(this).next().stop().animate({
              width: acc_width_half,
              left: '446px'
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half,
              left: '676px'
            }, 700);
          }
        
          break;
          
        case '2':
        
          // 1 is active
          if(acc_active == 1)
          {
            $j(this).stop().animate({
              left: '231px',
              width: acc_width_full
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half
            }, 700);            
          }
          
          // 3 is active
          else if(acc_active == 3)
          {
            $j(this).stop().animate({
              width: acc_width_full
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half,
              left: '676px'
            }, 700);            
          }
        
          break;
          
        case '3':
        
          // 1 is active
          if(acc_active == 1)
          {
            $j(this).stop().animate({
              left: '462px',
              width: acc_width_full
            }, 700);
            
            $j(acc_object).next().stop().animate({
              left: '231px'
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half
            }, 700);
            
          }
          
          // 2 is active
          else if(acc_active == 2)
          {
            $j(this).stop().animate({
              width: acc_width_full,
              left: '462px'
            }, 700);
            
            $j(acc_object).stop().animate({
              width: acc_width_half,
              left: '231px'
            }, 700);            
          }
        
          break;
          
        default:      
      }
      
      // Bring the new king!
      $j(this).find('.acc_list').fadeIn('slow');      
    }
    
    // Remove class from active
    $j(acc_object).removeClass('active_acc');
    // Add class to actual
    $j(this).addClass('active_acc');
    
  }, function() {
    //
  });
  
  // Open close project
  $j('.project_open').click(function() {
    $j(this).siblings('.project_content').slideToggle('slow');
    if($j(this).parent().hasClass('project_active'))
    {
      $j(this).parent().removeClass('project_active');
      $j(this).attr('title', 'Rozbaliť');
    }
    else
    {
      $j(this).parent().addClass('project_active');
      $j(this).attr('title', 'Zabaliť');
    }
    
    return false;
    
  });
  
  $j('.project_top_meta').click(function(click_data) {
  
  var targetname;
  
  //if($j.browser.webkit || $j.browser.mozilla)
  //{
    targetname = click_data.target.localName;
  //}
  //else
  //{
    //targetname = click_data.originalTarget.localName;
  //}
  
    if(targetname != 'img')
    {
    
      $j(this).parent().find('.project_content').slideToggle('slow', function() {
      
      });
    
    if($j(this).parent().hasClass('project_active'))
    {
      $j(this).parent().removeClass('project_active');
      $j(this).parent().find('.project_open').attr('title', 'Rozbaliť');
    }
    else
    {
      $j(this).parent().addClass('project_active');
      $j(this).parent().find('.project_open').attr('title', 'Zabaliť');
    }
    
    return false;
    
    }
    
  });
  
  $j('.project_title').click(function(click_data) {
  
  var targetname;
  
  //if($j.browser.webkit || $j.browser.mozilla)
  //{
    targetname = click_data.target.localName;
  //}
  //else
  //{
    //targetname = click_data.originalTarget.localName;
  //}
  
    if(targetname != 'img')
    {
  
    $j(this).parent().siblings('.project_content').slideToggle('slow');
    if($j(this).parent().parent().hasClass('project_active'))
    {
      $j(this).parent().parent().removeClass('project_active');
      $j(this).attr('title', 'Rozbaliť');
    }
    else
    {
      $j(this).parent().parent().addClass('project_active');
      $j(this).attr('title', 'Zabaliť');
    }
    
    return false;
    
    }
    
  });
  
  var image_dir = 'http://engeo.sk/wp-content/themes/engetech/images/';
  
  // HTML map funnny stuff
  $j('map area').click(function() {
    
    var map_id = $j(this).attr('id');
  
    if(!$j('#map_'+map_id).hasClass('active_image'))
    {      
      $j('.active_image').fadeTo('slow', 0).removeClass('active_image');
      $j('#map_'+map_id).fadeTo('slow', 1).addClass('active_image');      
    }
    
    switch(map_id)
    {
      case 'svk_west':
      
        $j('.active_local').removeClass('active_local');
        $j('.page-item-186').find('a').addClass('active_local');
        
        if(!$j('#local_content_186').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_186').fadeTo('slow', 1);
          $j('#local_content_186').addClass('local_content_active');
        }
        
        break;
        
      case 'svk_middle':
        $j('.active_local').removeClass('active_local');
        $j('.page-item-188').find('a').addClass('active_local');
        
        if(!$j('#local_content_188').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_188').fadeTo('slow', 1);
          $j('#local_content_188').addClass('local_content_active');
        }
        
        break;
        
      case 'svk_east':
        $j('.active_local').removeClass('active_local');
        $j('.page-item-190').find('a').addClass('active_local');
        
        if(!$j('#local_content_190').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_190').fadeTo('slow', 1);
          $j('#local_content_190').addClass('local_content_active');
        }
        
        break;
            
      default:
      
        // No time for debug, hope it works... Brain overflow
      
        break;      
    }
    
    Cufon.refresh();
    
    return false;
  })
  
  // Also on the children in the menu
  $j('.content_left_list_local .children a').click(function() {

    $j('.active_local').removeClass('active_local');
    $j(this).addClass('active_local');
    Cufon.refresh();

      if($j(this).parent().hasClass('page-item-186'))
      {
        if(!$j('#map_svk_west').hasClass('active_image'))
        {      
          $j('.active_image').fadeTo('slow', 0).removeClass('active_image');
          $j('#map_svk_west').fadeTo('slow', 1).addClass('active_image');      
        }
        if(!$j('#local_content_186').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_186').fadeTo('slow', 1);
          $j('#local_content_186').addClass('local_content_active');
        }     
      }
      else if($j(this).parent().hasClass('page-item-188'))
      {
        if(!$j('#map_svk_middle').hasClass('active_image'))
        {      
          $j('.active_image').fadeTo('slow', 0).removeClass('active_image');
          $j('#map_svk_middle').fadeTo('slow', 1).addClass('active_image');      
        }
        if(!$j('#local_content_188').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_188').fadeTo('slow', 1);
          $j('#local_content_188').addClass('local_content_active');
        }     
      }
      else if($j(this).parent().hasClass('page-item-190'))
      {
        if(!$j('#map_svk_east').hasClass('active_image'))
        {      
          $j('.active_image').fadeTo('slow', 0).removeClass('active_image');
          $j('#map_svk_east').fadeTo('slow', 1).addClass('active_image');      
        }
        if(!$j('#local_content_190').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_190').fadeTo('slow', 1);
          $j('#local_content_190').addClass('local_content_active');
        }     
      }  
      else if($j(this).parent().hasClass('page-item-192'))
      {
        if(!$j('#map_svk_out').hasClass('active_image'))
        {      
          $j('.active_image').fadeTo('slow', 0).removeClass('active_image');
          $j('#map_svk_out').fadeTo('slow', 1).addClass('active_image');      
        }
        if(!$j('#local_content_192').hasClass('local_content_active'))
        {
          $j('.local_content_active').fadeTo('slow', 0);
          $j('.local_content_active').removeClass('local_content_active');
          $j('#local_content_192').fadeTo('slow', 1);
          $j('#local_content_192').addClass('local_content_active');
        }     
      }
    
    return false;
  })
  
  // Left menu on local page
  if($j('.content_left_list_local').length)
  {
    var active = $j('.content_left_list_local .current_page_item');
    $j(active).find('a').css({ color: '#757575' });
    //$j(active).find('li:first').find('a').css({ color: '#258fcb'});
    $j(active).find('li:first').find('a').addClass('active_local');
  }
  
  // Show first content
  //$j('.local_content:first').show();
  
});
