﻿$(document).ready(function() {

    var myhtml = "";

    var $form = $('#BrowseSearchCollection');
    $form.keypress(function(e){         
        if (e.which == 13 && e.target.type != 'textarea' && e.target.type != "") {                               
            $.getJSON("Handler/Handler.ashx", { "action": "GallerySearch", "searchString": $("#txt_searchTerms").val(), "Refresh": new Date().getTime() }, function(data){              
                myhtml = '<div id="Carousel"><div class="Middle"><ul id="CarouselItems">'
                var count = 0;
                $.each(data, function(i, e){    
                    count += 1;                                                                   
                    if(e["ID"] > 0){
                        myhtml += '<li><a class="CP' + count.toString() + '" href="Painting.aspx?paintingid=' + e["ID"] + '"><img src="' + e["Image"] + '" alt="' + e["Title"] + '" /></a></li>';
                    }                                                
                });   
                
                myhtml += "</ul></div></div>"; 
                $("#resultContainer").html(myhtml);                
                
                if(count == 0){                
                    $("#CarouselButtons").hide(); 
                    $("#NoResults").show();  
                    $("#resultContainer").hide();            
                }else{
                    $("#CarouselButtons").show();
                    $("#NoResults").hide();
                    $("#resultContainer").show();
                    startCarousel();
                }
                                                                                
            });                                                                                      
        }
    });  
    
    $("#txt_searchTerms").focus(function(){    
        if($("#txt_searchTerms").val() == "Artist, title, medium or country"){
            $("#txt_searchTerms").val("");
        }       
    })
    
    $("#txt_searchTerms").blur(function(){
        if($("#txt_searchTerms").val() == ""){
            $("#txt_searchTerms").val("Artist, title, medium or country");
        }        
    })
              
})
