(function($) {

	$.fn.asmSelect = function(customOptions) {

		var options = {

			listType: 'ol',						// Ordered list 'ol', or unordered list 'ul'
			sortable: false, 					// Should the list be sortable?
			highlight: false,					// Use the highlight feature? 
			animate: false,						// Animate the the adding/removing of items in the list?
			addItemTarget: 'bottom',				// Where to place new selected items in list: top or bottom
			hideWhenAdded: false,					// Hide the option when added to the list? works only in FF
			debugMode: false,					// Debug mode keeps original select visible 

			removeLabel: 'remove',					// Text used in the "remove" link
			highlightAddedLabel: 'Added: ',				// Text that precedes highlight of added item
			highlightRemovedLabel: 'Removed: ',			// Text that precedes highlight of removed item

			containerClass: 'asmContainer',				// Class for container that wraps this widget
			selectClass: 'asmSelect',				// Class for the newly created <select>
			optionDisabledClass: 'asmOptionDisabled',		// Class for items that are already selected / disabled
			listClass: 'asmList',					// Class for the list ($ol)
			listSortableClass: 'asmListSortable',			// Another class given to the list when it is sortable
			listItemClass: 'asmListItem',				// Class for the <li> list items
			listItemLabelClass: 'asmListItemLabel',			// Class for the label text that appears in list items
			removeClass: 'asmListItemRemove',			// Class given to the "remove" link
			highlightClass: 'asmHighlight'				// Class given to the highlight <span>

			};

		$.extend(options, customOptions); 

		return this.each(function(index) {

			var $original = $(this); 				// the original select multiple
			var $container; 					// a container that is wrapped around our widget
			var $select; 						// the new select we have created
			var $ol; 						// the list that we are manipulating
			var buildingSelect = false; 				// is the new select being constructed right now?
			var ieClick = false;					// in IE, has a click event occurred? ignore if not
			var ignoreOriginalChangeEvent = false;			// originalChangeEvent bypassed when this is true

			function init() {

            	$original.change(originalChangeEvent);
                //if($.cookie("mod_radio")!='')
                    //setStation();

			}
            
            function setStation() {

                buildingSelect = true; 

                $original.children("option").each(function(n) {

                    var t = $(this);
                    var url;
                    var oEmbed; 

                    if(t.val()==$.cookie("mod_radio")) {
                        if(t.val()!=""){
                            t.attr('selected','selected');                           
                            buildSelect();
                        }
                        $.cookie("mod_radio", url, { expires: 30 });
                                            
                    }
                });

                buildingSelect = false; 
            }
            
			function selectClickEvent() {
				ieClick = true; 
			}

			function originalChangeEvent(e) {

				if(ignoreOriginalChangeEvent) {
					ignoreOriginalChangeEvent = false; 
					return; 
				}

				buildSelect();
			}

			function buildSelect() {

				buildingSelect = true; 

				$original.children("option").each(function(n) {

					var t = $(this);
                    var url;
                    var oEmbed; 

					if(t.is(":selected")) {
                                            
						url = t.val();
                        if(url!=""){
                            radio = t.text();                           
                            $("#online").html(play(url));                            
                        }else{
                            radio = 0;
                            $("#online").html("");
                        }
                        
                        $.cookie("mod_radio", url, { expires: 30 });
                        					
					}
				});

				buildingSelect = false; 
			}
            function play(s){
               var html  = '';
               
                 html +='<object id="WMPlay"';
                 if(-1 != navigator.userAgent.indexOf("MSIE")) html +=' classid="clsid:6BF52A52-394A-11d3-B153-00C04F79FAA6"';
                 else html +=' type="application/x-ms-wmp"';

                 html +=' width="240" height="50">';
                 html +='<param name="autoStart" value="true" />';
                 html +='<param name="URL" value="'+s+'" />';
                               
                 html +='<param name="uiMode" value="mini" />';
                 html +='<param name="volume" value="100" />';
                 html +='<param name="display" value="false" />';
                 html +='<param name="showDisplay" value="false" />';

                 html +='<param name="AutoSize" value="0" />';
                 html +='<param name="AutoRewind" value="true" />';

                 html +='<param name="ShowStatusBar" value="true"/>';
                 html +='<param name="CurrentPosition" value="false" />';
                 html +='<param name="ShowControls" value="true" />';
                 html +='<param name="enablecontextmenu" value="false" />';

                 html +='<embed type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" src="'+s+'" align="top" width="240" height="50" autostart="1" autosize="0" showcontrols="1" showdisplay="0" EnableContextMenu="0" volume="100" ShowStatusBar="1" showaudiocontrols="1" showpositioncontrols="0" autorewind="1"></embed>';
                 html +='</object>';

               return html;
            }

			init();
		});
	};

})(jQuery);
