$(document).ready(function () { 

    $('#headerSearchInput').autocomplete("/xhr/search/header_suggestions.php", {
        width: 236,
        selectFirst: false
    });

    $('#headerSearchInput').keydown(function(event) {
        if (event.keyCode == "40" || event.keyCode == "38") {
            var suggestion = $('.ac_results .ac_over').html();
            suggestion = suggestion.replace('<strong>', '');
            suggestion = suggestion.replace('</strong>', '');
            $('#headerSearchInput').val(suggestion);
        }
        if (event.keyCode == "13") {
            $('#srchform').submit();
            return false;
        }

    });

    toolTip.ini();
    
    //cust login popup
    custNavi.ini();
    
    // validate some forms
    if ($('#inquiryform').length > 0) {
        $('#inquiryform').validate();
    }    
    
    // are there any articles which want to have a tooltip ?:)
    /**
     * @todo: implement "clean" lazy-loading to avoid a lot of XHR-Request
     * while viewing an article page.
     * HQ and thumb-images have both the same id, just replacing "img/thumb" with
     * "img/hq" seems to be a better solution
     */
    //artToolTip.ini();


    if (typeof jQuery().fancybox == 'undefined') {
            var script = document.createElement('script');
            script.type = 'text/javascript';
            script.src = '/js/jquery/jquery.fancybox.pack.js';
            $('body').append(script);
    }

    $('a[rel="fancyboxMenu"]').fancybox({
        'width'              : 700,
        'height'             : 315,
        'showCloseButton'    : true,
        'hideOnOverlayClick' : false,
        'swf'                : {
            wmode: 'window',
            quality: 'high',
            bgcolor: '#ffffff',
            play: 'true',
            loop: 'true',
            scale: 'showall',
            menu: 'false',
            devicefont: 'false',
            salign: '',
            allowScriptAccess: 'always'
        }
    });
});


var custNavi = {
    
    ini : function () {
        
        if ($('.loginCustnaviLink').length > 0 && $('#singin').length > 0) {
        
            $('.loginCustnaviLink').click( function () {
                
                if ($(this).hasClass('active')) {
                    $(this).removeClass('active');
                    $('#singin').hide();                            
                } else {
                    $(this).addClass('active');
                    $('#singin').show();
                }
                
                return false;   
            });
        }
        
    }
    
};


var toolTip = {
    
    ini : function () {
        
        if ($('.toolTip').length > 0) {
            
            
            if (!$('.toolTip').hasClass('activeToolTip')) {
                $('.toolTip').append('<div class="toolTipBox" style="display: none;"></div>');
                $('.toolTip').addClass('activeToolTip');
            }
            
            $('.toolTip').hover(
                function () {
                    $(this).find('div').html($(this).attr('title')); 
                    $(this).find('div').show();       
                },
                function () {
                    $(this).find('div').hide();                     
                }
            );            
        }
                
    }
    
};


var artToolTip = {
    
    containerName : 'articleToolTipContainer',
    
    currentToolTipBoxID : null,
    
    ini : function () {
        
        // get all article links
        $('.articleShortToolTip').hover( 
            function () {
                
                // get attribute rel
                artId = $(this).attr('rel');
                
                // is there already a tooltip box ?
                if (!$(this).children('div.' + artToolTip.containerName).hasClass(artToolTip.containerName)
                    && $(this).find('div.' + artToolTip.containerName).length == 0) {
                    artToolTip.addToolTipBox(this, 'short', artId);
                } else {
                    artId = $(this).attr('rel');
                    artToolTip.currentToolTipBoxID = 'artToolTipBox' + artId;
                    $('#' + artToolTip.currentToolTipBoxID).show();
                }
                
            }, function () {
                artId = $(this).attr('rel');
                $('.articleToolTipContainer').hide(); 
            }
        );
        
    },
    
    addToolTipBox : function (item, type, artId) {
        
        // get article Infos
        $.getJSON('/xhr/art/json.php', {
                art : artId,
                loc : conf.loc
            },
            function(data, textStatus) {
                if (textStatus == 'success') { 
                    articleJson  = data;      
                    
                    if (artId == articleJson.id
                        && $(item).find('div.' + artToolTip.containerName).length == 0) {
      
                        currentToolTipBoxID = 'artToolTipBox' + artId;  
      
                        // preload image
                        imagePath = articleJson.imageHost + articleJson.images[0].grp140Img;
                        
                        imageObj = new Image();
                        imageObj.src = imagePath;
                        
                        imageObj.onload= function () {
                            $(item).prepend('<div style="display: none;" class="' + artToolTip.containerName + '" id="' + currentToolTipBoxID + '"></div>');
                            $('#' + currentToolTipBoxID).append('<div class="artToolTipImageContainer"><img src="' + imageObj.src + '" alt="" /></div>');
                            $('#' + currentToolTipBoxID).show();
                            artToolTip.currentToolTipBoxID = 'artToolTipBox' + artId;
                        }; 
                    }
                }
            }
        );
        
    }
    
};
