$(document).ready(function(){ // This is inital values 
$("#query").addClass("clsNonWorking");
$("#query").val($("#default").html())

$("#query").focus(function(){
  if($("#query").val() == $("#default").html())
  $(this).val("");
  $("#query").removeClass("clsNonWorking");
});

$("#query").blur(function(){
  if($("#query").val() == "")
  {
    $("#query").addClass("clsNonWorking");
    $("#query").val($("#default").html())
  }
      

});
// This is added inplace of create popup page
//---------------------------------
$("#CancelIdea").click(function(){
  $.unblockUI();
});
$("#CancelSubscribe").click(function(){
  $.unblockUI();
});
$("#SaveIdea").click(function(){
  $.ajax({
    type:'POST',
    cache:false,
    data: "title=" + $("#title1").val() + "&txtUsername=" + $("#txtUsername").val() + "&Description="     +
    $("#Description").val() + "&txtEmail=" + $("#txtEmail").val() + "&txtWebsite=" + $("#txtWebsite").val()  +
    "&submit=submit&blogId=" + $("#blogId").val() + "&chkProduct=" + $("#chkProduct").val() + "&chkComment=" + $("#chkComment").val() ,
    url: basePath + 'feedback.createpop.php',
    success: function(response){
      //alert(response);
      $.unblockUI();
      showRecords('new','newIdea',0);
      Sexy.info(response);
      /*$.blockUI({ message: "<p style='color:black;'>" + response + "</p>", css:{backgroundColor:'#B1EBA9'} , overlayCSS:  { backgroundColor: '#000000', opacity: 0.6}}); 
      setTimeout( function (){$.unblockUI({
        onUnblock:function (){showRecords('new','newIdea',0); }
      })}, 6000);
      */
    }
  });
  
});
//---------------------------------

$("#query").keyup(function(){
  //if($(this).val().length >= 2)
  {
    //$(".submit").hide();
    $('#indicator').show();
    $('#views').hide();

    
    var search = $("#query").val() == $("#default").html()? "" : $("#query").val();
    if(search != $("#default").html())
    {
      $.ajax({
      type:'POST',
      url: basePath + 'feedback.records.php',
      data: "suggest=" + search + "&type=search&blogId=" + blogId ,
      success: function(response){
        if(response == "")
        {
            $(".back").show();
            $(".clsVotes > h3").hide();
            $(".clsOr").hide();
        }
        else
        {
            $(".back").show();
            $(".clsVotes > h3").show();
            $(".clsOr").show();
        }
        $("#suggestions").html(response);
        $('#indicator').hide();
        //$(".submit").show();
      }
      });
    }
  }
});

$("#create").click(function(){
  
    /*
    $.ajax({
    type:'GET',
    cache: false, 
    url: basePath + 'feedback.createpop.php?blogId='+ blogId +'&title=' + ($("#default").html() != $("#query").val()? $("#query").val() : ""),
    success: function(response){
      $.blockUI({message : response, css:{ top: '50px', width:'400px', border:'0px;',cursor:'default'},fadeIn:  200});
      //Following code is added as per customer's wish
      
    }
    
  });
    */
    $("#title1").val($("#query").val() == $("#default").html()? "" : $("#query").val());
    $("#Description").val("");
    $("#txtEmail").val("");
    $("#txtWebsite").val("");
    $("#txtUsername").val("");
    $.blockUI({message : $(".popup"), css:{ top: '50px', width:'400px', border:'0px;',cursor:'default'},fadeIn:  200});
});
$("#subscribe_pop").click(function(){
  
   
    $("#title1").val($("#query").val() == $("#default").html()? "" : $("#query").val());
    $("#Description").val("");
    $("#txtEmail").val("");
    $("#txtWebsite").val("");
    $("#txtUsername").val("");
    $.blockUI({message : $(".popup1"), css:{ top: '175px', width:'400px', border:'0px;',cursor:'default'},fadeIn:  200});
});
$("#subscribe_pop1").click(function(){
  
   
    $("#title1").val($("#query").val() == $("#default").html()? "" : $("#query").val());
    $("#Description").val("");
    $("#txtEmail").val("");
    $("#txtWebsite").val("");
    $("#txtUsername").val("");
    $.blockUI({message : $(".popup2"), css:{ top: '175px', width:'400px', border:'0px;',cursor:'default'},fadeIn:  200});
});
//Initialize tabs
    $("#topIdea" ).parent().addClass("current");
    $("#topIdea > small").css("display","inline");

    // Subscription animation
    $(".clsGetUpdate").click(function(){
      $(this).addClass("clsSelect");
      $(".boxSubscribe").css('height','160px');
      $(".clsUpdateList").animate({top:'0'},1000,function(){
        $("#txtSubscribe").focus();
      });
    });
    $("#cmdSubscriptionCancel").click(function() {
      $(".clsGetUpdate").removeClass("clsSelect");
      $(".clsUpdateList").animate({top:'-160'},1000,function(){
        $(".boxSubscribe").css('height','0px');
      });
    });
    
//showRecords('top','topIdea');
$(".ajax-fc-container").captcha(); /* in this line note that ajax-fc-container is a class and we reference it with .(dot), if you want to change class to id, don't forget to replace the dot with # sign and also to describe it in css file. */
}); // End Of OnLoad Function 
var previousState = 'topIdea' ;  
function showRecords(str,id,page)
{
  try{
  var search = $("#query").val() == $("#default").html()? "" : $("#query").val();
  $('#views').show();

  $(".back").hide();
  $(".clsVotes > h3").hide();
  $(".clsOr").hide();

  removeCurrentTagPosition();  
  
   $("#" + id + " > small").css("display","inline");
   $("#" + id ).parent().addClass("current");
   if(id != previousState)
   $("#" + previousState + " > small").css("display","none");
   previousState = id ;
  $.ajax({
    type:'POST',
    url: basePath + 'feedback.records.php',
    data: "suggest=" + search + "&type="+str + "&blogId=" + blogId +"&page=" + page,
    success: function(response){
      $("#suggestions").html(response);
    }
   });
  }catch(ab)
  {
    //alert('error occured' + ab.description);
  }
  return false;
}

