$j = jQuery.noConflict();
$j(function(){
  
  if (window.ActiveXObject) window.ie = window[window.XMLHttpRequest ? 'ie7' : 'ie6'] = true;

    Pas = function(cateid, appid, used, _used) {
        this.cateid = cateid;
        this.appid = appid;
        this.used = used;
        this._used = _used;
    };
    
    var params = new Pas(0, 0, 0, 0);
    var includeTypes = '';
    var switcher = '';
  
  
  closedShadow= function() {
    $j('.product-search .loading').hide();
    $j('.product-search .product-shadow-control').hide();
  };
  
  openShadow = function(){
    $j('.product-search .loading').show();
    $j('.product-search .product-shadow-control').show();
  };
  if($j('.product-search')) {
    $j('.product-search select').each(function(){
        $j(this).attr('disabled', false);
        $j(this).attr('selectedIndex', -1);
        $j(this).children('option').each(function(){
            $j(this).attr('disabled', false);
        });
    });    
    $j('.product-search input[type=checkbox]:checked').attr('checked', false);
  }
    showSingleProduct = function(_title, _link) {
        $j('.single-product-jansen').show();
        $j('#single-product').html(_title);
        $j('#single-product-link a').attr('href', _link).show();
    };
    
    hideProductLink = function() {
        $j('.single-product-jansen').hide();
        $j('#single-product').html('');
        $j('#single-product-link a').attr('href', 'javascript: void(0);').hide();
    };
    
    getPosition = function(_id){
        var tmpArr = includeTypes.split(',');
        for(var i = 0; i < tmpArr.length; i++) {
            if(tmpArr[i] == _id) return i;
        }
    };
    
    otherOptions = function(_obj){
        openShadow();
        switcher = _obj.attr('id');
        
        if(includeTypes.search(switcher) < 0) {
            includeTypes += ',' + _obj.attr('id');
        }
        if(includeTypes.indexOf(',') === 0) {
            includeTypes = includeTypes.substr(1, includeTypes.length);
        }
        
        $j.post(baseURL + ajaxURL, {
            cateid: params.cateid,
            appid: params.appid,
            used: params.used,
            used2: params._used,
            switcher: switcher,
            position: includeTypes
        }, function(json){
            if(json.cateOptions) {
                $j('#p-cateid').html(json.cateOptions);
            }           
            if(json.appOptions) {
                $j('#p-appid').html(json.appOptions);
            }    
            if(json.result == 'reset') {
                hideProductLink();
            }           
            if(trim(json.product)) {
                showSingleProduct(json.product, json.productlink);
            }
            closedShadow();
        }, 'json');
    };
  
    if(window.ie) {
      $j('#p-cateid, #p-appid').ieSelectWidth({
        width: 251,
        containerClassName : 'select-container',
        overlayClassName : 'select-overlay'
      });
    }
  
    // category change
    $j('#p-cateid').change(function(){
        $j('#hidecate').val($j(this).val());
        params.cateid = $j(this).val();
      if(window.ie) {
        $j(this).parent().next().show();
      } else {
        $j(this).next().show();
      }
        otherOptions($j(this));
    });
    
    // application change
    $j('#p-appid').change(function(){
        $j('#hideapp').val($j(this).val());
        params.appid = $j(this).val();
        otherOptions($j(this));
      if(window.ie) {
        $j(this).parent().next().show();
      } else {
        $j(this).next().show();
      }
    });
    
    // check used
    $j('#p-used').click(function(){    if($j(this).attr('checked')) {params.used = 1;} else {params.used = 0;}
        $j(this).parent().find('span.hide').show();
        otherOptions($j(this));
    });
    // check used2
    $j('#p-used2').click(function(){    if($j(this).attr('checked')) {params._used = 1;} else {params._used = 0;}
    $j(this).next().show();
    otherOptions($j(this));
});
});

/**
 * value in array
 * @param needle
 * @param haystack
 * @param argStrict
 * @returns {Boolean}
 */
function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}

function trim(str)
{
    return str.replace(/(^\s*)|(\s*$)/g,"");
}
