﻿//VARS
var strLastHash = null;
var strSearchString = '';
var bitRunning = 0;
var bitAdvanced = false;
var intCounter = 0;
var strLastToggleId = null;
var strLastBlueToggleId = null;
var bitImage = true;

// LIVE SEARCH FUNCS
function LoadSearchPage() {
    bitRunning = 1;
    new PeriodicalExecuter(function(pe) {
        var strNewSearch = GetSearchFromHash();
        if (strLastHash == strNewSearch) {
            if (++intCounter == 20) {
                intCounter = 0;
                bitRunning = 0;
                pe.stop();
            }
            return;
        }
        else {
            intCounter = 0
            bitRunning = 1;
            strLastHash = strNewSearch;
            GetResults(strNewSearch);
        }

    }, 0.5);
}

function SearchKeyUp2(strSearch) {
currentPage = 1;
    
    if (bitRunning == 0)
        LoadSearchPage();

    strSearchString = strSearch;

    if (!bitAdvanced)
        UpdateUrl(strSearch);
}

function GetResultsFirst(strText) {
    document.observe("dom:loaded", function() {
        if (strText == null) {
            var strSearch = GetSearchFromHash();
            if (strSearch.length > 0) {
                $('ctl00_cphMain_txtSearch').value = strSearch.stripTags();
                GetResults(strSearch);
            }
        }
        else {
            GetResults(strText);
        }
    });
}

var currentPage = 1;
function GetResults(strText) {
    $('spanSearchText').update('SÖKRESULTAT - <i>' + strText.stripTags() + '</i>');
    document.title = strText + ' - Sveriges största båtdatabas';
    if (strText.length == 0) {
        $('divLiveSearch').update('<span class="spanSearchInfo">Sök genom att skriva i rutan<span>');
        return;
    }
    UpdateUrl(strText);
    PageMethods.GetSearchResult(strText.strip(), 1, bitImage, OutResults);
}

function GetNextResultPage(lnk) {
    jQuery(lnk).parent().html("<img src='/images/loading.gif'/>");
    currentPage = currentPage + 1;
    if (bitAdvanced)
        SearchAdv(currentPage, true);
    else
        PageMethods.GetSearchResult(GetSearchFromHash().strip(), currentPage, bitImage, OutResultsPage);
}

function OutResults(result) {

    if (result.length > 0)
        $('divLiveSearch').update(result);
    else
        $('divLiveSearch').update(' <b>inga träffar..</b>');
}

function OutResultsPage(result) {
    if (result.length > 0) {
        jQuery('.searh_result').append(result);
        jQuery('#np').remove();
    }
}

function UpdateUrl(strSearch) {
    window.location.hash = '#s=' + strSearch.stripTags();
}

function GetSearchFromHash() {
    return decodeURI(window.location.hash.substring(3));
}
// LIVE SEARCH FUNCS END
                       

// ADVANCED SEARCH START
function SearchAdv(pagenumber, isPaging) {
    if (pagenumber == null) {
        currentPage = pagenumber = 1;
    }
    var strText = $('ctl00_cphMain_txtSearch').value;
    if (!bitAdvanced) {
        GetResults(strText);
        return;
    }
    $('spanSearchText').update('SÖKRESULTAT - <i>' + strText.stripTags() + '</i>');
    var strCat = '';
    var catElements = document.getElementsByName("cat")
    for (var i = 0; i < catElements.length; i++) {
        if (catElements[i].checked) {
            strCat += catElements[i].value + ",";
        }
    }
    PageMethods.GetSearchResultAdvanced(strText, pagenumber, bitImage, $('txtYearFrom').value, $('txtYearTo').value, $('txtLengthFrom').value, $('txtLengthTo').value, $('txtWidthFrom').value, $('txtWidthTo').value, strCat, !isPaging ? OutResults : OutResultsPage)
}

//ADVENCED SEARCH END
      
//FX
function ToggleImageView() {
    currentPage = 1;
    bitImage = !bitImage;
    if (bitImage)
        $('aImgToggle').innerHTML = 'Visa utan bilder';
    else
        $('aImgToggle').innerHTML = 'Visa med bilder';

    if (!bitAdvanced)
        GetResults($('ctl00_cphMain_txtSearch').value)
    else
        SearchAdv();
}

