var shopListWidth = 700;
var shopOffset = 10;
var maxShopsPerRow = 9;
var minShopsPerRow = 7;
var maxShopRows = 5;
var maxShopRowHeight = 50;

var firstRow = new Object();
var secondRow = new Object();
var thirdRow = new Object();
var fourthRow = new Object();
var fifthRow = new Object();

var shopList = new Object();

function initShopListDecorator(){
    initRows();
    shopList.firstRow = firstRow;
    shopList.secondRow = secondRow;
    shopList.thirdRow = thirdRow;
    shopList.fourthRow = fourthRow;
    shopList.fifthRow = fifthRow;
    shopList.numberOfItems = 0;
    getNumberOfItems();
    putShopsToRows();
}

function initRows(){
    var rowCount = 0;
    firstRow.IDList = new Array();
    firstRow.xOffsetList = new Array();
    firstRow.yOffsetList = new Array();
    firstRow.widthList = new Array();
    firstRow.heightList = new Array();
    firstRow.rowOffset = rowCount*maxShopRowHeight;
    rowCount++;
    secondRow.IDList = new Array();
    secondRow.xOffsetList = new Array();
    secondRow.yOffsetList = new Array();
    secondRow.widthList = new Array();
    secondRow.heightList = new Array();
    secondRow.rowOffset = rowCount*maxShopRowHeight;
    rowCount++;
    thirdRow.IDList = new Array();
    thirdRow.xOffsetList = new Array();
    thirdRow.yOffsetList = new Array();
    thirdRow.widthList = new Array();
    thirdRow.heightList = new Array();
    thirdRow.rowOffset = rowCount*maxShopRowHeight;
    rowCount++;
    fourthRow.IDList = new Array();
    fourthRow.xOffsetList = new Array();
    fourthRow.yOffsetList = new Array();
    fourthRow.widthList = new Array();
    fourthRow.heightList = new Array();
    fourthRow.rowOffset = rowCount*maxShopRowHeight;
    rowCount++;
    fifthRow.IDList = new Array();
    fifthRow.xOffsetList = new Array();
    fifthRow.yOffsetList = new Array();
    fifthRow.widthList = new Array();
    fifthRow.heightList = new Array();
    fifthRow.rowOffset = rowCount*maxShopRowHeight;
}

function getNumberOfItems(){
    shopList.numberOfItems = $("#shops_container ul li").length;
}

