var attributeMode = $("#attributeMode").val();
$(document).ready(function(){

setInputTypeConstraints();
$(document).on("click","#add_new_options", function ()
{
	$("#optionGrid").removeClass("hide");
	loadOptions('I');
});
$(document).on("click","#add_new_options_vd", function ()
{
	$("#optionGridVd").removeClass("hide");
	loadOptions('V');
});

$(document).on("click",".deleteRow", function ()
{
if ($(".attributeOptionRow").not('.hide').length > 1 && !$(this).hasClass('delete-inactive'))
{
var element = $(this).parent().parent();
var row_is_delete_id = $(this).attr('rel');

jConfirm(msgSelectAtleastOne, 'Delete', function (deletedConfirm)
{
if (deletedConfirm == true)
{

$("#"+row_is_delete_id).val('1');
element.addClass("hide");
element.addClass("nodrag");
element.addClass("nodrop");
element.find('.hid_err_formAttributeAdd_validations').val(0);
if ($(".attributeOptionRow").not('.hide').length == 0)
{
$("#optionGrid").addClass("hide");
}

hideDeleteButton('I');
setTabelDrag('I');

}
});
}
}); 

$(document).on("click",".deleteRowVd", function ()
{
if ($(".attributeOptionRowVd").not('.hide').length > 0 && !$(this).hasClass('delete-inactive'))
{
var element = $(this).parent().parent();
var row_is_delete_id = $(this).attr('rel');

jConfirm(msgSelectAtleastOne, 'Delete', function (deletedConfirm)
{
if (deletedConfirm == true)
{

$("#"+row_is_delete_id).val('1');
/* $(".attributeOptionRow").addClass("hide");
$(".attributeOptionRow").addClass("nodrag");
$(".attributeOptionRow").addClass("nodrop");
 */
element.addClass("hide");
element.addClass("nodrag");
element.addClass("nodrop");
element.find('.hid_err_formAttributeAdd_validations').val(0);
/*if ($(".attributeOptionRowVd").not('.hide').length == 0)
{
$("#optionGridVd").addClass("hide");
}*/
hideDeleteButton('V');
setTabelDrag('V');
//hideDeleteButton('I');
//setTabelDrag('I');
}
});
}

});





$(document).on("click",".isDefaultCheckbox", function ()
{
var inputType = $("#inputType").val();	
if(inputType=='select')
{
if($(this).prop("checked"))
{
if ($(".attributeOptionRow").not('.hide').length > 1)
{
$(".isDefaultCheckbox").attr("checked",false);
$(this).attr("checked",true);
}
} 	
	
}
else if(inputType=='textbox' || inputType=='textarea' || inputType=='date' || inputType=='datetime')
{
	$(this).attr("checked",true);	
}
});


$('form[name="formAttributeAdd"]').bind('submit',function() {
  var chk = checkAttributeOptions();
  if(!chk)
  return false;
});

$("#validateRuleRequired").on("click",function(){
vaidateMessageValidation();	
});
$("#validateRule").on("change",function(){
vaidateMessageValidation();	
});
$("#validationMessage").on("blur",function(){
vaidateMessageValidation();	
});
$("#validationMessage").on("change",function(){
vaidateMessageValidation();	
});
});

function setInputTypeConstraints()
{
	var inputType = 'select';
	$(".attributeOptionsSections").show();					
        $(".forSelectType").show();	
        $("#optionGrid").removeClass("hide");
        $("#optionGridVd").removeClass("hide");
        
        if(attributeMode=='new' || ($("#attrOptionAddCount").val()<=0  && attributeMode!='new') || ($("#attrOptionVdAddCount").val()<=0  && attributeMode!='new'))
        {
              
                
                if(attributeMode=='new' || ($("#attrOptionAddCount").val()<=0  && attributeMode!='new'))
                {
                    setRowDelete('I');
                    loadOptions('I'); 
                }
                if(attributeMode=='new' || ($("#attrOptionVdAddCount").val()<=0  && attributeMode!='new'))
                {
                    setRowDelete('V');
                    loadOptions('V'); 
                }
        }
        else
        {
                setTabelDrag('I');
                hideDeleteButton('I');	
                setTabelDrag('V');
                hideDeleteButton('V');	
        }		
					
	
	setFileTypeFields();
	setCommonTypeFields();
	if(inputType=='textbox' || inputType=='textarea')
	{
		removeDisabled("#validateRule");
		removeDisabled("#value");
		$(".attributeOptionsSections").hide();	
	}
	else
	{
		setDisabled("#validateRule");
		$("#validateRule").val('');		
	}
}
function setDisabled(domField)
{
	$(domField).attr("disabled","disabled");
	$(domField).addClass("disabled");		
}
function removeDisabled(domField)
{
	$(domField).removeAttr("disabled");
	$(domField).removeClass("disabled");		
}
function setReadonly(domField)
{
	$(domField).attr("readonly","readonly");
			
}
function removeReadonly(domField)
{
	$(domField).removeAttr("readonly");
			
}



