﻿// JQuery file
  
  var masterId = 0;
  var pricemakerId = 0;
  var assortmentId = 0;
  var brandId = 0;
  //var isocode=
  
//lbel - vvaj
//updateAdvancedSearch = function(){ return false;
function updateAdvancedSearch()
{
    updateMasterAdvancedSearch();
    updatePriceMakerAdvancedSearch();
    updateAssortmentAdvancedSearch();
    updateBrandAdvancedSearch();
}

//lbel - vvaj
//updateProductGroups = function(object, parentId, brandId, type){   return false;
function updateProductGroups(object, parentId, brandId, type)
{
    object.html('');
    var id = null;
    $.ajax({
      type: "POST",
      url: "AutoComplete.asmx/GetProductGroups",                              
      data: "{parentId:"+parentId+",brandId:"+brandId+"}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      error: function(XMLHttpRequest, textStatus, errorThrown){
          //alert('error:' + errorThrown);
      },
      success: function(data) {  
        if(data.length>0)
        {              
            var option = '<option value="0">Vyberte...</option>';
            object.append(option);
                                  
            var items = data.split('|');            
            for(i=0;i<items.length;i++)
            {
                var params = items[i].split(';');                
                option = '<option value="'+ params[1] +'">' + params[0] + '</option>';
                object.append(option);            
                            
            }        
            if( items.length == 1)
            {
                id = items[0].split(';')[1];
                
                if( type == 'M' ) { masterId = id; updatePriceMakerAdvancedSearch(); }
                if( type == 'P' ) { pricemakerId = id;updateAssortmentAdvancedSearch(); }
                if( type == 'A' ) { assortmentId = id;updateBrandAdvancedSearch(); }
        
            }
        }
        
      }
    });
        
               
}

//lbel - vvaj
//updateMasterAdvancedSearch = function(){
function updateMasterAdvancedSearch()
{
    var object = $('#AdvancedSearchC_MasterDropDownList');  
    if(masterId==0)
    {                 
        updateProductGroups(object, 0, brandId, 'M');                          
    }
    else
    {
    
        /*$('#AdvancedSearchControl_MasterDropDownList option')
          .each(function(){ 
            if( $(this).val() == masterId )                    
                $(this).attr('selected','select');
                
          });*/
    }
}

//lbel
//updatePriceMakerAdvancedSearch = function(){ return false;
function updatePriceMakerAdvancedSearch()
{
    var object = $('#AdvancedSearchC_PriceMakerDropDownList');    
    object.html('');
    if(masterId!=0)
    {   
        object.removeAttr('disabled');                    
        updateProductGroups(object, masterId, brandId,'P');     
                
        /*if( pricemakerId != 0)
        {
            $('#AdvancedSearchC_PriceMakerDropDownList option').each(function(){ 
                //alert($(this));
                if( $(this).val() == pricemakerId )                    
                    $(this).attr('selected','select');
                    
              });
        } */    
    }
    else
    {
        object.attr('disabled','disabled');                
    }
    
}

//lbel - vvaj
//updateAssortmentAdvancedSearch = function(){ return false;
function updateAssortmentAdvancedSearch()
{
    var object = $('#AdvancedSearchC_AssortmentDropDownList');        
    object.html('');    
    
    if(pricemakerId!=0)
    {                
        object.removeAttr('disabled');
        updateProductGroups(object, pricemakerId, brandId,'A');          
    }
    else
    {
        object.attr('disabled','disabled');                
    }
    
}

