$(document).ready(function() {
        // $('.stick-newsletter').html($('.subscribe_dialog').html());
        // $('.stick-newsletter .submit').click(function(e) {
        //                                        e.preventDefault();
        //                                        $.post($(this).parents('form').attr('action'), { email: $('.stick-newsletter #id_email').val() },
        //                                          function(r) {
        //                                            alert(r);
        //                                            if (r.slice(0,2) == 'Ok') {
        //                                              $('.stick-newsletter #id_email').val('');
        //                                            }
        //                                          });
        //                                      });


        $.easing.def = "easeOutElastic";
        $('.subscribe').click(function(e) { e.preventDefault(); $('.subscribe_dialog').dialog({draggable:false, resizable:false, width: 500,
                            open: function(e, ui) {
                            $('.subscribe_dialog').parent().first().animate({top: '100px'}, {duration: 1000});
                            $('.subscribe_dialog input').focus();
                        }
                    });
            });
        $('.subscribe_dialog .dismiss').click(function(e) { e.preventDefault(); $('.subscribe_dialog').dialog('destroy');});
        $('.subscribe_dialog .submit').click(function(e) { e.preventDefault(); $.post($(this).parents('form').attr('action'),
                                                                               {email: $('#id_email').val()},
                                                                                      function(r) {
                                                                                          alert(r);
                                                                                          if (r.slice(0,2) == 'Ok') {
                                                                                              $('#id_email').val('');
                                                                                              $('.subscribe_dialog').dialog('destroy');
                                                                                          }
                                                                                      });
            });

        $('#changelanguage').click(function(e) { e.preventDefault(); $('#changelanguage_form').get(0).submit(); });
        $('#copyright').tooltip({ effect: 'fade', predelay: 100, position: "top left", offset: [ 0, 200]}).mouseover(function() { $('#copyright').oneTime('15s', function() { $('.tooltip').hide(); })});
        $('#subscribe').tooltip({ effect: 'fade', predelay: 100, position: "top left", offset: [ 0, 200]}).mouseover(function() { $('#subscribe').oneTime('15s', function() { $('.tooltip').hide(); })});
        //      $('img.jewel').attr({title: 'Tutti i modelli, i testi e le immagini in questo sito sono protetti', href: "http://www.siae.it/Faq_siae.asp"}).tooltip({delay: 0, position: "top center", offset: [30, 0]}).mouseover(function() { $('#copyright').oneTime('5s', function() { $('.tooltip').hide(); })});;

        var current_bg = 0;
        var bg_number = 4;
        var bg_duration = '10s';
        var bg_transition = 3000;
        /*
        $('#content').everyTime(bg_duration, 'changeBG', function() {
                next_bg = (current_bg + 1)  % bg_number;
                $('#home-' + (current_bg + 1)).fadeOut(bg_transition, function() {
                        $('#home-'+ (next_bg +1)).fadeIn(bg_transition);
                        current_bg = next_bg;
                    });
                    });*/

                $('#collection_link').mouseover(prevent(function() { $('#ring').hide(); $('#collection_menu').fadeIn() }));

        $('#menu').mouseout(function(e) {
                //                  // console.log('mouse out');
                    var x = $('#menu').offset().left;
                    var y = $('#menu').offset().top;
                    if (e.pageX < x || e.pageX > x + $('#menu').outerWidth() || e.pageY < y || e.pageY > $('#menu').outerHeight() + y) {
                        $('#collection_menu').fadeOut();
                    };
                     });
        $('.scroll-pane').jScrollPane();
    });


function magnify(div, imgurl) {
    $('#magnifier').remove();
    $(div).unbind('mousemove');
    $(div).append($('<div id="magnifier"></div>'));
    $('#magnifier').css({top: 0, left: 0, background: "url('" + imgurl + "') no-repeat scroll 0 0"});
    $(div).mousemove(follow($(div), $('#magnifier'), imgurl));
}

$('.gallery_cont').live('click', function(e) { $('#magnifier').toggle() });

function follow(div, mag, imgurl) {
    return function(e) {
        var pos = $(div).offset();
        var w = $(div).width();
        var h = $(div).height();
        var relPosX = e.pageX - pos.left;
        var relPosY = e.pageY - pos.top;
        var y = relPosY ;
        var x = relPosX ;
        var magX = parseInt(relPosX / w * 100) ;
        var magY = parseInt(relPosY / h * 100) ;
        //      console.log('x: ' + x + ' y: ' + y + ' magX: ' + magX + ' magY: '+ magY);
        if ( relPosX  > -50 && relPosX  < w + 50 && relPosY >-50 && relPosY <  50 + h) {
        mag.css({top: y - (mag.height() / 2),
                 left: x - (mag.width() / 2),
                 background: "url('" + imgurl + "') no-repeat scroll " + magX + "% " + magY + "%"
                   });
        }
    }
}

function prevent(fn) {
    return function(e) {
        e.preventDefault();
        fn($(this));
    }};

function stopMoving() {
    $('#coverflow').clearQueue();
    $('#coverflow').stop();
}

var cfMovementSpeed = 20000;
var cfMovementEasing = 'linear';

