function gotoUrlKeySearch(newUrl,content,type){
    window.location.href=newUrl+"?searchValue="+encodeURI(content)+"&searchType="+type;
}
function gotoKeySearch(content,type){
    gotoUrlKeySearch("list.html",content,type);
}
function keySearch(content, type) {
    //搜索所有频道
    searchMovies(fortuneRenderData.cspId,-1,type,encodeURI(content),1,fortuneRenderOptions.pageSize);
}

function trim(str)
{
    for (var i = 0; i < str.length && str.charAt(i) == " "; i++){

    }
    for (var j = str.length; j > 0 && str.charAt(j - 1) == " "; j--){

    }
    if (i > j)  return  "";
    return  str.substring(i, j);
}

function searchRegion(region, type) {
    keySearch(region,type);
}

function searchRegionForTv(region, type) {
    keySearch(region,type);
}
function doJdkpSearch(){
   instantSearch(true); 
}
function instantSearch(gotoListPage) {
    var contentObj = document.getElementById("inputSearchKey");
    if(contentObj==null&&form1){
        contentObj = form1.search_content;
    }
    var typeObj = document.getElementById("selectKey");
    if(typeObj==null&&form1){
        typeObj = form1.search_type;
    }
    var content = "";
    if(contentObj!=null){
        content = trim(contentObj.value);
    }

    var type ="";
    if(typeObj!=null){
        type = trim(typeObj.value);
    }
    type = type.toUpperCase();
    if(type=="subject"){
       type = "MEDIA_TYPE" 
    }
    if (content == "请输入关键词...") {
        document.getElementById("inputSearchKey").value = "";
    }
    else {

        var ch;
        var loc = content.search(/[%]/);
        if (loc == -1) {
            loc = content.search(/[#&+\\=?]/);
        }
        while (loc != -1) {
            ch = content.charAt(loc);
            content = content.replace(ch, encodeURIComponent(ch));
            loc = content.search(/[#&+\\=?]/);
        }
        if(gotoListPage){
            gotoUrlKeySearch("/page/jdkp/list.html",content,type);
        }else{
            searchMovies(fortuneRenderData.cspId,-1,type,encodeURI(content),1,fortuneRenderOptions.pageSize);
        }
    }
}
function changePageDisplayedNum() {

}

function decade(searchKey)
{

    if (searchKey == "199") {
        document.write("90年代");
    } else if (searchKey == "198") {
        document.write("80年代");
    } else if (searchKey == "197") {
        document.write("70年代");
    } else {
        document.write(searchKey);
    }

}

var caution = false;
function setCookie(name, value, expires, path, domain, secure)
{
        var curCookie = name + "=" + escape(value) +
                       ((expires) ? "; expires=" + expires.toGMTString() : "") +
                       ((path) ? "; path=" + path : "") +
                       ((domain) ? "; domain=" + domain : "") +
                       ((secure) ? "; secure" : "");
        if (!caution || (name + "=" + escape(value)).length <= 4000){
            document.cookie = curCookie;
        }else if (confirm("Cookie exceeds 4KB and will be cut!")){
            document.cookie = curCookie;
        }
}
function getCookie(name)
{
        var prefix = name + "=";
        var cookieStartIndex = document.cookie.indexOf(prefix);
        if (cookieStartIndex == -1)
            return null;
        var cookieEndIndex = document.cookie.indexOf(";", cookieStartIndex + prefix.length);
        if (cookieEndIndex == -1) cookieEndIndex = document.cookie.length;
        return unescape(document.cookie.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}
function deleteCookie(name, path, domain)
{
        if (getCookie(name)){
                 document.cookie = name + "=" +
                 ((path) ? "; path=" + path : "") +
                 ((domain) ? "; domain=" + domain : "") +
                 "; expires=Thu, 01-Jan-70 00:00:01 GMT";
         }
}
function fixDate(date){
    var base = new Date(0);
    var skew = base.getTime();
    if (skew > 0){
        date.setTime(date.getTime() - skew);
    }
}

function getCookieName(cspId,indexId){
    return "csp_"+cspId+"_browseMedia_"+indexId;
}

function saveCookie(cookieName,cookieValue){
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie(cookieName,
              cookieValue,
              now,null,null,null);
}

function saveHasBrowsed(cspId,channelId,contentId,contentName,movieType){
    var k=1;
    var browsedMovies = getHasBrowsed(cspId);
    var movie;
    for(k=0;k<browsedMovies.length&&k<9;k++){
        movie=browsedMovies[k];
        if(movie!=null&&movie.contentId==contentId){
            return;
        }
    }
    var value = cspId+"|"+channelId+"|"+contentId+"|"+contentName+"|"+movieType;
    var cookieName = getCookieName(cspId,0);
    saveCookie(cookieName,value);
    for(k=0;k<browsedMovies.length&&k<9;k++){
        movie=browsedMovies[k];
        cookieName = getCookieName(cspId,k+1);
        if(movie.movieType==null||typeof(movie.movieType)=="undefined"){
            movie.movieType="    ";
        }
        value = movie.cspId+"|"+movie.channelId+"|"+movie.contentId+"|"+movie.contentName+"|"+movie.movieType;
        saveCookie(cookieName,value);
    }
}

function getHasBrowsed(cspId){
    var i=0;
    var browsedMovies=[];
    for(;i<10;i++){
        var strData = getCookie(getCookieName(cspId,i));
        if(strData!=null){
            var data=strData.split("|");
            cspId = data.length>0?data[0]:-1;
            var channelId = data.length>1?data[1]:-1;
            var contentId = data.length>2?data[2]:-1;
            var contentName = data.length>3?data[3]:"";
            var movieType = data.length>4?data[4]:"";
            if(movieType==null||typeof(movieType)=="undefined" || movieType=="undefined"){
                movieType="    ";
            }
            var movie = {cspId:cspId,channelId:channelId,contentId:contentId,contentName:contentName,movieType:movieType};
            browsedMovies.push(movie);
        }
    }
    return browsedMovies;
}

function renderBrowsed(cspId){
    var browsedMovies = getHasBrowsed(cspId);
    for(var j=0,bl=browsedMovies.length;j<bl;j++){
        var movie = browsedMovies[j];
        if(movie.contentId>0){
            var url = fortuneRenderOptions.detailUrl+"?cspId="+movie.cspId+"&channelId="+
                      movie.channelId+"&contentId="+movie.contentId;
            if(movie.movieType!=null&&movie.movieType!=""){
                setInfo("browse_type_"+j,"["+movie.movieType+"]",10);
            }
            setLinkInfo("browse_link_"+j,url,movie.contentName,10);
        }
    }
}
