function searchDestinations(query,page){
    _requestQueue.push(
    $.ajax({

    type: "POST",

    url: "/SearchResults.aspx/searchDestinations",

    data: "{'searchQuery':'"+query.toString()+"',"
          
           +"'pageNum':'"+page.toString()+"'}", 
            

    contentType: "application/json; charset=utf-8", 

    dataType: "json",

    success: function(xhr, status) {
            
            var sb = "";
             if(xhr.length<1){
                sb+=buildNoItem();
            
            }
            
             
                    sb +=buildDestinationResult(xhr);
                   
                   $(".divDestinationResults .ShowingPages").html(buildPaging(page,xhr.TotalResults,"searchDestinations","#destresults"));

            
             
             $(".divDestinationResults .searchcontent").html(sb);
             TravelPack_Attach_Events();
                   
    },

    error: function(xhr, status, error) {

    var err = JSON.parse(xhr.responseText);

    $("#error").html("Error –> " + err.Message);

    }

    }));
    

}

function searchTravelPacks(query,page){
    _requestQueue.push(
    $.ajax({

    type: "POST",

    url: "/SearchResults.aspx/searchTravelPacks",

    data: "{'searchQuery':'"+query.toString()+"',"
          
           +"'pageNum':'"+page.toString()+"'}", 
            

    contentType: "application/json; charset=utf-8", 

    dataType: "json",

    success: function(xhr, status) {
            
            var sb = "";
             
           
             
                    sb +=buildTravelPackResult(xhr.SearchResults);
                   

            
             
             $(".divTravelPackResults .searchcontent").html(sb);
             
             $(".divTravelPackResults .ShowingPages").html(buildPaging(page,xhr.TotalResults,"searchTravelPacks","#tpresults"));

            
             
            
                   
    },

    error: function(xhr, status, error) {

    var err = JSON.parse(xhr.responseText);

    $("#error").html("Error –> " + err.Message);

    }

    }));
    return false;
}


function searchExpertPosts(query,page){
    _requestQueue.push(
    $.ajax({

    type: "POST",

    url: "/SearchResults.aspx/searchLocalExperts",

    data: "{'searchQuery':'"+query.toString()+"',"
          
           +"'pageNum':'"+page.toString()+"'}", 
            

    contentType: "application/json; charset=utf-8", 

    dataType: "json",

    success: function(xhr, status) {
            
            var sb = "";
            
            
             
                    sb +=buildLocalExpertResult(xhr.SearchResults);
             

            
             
              $(".divLocalExpertResults .searchcontent").html(sb);
             
               $(".divLocalExpertResults .ShowingPages").html(buildPaging(page,xhr.TotalResults,"searchExpertPosts","#leresults"));
               
               TravelPack_Attach_Events();    
    },

    error: function(xhr, status, error) {

    var err = JSON.parse(xhr.responseText);

    $("#error").html("Error –> " + err.Message);

    }

    }));
    return false;
}

