last_fm_flash = function(resource_id, title, element_id) {
  var flashvars = {
    lfmMode: 'playlist',
    FOD: true,
    resourceID: resource_id,
    resname: title,
    restype: 'label'
  };
  swfobject.embedSWF("http://cdn.last.fm/webclient/s12n/s/3/lfmPlayer.swf", element_id, "300", "221", "9.0.0", null, flashvars);  
};
open_player = function() {
  window.open('/player.html','player','width=400,height=320');
  return false;
};  
init_player_link = function() {
  player_link = $('player-link');
  if (player_link) {
    // Only pop is cookie not set
    if (!document.cookie.include('popped=player')) {
      open_player();
      document.cookie = 'popped=player; path=/';    
    }
    player_link.onclick = open_player;
  }
};
document.observe( 'dom:loaded', init_player_link );

var toggle_price = function(check_boxes, price_field){
	bool_total = false;
	check_boxes.each(function(b){ bool_total = bool_total || b.checked; });
	price_field.disabled = bool_total;
};

// Logo SWFU
var logo_swfu;
Event.observe(window, 'load', function() {
  form = $('logo-upload-form');
  if (form) {

    UploadHandler.uploadSuccess = function(fileObj, server_data) {
      json = server_data.evalJSON();
      UploadProgress.finish();
      
      $$('#logo-image').invoke('remove');
      $(this.customSettings.upload_form).up('div').insert({bottom: '<img id="logo-image" src="' + json.logo.public_filename + '" />'});
      // window.location = server_data.evalJSON().location;
    };
    
    UploadHandler.uploadComplete = function(fileObj) {
      /*  I want the next upload to continue automatically so I'll call startUpload here */
      if (this.getStats().files_queued > 0) {
        this.startUpload();
      } else {
        UploadProgress.finish();
      }
    };      
    
    logo_swfu = new SWFUpload({
      // Create the custom swfupload_photos_path in the routes.rb file
      // Session name must match with environment.rb 
      upload_url : form.action + '.js?_beef_core_session=' + $F('session-id'),
      flash_url : '/flash/swfupload.swf',
      file_post_name: 'logo[uploaded_data]', 

      file_types : "*.jpg;*.gif;*.png",
      file_types_description: "Web Image Files",
      file_size_limit : '3072',
      file_upload_limit : 1,
      file_queue_limit : 1,

      file_queue_error_handler : UploadHandler.fileQueueError,
      file_dialog_complete_handler : UploadHandler.fileDialogComplete,
      upload_progress_handler : UploadHandler.uploadProgress,
      upload_start_handler: UploadHandler.uploadStart,
      upload_error_handler : UploadHandler.uploadError,
      upload_success_handler : UploadHandler.uploadSuccess,
      upload_complete_handler : UploadHandler.uploadComplete,
      custom_settings : { 
        upload_form : 'logo-upload-form'
      },
      
      button_placeholder_id: 'flash-button',
      button_height: 19,
      button_width: 157,
      button_image_url: '/images/admin/file-uploads.png',
      button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES,
      button_disable : false,
      button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT,

      debug: false

    }); 
    if (logo_swfu) {
      // form.onsubmit = function(){return false;};
      form.select('p').invoke('hide');
    }
  }

});

var switch_news_tab = function(new_tab){
	if($(new_tab+'-news-tab').hasClassName('hidden')){
		$$('div.news-tab').each( function(el){ el.addClassName('hidden'); });
		$(new_tab+'-news-tab').removeClassName('hidden');
	}
};

document.observe( 'dom:loaded', function() {
	
	//flash message make byebye on clickyclick
	var flash = $('flash');
	if(flash) {
  	flash.observe('click', function(){ this.hide(); });	  
	}
	
  // Charecter counter
	$$('textarea').each(function(ta) {
	  if (ta.previous('.counter')) {
      ta.counter = ta.previous('.counter');
      ta.maximum = ta.previous('.maximum').innerHTML;
      ta.observe('keyup', function() {
        if (this.maximum) {
          max_value = parseInt(this.maximum);
          if (this.value.length >= max_value) {
            this.value = this.value.truncate(max_value, '');
          }
        }
        if (this.counter) {
          this.counter.update(this.value.length);
        }
      });	    
	  }
  });
 

  // Set search feilds default
  $$('input[value=Keywords]').each(function(q) {

    q.onfocus = function() {
      if (this.value == 'Keywords') {
        this.value = '';
      }
    };
    q.form.onsubmit = function() {
      if (q.value == 'Keywords') {
        q.value = '';
        return false;
      }
    };
    
  });
  
  if($('diary_free') && $('diary_members') && $('diary_price')){
  	toggle_price([$('diary_free'),$('diary_members')], $('diary_price'));
  }
  
    //adds the pseudo classes needed for ie6,7
  if(Prototype.Browser.IE) {
    $$("#navigation li").each(function(item) {
      item.onmouseover = function() {
        this.addClassName("sfhover");
      };
      item.onmouseout = function() {
        this.removeClassName("sfhover");
      };
    });
  }

  $$('#national-news-tab').invoke('hide');
  $$('#news-tab-switch li a').each(function(a, i) {
    if (i == 0) {
      a.toggleClassName('on');
    }
    a.onclick = function() {
      $$('#news-tab-switch li a').invoke('toggleClassName','on');
      $$('div.news-tab').invoke('toggle');
      return false;
    };



  });


  // Set search feilds default
  $$('input[value=Keywords]').each(function(q) {
    q.onfocus = function() {
      if (this.value == 'Keywords') {
        this.value = '';
      }
    };
  }); 

});
