//var SITE_URL = "http://www.pixelfind.net/demo/fmylife2/";
var SITE_URL = "http://thepriceless.net/";

//STORY PANEL
function showStoryPanel() {
  if ( $('#storyPannel').css('display') == 'none' ) {
    $('#storyPannel').show(0);
  } else {
    $('#storyPannel').hide(0);
  }
}

//VOTE COMMENTS
function voteComment(vote,userId,itemId) {
  var ajaxres = $.ajax( {
    type: "POST",
    url: "" + SITE_URL + "ajax_vote_comments.php",
    data: "vote=" + vote + "&ownerId=" + userId + "&itemId=" + itemId,
    async: false
    } ).responseText;
  
  if(typeof(ajaxres) != null) {
    if(vote == 'up') {
      var good = '<img src="images/up2_red.gif">';
      $('#comment_good_' + itemId + '').html(good);

      var bad = '<img src="images/down2.gif">';
      $('#comment_bad_' + itemId + '').html(bad);

      $('#up_' + itemId + '').html(ajaxres);
    } else if (vote == 'down') {
      var bad = '<img src="images/down2_red.gif">';
      $('#comment_bad_' + itemId + '').html(bad);

      var good = '<img src="images/up2.gif">';
      $('#comment_good_' + itemId + '').html(good);
      
      $('#down_' + itemId + '').html(ajaxres);
    }
  }  
}

//SHOW / HIDE COMMENT
function show_hide_comment(divId) {
  if ( $('#' + divId + '').css('display') == 'none' ) {
    $('#' + divId + '').show(0);
    $('#replacer_' + divId + '').hide(0);
    $('#show_' + divId + '').html('hide');
    $('#vote_main_' + divId + '').show(0);
    $('#vote_replacer_' + divId + '').hide(0);
  } else {
    $('#' + divId + '').hide(0);
    $('#replacer_' + divId + '').show(0);
    $('#show_' + divId + '').html('show');
    $('#vote_main_' + divId + '').hide(0);
    $('#vote_replacer_' + divId + '').show(0);
  }
}


//CHECK SUBMIT FIELDS
function checkallFields() {
  //alert($('#remLen').val());
  if( $('#image').val() != '') {
    if( $('#nickname').val() != '') {
      if( $('#remLen').val() < 1190 && $('#remLen').val() > 0) {
        $('#submitForm').submit();
        $('#submitPannel').html('<div style="position: absolute; left: 100px; top: -20px"><img src="images/ajaxloader4.gif"></div>');
      } else {
        alert('Please enter a short description from 10 to 1200 words');
      }
    } else {
      alert('Please enter your nickname.');      
    }
  } else {
    alert('Please select an image for your post.');    
  }
}

//SUBMIT PANEL
function displaySubmitSection() {
  if ( $('#submitPannel').css('display') == 'none' ) {
    $('#submitPannel').show(0);
  } else {
    $('#submitPannel').hide(0);
  }
}

//ADD TO FAVORITES + UNIQUE CHECK
function addtoVaforites(postId) {
  var ajaxres = $.ajax( { type: "GET", url: "ajax_add_to_favorites.php?id=" + postId, async: false } ).responseText;
  
  if(ajaxres != 'nay') {
    var msg = '<span><img onclick="javascript: removeFromVaforites(' + postId + ')" title="remove from favorites" style="cursor: pointer; cursor: hand"  src="images/heart_red.png" width="15" height="15"></span>';
    $('#place_' + postId).html(msg);
  } else {
    alert('Post already in favorites');
  }
}

//REMOVE FROM FAVORITES
function removeFromVaforites(postId) {
  var ajaxres = $.ajax( { type: "GET", url: "ajax_remove_from_favorites.php?id=" + postId, async: false } ).responseText;

  var msg = '<span><img onclick="javascript: addtoVaforites(' + postId + ')" title="add to favorites" style="cursor: pointer; cursor: hand"  src="images/heart_black.png" width="15" height="15"></span>';
  $('#place_' + postId).html(msg);
}

function universalDeleteConfirmation(id,msg,link) {
  var r = confirm(msg);

  if ( r == true ) {
    window.location = '' + SITE_URL + link + id + '';
  }
}

function vote(value, id, div) {  
  var msg = '';
  var ajaxres = $.ajax( { type: "GET", url: "" + SITE_URL + "ajax_vote.php?id=" + id + "&vote=" + value, async: false } ).responseText;
  
  if(typeof(ajaxres) != null) {
  	
  	if(div.indexOf("bad") != -1) {
  	  var msg = "What the F*uck (" + ajaxres + ")";
  	} else {
  	  var msg = "Fucking funny (" + ajaxres + ")";
  	}

    $("#" + div + "").html('oh yeah!!!');
    var t=setTimeout("changeLoader('" + msg + "','" + div + "')",1000);
  }
}

