﻿/// <reference path="jquery-1.2.3.pack.intellisense.js" />
var currentid;

$(document).ready(function() {
   
   //prevent form submission of textfield and enter key
   $("body").keydown(function(event){
        var key = (window.event) ? event.keyCode : event.which;   
        
        // Was key that was pressed a the enter key 13?           
        if ( key == 13 )     //return true; // if so, do nothing   else // otherwise, discard character     
        {
            
            //perform search
            $("#btnSearch").click();
                                  
            if (window.event) //IE       
                window.event.returnValue = null;     
            else //Firefox       
                event.preventDefault(event); 
        }
      
   });
   
   //hookup click event for search button
   $("#btnSearch").click(function(){                    
        
        //strip out all , : and double spaces
        var cleansearch = $("#txtSearch").val();
        cleansearch = cleansearch.replace(/,/g," ").replace(/  /g," ");
        
        //replace back value in search box
        $("#txtSearch").val(cleansearch);
        
        //set ingredients from cookie
         $.cookie('ingredients', $("#txtSearch").val());//, { expires: 10, path: '/', domain: 'scavengerchef.com', secure: true });
  
        //close popup if open
        jQuery(document).trigger('close.facebox');
                
        $("#results").hide();     
        $("#processing").fadeIn();            
        $("#btnSearch").attr('disabled','disabled');        
        
        performSearch($("#txtSearch").val(), 1);        
        
   }); 
   
   //retrieve ingredient cookie
   $("#txtSearch").val($.cookie('ingredients')); 
   
   //set focus to search bpx
   $("#txtSearch").focus();
               
   /*$(document).click(function(){
        jQuery(document).trigger('close.facebox'); 
        
   });*/
   
   
      
});
 
function registerSearchResults()
{
    $(".recipe").click(function(item){
        
        //get the details for recipe specified if not the same request already done
        if (currentid!=this.id)
        {
            $.get("RecipeDetail.aspx?r=" + encodeURI(this.id), function(response) {                    
                jQuery.facebox(response);
                
                $("#videos").youtube(
                  {  type    :'search',
                     keyword :$("#rtitle").text(),
                     inlineVideo:true
                  }
                );
                
                $("#lnkFlagSpam").click(function(){
                    //alert($(this).attr('rel'));
                    jQuery.post('/FlagSpam.aspx',{"r": $(this).attr('rel')},function(data,response){
                        $.blockUI(data);
                        
                        //register unblock
                        $("#exitpopup").click(function(){
                            $.unblockUI();
                        });
                    });
                });
                
                highlightKeywords($("#txtSearch").val());
                instantImage(document.getElementById('imgRecipe'), {tilt:'right',shadow:50,noshade:true,color:000000,preserve:true});                                                              
                
                //enable star rating                         
                //var id = $("input[name*='txtID']").val();                   
                $('#starrating').rating('RateRecipe.aspx', {maxvalue: 5});
                
                //enable close button
                $('.close_image').click($.facebox.close);
            });
        }
        else
        {
            $("#facebox").fadeIn();                
        }
                
        //clear all the other ones
        $(".recipe").attr('class','recipe');
        
        //set the current selected item
        $("#"+this.id).attr('class','recipe selected');                                       
        
        
        //store the id for use
        currentid = this.id;
        
        return false;
    });    
    
    $(".recipe").mouseover(function(item){
        this.style.background = '#5E522A';
        this.style.cursor = 'pointer';        
    });
    
    $(".recipe").mouseout(function(item){
        this.style.background = '';        
        this.style.cursor = '';
    });
    
    $(".recipeimage").each(function(){
        instantImage(document.getElementById(this.id), {tilt:'none',shadow:50,noshade:true,color:'#ffffff',preserve:true});
    });
    
    //setup pager links
    $(".pager").each(function(item){
        $(this).click(function(){              
            performSearch($("#txtSearch").val(), this.innerHTML);                   
        });
    });
    
    
}

function performSearch(searchterm, page)
{
    $.get("SearchResults.aspx?s=" + encodeURI(searchterm + "&p=" + page), function(response) {        
        $("#processing").hide();
        jQuery(document).trigger('close.facebox');        
        
        //Fix for IE7 dxtransform and cleartype with jquery fadein
        if (jQuery.browser.msie)
        {
            $("#results").html(response).show();                         
        }
        else
        {
            $("#results").html(response).fadeIn();                         
        } 
        
        $("#btnSearch").attr('disabled','');
        registerSearchResults();
    });
}

function preventDefault(e) 
{ 
 e=e||event;
 e.preventdefault? e.preventdefault() : e.returnValue = false; 
}

function highlightKeywords(keywords)
{
    //split the keywords and replace each with highlighted
    var arrKeywords = keywords.split(" ");    
    var ingredients = $("#ingredients").html();
    
    var output = ingredients;
    for(var i=0;i<arrKeywords.length;i++)
    {
        output = output.replace(arrKeywords[i],"<span class='keyword'>" + arrKeywords[i] + "</span>");
    }
        
    $("#ingredients").html(output);
}

function instantImage(image, options)
{
    //{tilt: value, shadow: value, shade: value, color: value, noshade: value, preserve: value }    
    cvi_instant.add(image, options);
}

function removeFilter(element)
{
    if(element.style.filter && element.style.removeAttribute){
        element.style.removeAttribute('filter');
    }
}