/*
 * Saint Saviour
 *
 * Main JavaScript functionality
 *
 */

var trace = function(msg) {
    try {
        console.log(msg);
    }
    catch (e) {}
};

// same as trace, but if console is disabled, pops an annoying msgbox
// DONT use this with, lets say, a loop
var debug = function(msg) {
    try {
        console.log(msg);
    }
    catch (e) {
        alert("DEBUG MESSAGE:\n\n" + msg);
    }
};


var featuredTimer = 0;


$(function() {
    
    /*
     * set up any UI visuals
     *
     */
    
    // hide search "GO" button (which is there simply for backwards compatibility)
    // UPDATE: not necessary; wrapped in a <noscript> tag instead
    //$('input#buttonSearch').hide();
    
    // clear the default search field text when focused
    // NOTE: to change what gets cleared, simple change the ALT attribute's value
    $('input#q').focus(function() {
        if ( $(this).attr('alt') == $(this).val() ) {
            $(this).val('');
        }
    });
    
    // when the search field loses focus, if it's blank, set it do it's default
    // NOTE: to change the default text, change the ALT attribute's value in the HTML
    $('input#q').blur(function() {
        if ( $(this).val() == '' ) {
            $(this).val( $(this).attr('alt') );
        }
    });
    
    //DEBUG
    // rotate between 3 sample images for the "featured" image
    /*var featuredTimer = window.setInterval(function() {
        $('div#featuredContent img').fadeOut(1000, function() {
            $(this).attr('src', function() {
                var oldSrc = $(this).attr('src');
                oldSrc = oldSrc.toString();
                var newImage = '';
                switch ( oldSrc.substring(oldSrc.lastIndexOf('/') + 1) ) {
                    case 'debug_featuredImage.png':
                        newImage = 'debug_featuredImage2.jpg';
                        break;
                    case 'debug_featuredImage2.jpg':
                        newImage = 'debug_featuredImage3.jpg';
                        break;
                    case 'debug_featuredImage3.jpg':
                        newImage = 'debug_featuredImage.png';
                        break;
                }
                return ('images/' + newImage);
            }).fadeIn(1000);
        });
    }, 8000);*/
    /* using cycle plugin instead
    featuredTimer = window.setInterval(function() {
        var images = $('div#featuredContent').find('img');
        if ( images.length < 1 ) {
            trace('error - only 1 featured image found');
        }
        else {
            //debug_featuredImage.png
            //debug_featuredImage2.jpg
            //debug_featuredImage3.jpg
            $(images[0]).animate({
                /*marginTop: ('-' + parseInt($(this).height() + 50) + 'px').toString()* /
                height: '0px',
                opacity: '0.4'
            }, 2000, '', function() {
                // occurs after the slideup animation is done
                //$(images[1]).after( $(images[0]).clone().css('marginTop', 'auto') );
                $(images[1]).after( $(images[0]).clone().css({
                    height: (($(images[1]).height()).toString() + 'px'),
                    opacity: '1.0'
                }) );
                $('div#featuredContent img:first-child').remove();
                
                $('div#featuredContent img:last-child').attr('src', function() {
                    var oldImage = $('div#featuredContent img:first-child').attr('src');
                    oldImage = oldImage.toString();
                    oldImage = oldImage.substring(oldImage.lastIndexOf('/') + 1);
                    var newImage = '';
                    switch ( oldImage ) {
                        case 'debug_featuredImage.png':
                            newImage = 'debug_featuredImage2.jpg';
                            break;
                        case 'debug_featuredImage2.jpg':
                            newImage = 'debug_featuredImage3.jpg';
                            break;
                        case 'debug_featuredImage3.jpg':
                            newImage = 'debug_featuredImage.png';
                            break;
                    }
                    return ('images/' + newImage);
                });
                
            });
        }
    }, 8000);
    
    //DEBUG
    // click to stop the now annoying rotating featured image
    $('div#featured').click(function() {
        window.clearInterval( featuredTimer );
        debug(
            'featured image timer cleared.' + "\n" +
            'rotation will not begin again until the page is refreshed.'
        );
    });*/
    
    
    $('#featuredContent').cycle({
        fx: 'fade',
        timeout: 7000
    });
    
    
    
    // repopulate the search field with the existing search query, if any
    var q = window.location.href;
    if ( q.indexOf('?q=') >= 0 ) {
        // grab everything after the ?q=
        q = q.substring( q.indexOf('?q=') + 3 );
        // snip off any other query strings
        if ( q.indexOf('&') >= 0 ) {
            // delete everything after the query
            q = q.substring( 0, q.indexOf('&') );
        }
        // set the field
        $('input#q').val( q );
    }
    
    
    
    
    jQuery.fn.supersleight = function(settings) {
        settings = jQuery.extend({
            imgs: true,
            backgrounds: true,
            shim: '../img/x.gif',
            apply_positioning: true
        }, settings);
        return this.each(function(){
            if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7 && parseInt(jQuery.browser.version) > 4) {
                jQuery(this).find('*').each(function(i,obj) {
                    var self = jQuery(obj);
                    // background pngs
                    if (settings.backgrounds && self.css('background-image').match(/\.png/i) !== null) {
                        var bg = self.css('background-image');
                        var src = bg.substring(5,bg.length-2);
                        var mode = (self.css('background-repeat') == 'no-repeat' ? 'crop' : 'scale');
                        var styles = {
                            'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='" + mode + "')",
                            'background-image': 'url('+settings.shim+')'
                        };
                        self.css(styles);
                    };
                    // image elements
                    if (settings.imgs && self.is('img[src$=png]')){
                        var styles = {
                            'width': self.width() + 'px',
                            'height': self.height() + 'px',
                            'filter': "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + self.attr('src') + "', sizingMethod='scale')"
                        };
                        self.css(styles).attr('src', settings.shim);
                    };
                    // apply position to 'active' elements
                    if (settings.applyPositioning && self.is('a, input') && self.css('position') === ''){
                        self.css('position', 'relative');
                    };
                });
            };
        });
    };
    
    
    $('body').supersleight();
    
    
}); // end jQuery dom-load wrapper

















