var deiza = {

	//Not working with 3x selections, need some corrections at the end
	selectVariant : function(oSelect){
		var sValue=$(oSelect).val();
		var sIteration=$(oSelect).attr("name");
		var iIteration=parseInt(sIteration.replace("v", ""));

	    //Get all available variants into MD array
		var aValues=new Array();
            $('#variant option').each(function(k,v){
                aValues[k]=$(this).text().replace(/(\s)+$/, "").split('|');
            });

		//Remove selections that does not suit for us from MD array
		$.each(aValues, function(i){
			if (aValues[i][iIteration]!=sValue) {
				aValues[i]='';
			}
		});

		var aSelectedValues=new Array();
		//Fill all selections with available options from MD array
		$('#mdFakeVariants select').each(function(i){
			aSelectedValues[i]=$(this).val();
			if (i!=iIteration) {
				var newValues=new Array();
				$.each(aValues, function(j){
					if (aValues[j][i]) {
						if ($.inArray(aValues[j][i],newValues)==-1) {
							newValues.push(aValues[j][i]);
						}
					}
				});
				var html='';
				$.each(newValues, function(k){
					html=html+'<option>'+newValues[k]+'</option>';
				});
				$(this).html(html);
			} else{
				$(this).html(aClonedHTML[i]);
			}
		});

		//Select the previous selected values
		var aSelectedNewValues=new Array();
		$('#mdFakeVariants select').each(function(i){
			$('option',this).each(function(j){
				if ($(this).val()==aSelectedValues[i]) {
					$(this).attr('selected',true);
				}
			});
			aSelectedNewValues.push($(this).val());
		});

		//Real variant select
		var sSelectedNewValues=aSelectedNewValues.join('|');
	    $('#variant option').each(function(i){
                if ($(this).text().replace(/(\s)+$/, "")==sSelectedNewValues)  $(this).attr('selected',true);
            });


		deiza.tasksAfterSelections();
		deiza.tasksToFakeSelections();
	},


    tasksAfterSelections: function(){
		var selected=$("#variant").find(":selected");
	var image=$(selected).attr('image');
        var code=$(selected).attr('code');
		var buyable=$(selected).attr('buyable');



		//Update Image
		if (image) {
			$("#productDetails .photo").attr("src",image);
		}

		//Update Price
		if ($("#productDetails .price .current").size()>0) {
			$("#productDetails .price .current").html( $( "#price-" + selected.attr( "value" ) ).html() );
        }

		//Update Product code
        if (code) {
            $("#productDetails .dynCode").html(code);
            $("#productDetails .productNr").removeClass("na");
        } else {
            $("#productDetails .productNr").addClass("na");
        }

		//Sho/hide buy button
        if (buyable=='1') {
            $("#productDetails .showBuyButton").removeClass("na");
        } else {
            $("#productDetails .showBuyButton").addClass("na");
        }

    },


	selectFakeVariant: function(object,selectName){
		//Check if link is not disabled
		var oLink=$(object).parent();
		if ($(oLink).hasClass("disabled")==true) {
			return deiza.ret_false();
		}

		var sValue=$(object).text();

		$('#mdFakeVariants select[name='+selectName+'] option').each(function(){
			if ($(this).text()==sValue) {
				$(this).attr("selected",true);
			}
		});

		deiza.selectVariant($("#mdFakeVariants select[name="+selectName+"]"));
		return deiza.ret_false();
	},


	tasksToFakeSelections: function(){

		//Sizes
		if ($("#mdFakeVariants .size").size()>0) {

			//Get current select options values
			var aSelectedNewValues=new Array();
			var sSelectedValue=$('#mdFakeVariants .size select').find(":selected").text();
			$('#mdFakeVariants .size select option').each(function(i){
				aSelectedNewValues.push($(this).text());
			});

			//Set to fake UL/LI
			$('#mdFakeVariants .size ul li').each(function(i){
				if ($.inArray($("a",this).text(),aSelectedNewValues)==-1) {
					$(this).addClass("disabled");
				} else {
					$(this).removeClass("disabled");
				}
				if ($("a",this).text()==sSelectedValue) {
					$(this).addClass("active");
				} else {
					$(this).removeClass("active");
				}
			});

		}


		//Colors
		if ($("#mdFakeVariants .color").size()>0) {

			//Get current select options values
			var aSelectedNewValues=new Array();
			var sSelectedValue=$('#mdFakeVariants .color select').find(":selected").text();
			$('#mdFakeVariants .color select option').each(function(i){
				aSelectedNewValues.push($(this).text());
			});

			//Set to fake UL/LI
			$('#mdFakeVariants .color ul li').each(function(i){
				if ($.inArray($("a",this).text(),aSelectedNewValues)==-1) {
					$(this).addClass("disabled");
				} else {
					$(this).removeClass("disabled");
				}
				if ($("a",this).text()==sSelectedValue) {
					$(this).addClass("active");
				} else {
					$(this).removeClass("active");
				}
			});

		}


	},

	selectListFilter: function(object,className,remove){
		var sId=$(object).attr('value');
		if (remove==1) {
			$("."+className+" option[value='']").attr("selected","selected");
		} else {
			$("."+className+" option[value='"+sId+"']").attr("selected","selected");
		}
		oxid.form.send('_filterlist');
		return deiza.ret_false();
	},


	checkRemoveCheckbox: function(object) {
        var parent=$(object).parent();
		$("input[type=checkbox]",parent).attr("checked",true);
        $("input[name=removeBtn]",parent).attr("disabled",false);
        $("#basketForm").submit();
        return deiza.ret_false();
    },


	removeOtherShippingPrams: function() {
		$(".otherShippingPrams").remove();
    },

    //Return false
    ret_false: function(){
        try
        {window.event.returnValue = false;} catch (error)
        {return false;}
    }

}



//On page load perform
$(document).ready(function(){

	$(".submit").click( function(event) {
		event.preventDefault();
		var form=$(this).closest("form");
		form.submit();
	});

	$("#menu li.firstLevel").hover(
		function () {
			$("#menu li.firstLevel.active").addClass("activeButNotShown");
			$("#menu li.firstLevel").removeClass("active");
		},
		function () {
			$("#menu li.firstLevel.activeButNotShown").addClass("active");
		}
	);



	//Perform on 4 step
	if ($(".blockL").size()>0 && $(".blockR").size()>0) {
		var blockLheight=$(".blockL").height();
		var blockRheight=$(".blockR").height();
		if (blockLheight>blockRheight) {
			var newheight=blockLheight;
		} else {
			var newheight=blockRheight;
		}
		if ($(".blockM").size()>0) {
			var blockMheight=$(".blockM").height();
			if (blockMheight>newheight) {
				var newheight=blockMheight;
			}
			$(".blockM").css("height",newheight+'px');
		}
		$(".blockL").css("height",newheight+'px');
		$(".blockR").css("height",newheight+'px');
	}

});