function putShopsToRows(){
    var currentRowWidth = 0;
    var currentRow = 0;
    var currentItemCount = 0;
    $(".shoplist li img").each(function(index){	
	switch (currentRow) {
	    case 0:
		if(!((currentRowWidth+$(this).width())>=shopListWidth-shopList.firstRow.IDList.length*shopOffset)&&shopList.firstRow.IDList.length<=maxShopsPerRow-1){
		    shopList.firstRow.IDList[index] = $(this).attr("id");
		    shopList.firstRow.widthList[index] = $(this).width();
		    shopList.firstRow.heightList[index] = $(this).height();
		    currentRowWidth+=$(this).width();
		}else{
		    currentRowWidth = 0;
		    currentItemCount = shopList.firstRow.IDList.length;
		    currentRow++;
		}
		break;
	    case 1:
		if(!((currentRowWidth+$(this).width())>=shopListWidth-shopList.secondRow.IDList.length*shopOffset)&&shopList.secondRow.IDList.length<=maxShopsPerRow-1){
		    shopList.secondRow.IDList[index-currentItemCount] = $(this).attr("id");
		    shopList.secondRow.widthList[index-currentItemCount] = $(this).width();
		    shopList.secondRow.heightList[index-currentItemCount] = $(this).height();
		    currentRowWidth+=$(this).width();
		}else{
		    currentRowWidth = 0;
		    currentItemCount+=shopList.secondRow.IDList.length;
		    currentRow++;
		}
		break;
	    case 2:
		if(!((currentRowWidth+$(this).width())>=shopListWidth-shopList.thirdRow.IDList.length*shopOffset)&&shopList.thirdRow.IDList.length<=maxShopsPerRow-2){
		    shopList.thirdRow.IDList[index-currentItemCount] = $(this).attr("id");
		    shopList.thirdRow.widthList[index-currentItemCount] = $(this).width();
		    shopList.thirdRow.heightList[index-currentItemCount] = $(this).height();
		    currentRowWidth+=$(this).width();
		}else{
		    currentRowWidth = 0;
		    currentItemCount+=shopList.thirdRow.IDList.length;
		    currentRow++;
		}
		break;
	    case 3:
		if(!((currentRowWidth+$(this).width())>=shopListWidth-shopList.fourthRow.IDList.length*shopOffset)&&shopList.fourthRow.IDList.length<=maxShopsPerRow-2){
		    shopList.fourthRow.IDList[index-currentItemCount] = $(this).attr("id");
		    shopList.fourthRow.widthList[index-currentItemCount] = $(this).width();
		    shopList.fourthRow.heightList[index-currentItemCount] = $(this).height();
		    currentRowWidth+=$(this).width();
		}else{
		    currentRowWidth = 0;
		    currentItemCount+=shopList.fourthRow.IDList.length;
		    currentRow++;
		}
		break;
	    case 4:
		if(!((currentRowWidth+$(this).width())>=shopListWidth-shopList.fifthRow.IDList.length*shopOffset)&&shopList.fifthRow.IDList.length<=maxShopsPerRow-1){
		    shopList.fifthRow.IDList[index-currentItemCount] = $(this).attr("id");
		    shopList.fifthRow.widthList[index-currentItemCount] = $(this).width();
		    shopList.fifthRow.heightList[index-currentItemCount] = $(this).height();
		    currentRowWidth+=$(this).width();
		}else{
		    currentRowWidth = 0;
		    currentItemCount+=shopList.fifthRow.IDList.length;
		    currentRow++;
		}
		break;
	    default:
		break;
	}
    });
//    alert("# of items in first row: "+shopList.firstRow.IDList.length+
//	"\n# of items in second row: "+shopList.secondRow.IDList.length+
//	"\n# of items in third row: "+shopList.thirdRow.IDList.length+
//	"\n# of items in fourth row: "+shopList.fourthRow.IDList.length+
//	"\n# of items in fifth row: "+shopList.fifthRow.IDList.length);
    alert("image widths, first row: \n"+getWidths());
}
function calcXOffset(){

}

function getWidths(){
    var output = "";
    for(var i=0;i<shopList.firstRow.widthList.length; i++){
	output+= "widthList["+i+"] = "+shopList.firstRow.widthList[i]+"\n";
    }
    return output;
}


function resizeLogos (){
				
	//alert ($("window").scrollTop());
	$("html").scrollTop(0);
	newHeight = $("body").height()- $("#shops_container").offset().top;
	
	if(newHeight < 180) {
		newHeight = 180;
		$("body").css("overflow", "auto")
	}
	else {
		$("body").css("overflow", "hidden");
	}
	
	if (newHeight > 300)
		newHeight = 300;

	$("#shops_container").css("height", newHeight+"px");
        $("#main_popup").css ("top", "-" + (newHeight + $("#main_popup").height() - 135) + "px");
	
	rowOffset = (newHeight - 6) / 5;
	
	$(".firstRow").each (function(){
		$(this).css("top", 0*rowOffset + "px");
		$(this).css("padding-top", "6px");
	});
	
	$(".secondRow").each (function(){
		$(this).css("top", 1*rowOffset + "px")
		$(this).css("padding-top", "6px");
	});
	
	$(".thirdRow").each (function(){
		$(this).css("top", 2*rowOffset + "px")
		$(this).css("padding-top", "6px");
	});
	
	$(".fourthRow").each (function(){
		$(this).css("top", 3*rowOffset + "px")
		$(this).css("padding-top", "6px");
	});
	
	$(".fifthRow").each (function(){
		$(this).css("top", 4*rowOffset + "px")
		$(this).css("padding-top", "6px");
	});
	
}


