optArray = new Array();

$(document).ready(function(){

  /* Color Chooser Implementation */
  
  $(".eshop_colorChooser").each(function(){
    pDiv = $(this);
    xOpt = '';
    $(this).parent().find(".eshop_OptionDiv:eq(0) div").each(function() {
      xOpt = $(this).attr("opt");
      pDiv.append('<div class="eshop_ColorDiv" style="background-color:'+xOpt+'" opt="'+xOpt+'"></div>');
    }); 
  });
  
  
  $(".eshop_colorChooser .eshop_ColorDiv").click(function(){
    xParent = $(this);
    
    $(this).parent().find("DIV").removeClass("eshop_ColorDivActive");
    
    
    $(this).parent().parent().find(".eshop_OptionDiv:eq(0) div").each(function() {
      if(xParent.attr("opt") == $(this).attr("opt")) clickEvent($(this));
    });
    

    xParent.addClass("eshop_ColorDivActive");
  });
  
  
  $(".eshop_OptionDiv div").click(function(e){
    
    clickEvent($(this));
    $("div.eshop_OptionDiv").slideUp("fast");
  });
  
  
  $(".eshop_OptActive").click(function(){
    //alert('xx');
    $(this).parent().find("div.eshop_OptionDiv").slideToggle("fast");
  });
  
  $("document.body").click(function(){
    //alert('xx');
    $("div.eshop_OptionDiv").slideUp("fast");
  });
  
  $(document.body).click(function(e){
    
    // Hide Quuick Access DIV on outside Click
    if($(e.target).is(":not(div.eshop_OptChooser *)"))
    {
        $("div.eshop_OptionDiv").slideUp("fast");
    }
  });

  
  $(".eshop_addcart a").click(function(){
  
        xParent = $(this).parent().parent();
        ctuid = xParent.find(".eshop_OptCombination").attr("ctuid");
        xcs = optArray[ctuid];
        
        
        defined = 0;
        xParent.find(".eshop_OptCombination div").each(function(){
            if($(this).attr("value") == xcs) defined = 1;
        });
        
        if(defined) document['article_submit_' +  ctuid].submit();
        else
        {
            $("#eshop_overlay").fadeIn("fast",function(){
                  $("#eshop_message_"+ctuid).show("fast");
            });
        }    
  });
  
  $(".eshop_message a").click(function(e){
        $("#eshop_message_"+ctuid).hide("fast",function(){
                   $("#eshop_overlay").fadeOut("fast");
        });
        
        e.preventDefault();
  });
  
  
  // Preset Options
  
  $(".tx-easyshop-pi3 .eshop_OptCombination").each(function(){
        
        xParent = $(this);
        setValues(xParent.parent().parent(),1);
        
        xValue = xParent.find("div:eq(0)").attr("value");
        optArray[xParent.attr("ctuid")] = xValue;
        
        sValue = xValue.split(",");
        for(i=0;i<sValue.length;i++)
        {
            clickEvent(xParent.parent().find(".eshop_OptionDiv:eq("+i+") div:eq(0)")); 
        }
  });
  
  $("body").append('<div id="eshop_overlay"></div>');
  arrayPageSize = getPageSize();
  $("#eshop_overlay").css({opacity:0.8,height:arrayPageSize[1]+'px'});
  
  
  checkEmptyChoosers();
  
});


function setValues(jQueryObject,defined)
{
        readOpt = jQueryObject.find(".eshop_OptCombination div:eq("+(defined-1)+")").text();
        xOpt = readOpt.split("|");
        if(xOpt[0] != '') jQueryObject.parent().find(".uidInput").attr("value",xOpt[0]);
        if(xOpt[1] != '') jQueryObject.parent().find(".nameInput").attr("value",xOpt[1]);
        if(xOpt[2] != '') jQueryObject.parent().find(".priceInput").attr("value",xOpt[2]);
        if(xOpt[2] != '') jQueryObject.parent().find(".priceDisplay").text(xOpt[2]);
        if(xOpt[3] != '') jQueryObject.parent().find(".shippingInput").attr("value",xOpt[3]);
        if(xOpt[4] != '') jQueryObject.parent().find(".optionsInput").attr("value",xOpt[4]);
}


function getPageSize() {
		    var jqueryPageSize = new Array($(document).width(),$(document).height(), $(window).width(), $(window).height());
		    return jqueryPageSize;
};

function checkEmptyChoosers()
{
        $(".tx-easyshop-pi3 .eshop_OptChooser").each(function(){
            $(this).css("visibility","visible");
            if($(this).find("div.eshop_Opt_Active").size() == 0) $(this).css("visibility","hidden");
        }); 
        
        $(".tx-easyshop-pi3 .eshop_OptChooser").each(function(){
            if($(this).find("div.eshop_Checked").size() == 0)
            {
              $(this).find("div.eshop_OptActive").text($(this).attr("defaultValue"));
              $(this).find("div.eshop_OptActive").addClass("eshop_Attention");
            }
        }); 
}


function clickEvent(xParent)
{
    //xParent = $(this);
    
    // Options 
    clickValue = xParent.attr("value");
    xnum = xParent.parent().parent().attr("num");
    xParent.parent().find("div").removeClass("eshop_Checked");
    xParent.addClass("eshop_Checked");
    xParent.parent().parent().parent().find(".eshop_OptActive:eq("+xnum+")").text(clickValue);
    xParent.parent().parent().parent().find(".eshop_OptActive:eq("+xnum+")").removeClass("eshop_Attention");
    
    ctuid = xParent.parent().parent().parent().find(".eshop_OptCombination").attr("ctuid");
    optArray[ctuid] = new Array();
    
    k=0;
    
    xParent.parent().parent().parent().find(".eshop_OptCombination div").each(function(){
        ctuid = $(this).parent().attr("ctuid");
        xValue = $(this).attr("value");
        aValue = xValue.split(","); 
        optArray[ctuid][k] = aValue;
        k++; 
    });
    
    
    // Check 
    z=0;
    xParent.parent().parent().parent().find(".eshop_OptionDiv div").addClass("eshop_Opt_Inactive").removeClass("eshop_Opt_Active");
    xParent.parent().parent().parent().find(".eshop_OptCombination div").each(function(){
        if(optArray[ctuid][z][xnum] == clickValue)
        {
            //xParent.parent().parent.parent().find(".eshop_OptionDiv div").addClass("eshop_Opt_Inactive");
            cols = optArray[ctuid][z].length;
            
            for(m=0;m<cols;m++)
            {
                xParent.parent().parent().parent().find(".eshop_OptionDiv:eq("+m+") div[value="+optArray[ctuid][z][m]+"]").removeClass("eshop_Opt_Inactive").addClass("eshop_Opt_Active");
            }
            
        }
        z++;
    });
    
    // Remove Checked if this item is not available
    xParent.parent().parent().parent().find(".eshop_OptionDiv .eshop_Opt_Inactive").removeClass("eshop_Checked");
    
    
    xCustomer = new Array();
    xParent.parent().parent().parent().find(".eshop_OptionDiv div.eshop_Checked").each(function(){
      
      xCustomer[xCustomer.length] = $(this).attr("value");
    });
    
    
    // Load Data
    xcs = xCustomer.join(",");
    optArray[ctuid] = xcs;
    
    defined = 0;
    l=1;
    xParent.parent().parent().parent().find(".eshop_OptCombination div").each(function(){
        if($(this).attr("value") == xcs) defined = l;
        l++;
    });
    
    if(defined)
    {
        setValues(xParent.parent().parent().parent(),defined);
    }
    
    checkEmptyChoosers();
}

