/**
 *
 * jQuery for shop/view view
 *
 */

  var ms_transition = 1000;
  var mtop = 0; var mleft = 0;  // picture margins

  $(window).bind('load', function () {


	/**
	 * gallery script, based on
	 * - http://www.codeassembly.com/jQuery-morphing-gallery/
	 * - http://www.appelsiini.net/2007/6/sequentially-preloading-images
	 */

    // collect image urls
    var images = new Array();
    $(".si_gallery a").each(function() {
      images.push($(this).attr('href'));
    });

    // load first image
		var preloadImage = new Image();
		preloadImage.src = images.shift();

		// preload other images sequentially
    var img = document.createElement('img');
    $(img).bind('load', function() {
      if(images[0]) {
        this.src = images.shift();
      }
    }).trigger('load');

		// show first image when loaded
		$(preloadImage).bind('load',function () {

		  // determine margins
		  mtop = (480 - preloadImage.height) / 2;
		  if (mtop < 2) mtop = 0;
      mleft = (480 - preloadImage.width) / 2;
      if (mtop < 2) mtop = 0;
		  
      // replace current img
			$('.nextImg').css('background-image','url(' + preloadImage.src + ')')
	                 .css('opacity','0')
                   .css('margin-top',mtop+'px')
                   .css('margin-left',mleft+'px');

			$('.nextImg').animate({opacity:'1'}, ms_transition);

      $('.currentImg').animate({opacity: 0}, ms_transition, function(e) {
				$('.currentImg').css('background-image','url(' + preloadImage.src + ')');
        $('.currentImg').css('opacity','1')
                        .css('margin-top',mtop+'px')
                        .css('margin-left',mleft+'px');
			});
		});

		// set thumb clicks
		$('.si_gallery a').click(function(e) {
			this.blur();
			e.preventDefault();
			$(".si_gallery a img").removeClass("selected");  // remove .selected class from all other links
			preloadImage.src = $(this).attr('href');
			$(this).children("img").addClass("selected"); // add .selected class to current link
		});

  });