function buildPaging(pageNum, totalItems,call,anchor){
    
    var searchQuery = $("#searchterm").val();
    var firstvis = 10*(pageNum-1)+1;
    var lastvis = totalItems>10*pageNum?10*pageNum:totalItems;
    var numPages = Math.ceil(totalItems / 10.0);
    var pageResult = "<div class='Showing'>"
                           + "<h2>SHOWING</h2>"
                           + "<a>"+firstvis.toString()+"</a>"
                           + "<h2>-</h2>"
                           + "<a>"+lastvis.toString()+"</a>"
                           + "<h2>OF</h2>"
                           + "<a>"+totalItems.toString()+"</a>"
                           + "</div>"
                           + "<div class='Pages'>"
                           + "<ul>";
       var paging = "";
     
       if(pageNum>1){
        paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',1);\">&laquo;</a></li>";
       }  
    if(pageNum<3){// case at the beginning of the list
       
       paging+= pageNum==1?"<li>1</li>":"<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',1);\">1</a></li>";
       if(totalItems>10)
       paging+= pageNum==2?"<li>2</li>":"<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',2);\">2</a></li>";
       if(totalItems>2*10)
       paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',3);\">3</a></li>";
       if(totalItems>3*10)
       paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',4);\">4</a></li>";
       if(totalItems>4*10)
        paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',5);\">5</a></li>";
        
    }else if(pageNum>numPages-3){ //case at the end of the list
        if(numPages-4>0){
        paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+(numPages-4).toString()+");\">"+(numPages-4).toString()+"</a></li>";
        }
        if(numPages-3>0){
        paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+(numPages-3).toString()+");\">"+(numPages-3).toString()+"</a></li>";
        }
        if(numPages-2>0){
            paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+(numPages-2).toString()+");\">"+(numPages-2).toString()+"</a></li>";
        }
       paging+= pageNum==numPages-1?"<li>"+(numPages-1).toString()+"</li>":"<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+(numPages-1).toString()+");\">"+(numPages-1).toString()+"</a></li>";
       paging+= pageNum==numPages?"<li>"+numPages.toString()+"</li>":"<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+numPages.toString()+");\">"+numPages+"</a></li>";
       
    
    }else{ //somewhere in the middle of the list
       
        for(x = pageNum-2;x<pageNum+3;x++){
            if(x>0){
            if(x == pageNum){
                paging+="<li>"+pageNum.toString()+"</li>";
            
            }else{
                paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+x.toString()+");\">"+x+"</a></li>"
            }
            }
            
        }
        
    
    }
    if(pageNum<numPages){
        paging+="<li><a href='"+anchor+"' onclick=\""+call+"('" + searchQuery + "',"+numPages.toString()+");\">&raquo;</a></li>";
       }  
    pageResult+=paging                           
                +"</ul>"
                +    "<p>PAGE</p>"
                +"</div>";

    return pageResult;
}

function getIcon(iconType){
    var iconLink = "";
    switch(iconType){
            case 'city':
                iconLink = "/i/category_icons/icn-city.jpg";
                break;
            case 'dining':
                iconLink ="/i/category_icons/icn-dining.gif"
                break;
            case 'lodging':
                iconLink ="/i/category_icons/icn-hotel.gif"
                break;
            case 'attraction':
                iconLink ="/i/category_icons/icn-attraction.gif"
                break;
            case 'activity':
                iconLink ="/i/category_icons/icn-activities.gif"
                break;
        
        
        }
        
        return iconLink;


}

function getStarRating(rating){
    var ratingResult = "";
    switch(rating){
        case 0:
            ratingResult = "/i/nostars-1.gif";
            break;
        case 1:
            ratingResult = "/i/screen/star1.gif";
            break;
        case 2:
            ratingResult = "/i/screen/star2.gif";
            break;
        case 3:
            ratingResult = "/i/screen/star3.gif";
            break;
        case 4:
            ratingResult = "/i/screen/star4.gif";
            break;
        case 5:
            ratingResult = "/i/screen/star5.gif";
            break;   
         
    
    
    }

    return ratingResult;

}
function getTrustedSources(sourceList,sourceListAlt,TrustedSourcesLink){
    
    retStr="<ul>";
    if(sourceList!=null){
        for(var x=0;x<sourceList.length;x++){
            retStr+="<li><a href='" + TrustedSourcesLink[x] + "'><img src='"+sourceList[x]+"' alt='"+sourceListAlt[x]+"' title='"+sourceListAlt[x]+"' /></a></li>";
        
        }
    }
    retStr+="</ul>";
    
    return retStr;

}

function buildDestinationResult(item){
    var destItems = item.SearchResults;
    var destResult = "";
    for(var x = 0;x<destItems.length;x++){
        var destItem = destItems[x];
        
        
       var section =  "<div class='divDestinationRow'>"
                            +"<div class='IconDestination'><img src='"+getIcon(destItem.DestType)+"'/></div>"
                         +   "<div class='DestinationInfo'>"
                          +      "<h1><a href='"+destItem.Link+"'>"+destItem.Title+"</a></h1>";

                           section+="<img src='"+getStarRating(destItem.StarRating)+"' />";
                           
                           section += "<p>"+destItem.DescAddress+"</p>"
                            +"</div>"
                        
                            +"<div class='AddTPK'><p class='add'><a href='#place,"+destItem.PlaceId+"'/></p></div>"
                        
                        +   "<div class='TrustResources'>"+getTrustedSources(destItem.TrustedSources,destItem.TrustedSourcesAlt,destItem.TrustedSourcesLink)+"</div>"
                            +"<div class='divLinkResults'>"
                            +   "<a href='"+destItem.Link+"'>"+destItem.Link+"</a>"
                            +"</div>"
                        +"</div>"
                    +"<hr />";
                    
        destResult += section;
    }
        
    return destResult;
}

