var $j = jQuery.noConflict();

$j(document).ready(function(){
  
  // alert($j('#imageGallery').length);
  
  // alert($j('body').length);
  
  // setup image gallery
  $j('div#imageGalleryThumbs img').click(function(){
    
    // get index of this img
    var index = $j(this).attr('id').split('_');
    
    index = index.pop();
    
    // hide all main images
    $j('div#imageGallery > img').hide();
    
    // show requested image
    $j('div#imageGallery img#main_img_' + index).show();
    
    // set selected thumb
    $j('div#imageGalleryThumbs img.selected').removeClass('selected');

    $j('div#imageGalleryThumbs img#thumb_img_' + index).addClass('selected');
    
  });
  
  // set login fields to clear when clicked
  $j('.loginField').focus(function(){
    
    $j(this).val('');
    
  });
  
  $j('.loginField').blur(function(){
    
    if($j(this).attr('id') == 'loginEmail')
    {
      var txt = 'Email';      
    }
    else
    {
      var txt = 'Password';
    }
    
    if($j(this).val() == '')
    {
      $j(this).val(txt);
    }
    
  });
  
  // set general search field to clear on click
  $j('#generalSearch').focus(function(){
    
    $j(this).val('');
    
  });
  
  $j('#generalSearch').blur(function(){
    
    if($j(this).val() == '')
    {
      $j(this).val('Search');
    }
    
  });
  
  // clicks for chef and mixo bios
  jQuery('#chefBioLink').click(function()
  {
    tb_show('', '#TB_inline?height=200&width=450&inlineId=chefBio', null);
  });
  
  jQuery('#mixologistBioLink').click(function()
  {
    tb_show('', '#TB_inline?height=200&width=450&inlineId=mixologistBio', null);
  });
  
  // disable glossary links
  jQuery('div#recipeMethod a[href*="glossary"]').attr('href', 'javascript:void(0);')
  
});

/*
 * send feedback email
 */
function sendFeedback()
{
  // get message to send
  var body = $j('#feedbackBody').val();

  $j.post(
    'ajax/sendFeedback.php',
    {'body': body},
    function(data){
      
      // remove submit button
      $j('#submit').remove();
      
      // show message to user
      if(data == 'MAIL_SENT')
      {
        $j('#feedbackBody').val('Thanks for your feedback!');
      }
      else
      {
        $j('#feedbackBody').val('Sorry, due to technical difficulties your feedback coud not be sent! Please try again later.');
      }
    },
    'text'
  );
}

/*
  sets the tab to open on the next page recipe -> recipes etc.
*/
function setLandingTab(search_zone, search_tab, landing_page)
{
  // set cookies
  jQuery.cookie('SEARCH_ZONE', search_zone, {path: "/", expires: 10});
  jQuery.cookie('SEARCH_TAB', search_tab, {path: "/", expires: 10});
  jQuery.cookie('SEARCH_STRING', '', {path: "/", expires: 10});
  jQuery.cookie('SEARCH_PAGE', 1, {path: "/", expires: 10});
  
  if(landing_page != '')
  {
    window.location = '/' + landing_page;
  }
}

