//window.customAdditionalImages = false;
$(document).ready(function(){
	//visa namn på gruppsidan men inte beskrivning
	$(".spaltgruppfalttabell").each(function(){
		$(this).find(".spaltgruppfaltrad:first").show();
	});
  //flytta kundvagn och sök
  $(".pnlComponents").prepend($(".pnlMiniBask"));
  $(".pnlComponents").prepend($(".pnlFind"));
  
  //infolänkar på prod
  if ($("body").hasClass("prod")){
	$(".pnlProdOrder").append($(".pnlMarketing"));
	$(".pnlMarketing").show();
  }
  
  if ($("body").hasClass("page") && $(".pnlFind").length==0){
	//alert($(".pnlMain").outerWidth() + " " + $(".pnlBody").outerWidth());
	//$(".pnlComponents").css("min-width", ($(".pnlBody").outerWidth()-$(".pnlMain").outerWidth())/2 + "px");
  }

  //centrera sidnavigering
  var headerWidth = $(".pnlPageheader").width();
  var menuWidth = $(".pnlPageNavigation").outerWidth();
  $(".pnlPageNavigation").css("padding-left",(headerWidth-menuWidth)/2+"px");
  
  //popupbilder på dept
  if ($("body").hasClass("dept")){
    $(".tceMiniBildImg img").each(function(){
        var src = $(this).attr("src");
        try{
            var bigSrc = src.substring(0,src.lastIndexOf(".")-1);
            var img = $("<img />");
            img.attr("src" , bigSrc + "l.jpg?r=" + Math.random()*99999);
            img.attr("type","jpg").css("width","500px").css("height","500px");
            img.load({pIMG:$(this)},function(e){
                $(this).addClass("deptPopupImage");
                $("body").append($(this));
                e.data.pIMG.mouseenter({lIMG:$(this)},function(e){
					e.data.lIMG.css("top",(e.pageY-(e.data.lIMG.outerHeight()/2)) +"px");
					var rightOverflow = ($(e.target).offset().left+$(e.target).outerWidth()+2+e.data.lIMG.outerWidth())-($(window).width()+$(window).scrollLeft());
					//alert(($(e.target).offset().left+$(e.target).outerWidth()+2+e.data.lIMG.outerWidth()) + " " + ($(window).width()+$(window).scrollLeft()));
					if (rightOverflow>0){
						//var leftOverflow = ($(e.target).offset().left-2-e.data.lIMG.outerWidth())+$(window).scrollLeft();
						//if (rightOverflow>leftOverflow){
							e.data.lIMG.css("left",($(e.target).offset().left-2-e.data.lIMG.outerWidth()) + "px");
						//}else{
						//	e.data.lIMG.css("left",($(e.target).offset().left+$(e.target).outerWidth()+2) + "px");
						//}
					}else{
						e.data.lIMG.css("left",($(e.target).offset().left+$(e.target).outerWidth()+2) + "px");	
					}
					e.data.lIMG.show();
                }).mouseleave({lIMG:$(this)},function(e){
                    e.data.lIMG.hide();
                });
            });
            img.error(function(){
                if ("jpg" == $(this).attr("type")){
                    $(this).attr("src" , bigSrc + "l.gif?r=" + Math.random()*99999);
                    $(this).attr("type","gif");
                }
            });
        }catch(err){
        }
    });
  }
	
  //bildspel
  //<div id="welcomeImageRotate" iName="ita-pict" iType="jpg" rotateTime="4000" transitionTime="1500"></div>
  var imgRotate = $("#welcomeImageRotate");
  if (imgRotate.length>0){
    imageLoad("#" + imgRotate.attr('id'),imgRotate.attr('iName'),imgRotate.attr('iType'),imgRotate.attr('rotateTime'),imgRotate.attr('transitionTime'),true);
  }

  //Sökknapp reagerar på returtangent
  $(".pnlFind .txtWithDefault").keypress(function(e){
    if (e.keyCode == 13){
      shopFindPanelFind($(".pnlFind .txtWithDefault").attr('id'));
      return false;
    }
  });
  
  if ($("body").hasClass("prod")){
	$(".imgProdImage").load(function(){
		
	});
	$(".hlkProdImage img").load(function(){
		var width = $(this).outerWidth();
		var height = $(this).outerHeight();
		if (width>height){
			$(this).css("width",350);
		}else{
			$(this).css("height",350);
		}
		
	});
  }
});

var imgList = new Array();
var imgIndex = 0;
function rotateImages(){
	var oldImgIndex = imgIndex;
	imgIndex++;
	if (imgIndex >= imgList.length){
		imgIndex = 0;
	}
	imgList[oldImgIndex].animate({opacity: 'toggle'},parseInt(imgList[oldImgIndex].attr("tTime")));
	imgList[imgIndex].animate({opacity: 'toggle'},parseInt(imgList[imgIndex].attr("tTime")));
	setTimeout(rotateImages,parseInt(imgList[imgIndex].attr("rTime")));
}
function imageLoaded(id,img){
	imgList[imgList.length] = img
	$(id).append(img);
	if (1 == imgList.length){
		imgList[0].show();
		$(id).css("width",img.outerWidth() + "px");
		$(id).css("height",img.outerHeight() + "px");
	}
	else{
		img.hide();
	}
}
function imageLoad(id,name,ext,rTime,tTime,run){
	var img = $("<img />");
	img.attr("src" , "/shop/assets/shop_images/" + name + imgList.length + "." + ext + "?r=" + Math.random()*99999);
	img.attr("rTime",rTime);
	img.attr("tTime",tTime);
	img.load(function(){
		imageLoaded(id,$(this));
		if (run){
			setTimeout(rotateImages,rTime/2);
		}
		imageLoad(id,name,ext,rTime,tTime,false);
	});
	img.error(function(){
		//setTimeout(rotateImages,rTime);
	});
}

