

$j(document).ready(function() {
  var _w=0;
  $j('.search-result .rel-videos ul .txt-box .txt-inner').each(function(){
    if($j(this).height()>_w){
      _w=$j(this).height();
    }
  });
//  alert('w:'+_w);
 if(_w>0){
    $j('.search-result .rel-videos ul .txt-box .txt-inner').each(function(){
      $j(this).height(_w);
    });
  }
});
// here search f

function URLDecode(encodedString){
  encodedString = encodedString.toString();
  var x=0;
  var _len = encodedString.length;
  var _new_string='';
  while( x < _len ){

    if( encodedString.charCodeAt(x) == 37 && x < _len - 2 && encodedString.substr( x+1 , 2).search(/[a-f0-9]{2}/i) == 0){
      var _hex = encodedString.substr(x+1,2);
    var _dehex = String.fromCharCode('0x'+_hex);
    _new_string+=_dehex;
    x+=2;
    }else{
      _new_string+=encodedString.substr(x,1);
  }
  x++;
  }
  _new_string = _new_string.replace(/\+/g,' ');
  return _new_string;
}

function URLEncode(clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString().replace(/ /g,'+');
  var regex = /(^[a-zA-Z0-9+_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
      output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function ucfirst (str) {
  
  str += '';
  str = str.toLowerCase();
  var str_arr = str.split(' ');
  var str_ret = '';
  for( var i=0;i<str_arr.length;i++){
    var str_i = str_arr[i];
    var f = str_i.charAt(0).toUpperCase();
    str_ret+= f + str_i.substr(1)+(i<str_arr.length-1?' ':'');
  }
  return str_ret;
}


function doSearchSmall(){
  var _term = $j('#search-small-box').val();
  if(_term.length >= 3 ){
    _term = URLEncode(_term);
    $j('#search-form-small').attr('action','/search/entertaiment/' + _term + '/1');
    $j('#search-form-small').submit();

  }else{
    alert('min 3 chars');
  }
  return void(0);
}
function doSearchById(term,sfid){
  var _term = term || false;
  if( _term === false ){
    var _form = $j('#'+sfid)[0];
    _term = $j(".dosearch",_form).val();
  }
  if(_term.length >= 3 ){
    _term = URLEncode(_term);
     $j('#'+sfid).attr('action','/search/entertaiment/' + _term + '/1');
     $j('#'+sfid).submit();
   }else{
     alert('min 3 chars');
   }
   return void(0);
}

function doSearchPage(page){
  
  var _loc_split = document.location.href.split('/');

  var _term = _loc_split[5];
  doSearch(URLDecode(_term),page);
  return void(0);
}
  
function doSearchBtn(form_id,type){
  var _type = type || false;
  var _form = $j('#'+form_id)[0];
//return doSearchById(false,form_id);
  return doSearch(false,1,_type,_form);
}
function doSearch(term,page,type,form){
  var page = page || 1;
  var term = term || false;
  var form = form || false;
	
  var _type = type || false;
  if(_type === false){
		_type = $j('input[name="search_type"]:checked').val();
	  $j('input[name="search_type"]:checked').attr('checked','');
	}
  if(_type == 'show' ){
    var type = 'shows';
  } else if(_type == 'music' ){
    var type = 'music';
  } else if(_type == 'movie' ){
    var type = 'movies';
  } else if(_type == 'people' ){
    var type = 'artists';
  }else{
    var type = 'entertaiment';
  }
//  $j('#search-top-box').curtain();
//  alert('t'+term);
  if( term === false ){
    if(form === false){
      term = $j("#bigsearchbox2").val();
    }else{
      term = $j(".dosearch",form).val();
    }
  }
//  alert('t:'+term+',t:'+type);
  if(term.length > 0){
    var _term = URLEncode(term);
    var _action = '/search/' + type + '/' + _term + '/' + page;
    if(form === false){
      var _form = $j('#search-form')[0];
    }else{
      _form = form;
    }
    $j(_form).attr('action',_action);
//    alert('act:'+_action);
    $j(_form).submit();
  }else{
//    $j('#search-top-box').curtain("remove");
    alert('empty query');
  }
  return void(0);
}

function fixEnter(field,e,small,type,sfid){
  var keynum;
  var keychar;
  var numcheck;
  var _small = small || false;
	var _type = type || false;
  var _sfid = sfid || false;
  if(window.event){
    keynum = e.keyCode;
  }else if(e.which){
    keynum = e.which;
  }
//alert(keynum);
//return false;
  keychar = String.fromCharCode(keynum);
  var _input_value = field.value;
//  alert(_input_value);
//  if(keynum==13 && document.getElementById("input-box-main").value.length == 0 ){
  if(keynum==13 && _input_value.length == 0 ){
    return false;
  }else if(keynum==13){
    if(_small===false){
      doSearch(_input_value,1,_type);
    }else if(_small===true){
      doSearchSmall(_input_value);
    }else if(_sfid!==false&&_small==2){
      doSearchById(_input_value,_sfid);
    }
    return false;
  }


  return true;
}