//lbel - vvaj
//updateBrandAdvancedSearch = function(){
function updateBrandAdvancedSearch()
{
    var productGroupId = assortmentId;
    if( productGroupId==0 ) productGroupId = pricemakerId;
    if( productGroupId==0 ) productGroupId = masterId;
    
    var object = $('#AdvancedSearchC_BrandDropDownList');
    object.html('');
    
    //if(productGroupId!=0)
    //{                   
        object.html('');
       $.ajax({
      type: "POST",
      url: "AutoComplete.asmx/GetBrands",                              
      data: "{productGroupId:"+productGroupId+"}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      error: function(XMLHttpRequest, textStatus, errorThrown){
          //alert('error:' + errorThrown);
      },
      success: function(data) { 
      if(data.length>0)
        {                     
            var option = '<option value="0">Vyberte...</option>';
            object.append(option);
            
            object.removeAttr('disabled');     
            var items = data.split('|');            
            for(i=0;i<items.length;i++)
            {
                var params = items[i].split(';');                
                option = '<option value="'+ params[1] +'">' + params[0] + '</option>';
                object.append(option);            
            }
        }
        else
        {
            object.attr('disabled','disabled');
        }
        
      }
    });   
    //}
}

//lbel - vvaj
//advancedSearchControl_MasterChanged = function(){      
function advancedSearchControl_MasterChanged()
{
  pricemakerId = 0;
  assortmentId = 0;  
  //updateMasterAdvancedSearch();
  updatePriceMakerAdvancedSearch();
  updateAssortmentAdvancedSearch();
  updateBrandAdvancedSearch();
}

// lbel - vvaj
//advancedSearchControl_PriceMakerChanged = function(){    
function advancedSearchControl_PriceMakerChanged()
{
    assortmentId = 0;  
    //updateMasterAdvancedSearch();
    //updatePriceMakerAdvancedSearch();
    updateAssortmentAdvancedSearch();
    updateBrandAdvancedSearch();
}

// lbel - vvaj
//advancedSearchControl_AssortmentChanged = function(){
function advancedSearchControl_AssortmentChanged()
{
    updateBrandAdvancedSearch();
}

//lbel - vvaj
//advancedSearchControl_BrandChanged = function(){
function advancedSearchControl_BrandChanged()
{
    if( masterId == 0 )
    {
        updateMasterAdvancedSearch();      
    }           
    
    updatePriceMakerAdvancedSearch();      
    updateAssortmentAdvancedSearch();          
}

//lbel - vvaj
//advancedSearchControl_BindChangeEvents = function(){
function advancedSearchControl_BindChangeEvents()
{
    $('#AdvancedSearchC_MasterDropDownList').change(function() {
        masterId = $(this).val();
        advancedSearchControl_MasterChanged();
    });
    
    $('#AdvancedSearchC_PriceMakerDropDownList').change( function() {
        pricemakerId = $(this).val();                
        advancedSearchControl_PriceMakerChanged();
    });
    
    $('#AdvancedSearchC_AssortmentDropDownList').change( function() {
        assortmentId = $(this).val();
        advancedSearchControl_AssortmentChanged();
    });
    
    $('#AdvancedSearchC_BrandDropDownList').change( function() {
        brandId = $(this).val();
        advancedSearchControl_BrandChanged();
    });
}


function callbacktest()
{
    alert('CALLBACK');
}
 