function removeCurrentTagPosition()
{

  $("#topIdea").parent().removeClass("current");
  $("#hotIdea").parent().removeClass("current");
  $("#newIdea").parent().removeClass("current");
  $("#acceptedIdea").parent().removeClass("current");
  $("#completedIdea").parent().removeClass("current");
  
}

function validate_feedbakComment()
{
  if($("#txtUsername").val() == "")
  {
     Sexy.error("Please enter username ", {onComplete: function(returnval){
       $("#txtUsername").focus();
       return false;
     } });  
     return false;
  }
  if($("#txtEmail").val() == "")
  {
     Sexy.error("Please enter your email address ", {onComplete: function(returnval){
       $("#txtEmail").focus();
       return false;
     } });  
     return false;
  }
  if(document.getElementsByName("captcha")[0] == undefined)
  {
     Sexy.error("You have not dragged desired image on that box .", { onComplete: function(returnval){
       
     } });
     return false;      
  }  
  
  return true ;
}

function subscribe()
{
  if($("#txtSubscribeEmail").val() == "")
  {
    Sexy.error("Please enter email address");
    return false;
  }
  if($("#txtSubscribeName").val() == "")
  {
    Sexy.error("Please enter your name");
    return false;
  }
  if(!isValidEmail($("#txtSubscribeEmail").val()))
  {
    Sexy.error("Enter Valid email address");
    return false;
  }
  $(".clsGetUpdate").removeClass("clsSelect");
  $.post(basePath + "subscribe.php",{'txtSubscribeName': $("#txtSubscribeName").val() , 'txtSubscribeEmail': $("#txtSubscribeEmail").val() , "txtblogid":  $("#txtblogid").val()},function(data){
    Sexy.info(data);
    $(".clsUpdateList").animate({top:'-160px'},1000,function(){
      $(".boxSubscribe").css('height','0px');
    });
   
  });
  
  return false;
}

function isValidEmail(str) {
      var reg1str = "(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)";
      var reg2str = "^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\
\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$";
      var reg1 = new RegExp(reg1str);
      var reg2 = new RegExp(reg2str);
      if (!reg1.test(str) && reg2.test(str)) return true;
      return false;
}