function setMoveToLeft() {
    stopMoving();
    $('#coverflow').animate({left: (parseInt($('#coverflow').css('left')) - 5000) + 'px'}, {easing: cfMovementEasing, duration: cfMovementSpeed});
}

function setMoveToRight() {
    stopMoving();
    $('#coverflow').animate({left: (parseInt($('#coverflow').css('left')) + 5000) + 'px' }, {easing: cfMovementEasing, duration: cfMovementSpeed});
}

function autoscroll2() {
    if (current_dialog && current_dialog.dialog('isOpen')) { return };
    autoscroll({pageX: event.clientX, pageY: event.clientY});
}

function autoscroll(e) {
    //    console.log('autoscroll');
    if (current_dialog && current_dialog.dialog('isOpen')) { return };
    if (!$('a', '.jqDock').length) { return };
    pos = { left: $('#coverflow').offset().left - parseInt($('#coverflow').css('left')),
            right: $('body').width() * 0.94  };
    ypos = { top: $('#coverflow').offset().top };
    ypos.bottom = ypos.top + $('a', '.jqDock').height();
    cond = e.pageY > ypos.top && e.pageY < ypos.bottom;
    docx = e.pageX;
    posdebug= 'client x: '+ e.pageX + ' client Y: ' + e.pageY + ' x:' + pos.left + ' y:' + ypos.top +' bottom:' + ypos.bottom;
    //console.log(posdebug);
    if (!cond) {
        //      console.log('stop 2');
        stopMoving(); return true;
    };
      if(e.pageX < pos.left) {
          //      console.log('left');
          setMoveToRight();
      } else {
          if(e.pageX > pos.right) {
              //              console.log('right');
              setMoveToLeft();
          } else {
              //              console.log('stop ' + posdebug);
              stopMoving();
          }
      }
      return true;
}

var dock;
var dockme;
var dockx;

function findSelected() {
    var max = -1; var maxEl = null; var maxI = -1;
    $('.jqDock img').each(function(i, el) {
            var currentHeight = $(el).height();
            if (currentHeight > max) {
                maxEl = $(el);
                max = currentHeight;
                maxI = i;
            }
        });
    return maxI;
}

function calculateStep(sel) {
    var ret = 0;
    $('.jqDock a').each(function(i, el) {
            if (i < sel) {
                ret += $(el).width();
            }
            if (i == sel) {
                ret += $(el).width() / 2;
            }

        });
    return ret;
}

  var msie = false;
  jQuery.each(jQuery.browser, function(i, val) {
  if (i=='msie') {
    msie = true;
  }});


function afterDock(Dock, ME, id) {
    var def = 0;
    if ($('.jqDock a').length < 15) {
    def = 2;
    } else {
        def = 10;
    }
    var selected = parseInt($.cookie('jewelSelected') || def);
    var delta = calculateStep(selected) / 2;
    //    $('.jqDock').animate({opacity: 0.3}).({left: -delta, opacity: 1}, {easing: cfMovementEasing, duration: cfMovementSpeed / 10, complete: function() {
    if (!msie) {
        $('.jqDock').css({left: -delta, opacity: 1});
    }
                dock = Dock;
                dockme = ME;
                Dock.Opts.duration = 1; //cfMovementSpeed / 10;
                Dock.OnDock = true;
                Dock.Current = selected;
                var x = 200 + selected * 35;
                ME.X = x;
                ME.deltaXY(id);
                ME.setSizes(id);
                ME.factorSizes(id);
                ME.doLabel(id, 'change');
                Dock.Timestamp = 0;
                Dock.Expanded=true;
                ME.X = 0;
                ME.Y = 0;
                Dock.Opts.duration = 1000;

    $('.jqDock').css({left: -delta - selected * 20 });
                //          }});
};

function zoom(event, delta) {
    var gallery = $(this);
    var h = currentElement.height();
    //    console.log('current ' + currentWidth);
    var nh = h * (1 + delta / 100);
    if(nh < 200) {
        nh = 200;
    };
    if (nh > 1500) {
        nh = 1500;
    }
    var nw = currentElement.width() * (nh / h);
    currentElement.attr({width: nw, height: nh}).css({width: nw + 'px', height: nh + 'px'});
}

function setInizialZoom(el) {
    var prop = 500 / el.height();
    //    console.log('prop: ' + prop + ' h: ' + el.height());
    nh = el.height() * prop;
    nw = el.width() * prop;
    el.show();
    el.animate({width: nw + 'px', height: nh + 'px'}, {duration: 1000, complete: function() { $(this).attr({width: nw, height: nh})}});
    //window.setTimeout(function () {
    //  el.attr({width: nw, height: nh}).css({width: nw + 'px', height: nh + 'px'});
      //        }, 1000);
}

function checkAutoOpen(slug) {
    if (jQuery.url.attr('anchor') == slug) {
        return true;
    }
    return false;
}


// change the name of the shown jewel
  function updateName() {
    if ($('#collection_menu').css('display') == 'none') {
        var name = $('.jqDockLabel').text();
        if($('#ring').text() != name ) {
          $('#ring').hide().text(name).show();
        }
    }
  }


