/**
 * Redirect user to URL immediately.
 * 
 * @param URL
 */
function redirect(URL) {
  var submitForm = document.createElement("FORM");
  document.body.appendChild(submitForm);
  submitForm.method = "GET";
  submitForm.action = URL;
  submitForm.submit();
  return false;
}

/**
 * Show dialog with claimed reward informations and image 
 * 
 * @param dialogId
 * @param rewardId
 */
function ajax_show_claimed_reward(dialogId, rewardId)
{
  $("#" + dialogId).dialog("option", "buttons", [
    {
      text: "Print",
      click: function() { $(this).jqprint(); }
    },
    {
      text: "Close",
      click: function() { $("#" + dialogId).dialog("close"); }
    }
  ]);
  
  
  $.ajax({
    type: "POST",
    url: base_url + '/home/ajax_show_claimed_reward/id/' + rewardId,
    beforeSend: function()
    {
      $("#" + dialogId).dialog('open');
      $("#" + dialogId).html('Loading...');
    },
    success: function(data)
    {
      $("#" + dialogId).html(data);
      $("#claimed-print").html(data);
    }
  });
}

function ajaxForm(formId, updateId, url, fSuccess)
{
  $.ajax({
    type: "POST",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
    },
    success: function(r)
    {
      if(fSuccess == '')
      {
        eval(fSuccess + '("' + r + '")');
      }
      else
      {
        $("#" + updateId).html(r);
      }
    }
  });
}

function ajaxGetRightStats()
{
  $.ajax({
    type: "POST",
    url: '/home/ajax_get_right_stats',
    success: function(r)
    {
      $(".stats-box").html(r);
    }
  });
}








function ajaxRegister(formId, updateId, url)
{
  $.ajax({
    type: "POST",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
      $("#register-box").html('Loading...');
    },
    success: function(r)
    {
      if(r == 'success')
      {
        $("#register-box").html('<div class="notice">You have successfully registered.<br/>Confirmation email has been sent to your email.<br/>Please click on confirmation link.</div>');
      }
      else
      {
        $("#" + updateId).html(r);
      }
    }
  });
}


function ajaxSignupYourBusiness(updateId, url, formId, logged)
{
  if(formId == "")
    formId = "tmp_form"

  $.ajax({
    type: "POST",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
      $("#" + updateId).html('Loading...');
    },
    success: function(r)
    {
      if(r == 'success')
      {
        if(logged == true)
        {
          $("#dialog-signup-your-business").html('<div class="notice">You have successfully applied to become a riinu partner. You will be notified, once you have been approved.</div>');
          $("#dialog-signup-your-business").bind( "dialogclose", function(event, ui) {
            top.location.href='/';
          });
        }
        else
        {
          $("#" + updateId).dialog('close');
          $("#signup-your-business-message").remove();
          $(".box1-background-register").html('<div class="notice">You have successfully registered.<br/>Confirmation email has been sent to your email.<br/>Please click on confirmation link.</div>');
        }
      }
      else
      {
        $("#" + updateId).html(r);
      }
    }
  });
}

function ajaxLogin(formId, updateId, url, redirectUrl)
{
  $.ajax({
    type: "POST",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
    },
    success: function(r)
    {
      if(r == 'success')
      {
        parent.location.href = redirectUrl;
      }
      else
      {
        $("#" + updateId).html(r);
      }
    }
  });
}

function createModalDialog(id, title, width, height)
{
  $("#" + id).remove();
  $("body").prepend('<div id="' + id + '" title="' + title + '"></div>');
  $("#dialog:ui-dialog").dialog("destroy");
  $("#" + id).dialog({
    width: width,
    height: height,
    autoOpen: false,
    resizable: false,
    modal: true
  });
}

function ajaxCreateCustomGroup(updateId, url, formId)
{
  if(formId == "") {
    formId = "tmp_form"
  }

  $.ajax({
    type: "GET",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
      $("#" + updateId).html('Loading...');
    },
    success: function(r)
    {
      if(r == 'success')
      {
        $("#" + updateId).dialog('close');
      }
      else
      {
        $("#" + updateId).html(r);
      }
    }
  });
}

function ajaxCreateGroup(formId, updateId, url, url2)
{
  $.ajax({
    type: "POST",
    url: url,
    data: jQuery("#" + formId).serialize(),
    beforeSend: function()
    {
    },
    success: function(r)
    {
      if(r == 'success')
      {
        $.ajax({
          url: url2,
          success: function(r)
          {
            $("#sf_activity_group_id").replaceWith(r);
          }
        });
        $("#" + updateId).dialog('close');
      }
      else
      {
        $("#" + formId).replaceWith(r);
      }
    }
  });
}

function show_block_ui()
{
	jQuery.blockUI({ message: "<span style='font-size: 16px; font-weight: bold; color: #FFF;'>Please be patient, this may take a moment.</span><br />", css: {
	    border: 'none',
	    left: (jQuery(window).width() - 240) /2 + 'px',
	    width: '240px',
	    padding: '20px 15px',
	    backgroundColor: '#000',
	    '-webkit-border-radius': '10px',
	    '-moz-border-radius': '10px',
	    opacity: .7,
	    color: '#fff'
	  } });
}

function hide_block_ui()
{
	setTimeout(jQuery.unblockUI, 100);

}


$.fn.serializeJSON = function()
{
  var output = {};
  var a = this.serializeArray();
  $.each(a, function() {
    output[this.name] = this.value;
  });
  return output;
};

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function jsddm_open()
{jsddm_canceltimer();
   jsddm_close();
   ddmenuitem = $(this).find('ul').css('display', 'block');}

function jsddm_close()
{  if(ddmenuitem) ddmenuitem.css('display', 'none');}

function jsddm_timer()
{  closetimer = window.setTimeout(jsddm_close, timeout);}

function jsddm_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

$(document).ready(function()
{  $('#mgTopMenu > li').bind('mouseover', jsddm_open)
   $('#mgTopMenu > li').bind('mouseout',  jsddm_timer)
 
   $(".trmain:odd").css("background-color", "#EEFFEE");
});
 
 
$(document).ready(function(){
  jQuery('#sf_guard_user_profile_message').keypress(function(event) {
      if (event.keyCode == 13) {
          event.preventDefault();
      }
  });
  
  jQuery("#tbaction a").click(function() {
    jQuery("#tbaction").hide();
  });
  
  
  createModalDialog("show-claimed-reward", "Riiward", 450, 250);
});





document.onclick = jsddm_close;

