function scwmp(){
	scastmpWindow = window.open("php/players.php?do=mp","mp","width=360,height=80");
}
function scrp(){
	scastrpWindow = window.open("php/players.php?do=rp","rp","width=420,height=160");
}
function scqt(){
	scastqtWindow = window.open("php/players.php?do=qt","qt","width=330,height=50");
}

var count = 0;
var files = 'php/';
var lastTime = 0;

function prepare(response) {
  var d = new Date();
  count++;
  d.setTime(response.time*1000);
  var mytime = d.getHours()+':'+d.getMinutes()+':'+d.getSeconds();
  var string = '<div class="shoutbox-list" id="list-'+count+'">'
	  + '<span class="shoutbox-list-time">'+mytime+'</span>'
	  + '<span class="shoutbox-list-nick">'+response.nickname+':</span>'
	  + '<span class="shoutbox-list-message">'+response.message+'</span>'
	  +'</div>';
  
  return string;
}

function success(response, status)  { 
  if(status == 'success') {
	lastTime = response.time;
	$('#daddy-shoutbox-response').html('<img src="'+files+'images/accept.png" />');
	$('#daddy-shoutbox-list').append(prepare(response));
	$('input[@name=message]').attr('value', '').focus();
	$('#list-'+count).fadeIn('slow');
	timeoutID = setTimeout(refresh, 3000);
  }
}

function validate(formData, jqForm, options) {
  for (var i=0; i < formData.length; i++) { 
	  if (!formData[i].value) {
		  alert('Please fill in all the fields'); 
		  $('input[@name='+formData[i].name+']').css('background', 'red');
		  return false; 
	  } 
  } 
  $('#daddy-shoutbox-response').html('<img src="'+files+'images/loader.gif" />');
}

function refresh() {
  $.getJSON(files+"daddy-shoutbox.php?action=view&time="+lastTime, function(json) {
	if(json.length) {
	  for(i=0; i < json.length; i++) {
		$('#daddy-shoutbox-list').append(prepare(json[i]));
		$('#list-' + count).fadeIn('slow');
	  }
	  var j = i-1;
	  lastTime = json[j].time;
	}
	//alert(lastTime);
  });
  timeoutID = setTimeout(refresh, 1000);
}

function loadData() {
	$('#lastten').load('php/lastten.php');
	$('#nowplaying').load('php/nowplaying.php');
	$('#streaminfo').load('php/streaminfo.php');
	$('#streamstatus').load('php/streamstatus.php');
};

$(document).ready(function() {

$(".moduletable_shoutbox").hide();

	var options = { 
	  dataType:       'json',
	  beforeSubmit:   validate,
	  success:        success
	}; 
	$('#daddy-shoutbox-form').ajaxForm(options);
	timeoutID = setTimeout(refresh, 1000);
	loadData();
	setInterval( loadData, 1000 );

$(".chatbutton").click(function () {
	$(".moduletable_shoutbox").slideToggle("slow");
});

	//When you click on a link with class of poplight and the href starts with a # 
$('a.poplight[href^=#]').click(function() {
    var popID = $(this).attr('rel'); //Get Popup Name
    var popURL = $(this).attr('href'); //Get Popup href to define size

    //Pull Query & Variables from href URL
    var query= popURL.split('?');
    var dim= query[1].split('&');
    var popWidth = dim[0].split('=')[1]; //Gets the first query string value

    //Fade in the Popup and add close button
    $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="images/stories/x.png" class="btn_close" title="Close Window" alt="Close" /></a>');

    //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
    var popMargTop = ($('#' + popID).height() + 80) / 2;
    var popMargLeft = ($('#' + popID).width() + 80) / 2;

    //Apply Margin to Popup
    $('#' + popID).css({
        'margin-top' : -popMargTop,
        'margin-left' : -popMargLeft
    });

    //Fade in Background
    $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
    $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 

    return false;
});

//Close Popups and Fade Layer
$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
    $('#fade , .popup_block').fadeOut(); //fade them both out
    $('#fade').remove();
    return false;
});

});
