/*!
 * 305designz.com
 * (c) 2009 305 & Conquistadore
 *
 * JavaScript Components // JQuery/Ajax
 */

var root = '/';

// Avoid interference when using $ by prototype and $ 
$(document).ready(function(){
  $('.portfolio a[title!=""]').lightBox();
  if ($.cookie('user[name]')) {
    $(".portfolio .img").filter(function(){
      $(this).hover(function(){
        split = this.id.match(/i_([0-9]+)/);
        id = split[1];
        $(".portfolio #o_"+id).css({display: 'inline'}).css({cursor: 'pointer'});
        $(".portfolio #i_"+id+" img.image").css({border: '1px solid #FFF'});
      },function(){
        split = this.id.match(/i_([0-9]+)/);
        id = split[1];
        $(".portfolio #o_"+id).css({display: 'none'});
        $(".portfolio #i_"+id+" img.image").css({border: '1px solid #777'});
      });
      $(this).click(function(){
        split = this.id.match(/i_([0-9]+)/);
        id = split[1];
        if (confirm("Are you sure you want to delete this item?")) {
          $.ajax({
            type: "GET",
            url: root+"index.php?ajax=deleteImg&id="+id,
            dataType: "xml",
            success: function(xml){
              error = $(xml).find('error').text();
              if (error == 'false') {
                $(".portfolio #i_"+id).fadeOut("slow");
              }
            }
          });
        }
      return false;
    });
    });
  }
  $('input[name=email]').change(function(){
    if (this.value != '' && !validateEmail(this.value)) alert('Please enter a valid E-Mail Address');
  });
  tooltip();
});

// Check login
function checkLogin () {
  user = $('form[name=login] input[name=username]').val();
  pass = $('form[name=login] input[name=password]').val();
  
  $.ajax({
    type: "GET",
    url: root+"index.php?ajax=checkLogin&user="+user+"&pass="+pass,
    async: false,
    dataType: "xml",
    success: function(xml){
      error = $(xml).find('error').text();
      if (error == 'false') {
        alert("User '"+user+"' successfully logged in.");
        window.location = '?view=home';
      }
      else alert("Error logging in.");
    }
  });
  return false;
}

// Check contact form
function checkContact () {
  $('form[name=contact]').filter(function(){
    val1 = $('input[name=name]').val();
    val2 = $('input[name=email]').val();
    val3 = $('textarea[name=message]').val();
    if (val1 != '' && val2 != '' && val3 != '')
      $(this).submit();
    else alert('Please enter all fields');
  });
  return false;
}

// Remove user cookie
function logout () {
  $.ajax({
    type: "GET",
    url: root+"index.php?ajax=logout",
    async: false,
    success: function(){
      alert("User successfully logged out.");
    }
  });
}

// Upload function to use more fields
var curCount = 0;
var max = 7;
function addFormField() {
    obj = $("#fields");
    curCount++;
    if (curCount < max)
      obj.append('<br /><span class="label">Title</span> <input type="text" name="title[]" style="margin-right: 20px;" /> <span class="label">File</span> <input type="file" name="file[]" />');
    else
      alert("Maximum reached.");
    return false;
}
function removeFormField(id) {
    $(id).remove();
}

// Set update status with loading icon
function submitUpload() {
  if ($('#fields input').val() == '') return false;
  else {
    $('div.loading').css({display: 'block'});
    return true;
  }
}

// Validates an email input
function validateEmail(value) {
  if (value.match(/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/)) return true;
}

// Tooltip
this.tooltip = function(){
  xOffset = 10;
  yOffset = 20;
  
  $("a.tooltip").hover(function(e){
    this.t = this.title;
    this.title = "";
    $("body").append("<p id='tooltip'>"+ this.t +"</p>");
    $("#tooltip")
      .css("top",(e.pageY - xOffset) + "px")
      .css("left",(e.pageX + yOffset) + "px")
      .fadeIn("fast");
  },
  function(){
    this.title = this.t;
    $("#tooltip").remove();
  });    
  $("a.tooltip").mousemove(function(e){
  $("#tooltip")
    .css("top",(e.pageY - xOffset) + "px")
    .css("left",(e.pageX + yOffset) + "px");
  });    
};

