// JavaScript Document

function checkAGB() {
  if(document.getElementById('fotodownloadAGB').checked == true) {
    return true;
  }
  else {
    alert('Bitte lesen und akzeptieren Sie die allgemeinen Gesch&auml;ftsbedingungen');
    return false;
  }
}

$(document).ready(function(){

  //Colorbox deaktivieren entsprechend Checkbox
  $('#toggle_colorbox').click(function() {
    $(".photoImg").toggleClass("cboxElement");
  });

  
  //Eintraege pro Seite verarbeiten
  $('.iPPselect').change(function() {
    $.cookie("fotoserviceItemsPerPage", $(this).val());
    $('#itemsPerPageForm').submit();
  });
  
  //Galerienpro Seite verarbeiten
  $('.giPPselect').change(function() {
    $.cookie("fotoserviceGalleriesPerPage", $(this).val());
    $('#galleriesPerPageForm').submit();
  });
  
    $("a[title=Alle]").click(function(event){
        $("input[name=kategorie]").attr("checked", true);
    });
    
    $("a[title=Keine]").click(function(event){
        $("input[name=kategorie]").attr("checked", false);
    });
    

    var count = 0;
    var y = 0;
    var v = 0;
    $(".photoImg").colorbox({
        transition: "elastic",
        
        speed: 300,
        
        initialWidth: function() {
        return $(this).children().first().children().first().width() * 2.5+1;
        },
        
        initialHeight: function(){
      var uuid = $(this).children().first().children().first().data('uuid');
      var iHeight = $(this).children().first().children().first().height() * 2.5+1;
      if (uuid) {
        iHeight = iHeight + 20;
      }
      return iHeight;
        },
        
        scalePhotos: true,
        
        title: function() {
      return "<div class='cbDesc1'>"+$(this).children().first().children().first().data('title')+"</div>";
        },
        //title: function() {
        //return $(this).children().first().children().first().data('desc');
        //return "<div class='cbDesc1'>"+$(this).next("div.photoDesc1").html()+"</div><div class='cbDesc2'>"+$(this).next().next("div.photoDesc2").html()+"</div>";
        //},
        
        
        //colorbox is sometimes (if the image doesn't load fast enough), unable to get its width
        //this code prevents colorbox from setting a width of 0
        innerWidth: function() {
        var b = $(this).children().first().children().first().width() * 2.5+1;
                if($.browser.msie){ b = b+10; }
        if(b > 10){ return b; }else{ return 250; }
        }, 
        
        innerHeight: function() { //analog to innerWidth
    var uuid = $(this).children().first().children().first().data('uuid');
    var c = $(this).children().first().children().first().height() * 2.5+1;
    if (uuid) {
      c = c + 20;
    }
        if($.browser.msie){ c = c+10; }
        if(c > 10){ return c; }else{ return 250; }
        },
        
        close: "", //hide close-button

        html: function() { //load + link content
    var uuid = $(this).children().first().children().first().data('uuid');
    var bar = $('<div>').append( $('#'+uuid+" > div.mgnlBar").first().clone() ).html();
    return bar + "<div id ='cbLayer' ><a href='"+$(this).children().first().children().first().data('link')+"'><img src='"+$(this).children().first().children().first().data('src')+"' title='"+$(this).children().first().children().first().data('title')+"' /></a></div>";
 
        },
        
        opacity: 0,

        top: function() { //determine the position...centering it to the small img
        var a = $(this).offset();
        var midSmall = a.top + $(this).height() / 2 + 2;
        var topLarge = midSmall - $(this).children().first().children().first().height() * 2.5 / 2 - 75 / 2;
        return topLarge;
        },
        
        left: function() { //analog to top
        var a = $(this).offset();
        var centerSmall = a.left + $(this).width() / 2 + 2;
        var leftLarge = centerSmall - $(this).children().first().children().first().width() * 2.5 / 2 - 50 / 2;
        return leftLarge;

        },
        
        fixed:  true //true = css-positioning: absolute; false = css-positioning: fixed
    
    }).hover(function(){
        z = $(this);
        count++;
        setTimeout(function(){
                if(count != v){ //prevent colorbox from loading the image multiple times
                z.click(); //load the image where the mousepointer is AFTER the 400ms-timout
                v = count;

                }
                
        }, 400);

    }, function() {
        /* $("#colorbox .mgnlDraggable").draggable({ revert: true, helper: "clone" }); */
    });
    /* 
    $("#cboxWrapper").hover(function(){
        
        }, function(){

        x = count;
        setTimeout(function(){
            if(count == x){ //close the colorbox after 400 ms, if the mouse points nowhere
            $.colorbox.close(); 
            }
        
        }, 400);
    
    });
    */

      
     //close the colorbox if no colorbox-mouseout-event was captured and the mouse is not over the colorbox 
     $(document).mousemove(function(e){
     var l = $("#cboxWrapper").offset().left;
     var t = $("#cboxWrapper").offset().top;
     var r = l + $("#cboxWrapper").width();
     var b = t + $("#cboxWrapper").height();
     
     var m = e.pageX;
     var n = e.pageY;

     if(l <= m && m <= r && t <= n && n <= b){
     }else{
          $.colorbox.close();
     }
     });
  
 
    $('body.fotoservice div.folderImg').each(function(){
    $(this).data('animated', 0);
    });

    $('body.fotoservice div.folderImg').hover(function(){


        if($.browser.msie){ //No Animation

              $(this).children().first().children('img.img_1').each(function(){
                  $(this).css('left', '36px');
                  $(this).css('top', '30px');
             });
              $(this).children().first().children('img.img_3').each(function(){
                  $(this).css('left', '-30px');
                  $(this).css('top', '-258px');

              });

              /*
              $(this).children().first().children('img.img_1').each(function(){
                  var g = $(this).offset();
                  var la = g.left+24;
                  var ta = g.top+24;
                  $(this).animate({
                  left: la,
                  top: ta,
                  }, 400, function(){
                  });
              });
              */
              
        }else if($(this).data('animated') ==  0){

              $(this).data('animated', 1);
              $(this).children().first().children('img.img_1').each(function(){
                  $(this).animate({
                  'left': '30px',
                  'top': '-50px'
                  }, 400, function(){
                  }).rotate({
                  'duration': 400,
                  'angle': 0,
                  'animateTo': 30
                  });
              });
                

              $(this).children().first().children('img.img_2').each(function(){
                  $(this).animate({
                  'left': '0px',
                  'top': '-76px'
                  }, 400, function(){
                  });
              });
                
              $(this).children().first().children('img.img_3').each(function(){

                  $(this).animate({
                  'left': '-60px',
                  'top': '-248px'
                  }, 400, function(){
                  }).rotate({
                  'duration': 400,
                  'angle': 0,
                  'animateTo': -45
                  });
              });
               
        }

    }, function(){

        if($.browser.msie){ //No Animation
        
              $(this).children().first().children('img.img_1').each(function(){
                  $(this).css('left', '0px');
                  $(this).css('top', '0px');
              });
              $(this).children().first().children('img.img_3').each(function(){
                  $(this).css('left', '0px');
                  $(this).css('top', '-228px');
              });
        
        }else if($(this).data('animated') == 1){
              $(this).data('animated', 2);
              $(this).children().first().children('img.img_1').each(function(){
                  $(this).animate({
                  'left': '0px',
                  'top': '0px'
                  }, 400, function(){
                      $(this).parent().parent().data('animated', 0);
                  }).rotate({
                  'duration': 400,
                  'angle': 30,
                  'animateTo': 0
                  });
              });
              $(this).children().first().children('img.img_2').each(function(){
                  $(this).animate({
                  'left': '0px',
                  'top': '-116px'
                  }, 400, function(){
                  });
              });
              
              $(this).children().first().children('img.img_3').each(function(){
                  $(this).animate({
                  'left': '0px',
                  'top': '-228px'
                  }, 400, function(){
                  }).rotate({
                  'duration': 400,
                  'angle': -45,
                  'animateTo': 0
                  });
              });
               
        }
    
    });
    
    
     
});