//lbel           
//updateRetailDetailImages = function(){
function updateRetailDetailImages()
{
     $.ajax({
      type: "POST",
      url: "Catalog.aspx/GetImages",                              
      data: "{}",
      contentType: "application/json; charset=utf-8",
      dataType: "json",
      error: function(XMLHttpRequest, textStatus, errorThrown){
          //alert(errorThrown);
      },
      success: function(data) {               
        $('#WelcomeRetailDetail_RIImage').html('');
        var split = data.split('|');
        if( split.length>0)
        {                                    
            for (i=0; i<split.length; i++) {
            
                var split2 = split[i].split('/');
                var name = split2[split2.length-1];    
                var option = '<option value="'+ split[i] +'">' + name + '</option>';
                $('#WelcomeRetailDetail_RIImage').append(option);
                
                // selected >>
            }
        }
      }
    });            
}
            
          $(document).ready(function(){
                var text = ($get(hiddenTextBoxId)).value; 
                if(text == "AdvSearch")
                {
                    advancedSearchControl_BindChangeEvents();
                    updateAdvancedSearch();      
                }
                
                $(window)._scrollable();
                
                
                $('.HdLnkTxt').live("mouseover", function()
                {
                    //$(this).removeClass("BackCol");  
                    //$(this).addClass("BackExp");  
                    
                    AKBROWSER.highlightItem(AKBROWSER.Highlighted, false); 
                    AKBROWSER.Highlighted = $(this).attr('id').split('_')[1];                    
                    AKBROWSER.highlightItem(AKBROWSER.Highlighted, true); 
                    
                    return false;
                });
                
            $('.HdLnkTxt').live("mouseout", function()
                {
                    //$(this).removeClass("BackExp");  
                    //$(this).addClass("BackCol");  
                    return false;
                });
                
            //AKBROWSER.setSelected(AKBSelected);   
            
            $('body').bind('keypress', function(e) {                
                AKBROWSER.keypress(e.keyCode);                
            }); 

            shortcut.add("Left",function() {                                            
                AKBROWSER.left();
            });
            
            shortcut.add("Right",function() {
                AKBROWSER.right();
            });
            
            shortcut.add("Up",function() {
                AKBROWSER.up();
            });
            
            shortcut.add("Down",function() {
                AKBROWSER.down();
            });
            
            /*shortcut.add("Backspace",function() {
                AKBROWSER.backspace(callbacktest);
            });*/  
            
            shortcut.add("Enter",function() {
                AKBROWSER.enter();
            });                        
            
            //shortcut.add("Backspace",function() {
	          //  ExecuteShortcut('Backspace');
            //});
             
           
             
            // lbel
           $('.IsLogon').live('keypress', function(){
                focusId = 'Logon';
            });  
            
            // lbel
            $('.IsFind').live('keypress', function(){
                focusId = 'Find';
                
            });  
            
            
            $('.IsExtend').live('keypress', function(){
                focusId = 'Extend';
            });  
            
            $('.IsRecalc').live('keypress', function(){
                focusId = 'Recalc';
            }); 
            
            $('.IsAdd2Cart').live('keypress', function(){
                focusId = 'Add2Cart';
            }); 
            
            $('.IsImportFile').live('keypress', function(){
                focusId = 'ImportFile';
            });
            $('.CartIATB').live('keypress', function(){
                focusId = 'InsuredAccident';
            });
           
            shortcut.add("Enter",function() { ShortCutEnter(); });
            //shortcut.add("Delete",function() { ShortCutDelete(); });
            
            shortcut.add("Escape",function() {
	            if($('#LogOnControlDiv').parent().css('display')=='none')
	            {
	                //$get('FindIB').click();
	            }
	            else
	            {
	                $get('CancelButton').click();
	            }
            });
            
            
            $('.RIImageManager').live('click', function(){                
                $("#RIImageManagerDialog").showImageManager();
                
                $("#RIImageManagerDialog").show();
                $("#RIImageManagerDialog").dialog({
		            bgiframe: true,
		            zIndex: 100001,
		            modal: true,
		            buttons: {
			            Ok: function() {
                            var deleted = $("#dialog").getTrashImages();
			                $("#RIImageManagerDialog").deleteTrashImages();				        
			                $("#RIImageManagerDialog").close();
				            $(this).dialog('close');
				            updateRetailDetailImages();
			            }
		            }
                });
                                
                $("#RIImageManagerDialog").dialog('open');
                $("#RIImageManagerDialog").parent().css('width','640px');
                //$("#RIImageManagerDialog").parent().css('left','10px');
                $("#RIImageManagerDialog").parent().css('top','10px');
                
                           
           });
     	 
                                                      
            $('#CatImgDiv').hide();
            $('#ace').hide();
            $('#MotorDiv').hide();                            		
            $('#acem').hide();
            $("#CartAddDiv").hide();                       
          
            $('.HasImage').live("mouseover", function(){                
                $(this).oneTime(1000,'AKTimer',function()
                        {  
                            var imgName = $(this).attr('ImageName')
                            //alert('imgName: ' + imgName);
                            $.ajax({
                              type: "POST",
                              url: "Catalog.aspx/GetCatImage",                              
                              data: "{imgName:'"+imgName+"'}",
                              contentType: "application/json; charset=utf-8",
                              dataType: "json",
                              error: function(XMLHttpRequest, textStatus, errorThrown){
                                  //alert(errorThrown);
                              },
                              success: function(msg) {
                                if(msg.length > 0)
                                {
                                    $('#CatImg').attr('src', msg);
                                    $('#CatImg').attr('alt', imgName);
                                    $('#CatImgDiv').show();           
                                    $('#ace').show(); 
                                }
                              }
                            });
                                               
                            //var pos = $(this).offset();
                            //var width = $(this).width();  
                                                        
                            //$("#CatImgDiv").css( { "position":"absolute", "left": (pos.left + width) + "px", "top":pos.top + "px" } );                                             
                            //$("#CatImgDiv").css( { "position":"absolute", "left": (pos.left + width) + "px", "top":pos.top - 240 + "px" } );                                             
                                             
                                                       
                        });
                    })
                       .live("mouseout", function(){
                        $('.HasImage').stopTime('AKTimer');                        
                        $('#CatImgDiv').hide(); 
                        $('#ace').hide();
                        
                    });

             $('#CatImg').error(function(){
                $(this).attr({
                    src: 'Images/EmptyImage_M.gif'
                });
             }); 
				
				
            $('.HasMotor').live("mouseover", function(){                
                $(this).oneTime(1000,'AKTimer',function()
                        {                      
                            var pos = $(this).offset();
                            var width = $(this).width();  
                            var catalogId = $(this).attr('CatalogId');
                                                        
                            //$('#MotorDiv').show();
                            //$('#acem').show();
                            
                            
                            $.ajax({
                              type: "POST",
                              url: "Catalog.aspx/GetMotorInfo",                              
                              data: "{catalogId:"+catalogId+"}",
                              contentType: "application/json; charset=utf-8",
                              dataType: "json",
                              error: function(XMLHttpRequest, textStatus, errorThrown){
                                  //alert(errorThrown);
                              },
                              success: function(msg) {
                                if(msg.length > 0)
                                {
                                    $('#MotorDiv').show();
                                    $('#acem').show();
                                    $("#MotorDiv").html(msg);
                                }
                              }
                            });
                                     
                            //$('#Image1').attr('src', $(this).attr('ImageName'));                            
                        });
                    })
                       .live("mouseout", function(){
                        $('.HasMotor').stopTime('AKTimer');                        
                        $('#MotorDiv').hide(); 
                        $('#acem').hide();
                        
                    });
            
            //$("#ProgressDialog").hide();
            
            $('.JQBtn').live("mouseover", function(){this.style.textDecoration='underline'; this.style.color='blue'; return false;});
            $('.JQBtn').live("mouseout", function(){this.style.textDecoration='underline'; this.style.color='black'; return false;});
            $('.JQBtnNoBold').live("mouseover", function(){this.style.textDecoration='underline'; this.style.color='blue'; return false;});
            $('.JQBtnNoBold').live("mouseout", function(){this.style.textDecoration='underline'; this.style.color='black'; return false;});
            $('.JQBtnNoColor').live("mouseover", function(){this.style.textDecoration='underline'; return false;});
            $('.JQBtnNoColor').live("mouseout", function(){this.style.textDecoration='underline'; return false;});
            $('.JQBtnNoBoldNoColor').live("mouseover", function(){this.style.textDecoration='underline'; return false;});
            $('.JQBtnNoBoldNoColor').live("mouseout", function(){this.style.textDecoration='none'; return false;});
	    
	        $(".MOWBin").droppable(
            { 
                over: function(event, ui) { $(ui.draggable).addClass("DeleteItem");},
                out: function(event, ui) { $(ui.draggable).removeClass("DeleteItem");},
                drop: function(event, ui) { MOWItemDelete(); }
            });
            $(".WClickable").live("click", function() { WItemClick($(this));});
            $(".WClickable").live("dblclick", function() { WItemDoubleClick($(this));});
            $(".RILstBox").live("dblclick", function() { CatalogDblClick($(this));});
            
            $(".WSortable").sortable(
            {
                start: function(event, ui) 
                { 
                    ////alert('sortable start JQuery, itemId: ' + $(ui.item).attr('id')); 
                    startPosition = $(ui.item).parent().children().index(ui.item);
                    WItemClick($(ui.item)); 
                },
                stop: function(event, ui) 
                { 
                    var stopPosition = $(ui.item).parent().children().index(ui.item); 
                    if(stopPosition != startPosition)
                    {
                        ////alert('sortable end JQuery, stoppos: ' + stopPosition); 
                        ($get(hiddenTextBoxId)).value= 'ChangePosWelcomeItem#' + startPosition + '|' + stopPosition;       	           
                        ($get(hiddenButtonId)).click();    
                    }
                }
            });
            $('#RISliderX').slider( 
            {
                min: 0,
                max: 300,
                value: $('.RIOffsetX').val()
            });
            $('#RISliderX').live('slidechange', function()
            { 
                var posX = $('#RISliderX').slider("value");
                $('.RIOffsetX').val(posX);
                var posY = $('.RIOffsetY').val();
                SetPreviewTextOffset(posX, posY);
            });
            $('.RIOffsetX').live('keyup', function(e)
            {
                var posX = $('.RIOffsetX').val();
                $('#RISliderX').slider('value', posX);
                var posY = $('.RIOffsetY').val();
                SetPreviewTextOffset(posX, posY);
            });
            $('#RISliderY').slider( 
            {
                min: 0,
                max: 300,
                value: $('.RIOffsetY').val()
            });
            $('#RISliderY').live('slidechange', function()
            { 
                var posY = $('#RISliderY').slider("value");
                $('.RIOffsetY').val(posY);
                var posX = $('.RIOffsetX').val();
                SetPreviewTextOffset(posX, posY);
            });
            $('.RIOffsetY').live('keyup', function(e)
            {
                var posY = $('.RIOffsetY').val();
                $('#RISliderY').slider('value', posY);
                var posX = $('.RIOffsetX').val();
                SetPreviewTextOffset(posX, posY);
            });
            
            
            $(".MIExpiree").datepicker($.datepicker.regional['cs'],
            {
                minDate: "0d",
			    changeMonth: true,
			    changeYear: true,
			    dateFormat: 'd M, y',
			    showButtonPanel: true
		    });
		    
		    $(".MIExpire").datepicker(
            {
                minDate: "0d",
			    changeMonth: true,
			    changeYear: true,
			    dateFormat: 'd M, y',
			    showButtonPanel: true
		    });

            $('.RIText').live('keyup', function(e){
                var text = $('.RIText').val().replace(/\n/gi,"<br/>");
                $('.WRIPanel').html(text);
            });  
            
            $('#RegC_RegCustomerTypePerson').live('click', function()
            {
                $('#RegPersonDiv').attr('style', 'visibility: visible; ');
                $('#RegCompanyDiv').attr('style', 'visibility: hidden; display: none;');
                //$('#RegC_RegComBusinessBranch').attr('style', 'visibility: hidden; display: none;');
                $('.RegComBusinessBranch').attr('style', 'visibility: hidden; display: none;');
            });
            $('#RegC_RegCustomerTypeCompany').live('click', function()
            {
                $('#RegPersonDiv').attr('style', 'visibility: hidden; display: none;');
                $('#RegCompanyDiv').attr('style', 'visibility: visible;');
                //$('#RegControl_RegComBusinessBranch').attr('style', 'visibility: visible;');
                $('.RegComBusinessBranch').attr('style', 'visibility: visible;');
                
            });
            
            $('.MOverOut').live('mouseover', function() { MOver(this); })
                          .live('mouseout', function() { MOut(this); });
                          
            // MOverOut for left panel (catalog panel)
            $('.MOverOutLP').live('mouseover', function() { if(AKBROWSER.Enabled) MOverLP(this); else MOver(this);})
                            .live('mouseout', function() { MOut(this); });
            
            // zatim vypnuto              
            $('.OnlyDigitttt').live('keydown', function(e)
            {
                var charCode = (e.which) ? e.which : event.keyCode
                if (charCode > 31 && (charCode < 48 || charCode > 57))
                    return false;
                
                return true;
            });
            
            // vvaj - vyber cms stranky pro editaci
            $('.EditCMSPage').live('click', function() 
            { 
                $('#HiddenTextBox').val('ShowCMSPage');
                ShowListCMSPages();
            });
            
            // MID - change title text
            $('.MIDTitle').live('keyup', function(e){
                var text = $('.MIDTitle').val().replace(/\n/gi,"<br/>");
                $('.MITitle').html(text);
            });
            
            // MID - change label text
            $('.MIDLabel').live('keyup', function(e){
                var text = $('.MIDLabel').val().replace(/\n/gi,"<br/>");
                $('.MILabel').html(text + '<br/><br/>');
            });
            
            // MID - change text text
            $('.MIDText').live('keyup', function(e){
                var text = $('.MIDText').val().replace(/\n/gi,"<br/>");
                $('.MIText').html(text + '<br/><br/>');
            });
            
            $('.MIDLinkTB').live('keyup', function(e){
                var text = $('.MIDLinkTB').val();
                MIDShowLink(text.length > 0);
            });
            
            /*$('.MIDTitleColor, .MIDLabelColor, .MIDTextColor').ColorPicker({
	                onSubmit: function(hsb, hex, rgb, el) {
	                    alert('onSubmit');
		                $(el).val(hex);
		                $(el).ColorPickerHide();
	                },
	                onBeforeShow: function () {
	                    alert('onBeforeShow');
		                $(this).ColorPickerSetColor(this.value);
	                },
	                onChange: function (hsb, hex, rgb) {
            		    $(this).css('backgroundColor', '#' + hex);
	                }

                })
                .bind('keyup', function(){
                    alert('keyup');
	                $(this).ColorPickerSetColor(this.value);
            });*/
            
            /*$('.CartIATB').live('keyup', function(e)
            {
                var isEmpty = false;
                $('.CartIATB').each(function()
                {
                    var text = $(this).val();
                    if(text.length <= 0)
                        isEmpty = true;
                });
                //alert('isempty: ' + isEmpty);
                $(".InsuredAccident").each(function()
                {
                    if (isEmpty) {
                        $(this).attr("disabled", "disabled");
                        $("input:checkbox").not(this).attr("disabled","disabled");
                    } else {
                        $("input:checkbox").not(this).removeAttr("disabled");
                        $(this).removeAttr("disabled");
                    }
                });
                
                if(!isEmpty)
                {
                    ($get(hiddenTextBoxId)).value = 'IAChangeTB#' + $('#CPUTB').val() + "|" + $('#LicencePlateTB').val();
                    ($get(hiddenButtonId)).click();
                }
            });
            */
            
            $('.CartIATB').live('focusout', function()
            {
                ($get(hiddenTextBoxId)).value = 'IAChangeTB#' + $('#CPUTB').val() + "|" + $('#LicencePlateTB').val();
                ($get(hiddenButtonId)).click();
            });
            
            /*$(".InsuredAccident").live('click',  function(e)
            {
                var checked = $("input:chec
                kbox").not(this).is(':checked');
                alert('checked: ' + checked);
            });
            */
        });
                
        $('#StartTrackBtn').live('click',function(){            
            var usr = ($get('TrackTB')).value;
            if( usr=='')
            {                
                $(this).stopTime('TrackTimer');
            }
            else
            {
                $(this).everyTime(5000,'TrackTimer',function()
                {                
                    ($get('trackBtn')).click();                       
                });
            }
        });

        
