var search_option_html = '';
search_option_html += '		<!-- searchoption -->';
search_option_html += '		<div class="search-option-area" id="search_option_area" style="display:none;">';
search_option_html += '			<div class="search-option-container clearfix">';
search_option_html += '				<form id="search_option_form" >';
search_option_html += '					<table border="0" cellspacing="0" cellpadding="0">';
search_option_html += '						<tr>';
search_option_html += '							<td>';
search_option_html += '								<div class="box-1">';
search_option_html += '									<p class="title">Search Field</p>';
search_option_html += '									<p class="all-check"><input id="search_type_0" name="search_type_0" type="checkbox" value="" />&nbsp;&nbsp;<label for="search_type_0">All</label></p>';
search_option_html += '									<p><input id="search_type_1" name="search_type_1" type="checkbox" value="" />&nbsp;&nbsp;<label for="search_type_1">Title</label></p>';
search_option_html += '									<p><input id="search_type_2" name="search_type_2" type="checkbox" value="" />&nbsp;&nbsp;<label for="search_type_2">Description</label></p>';
search_option_html += '									<p><input id="search_type_4" name="search_type_4" type="checkbox" value="" />&nbsp;&nbsp;<label for="search_type_4">Tags</label></p>';
search_option_html += '								</div>';
search_option_html += '							</td>';
search_option_html += '							<td>';
search_option_html += '								<div class="box">';
search_option_html += '									<p class="title">Post Date</p>';
search_option_html += '									<select class="site" name="search_pdate" id="search_pdate">';
search_option_html += '										<option value="0"> Any time </option>';
search_option_html += '										<option value="7"> Within 1 week </option>';
search_option_html += '										<option value="30"> Within 1 month </option>';
search_option_html += '										<option value="90"> Within 3 months </option>';
search_option_html += '										<option value="180"> Within 6 months </option>';
search_option_html += '										<option value="365"> Within 1 year </option>';
search_option_html += '										<option value="999"> 1 year ago </option>';
search_option_html += '									</select>';
search_option_html += '								</div>';
search_option_html += '							</td>';
search_option_html += '							<td>';
search_option_html += '								<div class="box">';
search_option_html += '									<p class="title">Duration</p>';
search_option_html += '									<select class="site" name="search_length" id="search_length">';
search_option_html += '										<option value="0,0"> All </option>';
search_option_html += '										<option value="0,30"> < 30sec </option>';
search_option_html += '										<option value="30,60"> 30s ~ 1min </option>';
search_option_html += '										<option value="60,180"> 1 ~ 3min </option>';
search_option_html += '										<option value="180,360"> 3 ~ 6min </option>';
search_option_html += '										<option value="360,600"> 6 ~ 10min </option>';
search_option_html += '										<option value="600,900"> 10 ~ 15min </option>';
search_option_html += '										<option value="900,1800"> 15 ~ 30min </option>';
search_option_html += '										<option value="1800,0"> > 30min </option>';
search_option_html += '									</select>';
search_option_html += '								</div>';
search_option_html += '							</td>';
search_option_html += '							<td>';
search_option_html += '								<div class="box" >';
search_option_html += '									<p class="title">SafeMode</p>';
search_option_html += '									<div style="padding: 0pt 0pt 10px; width: 500px;">';
search_option_html += '									<p class="small">Woopie filters the content that maybe unsuitable for the users who are not over 18 years of age. Select the OFF box below if you are over 18 and would like to turn OFF the Safe Search Filter. />';
search_option_html += '																		</div>';
search_option_html += '									<p><input name="search_safe" id="search_safe" type="radio" value="0" />&nbsp;&nbsp;<label for="search_safe">ON</label></p>';
search_option_html += '									<p><input name="search_safe" id="search_safe_off" type="radio" value="off" />&nbsp;&nbsp;<label for="search_safe_off">OFF</label></p>';
search_option_html += '								</div>';
search_option_html += '							</td>';
search_option_html += '						</tr>';
search_option_html += '					</table>';
search_option_html += '					<div class="button-right"><input name="" onClick="resetSearchOption();" type="button" value="Reset" /><input name="" onClick="closeSearchOption();" type="button" value="OK" /></div>';
search_option_html += '				</form>';
search_option_html += '				<div class="contents-clear"></div>';
search_option_html += '			</div>';
search_option_html += '		</div>';
search_option_html += '		<!-- /searchoption -->';