function buildTravelPackResult(items){
    var tpkResult = "";
    for(var x=0;x<items.length;x++){
        tpkItem = items[x];
        var section = "<div class='divTravelPackRow'>"
                                    +"<div class='CopyTPK'>"
                                      + "<p class='add'><a href='#"+tpkItem.TravelPackId.toString()+"' /></p>"
                                    +"</div>"
                                    +"<div class='TPKrow'>"
                                        +"<img src='"+tpkItem.Image+"' />"
                                    +"</div>"
                                    +"<div class='TravelPackTitle'>"
                                      + "<a href='"+tpkItem.Link+"'>"+tpkItem.Title+"</a> <p> by </p><a href='"+tpkItem.OnwerProfileLink+"'>"+tpkItem.OwnerName+"</a>"
                                    +"</div>"
                                    +"<div class='TravelPackLocation'>"
                                        +"<a>"+tpkItem.Locations+"</a>"
                                        +"<p>"+tpkItem.Description+"</p>"
                                    +"</div>"
                                    +"<div class='divLinkResults2'>"
                                      + " <a href='"+tpkItem.Link+"'>"+tpkItem.Link+"</a>"
                                    +"</div>"
                            +"</div>"
                            +"<hr />";
       tpkResult+=section;
    }
    return tpkResult;
}

function buildLocalExpertResult(items){

    var result = "";
    for(x = 0;x<items.length;x++){
                var item = items[x];
                 var section =  "<div class='divLocalExpertRow'>"
                                           +"<div class='LExprow'>"
                                            +    "<img src='"+item.LocalExpertImage+"' />"
                                            +"</div>"
                                           +" <div class='LocalExpertTitle'>"
                                            +    "<a href='"+item.Link+"'>"+item.Title+"</a><p> by </p><a href='"+item.LocalExpertUrl+"'>"+item.LocalExpertName+"</a>"
                                            +"</div>"
                                           +" <div class='LocalExpertLocation'>"
                                            +    "<a>"+item.Location+" Expert</a>"
                                            +    "<p>"+item.Description+"</p>"
                                            +"</div>"
                                            +"<div class='divLinkResults2'>"
                                             +   "<a href='"+item.Link+"'>"+item.Link+"</a>"
                                            +"</div>"
                                      +"</div>"
                                      +"<hr />";
    result+=section;
    }
    return result;
}

/**
 * help section click handlers
 */
$(document).ready(function(){

    //populate the search term
    $("#site_search").val($("#searchterm").val());
    //collapse all sections
    $(".help").css("display","none");
    //attach click handlers
    $(".HelpButton .icon a").click(function(){
        
        var helpsection = $(this).parents(".searchSection").find(".help");
       
        if(helpsection.css("display")=="none"){
            //show the help
           
            jQuery.each(jQuery.browser, function(i) {
              if($.browser.msie){
                //$("#div ul li").css("display","inline");
                helpsection.css("display","block");
              }else{
                helpsection.css("display","table");
              }
            });

            
        }else{
            //hide the help
            helpsection.css("display","none");
        }
        return false;
    });
    //hide the paging controls until show all clicked
    $(".ShowingPages").css("display","none");
    $(".ViewAllResults a").click(function(){
        
        var paging = $(this).parents(".searchSection").find(".ShowingPages");
        
        if(paging.css("display")=="none"){
            paging.css("display","block");
            $(this).parent().css("display","none"); //hide the show all items button
            //show the hidden 5 extra results
            var hiddenSection = $(this).parents(".searchSection").find(".desthidden").css("display","block");
        }else{
            //shouldn't reach here since the button is hidden after it is clicked
        }
        return false;
    
    });
    
    //load the mini map
    try{
    loadMap();
    }catch(ex){
    
    }
    //TravelPack_Attach_Events();
});

var _requestQueue = new Array();


function cancelRequests(){
    while(_requestQueue.length>0){
        _requestQueue.pop().abort();
    }

}

//Helper to get an element by its className
function getElementByClassName(parent,element_name,class_name){
	var arr_temp = parent.getElementsByTagName(element_name);
	var element;
	for(i=0;i<arr_temp.length;i++){
		if(arr_temp[i].className == class_name) element = arr_temp[i];
	}
	return element;
}
