// Copyright Marcus Engene / pond5 INC all rights reserved.

P5.Search = {};

P5.Search.ordbyChange = function (in_noe)
{
    P5.setP5Cookie('sb',$("searchob" + in_noe).value);
    window.location = "/" + P5.seo(P5._bm) + "/" + P5._aq + ".html";
};

P5.Search.rppChange = function (in_noe)
{
    P5.setP5Cookie('no',$("searchrpp" + in_noe).value);
    window.location = "/" + P5.seo(P5._bm) + "/" + P5._aq + ".html";
};

P5.Search.searchgoto = function (in_noe) {
    var page = parseInt($F("gotopage" + in_noe));
    if (isNaN(page)) {
        P5.slideMessage ("that was a bad number indeed");
        return;
    }
    if (0 >= page) { page = 0; } else { page -= 1; }
    var pagepp = page + 1;
    window.location = "/" + P5.seo(P5._bm) + "/" + P5._aq + '_' + pagepp + ".html";
};

P5.Search.reverse = function () {
    if (100 > P5.getP5CookieNvl('sb',1)) {
        P5.setP5Cookie('sb', P5.getP5CookieNvl('sb',1) + 100);
    } else {
        P5.setP5Cookie('sb', P5.getP5CookieNvl('sb',1) - 100);
    }
    
    window.location = "/" + P5.seo(P5._bm) + "/" + P5._aq + ".html";
};

P5.Search.advOc = function ()
{
    var hideable = $("idsearchAdvHideable");
    if (!hideable) {
        alert("Cannot find hideable search screen?!");
        return;
    }
    if (1 == P5._jsCookie.get('adv'))
    {
        P5.setP5Cookie('adv',0);
        hideable.hide();
        $("idsearchAdvOcImg").src = "/v5icons/arr_left.gif";
    } else {
        P5.setP5Cookie('adv',1);
        hideable.show();
        $("idsearchAdvOcImg").src = "/v5icons/arr_down.gif";
    }
};

P5.Search.advGo = function (special)
{
    var bitmask = 0;
    var searchwithin_select = $("idsearchwithinch");
    var aspect_select = $("idsearchaspect");
    var rating_select = $("idsearchrating");
    var rating_select_pts = $("idsearchratingpts");
    var duration_select = $("idsearchduration");
    var duration_tc = $("idsearchdurationsec");
    var price_select = $("idsearchprice");
    var price_tc = $("idsearchpriceusd");
    var sales_select = $("idsearchsales");
    var sales_tc = $("idsearchsalestc");
    var verbose_select = $("idsearchverbose");
    var artist_tc = $("idsearchartist");
    var advs = '';
    var searchstr = P5._q.strip();
    var advsearchstr = '';
    var both = false;
    var query = '';
    var sb = $("searchob0");
    var tt;
    if ($("idsearchwithintc")) {
        $("idsearchwithintc").value.strip();
    }
    if (searchwithin_select && 'new' == searchwithin_select.value) {
        both = false;
        advsearchstr = '';
        searchstr = $("idsearchwithintc").value.strip();
        query = encodeURIComponent(searchstr);
    } else {
        both = (0 !== searchstr.length && 0 !== advsearchstr.length) ? true : false;
        query = encodeURIComponent(((!both) ? '' : '(') +
                                   advsearchstr +
                                   ((!both) ? '' : ') AND (') +
                                   searchstr +
                                   ((!both) ? '' : ')'));
    }
    if (aspect_select && '-' != aspect_select.value) {
        advs += ((0 === advs.length) ? '' : ' ') + "aspect:" + aspect_select.value;
    }
    if (rating_select && rating_select_pts && '-' != rating_select_pts.value) {
        advs += ((0 === advs.length) ? '' : ' ') + rating_select.value + ':' + rating_select_pts.value;
    }
    if (duration_select && duration_tc && '' !== duration_tc.value.strip() && !isNaN(parseInt(duration_tc.value))) {
        advs += ((0 === advs.length) ? '' : ' ') + duration_select.value + ':' + parseInt(duration_tc.value.strip()) + '000';
    }
    if (price_select && price_tc && '' !== price_tc.value.strip() && !isNaN(parseInt(price_tc.value.strip()))) {
        advs += ((0 === advs.length) ? '' : ' ') + price_select.value + ':' + parseInt(price_tc.value.strip());
    }
    if (sales_select && sales_tc && '' !== sales_tc.value.strip() && !isNaN(parseInt(sales_tc.value))) {
        advs += ((0 === advs.length) ? '' : ' ') + sales_select.value + ':' + parseInt(sales_tc.value.strip());
    }
    if (verbose_select && '1' != verbose_select.value) {
        advs += ((0 === advs.length) ? '' : ' ') + "verbose:" + verbose_select.value;
    }
    if (artist_tc && '' !== artist_tc.value.strip()) {
        advs += ((0 === advs.length) ? '' : ' ') + "artist:" + artist_tc.value.strip();
    }
    if (sb) {
        if (1 == P5._search_sort_reverse && 100 > sb.value) {
            tt = parseInt(sb.value,10) + 100;
            P5.setP5Cookie('sb',tt);
        }
        if (0 === P5._search_sort_reverse && 100 <= sb.value) {
            tt = parseInt(sb.value,10) - 100;
            P5.setP5Cookie('sb',tt);
        }
    }
    
    if ($("search_format_hd")) {
        if ($("search_format_hd").checked) { bitmask += 1; }
        if ($("search_format_pal").checked) { bitmask += 2; }
        if ($("search_format_ntsc").checked) { bitmask += 4; }
        if ($("search_format_multimedia").checked) { bitmask += 8; }
        tt = $("search_format_music");
        if (tt) { if ($("search_format_music").checked) { bitmask += 16; } }
        tt = $("search_format_soundeffect");
        if (tt) { if ($("search_format_soundeffect").checked) { bitmask += 32; } }
    } else {
        bitmask = 63;
    }
    
    if (P5._jsCookie.get('bm') != bitmask)
    {
        P5.setP5Cookie('bm',bitmask);
    }
    
    if ("advanced" == special) {
        P5.setP5Cookie('adv',1);
        P5.setP5Cookie('no',$("searchrppadv").value);
        P5.setP5Cookie('sb',$("searchobadv").value); // TODO
    }
    
    var url = "/index.php?page=search" +
              "&p=0" +
              "&q=" + query +
              "&advs=" + encodeURIComponent(advs);
    window.location = url;
};

P5.Search.exclude = function (excluded)
{
    $("qsearchid").value += " !artist:" + excluded;
    P5.searchfun ();
};
