jQuery.fn.fadeToggle = function(speed, easing, callback) {
   return this.animate({opacity: 'toggle'}, speed, easing, callback);
};
var categories = 'b';
var heavyImage = new Array();
$(document).ready(function (){
  $.ajax({
    url        : 'result.php?row=first',
    type       : 'POST',
    dataType   : 'json',
    data       : {cat : categories},
    success    : function (data){
      if(data.id) {
        $('.jsg_preview > img').fadeOut('slow',function (){
          $('.jsg_preview > img').attr('alt',data.id)
                                 .attr('src','images/gallery/'+ data.url)
                                 .attr('class','preview_photo')
                                 .fadeIn('slow');
        });
        $('.jsg_rate_count').text(data.rate_count + ' ratings');
        var f = -1 * (Math.floor(data.rate_total)-data.rate_total);
        var rate = (f>=0.5) ? Math.ceil(data.rate_total) : Math.floor(data.rate_total);
        for (var i=0; i<$('.tower').length; i++) {
          if (i<rate) {
            $('.tower:eq('+i+')').css('background-position','0 -20px').addClass('on');
          } else {
            $('.tower:eq('+i+')').css('background-position','0 0px').removeClass('on');
          }
        }
        for(var j=0; j<data.thumb.length; j++) {
          $('.thumb_img:eq('+j+') > img').attr('src','images/gallery/'+ data.thumb[j].thumb_url).attr('alt',data.thumb[j].id);
          if (j>2 ||j<data.thumb.length-3 ) {
            heavyImage[j] = new Image();
            heavyImage[j].src = "images/gallery/"+data.thumb[j].url;
          }
        }
      } else {
        $('.jsg_preview').html('<p>' + data.error + '</p>');
      }
    },
    beforeSend : function (){
      //$('.jsg_preview > img').attr('alt','').attr('src','images/preloader.gif');
    }
  });

  $('.tower').hover(function () {
    var oEl = $('.tower').index(this);
    for (var i=0; i<$('.tower').length;i++) {
      if (oEl >= i) {
        $('.tower:eq('+i+')').css('background-position','0 -40px');
      } else {
        break;
      }
    }
  }, function () {
    for (var i=0; i<$('.tower').length;i++) {
      if ($('.tower:eq('+i+')').is('.on')) {
        $('.tower:eq('+i+')').css('background-position','0 -20px');
      } else {
        $('.tower:eq('+i+')').css('background-position','');
      }
    }
  });
  $('.jsg_send2mail').click(function (){
    var top = $('.jsg_preview').position().top + $('.jsg_preview').height() + 20 - $('.jsg_sendform').height();
    $('.jsg_sendform').css('top',top);
    $('.jsg_sendform').fadeToggle();
  });
  $('.jsg_cancel').click(function (){
    $('.jsg_sendform').fadeOut();
  });
  $('.jsg_send').click(function (){
    var from = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test($('input[name=from]').val());
    var to   = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test($('input[name=to]').val());
    if(!from) {
      $('input[name=from]').css({
        'background' : 'url(images/email_error.png) no-repeat right #FFF'
      });
    } else {
      $('input[name=from]').css({
        'background' : '#FFF'
      });
    }
    if(!to) {
      $('input[name=to]').css({
        'background' : 'url(images/email_error.png) no-repeat right #FFF'
      });
    } else {
      $('input[name=to]').css({
        'background' : '#FFF'
      });
    }

    if (from && to) {
      $.ajax({
        url        : 'result.php?row=send',
        type       : 'POST',
        dataType   : 'json',
        data       : {from : $('input[name=from]').val(), to : $('input[name=to]').val(), message : $('textarea[name=message]').val()},
        success    : function (data){
          if(data.result) {
            $('.jsg_result').html('Photo sended');
            $('.jsg_sendform').fadeOut(1000);
          } else {
            $('.jsg_preview').html('<p>' + data.error + '</p>');
          }
        },
        beforeSend : function (){
          $('.jsg_result').html('<img alt="" src="images/ajax-loader.gif" />');
        }
      });
    } else {
      $('.jsg_result').html('enter valid e-mail address');
    }
  });

  $('.jsg_next').click(function (){
    $.ajax({
      url        : 'result.php?row=next',
      type       : 'POST',
      dataType   : 'json',
      data       : {id : $('.jsg_preview > img').attr('alt'), cat : categories},
      success    : function (data){
        if(data.id) {
          $('.jsg_preview > img').fadeOut('slow',function (){
            $('.jsg_preview > img').attr('alt',data.id)
                                   .attr('src','images/gallery/'+ data.url)
                                   .attr('class','preview_photo')
                                   .fadeIn('slow');
          });
          
          $('.jsg_rate_count').text(data.rate_count + ' ratings');
          var f = -1 * (Math.floor(data.rate_total)-data.rate_total);
          var rate = (f>=0.5) ? Math.ceil(data.rate_total) : Math.floor(data.rate_total);
          for (var i=0; i<$('.tower').length; i++) {
            if (i<rate) {
              $('.tower:eq('+i+')').css('background-position','0 -20px').addClass('on');
            } else {
              $('.tower:eq('+i+')').css('background-position','0 0px').removeClass('on');
            }
          }
          for(var j=2; j<data.thumb.length-3; j++) {
            //$('.thumb_img:eq('+j+') > img').attr('src','images/gallery/'+ data.thumb[j].thumb_url).attr('alt',data.thumb[j].id);
            heavyImage[j] = new Image();
            heavyImage[j].src = "images/gallery/"+data.thumb[j].url;
          }
            $(".thumb_img:first-child").animate({width:'hide'},1000,"linear",function (){
              $(".thumb_img:first-child").remove();
              $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[12].thumb_url+"' alt='"+data.thumb[12].id+"' /></div>");
            });
        } else {
          $('.jsg_preview').html('<p>' + data.error + '</p>');
        }
      },
      beforeSend : function (){
        //$('.jsg_preview').html('<img align="center" alt="" src="images/preloader.gif" />');
      }
    });
  });

  $('.jsg_prev').click(function (){
    $.ajax({
      url        : 'result.php?row=prev',
      type       : 'POST',
      dataType   : 'json',
      data       : {id : $('.jsg_preview > img').attr('alt'), cat : categories},
      success    : function (data){
        if(data.id) {
          $('.jsg_preview > img').fadeOut('slow',function (){
            $('.jsg_preview > img').attr('alt',data.id)
                                   .attr('src','images/gallery/'+ data.url)
                                   .attr('class','preview_photo')
                                   .fadeIn('slow');
          });
          
          $('.jsg_rate_count').text(data.rate_count + ' ratings');
          var f = -1 * (Math.floor(data.rate_total)-data.rate_total);
          var rate = (f>=0.5) ? Math.ceil(data.rate_total) : Math.floor(data.rate_total);
          for (var i=0; i<$('.tower').length; i++) {
            if (i<rate) {
              $('.tower:eq('+i+')').css('background-position','0 -20px').addClass('on');
            } else {
              $('.tower:eq('+i+')').css('background-position','0 0px').removeClass('on');
            }
          }
          for(var j=2; j<data.thumb.length-3; j++) {
            //$('.thumb_img:eq('+j+') > img').attr('src','images/gallery/'+ data.thumb[j].thumb_url).attr('alt',data.thumb[j].id);
            heavyImage[j] = new Image();
            heavyImage[j].src = "images/gallery/"+data.thumb[j].url;
          }
          $(".thumb_img:last-child").remove();
          $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[0].thumb_url+"' alt='"+data.thumb[0].id+"' /></div>");
          $(".thumb_img:first-child").show(1000);
        } else {
          $('.jsg_preview').html('<p>' + data.error + '</p>');
        }
      },
      beforeSend : function (){
        //$('.jsg_preview').html('<img align="center" alt="" src="images/preloader.gif" />');
      }
    });
  });

  $('.tower').click(function (){
    $.ajax({
      url        : 'result.php?row=set_rate',
      type       : 'POST',
      dataType   : 'json',
      data       : {id : $('.jsg_preview > img').attr('alt'), rate : $('.tower').index(this)+1},
      success    : function (data){
        if(data.rate_total) {
          $('.jsg_rate_count').text(data.rate_count + ' ratings');
          var f = -1 * (Math.floor(data.rate_total)-data.rate_total);
          var rate = (f>=0.5) ? Math.ceil(data.rate_total) : Math.floor(data.rate_total);
          for (var i=0; i<$('.tower').length; i++) {
            if (i<rate) {
              $('.tower:eq('+i+')').css('background-position','0 -20px').addClass('on');
            } else {
              $('.tower:eq('+i+')').css('background-position','0 0px').removeClass('on');
            }
          }
        } else if(data.status) {
          $('div.jsg_rate_mes').show().html('You have already voted...');
          setTimeout(function () {
            $('div.jsg_rate_mes').fadeOut(2000);
          }, 2000);
        } else {
          $('.jsg_preview').html('<p>' + data.error + '</p>');
        }
      },
      beforeSend : function (){
        
      }
    });
  });

 $('.thumb_img').live("click",function (){
   var thumb = this;
    $.ajax({
      url        : 'result.php?row=id',
      type       : 'POST',
      dataType   : 'json',
      data       : {id : $(this).find('img').attr('alt'), cat : categories},
      success    : function (data){
        if(data.id) {
          $('.jsg_preview > img').fadeOut('slow',function (){
            $('.jsg_preview > img').attr('alt',data.id)
                                   .attr('src','images/gallery/'+ data.url)
                                   .attr('class','preview_photo')
                                   .fadeIn('slow');
          });
          $('.jsg_rate_count').text(data.rate_count + ' ratings');
          var f = -1 * (Math.floor(data.rate_total)-data.rate_total);
          var rate = (f>=0.5) ? Math.ceil(data.rate_total) : Math.floor(data.rate_total);
          
          for (var i=0; i<$('.tower').length; i++) {
            if (i<rate) {
              $('.tower:eq('+i+')').css('background-position','0 -20px').addClass('on');
            } else {
              $('.tower:eq('+i+')').css('background-position','0 0px').removeClass('on');
            }
          }
          for(var j=2; j<data.thumb.length-3; j++) {
            //$('.thumb_img:eq('+j+') > img').attr('src','images/gallery/'+ data.thumb[j].thumb_url).attr('alt',data.thumb[j].id);
            heavyImage[j] = new Image();
            heavyImage[j].src = "images/gallery/"+data.thumb[j].url;
          }
          //console.log($('.thumb_img').index(thumb));
          if ($(".thumb_img").index(thumb) == 3){
            $(".thumb_img:last-child").remove();
            $(".thumb_img:last-child").remove();
            $(".thumb_img:last-child").remove();
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[0].thumb_url+"' alt='"+data.thumb[0].id+"' /></div>");
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[1].thumb_url+"' alt='"+data.thumb[1].id+"' /></div>");
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[2].thumb_url+"' alt='"+data.thumb[1].id+"' /></div>");
            $(".thumb_img:first-child").show(1000);
            $(".thumb_img:eq(1)").show(1000);
            $(".thumb_img:eq(2)").show(1000);
          } else if ($(".thumb_img").index(thumb) == 4) {
            $(".thumb_img:last-child").remove();
            $(".thumb_img:last-child").remove();
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[0].thumb_url+"' alt='"+data.thumb[0].id+"' /></div>");
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[1].thumb_url+"' alt='"+data.thumb[1].id+"' /></div>");
            $(".thumb_img:first-child").show(800);
            $(".thumb_img:eq(1)").show(800);
          } else if ($(".thumb_img").index(thumb) == 5) {
            $(".thumb_img:last-child").remove();
            $(".cont_thumb").prepend("<div style='display:none;' class='thumb_img' ><img src='images/gallery/"+data.thumb[0].thumb_url+"' alt='"+data.thumb[0].id+"' /></div>");
            $(".thumb_img:first-child").show(600);
          } else if ($(".thumb_img").index(thumb) == 7) {
            $(".thumb_img:first-child").animate({width:'hide'},600,"linear",function (){
              $(".thumb_img:first-child").remove();
              $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[12].thumb_url+"' alt='"+data.thumb[12].id+"' /></div>");
            });
          } else if ($(".thumb_img").index(thumb) == 8) {
            $(".thumb_img:first-child").animate({width:'hide'},600,"linear",function (){
              $(".thumb_img:eq(1)").animate({width:'hide'},600,"linear",function (){
                $(".thumb_img:first-child").remove();
                $(".thumb_img:first-child").remove();
                $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[11].thumb_url+"' alt='"+data.thumb[11].id+"' /></div>");
                $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[12].thumb_url+"' alt='"+data.thumb[12].id+"' /></div>");
              });
            });
          } else if ($(".thumb_img").index(thumb) == 9) {
            $(".thumb_img:first-child").animate({width:'hide'},600,"linear",function (){
              $(".thumb_img:eq(1)").animate({width:'hide'},600,"linear",function (){
                $(".thumb_img:eq(2)").animate({width:'hide'},600,"linear",function (){
                  $(".thumb_img:first-child").remove();
                  $(".thumb_img:first-child").remove();
                  $(".thumb_img:first-child").remove();
                  $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[10].thumb_url+"' alt='"+data.thumb[10].id+"' /></div>");
                  $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[11].thumb_url+"' alt='"+data.thumb[11].id+"' /></div>");
                  $(".cont_thumb").append("<div class='thumb_img' ><img src='images/gallery/"+data.thumb[12].thumb_url+"' alt='"+data.thumb[12].id+"' /></div>");
                });
              });
            });
          }

        } else {
          $('.jsg_preview').html('<p>' + data.error + '</p>');
        }
      },
      beforeSend : function (){
        //$('.jsg_preview').html('<img align="center" alt="" src="images/preloader.gif" />');
      }
    });
 });
});


