
// Related Accessories properties & methods incapsulating object

var REL_ACC = new Object();

// custom fields for related accessories
REL_ACC.customField1136 = "";
REL_ACC.customField1202 = "";

// total subcontainers count
REL_ACC.subContainersCounter = 0;

// medium page width
REL_ACC.pageMediumPartWidth = 511;

// check if need to set bottom border for subcontainer in case of single finishing div
REL_ACC.needToSetBottomBorderForSingleDiv = false;

// total related accessories count
REL_ACC.relAccTotalCount = 0;

//REL_ACC.browserTypeIE = Prototype.Browser.IE ? true : false;
REL_ACC.browserTypeIE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;

/*
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;
*/

// returns related accessories total count on the basis of passed string
REL_ACC.getRelAccCount = function(relAccStr)
{
   var splittedArray = relAccStr.split(";");
   var splittedArrayLength = splittedArray.length - 2;

   return (splittedArrayLength > 0) ? splittedArrayLength : 0;
}

// calculates total subcontainers counter to use it for bottom border drawing
REL_ACC.getSubContainersCount = function()
{
   var totalSubcontainersCount = 0;

   if (this.relAccTotalCount <= 4)
   {
      if (this.relAccTotalCount == 4)
         totalSubcontainersCount = 2;
      else
         totalSubcontainersCount = 1;
   }
   else
   {
      totalSubcontainersCount = this.relAccTotalCount / 3;
      var totalSubcontainersCountRest = this.relAccTotalCount % 3;
      if (totalSubcontainersCountRest > 0)
         totalSubcontainersCount++;
   }

   return parseInt(totalSubcontainersCount);
}

// calculates single div with rel acc width
REL_ACC.calculateRelAccSingleDivWidth = function(currentCounter)
{
   var resultWidth = 0;
   var needRightBorder = 0;

   if (this.relAccTotalCount == 1)
   {
       // set total width
       resultWidth = this.pageMediumPartWidth;
       this.needToSetBottomBorderForSingleDiv = true;                
   }
   else
   {
       // check current rel acc counter and total rel accessories count
       // check if total number of related accessories is even
       if (this.relAccTotalCount <= 4)
       {
          // check if it's not divided by 3 (4 etc.)
          if (this.relAccTotalCount % 3 != 0)
          {
             resultWidth = this.pageMediumPartWidth / 2;
             if (currentCounter % 2 != 0)
             {
                needRightBorder = 1;
             }
          }
          else
          {
             // 6 etc.
             resultWidth = this.pageMediumPartWidth / 3;
             if (currentCounter % 3 != 0)
             {
                needRightBorder = 1;
             }
          }
       }
       else
       {
          var restByThreeDivide = this.relAccTotalCount % 3;
          var restReverted = this.relAccTotalCount - restByThreeDivide;

          if (currentCounter <= restReverted)
          {
             resultWidth = this.pageMediumPartWidth / 3;
             if (currentCounter % 3 != 0)
             {
                needRightBorder = 1;
             }
          }
          else
          {
             if (restByThreeDivide == 1)
             {
                resultWidth = this.pageMediumPartWidth;
                this.needToSetBottomBorderForSingleDiv = true;                
             }
             else if (restByThreeDivide == 2)
             {
                resultWidth = this.pageMediumPartWidth / 2;
                if (currentCounter == restReverted + 1)
                {
                   needRightBorder = 1;
                }
             }
          }
       }
   }

   // set single div width
   $('rel_acc_div_single_' + currentCounter).style.width = resultWidth + "px";

   // check if needed right border for single div
   if (needRightBorder)
   {
       $('rel_acc_div_single_' + currentCounter).style.borderRight = "1px solid rgb(230, 230, 230)";
   }
}

// checks if need to set bottom border of the subcontainer
REL_ACC.setSubContainerBottomBorder = function(currentSubContainerCounter)
{
   // check if needed bottom border
   var totalSubContainersCount = this.getSubContainersCount();
   var currentSubContainer = $('rel_acc_div_sub_container_' + currentSubContainerCounter);

   if ((currentSubContainerCounter < totalSubContainersCount || this.needToSetBottomBorderForSingleDiv) && currentSubContainer != null)
   {
      currentSubContainer.style.borderBottom = "1px solid rgb(230, 230, 230)";
   }
}

// internal function for containers closing/opening, border & overflow setting
REL_ACC.processContainersCreation = function()
{
    // check if need to 
    // 1) close previous containers tags
    // 2) set bottom border for previous sub container
    // 3) set overflow hidden in all cases except IE
	this.setSubContainerBottomBorder(this.subContainersCounter);
	this.subContainersCounter++;
	var documentWriteContent = '</div></div><div class="rel_acc_div_container" id="rel_acc_div_container_' + this.subContainersCounter + '"><div class="rel_acc_div_sub_container" id="rel_acc_div_sub_container_' + this.subContainersCounter + '">';
	document.write(documentWriteContent);

	//if (!REL_ACC.browserTypeIE7)
	//{
		$('rel_acc_div_sub_container_' + this.subContainersCounter).style.overflow = 'hidden';
	//}
}

// set bottom border if needed
REL_ACC.addContainers = function(currentRelAccCounter)
{
	if (this.relAccTotalCount <= 4)
	{
        if (this.relAccTotalCount % 3 == 0)
        {
            if (currentRelAccCounter % 3 == 1)
			{
				this.processContainersCreation();
            }
   	    }
   	    else
   	    {
            if (currentRelAccCounter % 2 == 1)
            {
				this.processContainersCreation();
            }
   	    }
	}
	else
	{
        if (currentRelAccCounter % 3 == 1)
		{
			this.processContainersCreation();
		}
	}
}

REL_ACC.initContainers = function()
{
    if (this.relAccTotalCount > 0)
    {
       document.write('<div class="rel_acc_div_container" id="rel_acc_div_container_0" style="display: none;"><div class="rel_acc_div_sub_container" id="rel_acc_div_sub_container_0">');
    }
}

REL_ACC.closeContainers = function()
{
    if (this.relAccTotalCount > 0)
    {
       document.write('</div></div>');
       // check if need to set subcontainer bottom border in case of single finishing div
       this.setSubContainerBottomBorder(this.subContainersCounter);
    }
}