function addPost(ownerId) {    
  if(ownerId != "") {  
    if ( $('#newPostPlace').css('display') == 'none' ) {
      $('#newPostPlace').slideDown(500);
    } else {
      $('#newPostPlace').slideUp(500);
    }
  } else {
    if ( $('#newPostPlaceAnonymous').css('display') == 'none' ) {
      $('#newPostPlaceAnonymous').slideDown(500);
    } else {
      $('#newPostPlaceAnonymous').slideUp(500);
    }
  }
}

function reset() {
  $('#title').val('');  
  $('#content').val('');
  $('#category').val('');   
  
  $('#title1').val('');  
  $('#content1').val('');
  $('#category1').val('');  
  $('#nickname').val('') ; 
}

function clearResponse() {
  $("#msgDiv2").html('');
  $("#msgDiv1").html('');
}

function ajaxPlaceAdd(ownerId) { 
  
  if (ownerId != "") {
    var title = $('#title').val();  
    var content = $('#content').val();
    var category = $('#category').val(); 
    var clientIp = $('#clientIp').val();
    var msgWindow = "#msgDiv2";    
  } else {
    ownerId = $('#nickname').val() ;
    var title = $('#title1').val();  
    var content = $('#content1').val();
    var category = $('#category1').val(); 
    var clientIp = $('#clientIp1').val();
    var msgWindow = "#msgDiv1";
  }
    
    
  if (title.replace(/^\s+|\s+$/g, '') == '') {
    alert("Title can not be empty");
  } else if (content.replace(/^\s+|\s+$/g, '') == '') {
    alert("Content can not be empty");
  } else if (category == '-1') {
    alert("Please select a category");
  } else {
    var ajaxres = $.ajax( { 
    type: "POST", 
    url: "" + SITE_URL + "ajax_addPost.php", 
    data: "ownerId=" + ownerId + "&title=" + title + "&content=" + content + "&category=" + category + "&clientIp=" + clientIp,
    async: false    
    } ).responseText;
  
    if(typeof(ajaxres) != null) {  	
      $(msgWindow).html(ajaxres);    
    }
    
    reset();
    
    setTimeout("clearResponse()",2000);
  }
}

function approve_reject_extended(ownerId,postId,vote){
  var ajaxres = $.ajax( {
  type: "POST",
  url: "" + SITE_URL + "ajax_moderate.php",
  data: "ownerId=" + ownerId + "&postId=" + postId + "&vote=" + vote,
  async: false
  } ).responseText;
  
  if(typeof(ajaxres) != null) {
    $("#moderatePost").html(ajaxres);
  }
}

function approve_reject(ownerId,postId,vote) {

  if(ownerId == '') {
    ownerId = "anonymous";
  }

  var loader = '<h1>Sending your vote...</h1> <img src="images/ajaxloader4.gif">';  
  $("#moderatePost").html(loader);
  
  var t=setTimeout("approve_reject_extended('" + ownerId + "','" + postId + "','" + vote + "');",1000);
}

function changeLoader(message,div) {
  $("#" + div + "").html(message);
}

function reportAbbuse(postId) {
  $('#reportTag').html('abuse reported');
  var t=setTimeout("abuseChange( " + postId + ")",1000);
}

function abuseChange(postId) {
  var ajaxres = $.ajax( {
  type: "POST",
  url: "" + SITE_URL + "ajax_report_abbuse.php",
  data: "postId=" + postId,
  async: false
  } ).responseText;

  if(typeof(ajaxres) != null) {
    $("#moderatePost").html(ajaxres);
  }
}

function ajax_display_how_to() {
  if ($('#howitworks').css('display') == 'none' ) {
    $('#howitworks').slideDown(500);
  } else {
    $('#howitworks').slideUp(500);
  }
}

function find(key) {  
  var keyword = $('#search-text').val();
  window.location = '' + SITE_URL + 'search-' + keyword + '';
}




function ajax_display_submit() {
  if ($('#submit-new').css('display') == 'none' ) {
    $('#submit-new').slideDown(500);
  } else {
    $('#submit-new').slideUp(500);
  }
}




function rest(id){
				if(document.getElementById(id).style.display == 'block'){
					document.getElementById(id).style.display = 'none';
				}
		
				else{
					document.getElementById(id).style.display = 'block';
				}
			}

