/// BASE

		var _content_height = 0;

		var _interval_checkContentHeight;

		function debug(str) {
			if (typeof(console) == 'object') {
				console.log(str);
			}	
		}

		function isset(variable) {
			return (variable == undefined);	
		}	

		function notify(title, body) { 
			parent.notify(title,body);
		}

		function base_init() {
			initIframe();
			minifyButtonActions();
			initActions();
			$("input:text").addClass('text');
			$("input:submit").addClass('submit'); 
			$(".stripeMe").stripeMe();
			$('input[@type=radio].star').rating();
			$('div.tagCloud').tagCloud();
			$('a.button.action.playNow').activateButtons_playNow();
			$('a.button.action.addToPlaylist').activateButtons_addToPlaylist();
			$('a.button.action.deleteFromPlaylist').activateButtons_deleteFromPlaylist();
			$('input.buttonLink').buttonLink();
		}

		function minifyButtonActions() {
			$("a.button.action.mini").each(function(){
				var $this = $(this);
				if (!$this.attr('title')) {
					$this.attr('title',$this.text());
				};
				$this.html('&nbsp;');
			})	
		};

		function initActions() {
			// sendMessageActions
			$('.action.sendMessage').click(function() {
				var _data = $(this).metadata();
				window.location = '/message/compose?to='+_data.sendMessage_to.val();
				return false;
			})
			//
			$('.action.deleteMessage').click(function() {
				var _data = $(this).metadata();
				window.location = '/message/'+_data.deleteMessage_id.val()+'/delete';
				return false;
			});
		}

		function initIframe() {
      // trace(typeof(parent.content_height));
			if (typeof(parent.content_height) == 'function') {
				_interval_checkContentHeight = setInterval("checkContentHeight()", 1000);
			} else {
        reframeContent();
        return false;
			};
			//activateSubMenu();
		}

		function reframeContent() {
			var _location = '/#'+document.location.pathname+document.location.search;
      window.location = _location;
		}

		function checkContentHeight() {
      //var _detected_content_height = $(document).height();
      var _detected_content_height = $("#body-cont").height();
      // debug("H="+_detected_content_height);
      if (_detected_content_height != _content_height) {
        debug('Detected content height change, '+_detected_content_height);
				// Notify parent
				_content_height = _detected_content_height;
				parent.content_height(_detected_content_height+40);
			}
		}

		function activateSubMenu() {
			var _subMenu = $('#main_menu ul li.active a').attr('rel');
			debug('Submenu: .'+_subMenu);
			$("."+_subMenu).css({display:'block'});	
		}

// PLUGINS

$.fn.stripeMe = function() {
	return this.each(function() {
		var _data = $(this).metadata();
		if (typeof(_data.stripeMe) == 'undefined') {
			_selector = 'tr';	
		} else {
			_selector = _data.stripeMe;
		}
		$(this).find(_selector+':even').each(function() {
			$(this).addClass('alt');	
		})
	})
}

$.fn.buttonLink = function() {
	return this.each(function() {
		var $this = $(this);
		var _data = $this.metadata();
		var _href = _data.href;
		$this.click(function() {
			window.location = _href;
			return false;	
		});
	})	
}