function ToggleAdvSearch() {
    bitAdvanced = !bitAdvanced;
    Effect.toggle('divAdvSearch', 'blind');
}

function Toggle(id) {
    Effect.toggle(id, 'blind');
}

function ToggleBoatTab(divId, aId) {
    if (divId == strLastBlueToggleId) {
        Effect.toggle(divId, 'blind')
        strLastBlueToggleId = null;
    }
    else if (strLastBlueToggleId == null) {
        Effect.toggle(divId, 'blind')
        strLastBlueToggleId = divId;
    }
    else {
        $(strLastBlueToggleId).setStyle({ display: 'none' });
        $(divId).setStyle({ display: '' });
        strLastBlueToggleId = divId;
    }
}

function ToggleMember(id, idHighLight) {
    new Effect.Highlight(idHighLight, { startcolor: '#dfe5f5', endcolor: '#ffffff' });

    if (strLastToggleId != null && strLastToggleId != id)
        Effect.BlindUp(strLastToggleId, { duration: 0.5 });

    if (strLastToggleId == id) {
        if ($(id).getStyle('display') == 'block')
            Effect.BlindUp(strLastToggleId, { duration: 0.5 });
        else
            Effect.BlindDown(id, { duration: 0.5 });
    }
    else
        Effect.BlindDown(id, { duration: 0.5 });

    strLastToggleId = id;
}

function Show(id) {
    document.observe("dom:loaded", function() {
        $(id).setStyle({ display: 'block' });
    });
}          


//IMAGES
function LoadPressImage(imageName, strTitle) {
    $('aPressBig').href = 'http://images.sokbat.se/upl/normal700/' + imageName
    $('aPressBig').title = strTitle;
    $('imgPressBig').src = 'http://images.sokbat.se/upl/normal285/' + imageName;
}

function LoadPressImageFirst(imageName, strTitle) {
    document.observe("dom:loaded", function() {
        $('aPressBig').href = 'http://images.sokbat.se/upl/normal700/' + imageName;
        $('aPressBig').title = strTitle;
        $('imgPressBig').src = 'http://images.sokbat.se/upl/normal285/' + imageName;
    });
}                   


//NAVIGATION
function GotToPage(strUrl) {
    window.location.href = strUrl;
}

function ImagePopUp(intImageId) {
    window.open('boatimagepopup.aspx?intImageId=' + intImageId, 'Bild', 'height=800,width=750,left=' + screen.width / 4 + ',top=100,resizable=no,scrollbars=no,toolbar=no,status=no');
}

function EventList(intEventId) {
    window.open('eventlistpopup.aspx?eid=' + intEventId, 'print', 'height=800,width=750,left=' + screen.width / 4 + ',top=100,resizable=no,scrollbars=yes,toolbar=no,status=no');
}
 
 //EVENT
function AddBoat(intEventBoatId) {
    new Ajax.Request('service/eventservice.aspx?action=1&ebid=' + intEventBoatId,
  {
      method: 'get',
      onSuccess: function(transport) {
          $(intEventBoatId.toString()).hide();
      }
  });
}

///WIKI
function Wiki(intModelId, intSessionId) {
    new Ajax.Request('/service/wikiservice.aspx',
  {
      method: 'post',
      parameters:
      {
          intModelId: intModelId
         , intSessionId: intSessionId
         , dLength: $('txtLength').value
         , dWidth: $('txtWidth').value
         , dWeight: $('txtWeight').value
         , dDepth: $('txtDepth').value
         , intPassengers: $('txtPassengers').value
         , intBeds: $('txtBeds').value
         , dSailArea: $('txtSailArea').value
         , intHpFrom: $('txtHpFrom').value
         , intHpTo: $('txtHpTo').value
      },
      onSuccess: function(transport) {

          if (transport.responseText.startsWith('1')) {
              $('snWikiStatus').update(transport.responseText.substring(1));
              $('btnSaveWiki').hide();
          }
          else
              $('snWikiStatus').update(transport.responseText.substring(1));

      },
      onFailure: function(transport) {
          $('snWikiStatus').update('Tyvärr gick något fel, försök gärna igen lite senare.');
      }

  });
}
                     
                     
                     
