/**
 * jPrelisten - jQuery plugin for prelisten mobile content
 * Developed for net mobile maxmedia web portals
 *
 * @requires jQuery v1.2.6
 *
 * @author Alexander Gut <alexander.gut@net-m.de>
 * @version 1.0
 * @modified %%INFO 2009-01-23 15:19:05+0100 rgarcia%%
 */
/**
 * Adds prelisten function based on adding an swf-player to DOM.
 * 
 * The HTML markup that is used can be as simple as... <img class="playbtn
 * $precty oid_$itemOID" src='...' width="$!{config_items_icon_prelisten_width}"
 * width="..." height="..." border="0" alt="..." /> or <a href="#"
 * class="playbtn $precty oid_$itemOID">Prelisten</a>
 * 
 * It is important, that clickable entity (e.g. <img>-tag) has only classes
 * shown above!!
 * 
 * Usually you will have all prelisten buttons in some container, like DIV or TD
 * element. It is advisable to set some class to this container, like
 * class="prelisten".
 * 
 * To activate jPrelisten you should add this lines to the bottom of HTML code:
 * 
 * <script type="text/javascript" src="/path/to/jquery.prelisten.js"></script>
 * <script type="text/javascript"> jQuery(document).ready(function(){
 * 
 * jQuery( ".prelisten > .playbtn" ).prelisten({cid:"$cid"}); }); </script>
 * 
 * It is also required to set some default values:
 * 
 * imgBtn - use images for prelisten (default=true) imgPlay -
 * '/path/to/icon_prelisten_play.gif' if using images imgStop -
 * '/path/to/icon_prelisten_stop.gif' if using images imgPlayHover -
 * '/path/to/icon_prelisten_play_over.gif' if using images imgStopHover -
 * '/path/to/icon_prelisten_stop_over.gif' if using images playerFile -
 * '/path/to/streamplayer.swf'
 * 
 */
(function($) { // Compliant with jQuery.noConflict()

	$.fn.prelisten = function(opts) {
		// extend default settings with given parameters
		var opts = $.extend( {}, $.fn.prelisten.defaults, opts);
		// override public default values with new settings
		$.fn.prelisten.defaults = opts;

		// buttons = this; // used to find current playing element
		// $.fn.prelisten.allEls = buttons;
		$.fn.prelisten.allEls = $($.merge($.makeArray($.fn.prelisten.allEls), $
				.makeArray(this)));

		// react on hover over prelisten button
		function makeHover(el) {
			$el = $(el);
			// set current status !before! doing something
			$el.toggleClass(opts.hoveredClass);
			if (opts.imgBtn) {
				// if image, replace src depending on current image (play/stop)
				if ($el.hasClass(opts.hoveredClass)) {
					// mouseover
					if ($el.hasClass(opts.activeClass)) {
						$el.attr('src', opts.imgStopHover);
					} else {
						$el.attr('src', opts.imgPlayHover);
					}
				} else {
					// mouseout
					if ($el.hasClass(opts.activeClass)) {
						$el.attr('src', opts.imgStop);
					} else {
						$el.attr('src', opts.imgPlay);
					}
				}
			}
		}
		;

		// react on click on prelisten button
		function makeClick(el, event) {
			$el = $(el);
			if ($el.hasClass(opts.activeClass)) {
				// if current button is playing
				// replace src if img
				if (opts.imgBtn) {
					$el.attr('src', opts.imgPlay);
				}
				stopPlaying();
			} else {
				// if current button is not playing
				// replace src if img
				if (opts.imgBtn) {
					$el.attr('src', opts.imgStop);
				}
				startPlaying(el);
			}
			event.preventDefault();
		}
		;

		function startPlaying(el) {
			$el = $(el);
			// if something is already playing, stop it
			if ($.fn.prelisten.playingEl != '') {
				stopPlaying();
			}
			$el.addClass(opts.activeClass);
			// ---------------------------------
			classes = $el.attr('class');
			params = classes.split(' ');
			tempId = jQuery.grep(params, function(n, i) {
				return (n.substr(0, 4) == 'oid_');
			})
			oid = tempId.join(", ").slice(4);
			cty = $.trim(classes.replace("playbtn", "").replace("playing", "")
					.replace("hovered", "").replace(tempId.join(", "), ""));
			// ---------------------------------

			inc_txt = '<'
					+ 'object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,79,0" id="streaming" width="0" height="0" /><'
					+ 'param name="movie" value="'
					+ opts.playerFile
					+ '" /><'
					+ 'param name=FlashVars value="id='
					+ oid
					+ '&type='
					+ cty
					+ '&divid='
					+ oid
					+ '&cid='
					+ opts.cid
					+ '" /><'
					+ 'param name="bgcolor" value="#000000" /><'
					+ 'param name="quality" value="best" /><param name="scale" value="exactfit" /><'
					+ 'param name="allowscriptaccess" value="samedomain" /><'
					+ 'embed type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" width="0" height="0" name="streaming" src="'
					+ opts.playerFile
					+ '" bgcolor="#000000" quality="best" scale="exactfit" swLiveConnect="true" allowScriptAccess="samedomain" FlashVars="id='
					+ oid + '&type=' + cty + '&divid=' + oid + '&cid='
					+ opts.cid + '"><' + '/embed><' + '/object>';
			$('#' + opts.playerId).append(inc_txt);

			$.fn.prelisten.playingEl = oid;
		}
		;

		function stopPlaying() {
			$.fn.prelisten.stop();
		}
		;

		// iterate over all given elements
		return this.each(function(idx) {
			$this = $(this);
			$(this).bind("mouseover", function() {
				makeHover(this);
			}).bind("mouseout", function() {
				makeHover(this);
			}).bind("click", function(e) {
				makeClick(this, e);
			});
		});

	};

	/*
	 * private function for debugging
	 */
	function debug($obj) {
		if (window.console && window.console.log)
			window.console.log('' + $obj);
	}
	;

	$.fn.prelisten.playingEl = '';
	jQuery.fn.prelisten.allEls = [];

	$.fn.prelisten.stop = function() {
		$stopEl = $.fn.prelisten.allEls.filter(".oid_"
				+ $.fn.prelisten.playingEl);
		classes = $stopEl.attr('class');
		$stopEl.removeClass($.fn.prelisten.defaults.activeClass);
		if ($.fn.prelisten.defaults.imgBtn) {
			$stopEl.attr('src', $.fn.prelisten.defaults.imgPlay);
		}
		$('#' + $.fn.prelisten.defaults.playerId).empty();
		$.fn.prelisten.playingEl = '';
	};

	// default values
	$.fn.prelisten.defaults = {
		cid : '',
		activeClass : 'playing',
		hoveredClass : 'hovered',
		imgClass : 'playbtn',
		playerId : 'streamplayer',
		imgBtn : true,
		imgPlay : '/me/pub/c/2100947/j/de/web/img/icon_prelisten_play.gif',
		imgStop : '/me/pub/c/2100947/j/de/web/img/icon_prelisten_stop.gif',
		imgPlayHover : '/me/pub/c/2100947/j/de/web/img/icon_prelisten_play_over.gif',
		imgStopHover : '/me/pub/c/2100947/j/de/web/img/icon_prelisten_stop_over.gif',
		playerFile : '/me/pub/c/2100947/j/de/web/img/streamplayer.swf'
	};

})(jQuery);