// MESSAGE

	function messageCompose_addRecipient(id,name) {
		var _recipient = $('#messageComposeTo').before("<div class='recipient' id='recipient_"+id+"'>"+name+"<input type='hidden' name='to[]' value='"+id+"'><img src='/asset/icons/sweetie/12-em-cross.png' onclick='messageComposeTo_removeRecipient($(\"#recipient_"+id+"\"))'></div>");
		messageComposeTo_fixHeight();
	}

	function messageComposeTo_itemSelect(li) {
		if (li.extra != "") {      
			messageCompose_addRecipient(li.extra, $(li).text());
			$('#messageComposeTo').val('');
			$('#messageComposeTo').focus();
		}
	}

	function messageComposeTo_fixHeight() {
		$('#recipientList_dummy').height($('#recipientList').height());	
	}

	function messageComposeTo_removeRecipient(recipient) {
		recipient.remove();
		messageComposeTo_fixHeight();
	}

	function autoComplete (field, select, property, forcematch) {
    var found = false;
    for (var i = 0; i < select.options.length; i++) {
    if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) {
        found=true; break;
      }
    }
    if (found) { select.selectedIndex = i; }
    else { select.selectedIndex = -1; }
    if (field.createTextRange) {
      if (forcematch && !found) {
        field.value=field.value.substring(0,field.value.length-1); 
        return;
			}
      var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
      if (cursorKeys.indexOf(event.keyCode+";") == -1) {
        var r1 = field.createTextRange();
        var oldValue = r1.text;
        var newValue = found ? select.options[i][property] : oldValue;
        if (newValue != field.value) {
          field.value = newValue;
          var rNew = field.createTextRange();
          rNew.moveStart('character', oldValue.length) ;
          rNew.select();
				}
			}
		}
	}

	function messageCompose_init() {  
		$("#recipientList").click(function() {
      $("#messageComposeTo").focus();
		});
    
		$("#messageComposeTo").autocomplete("/lookup/ac/friend", {
      onItemSelect: messageComposeTo_itemSelect,
      delay:200,
      matchSubset:0,
      matchContains:1,
      mustMatch:1
    });
		
		$("#messageComposeTo").keydown(function(kc) {
			if (kc.keyCode == 8 || kc.keyCode == 40) {
				// Pressed Delete
				messageComposeTo_removeRecipient($("#recipientList .recipient:last"));
			}
		});		
		setTimeout("$('#messageComposeTo').focus()",900);
	}

// PLAYER CONTROL
    
	$.fn.activateButtons_playNow = function() {
		return this.each(function() {
			$(this).click(function() {
				var $this = $(this);
				//var _data = $this.metadata();
        var _comedy = $this.attr('rel').substr(6);
				var _data = _metadata[_comedy];
				var _id 		=  _data.comedyid;
				var _title 		=  _data.title;
				var _url_audio 	=  _data.url_audio;
				var _url_web 	=  _data.url_web;
				player_playnow(_id,_title,_url_audio,_url_web);
        //player_playnow( _id, _title);
				return false;
			});
		});
	}
  
	$.fn.activateButtons_addToPlaylist = function () {
		return this.each(function() {
			$(this).click(function() {
				var $this 		= $(this);
				var _data 		= $this.metadata();
				var _id 		=  _data.comedyid;
				var _title 		=  _data.title;
				var _href 		= $this.attr('href');
				$.get(_href);
				notify("Added to playlist", _title);
				return false;	
			});
		});
	}

	$.fn.activateButtons_deleteFromPlaylist = function () {
		return this.each(function() {
			$(this).click(function() {
				var $this		= $(this);
				var _data		= $this.metadata();
				var _id			= _data.comedyid;
				var _title		= _data.title;
				var _remove		= _data.remove;
				var _href		= $this.attr('href');
				$.get(_href);
				if (!(_remove === undefined)) {
					$(_remove).remove();	
				}
				notify("Removed from playlist",_title);
				return false;
			});
		});	
	}


  function player_playnow(id,title,url_audio,url_web) {
		parent.control_player_playnow(id,title,url_audio,url_web);
	}

  // function player_playnow(id, title) {
    // control_player_playnow_playlist(id, title);
  // }

	function player_play() {
		parent.control_player_play();
	}

	function player_next() {
		parent.control_player_next();	
	}

	function player_prev() {
		parent.control_player_prev();	
	}

	function player_pause() {
		parent.control_player_pause();	
	}

	function player_volume(amount) {
		parent.control_player_volume(amount);	
	}

	function player_mute() {
		player_volume(0);	
	}

  function trace(str) {
    if (typeof(console) == 'object') {
      console.log(str);
    }	
  }
