var SearchEngine = {};
SearchEngine.pagesize = 6;
SearchEngine.titleL = 30;
SearchEngine.descL = 80;


jQuery(document).ready(function() {
    // Safely inject CSS3 and give the search results a shadow
    var cssObj = {
        'box-shadow' : '#000 5px 5px 30px', // Added when CSS3 is standard
        '-webkit-box-shadow' : '#000 5px 5px 30px', // Safari
        '-moz-box-shadow' : '#000 5px 5px 30px'
    }; // Firefox 3.5+
    jQuery("#suggestions").css(cssObj);

    var mozilla   = jQuery.browser.mozilla;   // Mozilla
    var msie      = jQuery.browser.msie;      // Internet Explorer Win / Mac
    var chrome      = jQuery.browser.chrome;      // Internet Explorer Win / Mac
    var opera     = jQuery.browser.opera;     // Opera
    var safari    = jQuery.browser.safari;    // Safari

    var offset = jQuery("#SearchInputString").offset();
    var width = jQuery("#SearchInputString").width();
    var otherOffset = -12;
    //if(safari || chrome)otherOffset = 108;

    //jQuery('#suggestions').css('left',(offset.left - width + otherOffset)+'px');

    // Fade out the suggestions box when not active
    jQuery("#SearchInputString").blur(function(){
        jQuery('#suggestions').fadeOut();
    });
});

function doCufon(){
    return;
    Cufon('#suggestions span.category , #searchresults span.pagination_footer , #searchresults span.searchheading', {
        fontFamily: 'Breil_HV'
    });
}

function lookup(inputString) {
    if(inputString.length == 0) {
        jQuery('#suggestions').fadeOut(); // Hide the suggestions box
    } else {
        jQuery.post( ASAP.LIVESITE.URL + "/json.php",
        {
            queryString: ""+inputString+"" ,
            pagesize:SearchEngine.pagesize ,
            titleL:SearchEngine.titleL ,
            descL:SearchEngine.descL,            
            lang: ASAP.LANG,
            option: 'com_ajax',
            task:'search'

        }, function(data) { // Do an AJAX call
            jQuery('#suggestions').html(data); // Fill the suggestions box
            doCufon();

            jQuery('#suggestions').fadeIn('slow'); // Show the suggestions box

            jQuery('#suggestions').attr('inputString',inputString);
            jQuery('#suggestions').attr('page',1);
            setArrowTrigger();
        });
    }
}


function setArrowTrigger(){

    jQuery('#suggestions').bind('mouseout' , function(){
        jQuery("#SearchInputString").focus();
    }
    );

    jQuery('#right_arrow').click( function(){
        var inputString = jQuery('#suggestions').attr('inputString');
        var page = parseInt( jQuery('#suggestions').attr('page') );
        page++;
        jQuery.post( ASAP.LIVESITE.URL + "/json.php",
        {
            queryString: ""+inputString+"" ,
            page:page ,
            pagesize:SearchEngine.pagesize ,
            titleL:SearchEngine.titleL ,
            descL:SearchEngine.descL,            
            lang: ASAP.LANG,
            option: 'com_ajax',
            task:'search'
        }, function(data) { // Do an AJAX call
            jQuery('#suggestions').html(data); // Fill the suggestions box
            doCufon();
            jQuery('#suggestions').fadeIn();
            jQuery('#suggestions').attr('page',page);
            setArrowTrigger();
        });
    });


    jQuery('#left_arrow').click( function(){
        var inputString = jQuery('#suggestions').attr('inputString');
        var page = parseInt( jQuery('#suggestions').attr('page') );
        if(page>1)page--;
        jQuery.post( ASAP.LIVESITE.URL + "/json.php",
        {
            queryString: ""+inputString+"" ,
            page:page ,
            pagesize:SearchEngine.pagesize ,
            titleL:SearchEngine.titleL ,
            descL:SearchEngine.descL,            
            lang: ASAP.LANG,
            option: 'com_ajax',
            task:'search'
        }, function(data) { // Do an AJAX call
            jQuery('#suggestions').html(data); // Fill the suggestions box
            doCufon();
            jQuery('#suggestions').fadeIn();
            jQuery('#suggestions').attr('page',page);
            setArrowTrigger();
        });
    });

}