var safe_option_html = '';
safe_option_html += '		<!-- searchedit -->';
safe_option_html += '		<div class="search-option-area-edit" id="safe_option_area" style="display:none;">';
safe_option_html += '			<div class="search-option-container clearfix">';
safe_option_html += '				<form>';
safe_option_html += '					<table border="0" cellspacing="0" cellpadding="0">';
safe_option_html += '						<tr>';
safe_option_html += '							<td>';
safe_option_html += '								<div class="box-1" >';
safe_option_html += '									<p class="title">SafeMode</p>';
safe_option_html += '									<p><input name="search_safe" id="search_safe" type="radio" value="0" />&nbsp;&nbsp;<label for="search_safe">ON</label></p>';
safe_option_html += '									<p><input name="search_safe" id="search_safe_off" type="radio" value="off" />&nbsp;&nbsp;<label for="search_safe_off">OFF</label></p>';
safe_option_html += '								</div>';
safe_option_html += '							</td>';
safe_option_html += '							<td>';
safe_option_html += '								<div class="box">';
safe_option_html += '									<p>About SafeMode</p>';
safe_option_html += '									<p class="small">Woopie filters the content that maybe unsuitable for the users who are not over 18 years of age. Select the OFF box below if you are over 18 and would like to turn OFF the Safe Search Filter.';
safe_option_html += '									<br />Select the OFF box below if you are over 18 and would like to turn OFF the Safe Search Filter. </p>';
//safe_option_html += '									<br />上記の内容に、同意して閲覧される場合は「OFF」を選択してください。</p>';
safe_option_html += '								</div>';
safe_option_html += '							</td>';
safe_option_html += '					<td class="button">';
safe_option_html += '						<input onClick="closeSearchOption();" name="" type="button" value="閉じる" />';
safe_option_html += '					</td>';
safe_option_html += '						</tr>';
safe_option_html += '					</table>';
safe_option_html += '				</form>';
safe_option_html += '				<div class="contents-clear"></div>';
safe_option_html += '			</div>';
safe_option_html += '		</div>';
safe_option_html += '		<!-- /searchedit -->';

