/*
function initBagGallery(con,en) {
    var tw = new Fx.Tween(en);
    con.getElements('a').each(function(a) {
        a.addEvent('click', function() {
            tw.
        });
    });
}
*/

function numCorrect(val) {
    val = val.trim().replace(/,/g,".");
    return val.toFloat().round(1);
}

function initSearchForm() {
    var act = $('cat').getElement('li.act');
    $('cat').getElements('a').each(function(a) {
        a.addEvent('click', function() {
            act.removeClass('act');
            act = a.getParent();
            act.addClass('act');
            $('category').set('value', a.getProperty('rel'));
            $('chosen_bag').set('text', a.getProperty('title'));
        });
    });
}

function imageflow(num) {
  if (Browser.Engine.trident4)
  {
      con = 'books_alt';
  } 
  else
  {
      con = 'books';
  }
  if (typeof num != 'number') {
      num = null;
  }
  var simages_src = new Array();
  $('slider').setStyle('display','block');
  var simages = $$('#'+con+' img');
  simages.each(function(s){
    simages_src.push(s.getProperty('src'));
  })
  new Asset.images(simages_src, {
              onComplete:  function() {
                $('loader').setStyle('display','none');
                $(con).setStyle('position','relative');
                $$('#'+con+' img').setStyles({
                  'position': 'absolute',
                  'border':   'none'
                });
                imgs = x = num != null ? num : Math.round($$('#books img').length / 2);
                imgs = (imgs > 0 ? imgs-1 : 0);
                mf = new MooFlow({
              		container: 'imageflow',
              		imgContainer: con,
              		images: '#'+con+' img',
              		caption: 'captions',
              		slider: {slider:'slider_inner',knob:'knob'},
              		useMouseWheel: true,
              		useKeyInput: true,
              		reflection: 0.8,
              		startIndex: imgs
              	});
              	$('books').setStyle('visibility','visible');
	 }
  });
};



/* JQUERY */

var loaded = [], loading = false;
var url = false;

function fireBackground(tmb, href) {
    $(tmb).parents('#thumbs:first').find('.active').removeClass('active').find('img').css('opacity', 0.8);
    $(tmb).find('img').css('opacity', 1).parent().addClass('active');
    
    $('#slideshow .activeslide').removeClass('activeslide').addClass('prevslide');
    $('#slideshow .nextslide').hide().attr('src', href).removeClass('nextslide').addClass('activeslide').fadeIn(500);
    $('#slideshow .prevslide').removeClass('prevslide').addClass('nextslide');
}

function loadImage(tmb, href) {
    if (jQuery.inArray(href, loaded) < 0) {
        loading = true;
        $('#gallery #thumbs').addClass('loading');
        $.cacheImage(href, {
            load : function (e) {
                loaded.push(href);
                fireBackground($(tmb), href);
                loading = false;
                $('#gallery #thumbs').removeClass('loading');
            }
        });
    } else {
        fireBackground($(tmb), href);
    }
}

function initGallery() {    
    $('#gallery #thumbs a').not('.active').find('img').css('opacity', 0.8);
    $('#thumbs a').each(function(i, a) {
        $(a).hover(
            function() { $(this).find('img').css('opacity', 1); },
            function() { if (!$(this).hasClass('active')) { $(this).find('img').css('opacity', 0.8); } }
        );
        $(a).bind('click', function(e) {
            if (loading !== true) {
                var href = $(this).attr('href');
                loadImage($(a), href);
            }
            return false;
        });
    });
}