$(window).load(function()
{ 
  checkheight();
});

$(window).resize(function()
{
  checkheight();
});

$(function() {
  $( "#searchterm" ).autocomplete({
    source: absoluteURL+"search.jsp",
    minLength: 1,
    delay: 200
  });
  $( "#searchterm" ).keypress(function(event)
  {
    if (event.which==13) {searchsite();}
  });
});

function checkheight()
{
  var height = ($("#maintextholder").length > 0)?($('#maintextholder').height()+300):($('#departmenttextholder').height()+272);
  var chkheight = ($("#submenuright").length > 0)?($('#submenuright').height()+312):0;
  //alert(height +' - '+ chkheight);
  height = (chkheight>height)?chkheight:height;
  chkheight = $('#leftcolumn').height() + 330;
  height = (chkheight>height)?chkheight:height;
  chkheight = $(window).scrollTop() + $(window).height();
  height = (chkheight>height)?chkheight:height;
  
  $('#contentDiv').height(height);
  if ($('#maintextholder').length > 0)
    $('#maintextholder').height(height-300);
  else
    $('#departmenttextholder').height(height-272);
}

function searchsite()
{
  var searchterm = $('#searchterm').val();
  if (searchterm.length>0)
    window.location.href = absoluteURL+"?action=search&searchterm="+escape(searchterm);
}

function swapImg( $img, imageUrl )
{
  var actions = 2;
  var img = $('<img src="' + imageUrl + '" />').load( next );
  $img.animate({opacity: 1.0}, { duration: 1, queue: false, complete: next });
  //$img.fadeTo( 1, 1.0, next );
 
  function next() {
    if( --actions ) return;
    $img.attr({ src: imageUrl }).animate({opacity: 1.0}, { duration: 100, queue: false, complete: next });
    //$img.attr({ src: imageUrl }).fadeTo( 100, 1.0 );
  }
}

function startSlideShow()
{
  $.get(absoluteURL+'ajax/ajax.jsp', { action: 'getRandomPhoto', id: departmentID }, function(imageUrl)
  {
    try
    {
      // try to clone the photo and than fade it out
      $clone = $('#photo1').clone(true).css("z-index","4").appendTo('#contentDiv');
      swapImg($('#depphoto'), imageUrl);
      $clone.fadeOut(500, function() {
        $clone.remove();
      });
    }
    catch(err)
    {
      swapImg($('#depphoto'), imageUrl);
    }
  });
}

function trim(value)
{
  value = value.replace(/^\s+/,''); 
  value = value.replace(/\s+$/,'');
  return value;
}

function manageRelation(jcbid, jgroupid, jarticleid)
{
  var jchecked = $('#'+jcbid).is(':checked');
  var jaction = jchecked?'addRelation':'removeRelation';

  $.get(absoluteURL+'ajax/ajax.jsp', { action: jaction, groupid: jgroupid, articleid: jarticleid }, function(output)
  {
    var color = 'red';
    try
    {
      if (trim(output) == "success")
        color = 'lime';
    }
    catch(err) {}

    $('#'+jcbid).parent().css('background-color',color);
  });
}

function updateSmallCart()
{
  $.get(absoluteURL+'ajax/ajax.jsp', { action: 'getSmallCart' }, function(output)
  {
    $('#smallcart').html(output);
    $('#smallcart').show();
    $('#smallcartplaceholder').hide();
  });
}

function updateBigCart()
{
  $.get(absoluteURL+'ajax/ajax.jsp', { action: 'getBigCart' }, function(output)
  {
    $('#bigcart').html(output);
  });
}

function addToCart(jarticleid, jurl)
{
  var jarticlecount = 1;
  if ($('#articlecount').length > 0)
    jarticlecount = $('#articlecount').val();
  
  $.get(absoluteURL+'ajax/ajax.jsp', { action: 'addToCart', articleid: jarticleid, articlecount: jarticlecount }, function(output)
  {
    if (trim(output) == "success")
    {
      //updateSmallCart();
      window.location = jurl+'?showcart=1';
    }
  });
}

function updateCartRow(jarticleid)
{
  if ($('#articlecount'+jarticleid).length > 0)
  {
    var jarticlecount = $('#articlecount'+jarticleid).val();
  
    $.get(absoluteURL+'ajax/ajax.jsp', { action: 'updateCartRow', articleid: jarticleid, articlecount: jarticlecount }, function(output)
    {
      if (trim(output) == "success")
      {
        updateSmallCart();
        updateBigCart();
      }
    });
  }
}

function deleteCartRow(jarticleid)
{  
  $.get(absoluteURL+'ajax/ajax.jsp', { action: 'deleteCartRow', articleid: jarticleid }, function(output)
  {
    if (trim(output) == "success")
    {
      updateSmallCart();
      updateBigCart();
    }
  });
}