var set_search_flag = 0;
var search_save_flag = 0;// 0:no_save 1:save ok
var safe_set_flag = 0;// 1:already setted
var search_kw = '';
var search_safe = 0;
var search_type = 0;//allinfo
var search_site = '';
var search_pdate = 0;// pdate 
var search_pds = 0;// pdate start
var search_pde = 0;// pdate end
var search_length = '0,0';// length
var search_vmin = 0; // vlen min
var search_vmax = 0; // vlen max
var search_sort = 'default'; // sorttype
$(function() {
  $('#channel-header-area').before( search_option_html);
  $("#select_sort").change( function(){
        $( "#select_sort option:selected" ).each(function () {
            if( search_sort != $(this).val()){
              var new_uri = location.href.replace(/&sort=[^&]+/,'');
              new_uri = new_uri.replace(/&page=[^&]*/,''); // to Top
              new_uri = new_uri.replace(/\#/,''); // 
              if( $(this).val().match(/vnum|vlength|pdate|siteurl/) ){
                if (new_uri.indexOf('?') == -1)
                  location.href = new_uri + '?sort=' + $(this).val();
                else
                  location.href = new_uri + '&sort=' + $(this).val();
              }else{
                location.href = new_uri;
              }
              select_disable();
            }
        });
  }).change();
  $("#select_site").change( function(){
        $( "#select_site option:selected" ).each(function () {
            if( 'default' != $(this).val()){
              var new_uri = location.href.replace(/&siteurl=[^&]+/,'');
              new_uri = new_uri.replace(/&page=[^&]*/,''); // to Top
              new_uri = new_uri.replace(/\#/,''); // 
              if( $(this).val().match(/all/) ){
                location.href = new_uri ;
              }else{
                if (new_uri.indexOf('?') == -1)
                  location.href = new_uri + '?siteurl=' + $(this).val();
                else
                  location.href = new_uri + '&siteurl=' + $(this).val();
              }
              select_disable();
            }
        });
  }).change();
  function select_disable(){
    $("#select_sort").attr('disabled','true');
    $("#select_site").attr('disabled','true');
  }
  $('#search_option').toggle(function(){
	$("#login_form_area:visible").fadeOut("fast");
    $("#safe_option_area:visible").fadeOut("fast");
    $("#search_option_area:hidden").slideDown("normal");
    search_save_flag = 0;
    setSearchOption();
    setSearchOptionIni();
    setSafeOptionIni();
    search_save_flag = 1;
    return false;
  },
	  function(){
			closeSearchOption();
		}
	);
  $('#safe_option').click(function(){
	$("#login_form_area:visible").fadeOut("fast");
    $("#search_option_area:visible").fadeOut("fast");
    $("#safe_option_area:hidden").slideDown("normal");
    search_save_flag = 0;
    setSearchOption();
    setSafeOptionIni();
    search_save_flag = 1;
    return false;
  });
  function checkPdate(){
          search_pdate = 0;
          search_pds = 0;
          search_pde = 0;
          var i = 0;
          var d = new Date();
          d = parseInt(d/1000);
          var d_7 = d - 7*24*60*60;
          var d_30 = d - 30*24*60*60;
          var d_90 = d - 90*24*60*60;
          var d_180 = d - 180*24*60*60;
          var d_365 = d - 365*24*60*60;
          $("#search_pdate option:selected").each(function () {
             //var new_pdate = $(this).val().split(',');
              //search_pds = new_pdate[0];
              //search_pde = new_pdate[1];
              search_pdate = $(this).val();
              switch($(this).val()){
                case '7':
                  search_pds = d_7;
                  break;
                case '30':
                  search_pds = d_30;
                  break;
                case '90':
                  search_pds = d_90;
                  break;
                case '180':
                  search_pds = d_180;
                  break;
                case '365':
                  search_pds = d_365;
                  break;
                case '999':
                  search_pde = d_365;
                  break;
              }
        });
        // save
        saveSearchOption();
  }
  function checkVlength(){
      $("#search_length option:selected").each(function () {
              search_length =  $(this).val();
             var new_len = $(this).val().split(',');
              search_vmin = new_len[0];
              search_vmax = new_len[1];
      });
      // save
      saveSearchOption();
  }
  function setSearchOption(){
    if( set_search_flag == 0 ){

    $("#search_type_0").click(function () {
          setSearchType(0);
        })
    $("#search_type_1").click(function () {
          setSearchType(1);
        })
    $("#search_type_2").click(function () {
          setSearchType(2);
        })
    $("#search_type_4").click(function () {
          setSearchType(4);
        })

    $("#search_pdate").change( checkPdate ).change();
    $("#search_length").change( checkVlength ).change();
    $("#search_safe").change(function () {
          search_safe = $("input[@name=search_safe]:checked").val();
          // save safe
          if( search_safe == '0') saveSafeOption();
        })
        .change();
    $("#search_safe_off").change(function () {
          search_safe = $("input[@name=search_safe]:checked").val();
          if( search_safe == 'off' ) saveSafeOption();
        })
        .change();
    } // set search flag
    set_search_flag = 1;
  }
  function setSearchOptionIni(){
    if($.cookie('type') ) {
      search_type = $.cookie('type') ;
      if(search_type==0){
        $("#search_type_0").attr( 'checked', 'checked');
      }else{
        switch(search_type){
          case '1':
            $("#search_type_1").attr( 'checked', 'checked');
            break;
          case '2':
            $("#search_type_2").attr( 'checked', 'checked');
            break;
          case '3':
            $("#search_type_1").attr( 'checked', 'checked');
            $("#search_type_2").attr( 'checked', 'checked');
            break;
          case '4':
            $("#search_type_4").attr( 'checked', 'checked');
            break;
          case '5':
            $("#search_type_1").attr( 'checked', 'checked');
            $("#search_type_4").attr( 'checked', 'checked');
            break;
          case '6':
            $("#search_type_2").attr( 'checked', 'checked');
            $("#search_type_4").attr( 'checked', 'checked');
            break;
          case '7':
            $("#search_type_1").attr( 'checked', 'checked');
            $("#search_type_2").attr( 'checked', 'checked');
            $("#search_type_4").attr( 'checked', 'checked');
            break;
          default:
            $("#search_type_0").attr( 'checked', 'checked');
            break;

        }
      }
    }else{
      search_type = $.cookie('type') ;
      $("#search_type_0").attr( 'checked', 'checked');
    }

    search_pdate = ($.cookie('pdate'))?$.cookie('pdate'):'0';
    if( search_pdate != 0 ){
      $("#search_pdate option").each(function () {
        if( $(this).val().match( search_pdate ) ){
          $(this).attr('selected','selected');
        }
      }).change();
    }
    checkPdate();
    
    search_length = ($.cookie('length'))?$.cookie('length'):'0,0';
    if( !search_length.match(/^0,0/) ){
      $("#search_length option").each(function () {
      if( $(this).val().match( search_length ) ){
        $(this).attr('selected','selected');
        }
      }).change();
    }
    checkVlength();
  }
  function setSafeOptionIni(){
    $.ajax({
      url:'/video/setsafe' ,
      type: 'GET',
      data: {'type':1,'randno':Math.random(1)},
      timeout: 1000,
      success: function(data) {
      search_safe = data;
        if(search_safe == 'off'){
          $("#search_safe_off").attr( 'checked', 'checked');
        }else{
          $("#search_safe").attr( 'checked', 'checked');
        }
	    }
	  });
  }

  // set initial data
   setSearchOptionIni();
   //setSafeOptionIni();
  
});
  function setSafeValue(){
    $.ajax({
      url:'/video/setsafe' ,
      type: 'GET',
      data: {'safe':search_safe,'randno':Math.random(1)},
      timeout: 1000,
      success: function(data) {
      search_safe = data;
	    }
	  });
  }
  function getSafeValue(){
    $.ajax({
      url:'/video/setsafe' ,
      type: 'GET',
      data: {'type':'1','randno':Math.random(1)},
      timeout: 1000,
      success:function(data) {
      search_safe = data;
	    }
	  });
  }

  function setSearchType(stype){
    search_type = 0;
    if( stype == 0 ){
      search_type = 0;
    }else{
      if( $("#search_type_1").attr("checked") ){
        search_type += 1;
      }
      if( $("#search_type_2").attr("checked") ){
        search_type += 2;
      }
      if( $("#search_type_4").attr("checked") ){
        search_type += 4;
      }

    }
    if( search_type== 0 ){
      $("#search_type_1").attr("checked",'');
      $("#search_type_2").attr("checked",'');
      $("#search_type_4").attr("checked",'');
      $("#search_type_0").attr("checked",'checked');
    }else{
      $("#search_type_0").attr("checked",'');
    }
    // save
    saveSearchOption();
  }
function resetSearchOption(){
   setSearchType(0);
   $("#search_type_0").attr("checked",'checked');

   $("#search_pdate option").each(function () {
   if( $(this).val() == '0' ) $(this).attr('selected','selected');          }).change();
   $("#search_length option").each(function () {
   if( $(this).val() == '0,0' ) $(this).attr('selected','selected');          }).change();
   search_kw = '';
   search_safe = 0;
   search_type = 0;//allinfo
   search_site = '';
   search_pdate = 0;// pdate 
   search_pds = 0;// pdate start
   search_pde = 0;// pdate end
   search_length = '0,0';// length
   search_vmin = 0; // vlen min
   search_vmax = 0; // vlen max

  // save
  saveSearchOption();
}
function closeSearchOption(){
    $("#search_option_area:visible").slideUp("normal");
    $("#safe_option_area:visible").slideUp("normal");
}

// safe
function saveSafeOption(){
  if( search_save_flag == 1  ){
    setSafeValue();
  }
}
// search
function saveSearchOption(){
  if( search_save_flag == 1  ){
  $.cookie('length',search_length,{ expires: null, path:'/' });
  $.cookie('pdate',search_pdate,{ expires: null, path:'/' });
  if(search_pds != 0){
    $.cookie('pds',search_pds,{ expires: null, path:'/'  });
  }else{
    $.cookie('pds','',{ expires: 0, path:'/'  });
  }
  if(search_pde != 0){
    $.cookie('pde',search_pde,{ expires: null, path:'/'  });
  }else{
    $.cookie('pde','',{ expires: 0, path:'/'  });
  }
  if(search_vmin != 0){
    $.cookie('vmin',search_vmin,{ expires: null, path:'/'  });
  }else{
    $.cookie('vmin','',{ expires: 0, path:'/'  });
  }
  if(search_vmax != 0){
    $.cookie('vmax',search_vmax,{ expires: null, path:'/'  });
  }else{
    $.cookie('vmax','',{ expires: 0, path:'/'  });
  }
  if(search_type != 0){
    $.cookie('type',search_type,{ expires: null, path:'/'  });
  }else{
    $.cookie('type','',{ expires: 0, path:'/'  });
  }

  } // save flag
  //closeSearchOption();
}
// submit search
function submitSearch( search_type ){
  var search_key = $('#kw').val();
  if(search_type && search_type=='footer'){
    search_key=$('#kw-footer').val(); 
  }
  if( search_key.length > 0 ){
    if ($('#search_type_selected').hasClass('search_type_user_selected')) {
      var encodedKw = encodeURIComponent( search_key )
      var action_new =  '/friend/search?kw=' + encodedKw + '#!&q=' + encodedKw;
    } else {
      var action_new =  $('#search_box').attr('action') + '?kw=' + encodeURIComponent( search_key ) ;
      if($.cookie('pds') )  action_new = action_new + '&pds=' + $.cookie('pds');
      if($.cookie('pde') )  action_new = action_new + '&pde=' + $.cookie('pde');
      if($.cookie('vmin') )  action_new = action_new + '&vmin=' + $.cookie('vmin');
      if($.cookie('vmax') )  action_new = action_new + '&vmax=' + $.cookie('vmax');
  //    if($.cookie('site') )  action_new = action_new + '&siteurl=' + $.cookie('site');
      if($.cookie('type') )  action_new = action_new + '&st=' + $.cookie('type');
      if($.cookie('sstatus') && $.cookie('sstatus')=='1' )  action_new = action_new + '&status=1';
    }
    location.href=action_new;
    //$('#search_box').attr('action', action_new);
    return false;

  }else{
    return false;
  }
}