function loadOptions(arg) 
{
	
	if(postCallback=="1")
	{
		postCallback = '';
		setTabelDrag(arg);
		hideDeleteButton(arg);		
		return false;
	}	
	
	$(".attributeOptionsSections").show();	
	$("#attroptions_loader_"+arg).removeClass("hide");
        if(arg=='V'){
            var attrOptionCount = $("#attrOptionVdAddCount").val();
        } else {
            var attrOptionCount = $("#attrOptionAddCount").val();
        }
    $.ajax({
        type: "POST",
		async:false,
        url: GLOBAL_BASE_PATH + '/watchvideo/ajaxLoadOptions/',
        data: {
                        "attrOptionCount": attrOptionCount,"filetype":arg
        },
        success: function (data) {		
            $("#attroptions_loader_"+arg).addClass("hide");
            attrOptionCount = parseInt(attrOptionCount) + 1;
           
            if(arg=='V'){
                $("#attrOptionVdAddCount").val(attrOptionCount);
            } else {
                $("#attrOptionAddCount").val(attrOptionCount);
            }
            $('#attroptions_loader_'+arg).before(data)
            $("#attroptions_loader_"+arg).addClass("hide");
	    
	   $(".variant_image").show();			

	
            setTabelDrag(arg);
            hideDeleteButton(arg);
			
        }
    });
}

function setRowDelete(arg)
{
	if(postCallback=="1")
	return false;
        if(arg=='I'){
            $(".attributeOptionsRows").val('1');	
            $(".attributeOptionRow").addClass("hide");
            $(".attributeOptionRow").addClass("nodrag");
            $(".attributeOptionRow").addClass("nodrop");
        } else {
            $(".attributeOptionsRowsVd").val('1');	
            $(".attributeOptionRowVd").addClass("hide");
            $(".attributeOptionRowVd").addClass("nodrag");
            $(".attributeOptionRowVd").addClass("nodrop");
        }
}

function setTabelDrag(argvalue)
{
	setPriority(argvalue);
        if(argvalue=='I') {            
            if ($(".attributeOptionRow").not('.hide').length > 1)
            {		
                    
                    $("#tableElm").tableDnD({
                    onDragClass: "myDragClass",
                    onDrop: function(table, row) {
                    var	Order	=	escape($.tableDnD.serialize());		
                    Order	=	Order.replace(/tableElm%5B%5D%3D/g,'');
                    Order	=	Order.replace(/%26/g,'|');
                    saveListOrder(Order,argvalue);
                    }		
                    });			
            }
            else
            {
                    $(".attributeOptionRow").not('.hide').css("cursor","default");	
            }
        } else {
            
             if ($(".attributeOptionRowVd").not('.hide').length > 1)
            {		
                    $("#tableElmV").tableDnD({
                    onDragClass: "myDragClass",
                    onDrop: function(table, row) {
                    var	Order	=	escape($.tableDnD.serialize());		
                    Order	=	Order.replace(/tableElmV%5B%5D%3D/g,'');
                    Order	=	Order.replace(/%26/g,'|');
                    saveListOrder(Order,argvalue);
                    }		
                    });			
            }
            else
            {
                    $(".attributeOptionRowVd").not('.hide').css("cursor","default");	
            }
        }
}

function saveListOrder(order,argvalue)
{
	if(order)
	{
		var orderList=order.split("|");	
		var priority = 1;		
		for (var i=0;i<orderList.length;i++)
		{ 
			if(orderList[i]!='' && orderList[i]!='attroptions_loader_'+argvalue)
			{
				if(!$("#"+orderList[i]).hasClass('hide'))
				{
					var currentID = orderList[i].split("_");
                                        if(argvalue=='I') {
                                            $("#attrOptionsGroup_"+currentID[1]+"_sortOrder").val(priority);	
                                        } else {
                                            $("#attrOptionsGroupVd_"+currentID[1]+"_sortOrder").val(priority);	
                                        }
					priority++;	
				}				
			}
		}
	}
}

