/*
 * (c) 2008-9 Jason Frame
 * Auxiliary element code based on work by pjesi (http://wtf.hax.is/)
 */
(function ($) {
	
	/**
	 * Initialise input hints on all matched inputs.
	 *
	 * Usage examples:
	 *
	 * Add hints to all inputs with the 'title' attribute set:
	 *   $('input[title],textarea[title]').inputHint();
     *
     * Add hints to all matched elements, grabbing the hint text from each element's
     * adjacent <kbd/> tag:
     *   $('input').inputHint({using: '+ kbd'});
	 *
	 * Options keys:
	 *  using: jQuery selector locating element containing hint text, relative to
	 *         the input currently being considered.
	 *  hintAttr - tag attribute containing hint text. Default: 'title'
	 *  hintClass - CSS class to apply to inputs with active hints. Default: 'hint'
	 */
	$.fn.inputHint = function(options) {
		
		options = $.extend({hintClass: 'hint', hintAttr: 'title'}, options || {});
		
		function hintFor(element) {
			var h;
			if (options.using && (h = $(options.using, element)).length > 0) {
				return h.text();
			} else {
				return $(element).attr(options.hintAttr) || '';
			}
		}

		function showHint() {
			if ($(this).val() == '') {
				$(this).addClass(options.hintClass).val(hintFor(this));
			}
		}

		function removeHint() {
			if ($(this).hasClass(options.hintClass)) $(this).removeClass(options.hintClass).val('');
		}
		
		this.filter(function() { return !!hintFor(this); })
			.focus(removeHint).blur(showHint).blur();

        this.each(function() {
            var self = this;
            $(this).parents('form').submit(function() { removeHint.apply(self); });
        });

		return this.end(); // undo filter

	};
	
})(jQuery);

var fonts = {
    gridmag: { 
        font: { src: '/images/sifr-gridmag.swf' },
        replace: {
          selector: 'div.page-title h1, div.page-title h2',
          css: [
            '.sIFR-root { text-transform: uppercase; color: #716098; }'
          ]
        }
    },
    weegrid: {
        font: { src: '/images/sifr-weegrid.swf' },
        replace: {
          selector: 'div.page-title h1, div.page-title h2',
          css: [
            '.sIFR-root { text-transform: uppercase; font-weight: bold; color: #8752A1; }'
          ]
        }
    }
};

sIFR.activate(fonts[CURRENT_SITE].font);
sIFR.replace(fonts[CURRENT_SITE].font, fonts[CURRENT_SITE].replace);

function Slideshow(container, options) {
    
    var $items      = $(container).find('> ul'),
        self        = this,
        $li         = $items.find('li:first'),
        itemWidth   = $li.outerWidth(true);

    function scrollTo(index) {
        $items.animate({marginLeft: -itemWidth * index});
    }
    
    var $pages = $(options.paginator);
    $pages.click(function() {
        $pages.removeClass('selected');
        $(this).addClass('selected');
        scrollTo($pages.index(this));
        return false;
    });

};

$(function() {
	$('input').each(function() { $(this).addClass(this.type); });
	$('input[title]').inputHint();
	
	$('.media-player').each(function() {
		var mediaUrl	= $(this).find('.media-url').attr('href'),
			width		= parseInt($(this).css('width'), 10),
			height		= parseInt($(this).css('height'), 10),
			background	= $(this).find('img').attr('src') || null,
			player		= null;
			
		if ($(this).is('.flash-player')) {
			player = createFlashPlayer(mediaUrl, width, height);
		} else {
			var type = $(this).is('.video-player') ? 'video' : 'sound';
			player = createMediaPlayer(type, mediaUrl, width, height, background);
		}
		
		if (player) player.write(this);
	});
	
	$('.expandable').each(function() {
	    var $panel = $(this),
	        $expanders = $(this.parentNode).find('.expander');
        $expanders.click(function() {
            $panel.slideToggle();
            $expanders.toggleClass('expanded');
            return false;
        });
	});
	
	$('.tab-pane').each(function() {
	    var $buttons = $(this).find('.tab-bar li'),
	        $tabs    = $(this).find('.tab');
	    $buttons.click(function() {
	        $buttons.removeClass('selected');
	        $(this).addClass('selected');
	        $tabs.hide().eq($buttons.index(this)).show();
	        return false;
	    });
	    $buttons.eq(0).click();
	});
	
    // $('#newsletter-blurb .actuator').click(function() {
    //  var $ns = $('#newsletter-signup');
    //  $ns.stop().animate({width: $ns.width() > 0 ? 0 : 233});
    //  return false;
    // });
    // 
    // $('#newsletter-signup').submit(function() {
    //     var $input = $(this).find('[name=email]');
    //     $.ajax({
    //         url: '/newsletter_signup',
    //         type: 'POST',
    //         success: function() {
    //             alert('Thank you for signing up');
    //             $input.val('');
    //         },
    //         error: function() {
    //             alert('An error occurred; please ensure the email address entered is valid');
    //         },
    //         data: {email: $input.val()}
    //     })
    //     return false;
    // });
	
});

(function(scope) {
	
	var nextVideoIndex = 0;
	
	scope.createFlashPlayer = function(flashUrl, width, height, params) {
		params = params || {};
		var p = new SWFObject(flashUrl, 'flash-player-' + (nextVideoIndex++), width, height, 9);
		for (var k in params) p.addParam(k, params[k]);
		return p;
	};
	
	scope.createMediaPlayer = function(type, url, width, height, bg) {
		var fv = 'stretching=fill&file=' + url + '&type=' + type;
		if (bg) fv += '&image=' + bg;
		return createFlashPlayer('/images/jw/player.swf', width, height, {
			allowfullscreen: 'true',
			flashvars: fv
		});
	};
	
})(this);