var curScaledProductPic = 0;
var scaledImagesStripeInitOpacity = 0.3;

$(function(){
	curScaledProductPic = 0;
	var stripeImages = $('#homeImageScroller .img img').length;
	var loadedStripeImages = 0;
	$('#homeImageScroller .img img').onImagesLoaded(function(_this){
		loadedStripeImages++;
		//console.log( "trigger Loaded");
		if (loadedStripeImages>=stripeImages)
			fixScrollerWidth();
	  });
	
	// init
	$(".scaledImagesStripe .img:first").addClass("currentPhoto");
	$(".scaledImagesStripe .img:not(.currentPhoto) img").fadeTo(0, scaledImagesStripeInitOpacity);
	
	
	$(".scaledImagesStripe .btnLeft:first").click( scaledImagesStripeMoveLeft );
	$(".scaledImagesStripe .btnRight:first").click( scaledImagesStripeMoveRight);
	
	$(".scaledImagesStripe .btnLeft:first").hover( 
		function(e) { $(e.currentTarget).addClass("btnLeftHover"); }, 
		function(e) { $(e.currentTarget).removeClass("btnLeftHover"); }
	);
	$(".scaledImagesStripe .btnRight:first").hover( 
		function(e) { $(e.currentTarget).addClass("btnRightHover"); }, 
		function(e) { $(e.currentTarget).removeClass("btnRightHover"); }
	);
	
	$("#homeImageScroller .img").bind("click", gotoToLink );	
	
	/*
	if (stripeImages>1)
	{
		$(".scaledImagesStripe .btnRight:first").fadeIn();
	}
	*/
	
})

function fixScrollerWidth()
{
	$(".scaledImagesStripeMask:first").width(0);
	
	$('.scaledImagesStripeMask:first .img img').each(function(i,el) 
	{
		var tmpImg = $("<img />");
		tmpImg 
			.attr("src", $(el).attr("src"))
			.load(function() {
				pic_real_width = this.width;   // Note: $(this).width() will not work
				var ww = $(".scaledImagesStripeMask:first").width();
				$(".scaledImagesStripeMask:first").width( ww + pic_real_width );
			});
		if ($.browser.msie) tmpImg.trigger("load");
	});
	
	if ( $(".scaledImagesStripeMask:first .img").length !=0 )
		showScrollerScaledPhoto();
}

function scaledImagesStripeMoveLeft(e)
{
	if ( $(".scaledImagesStripe .scaledImagesStripeMask:first").is(":animated") ) return;
	
	if ( curScaledProductPic-1<0 ) return;

	curScaledProductPic--;
	showScrollerScaledPhoto();
}

function scaledImagesStripeMoveRight(e)
{
	if ( $(".scaledImagesStripe .scaledImagesStripeMask:first").is(":animated") ) return;
	if ( curScaledProductPic+1>=$(".scaledImagesStripe .img").length ) return;

	curScaledProductPic++;
	showScrollerScaledPhoto();
}

function showScrollerScaledPhoto()
{
	// Get pic count
	// var picCount = $(".scaledImagesStripe .scaledImagesStripeMask:first .img").length;
	// Get pics offset
	var pic = $(".scaledImagesStripe .scaledImagesStripeMask:first .img:nth-child(" + (curScaledProductPic+1) + ")");
	/*if (pic.length==0) 
	{
		$("#ambienceController").hide();
		return;
	}*/
	
	/*
	if (curScaledProductPic==0)
	{
		$(".scaledImagesStripe .btnLeft:first").fadeOut();
	} else {
		$(".scaledImagesStripe .btnLeft:first").fadeIn();
	}
	
	if (curScaledProductPic+1==picCount)
	{
		$(".scaledImagesStripe .btnRight:first").fadeOut();
	} else {
		$(".scaledImagesStripe .btnRight:first").fadeIn();
	}
	*/
	
	var c = $(".currentPhoto:first");
	c.removeClass("zoomCursorHomePage").removeClass("currentPhoto")
		.find("img:first")
		.stop(true,true).clearQueue().fadeTo(200, scaledImagesStripeInitOpacity);
	
	var sW = parseInt( $(".scaledImagesStripeMask:first").width() );
	var mW = parseInt( $(".scaledImagesStripeMaskContainer:first").width() );
	
	xOffs = -parseInt(pic.position().left);
	if (sW + xOffs < mW && curScaledProductPic!=0)
	{ 
		xOffs = -sW+mW;
	} 
	
	$(".scaledImagesStripe .scaledImagesStripeMask:first")
		.stop(true,true)
		.clearQueue()
		.animate({left: xOffs+"px"}, 800, "easeInOutQuart", function() {
			pic.find("img:first").animate( {"opacity":1}, 200);
		});
	pic.addClass("currentPhoto");
	
	if ( pic.attr("link") != "")
		pic.addClass("zoomCursorHomePage");
	
	// text
	var t = (curScaledProductPic+1) + " | " + $(".scaledImagesStripe .scaledImagesStripeMask:first .img").length;
	$(".scaledImagesStripe .controls:first .text:first").html( t );
}

function gotoToLink(e)
{
	var o = $(e.currentTarget);
	if (o.attr("link")=="" || !o.hasClass("currentPhoto") ) return;
	
	location.href= o.attr("link");
}

jQuery.fn.onImagesLoaded = function(_cb) { 
  return this.each(function() {
 
    var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
        _cont = this,
            i = 0,
    _done=function() {
      if( typeof _cb === 'function' ) _cb(_cont);
    };
 
    if( $imgs.length ) {
      $imgs.each(function() {
        var _img = this,
        _checki=function(e) {
          if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
          {
            if( ++i===$imgs.length ) _done();
          }
          else if( _img.readyState === undefined ) // dont for IE
          {
            $(_img).attr('src',$(_img).attr('src')); // re-fire load event
          }
        }; // _checki \\
 
        $(_img).bind('load readystatechange', function(e){_checki(e);});
        _checki({type:'readystatechange'}); // bind to 'load' event...
      });
    } else _done();
  });
};