function hideDeleteButton(argValue)
{	if(argValue=='I') {
        
            if ($(".attributeOptionRow").not('.hide').length > 1)
            {
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").removeClass('delete03-inactive');
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").addClass('delete03');		
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").show();	
            }
            else
            {
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").removeClass('delete03');
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").addClass('delete03-inactive');		
            }
        } else {
            
            if ($(".attributeOptionRowVd").not('.hide').length > 0 )
            {
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").removeClass('delete03-inactive');
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").addClass('delete03');		
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").show();	
            }
            else
            {
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").removeClass('delete03');
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").addClass('delete03-inactive');		
            }
        }
}
function hideDeleteButtonVideo()
{	//if(argValue=='I') {
        
            if ($(".attributeOptionRow").not('.hide').length > 1)
            {
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").removeClass('delete03-inactive');
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").addClass('delete03');		
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").show();	
            }
            else
            {
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").removeClass('delete03');
                    $(".attributeOptionRow").not('.hide').find(".deleteRow").addClass('delete03-inactive');		
            }
       // } else {
            
            if ($(".attributeOptionRowVd").not('.hide').length > 0 )
            {
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").removeClass('delete03-inactive');
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").addClass('delete03');		
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").show();	
            }
            else
            {
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").removeClass('delete03');
                    $(".attributeOptionRowVd").not('.hide').find(".deleteRowVd").addClass('delete03-inactive');		
            }
       // }
}
function setPriority(argvalue)
{
	var priority = 1;
        if(argvalue=='I') {
            $(".attributeOptionRow").each(function(index, element) {
                if(!$(this).hasClass('hide'))
                        {
                                var currentID = $(this).attr("id");
                                var currentRow = currentID.split("_");
                                $("#attrOptionsGroup_"+currentRow[1]+"_sortOrder").val(priority);
                                priority++;			
                        }
            });	
        } else {
            $(".attributeOptionRowVd").each(function(index, element) {
                if(!$(this).hasClass('hide'))
                        {
                                var currentID = $(this).attr("id");
                                var currentRow = currentID.split("_");
                                $("#attrOptionsGroupVd_"+currentRow[1]+"_sortOrder").val(priority);
                                priority++;			
                        }
            });	
        }
}
function setFileTypeFields()
{
	
	$(".forFileType").hide();
        $(".forFileTypeSection").val('');
        setDisabled(".forFileTypeSection");	
	
}
function checkAttributeOptions()
{
	var optionTitle = new Array();
	$('input[fieldname="varValue"]:visible').each(function(index, element) {
		var optionName = $(this).val();
		optionName = $.trim(optionName);
		if(optionName!='')
		optionTitle.push(optionName);
    });
	var arrayLength = optionTitle.length;	
	if($.unique(optionTitle).length!=arrayLength && arrayLength>0)
	{
		jAlert(msgSelectDifferentNamesoption);
		return false;
	}
	return true;
}


function setCommonTypeFields()
{
	$(".forCommonType").hide();
        $(".forCommonTypeSection").val('');
        setDisabled(".forCommonTypeSection");	
	
}

 function deletevariantImages(opid,imageType) {
	var curr_id = Math.random();
var fileName = $("#OptionVariant"+imageType+"_"+opid+"_temp").val();
var nofile = $("#no_image_path").val();
        jConfirm(msgimageDelConfirm, 'Delete', function (r) {
            if (r == true) {
$.ajax({
type: "GET",
url: GLOBAL_BASE_PATH+"/AjaxUploadImage/uploads/",
data: "fileFieldName="+encodeURIComponent(fileName)+"&method=delete&curr_id="+opid+"/",
success: function (data) {
$("#div_upload_sub_OptionVariant"+imageType+"_"+opid).html("<img style=\"padding-top:2px;cursor:pointer;\" src=\""+nofile+"\" width=\"77\" id=\"imageFileOptionVariant"+imageType+"_"+opid+"\" />");
$("#imageEditboxOptionVariant"+imageType+"_"+opid).css("display","none");
$("#noImageCheckbox").css("display","none");
$("#OptionVariant"+imageType+"_"+opid+"").val("");
$("#OptionVariant"+imageType+"_"+opid+"").val("");
$("#OptionVariant"+imageType+"_"+opid+"Url").val("");
}
});

	    }	    
	    
        });  
}