var Messenger = {
  hide_error: null,
  hide_notice: null,
  error: function(message) {
    $('#flasherror').html(message);
    $('#flasherror').fadeIn(300);
    setTimeout(function(){
      $('#flasherror').fadeOut(300);
    }, 5000);
  },

  notice: function(message) {
    $('#flashnotice').html(message);
    $('#flashnotice').fadeIn(300);
    setTimeout(function(){
      $('#flashnotice').fadeOut(300);
    }, 5000);
  }
};
$(function(){
  $("body").append('<div id="flasherror"></div><div id="flashnotice"></div>');
});


var Utils = {
  dialog: function(title, text, options){
    var dialog = $("_dialog");
    if(dialog.length == 0){
      var dialog_window = document.createElement("div");
      dialog_window.id = "_dialog";
      $(document.body).append(dialog_window);
      dialog = $(dialog_window);
    }
    $(dialog_window).empty();
    var bd = document.createElement("div");
    $(bd).append(text);
    $(dialog_window).append(bd);
    options.title = title;
    options.bgiframe = true;
    if(!options.modal) options.modal = true;
    dialog.dialog(options);
  }
}

$(function(){
  var send_to = null;

  var switch_to = function(i){
    $("#chat_title li").removeClass("act");
    $("#cu_" + i).addClass("act");
    $("#chat_body .chat_body").hide();
    $("#cb_" + i).show();
    send_to = i;
  };

  $("#chat_title li").live("click", function(e){
    var i = this.id.split('_')[1];
    switch_to(i);
    return false;
  });


  var update_chat = function(data, switch_to_last){
    if(data && data['title']){
      for(i in data['title']){
        if($("#cu_" + i).length) continue;
        $("#chat_title").append(data['title'][i]);
      }
      for(i in data['body']){
        if(!$("#cb_" + i).length){
          $("#chat_body").append('<div style="display:none" class="chat_body" id="cb_' + i + '"></div>');
        }
        $("#cb_" + i).append(data['body'][i]);
      }
      if(switch_to_last) switch_to(i);
      $("#chat_body").scrollTop(99999);
    }
  };

  $("#chat_send").click(function(e){
    $("#chat").css("top", 100 + $(window).scrollTop());
    $("#chat").show();
    $.post(this.href, {}, function(data){
      update_chat(data, true);
    }, 'json');
    return false;
  });

  $("#chat_send_input").click(function(e){
    $.post(this.href, {'id': send_to, 'message': $("#chat_input").val()}, function(data){
      $("#chat_input").val("");
      update_chat(data, true);
    }, 'json');
    return false;
  });

  $("#chat_close").click(function(e){
    $("#chat").hide("slow");
    return false;
  });

  var fetch = function(){
    $.get($("#chat").attr("rel") + "?r=" + Math.random(), {}, function(data){
      if(data && data['title']){
        $("#chat").css("top", 100 + $(window).scrollTop());
        $("#chat").show();
      }
      update_chat(data, true);
    }, 'json');
    window.setTimeout(fetch, 30000);
  }
  if($("#chat").length){
    fetch();
  }
});

function selectBabeVoting(radioObj)
{ 
    var cnt = -1;
    for (var i=radioObj.length-1; i > -1; i--) {
        if (radioObj[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1)
	{
	alert('Please Register or Login to VOTE !.');		
	document.frmVoting.submit();
	}
	else alert('Please select a Babe for Voting.');
	
}

function selectBillVoting(radioObj)
{ 	
	 var cnt = -1;
    for (var i=radioObj.length-1; i > -1; i--) {
        if (radioObj[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1)
	{
		alert('Please Register or Login to VOTE !.');	
		document.frmBillVoting.submit();
	}
	else alert('Please select a Billionaire for Voting.');
	
}

function selectRegBabeVoting(radioObj,isregtered)
{ 
    var cnt = -1;
    for (var i=radioObj.length-1; i > -1; i--) {
        if (radioObj[i].checked) {cnt = i; i = -1;}
    }
	if(cnt==-1){
		alert('Please select a Babe for Voting');	
	}
	if (cnt > -1)
	{
		alert('Your VOTE has been submitted.\nThanks for participating in this month\'s voting.');
		document.getElementById('babevoting').disabled=true;
	}
	
}

function selectRegBillVoting(radioObj, isRegistered)
{
	var cnt = -1;
    for (var i=radioObj.length-1; i > -1; i--) {
        if (radioObj[i].checked) {cnt = i; i = -1;}
    }
    if(cnt==-1){
		alert('Please select a Billionaire for Voting');	
	}
	if (cnt > -1)
	{
		alert('Your VOTE has been submitted.\nThanks for participating in this month\'s voting.');
		document.getElementById('billvoting').disabled=true;
	//	alert('Please Register to Vote.');	
	//  document.frmBillVoting.submit();
	}
	
}



