$(document).ready( function() {

    var xOffset = -30;
    var yOffset = -300;

    var bg_array = ['assets/img/bg-first.jpg', 'assets/img/bg-second.jpg'];
    var index = 0;
    var timer = null;
    var background = $('#background');
  

    background.attr('src', bg_array[index]).fadeIn(1000);

    setTimer();


    $(function(){
        $.superbox();
    });

    $.superbox.settings = {
        boxId: "superbox", // Id attribute of the "superbox" element
        boxClasses: "", // Class of the "superbox" element
        overlayOpacity: .6, // Background opaqueness
        boxWidth: "600", // Default width of the box
        boxHeight: "400", // Default height of the box
        loadTxt: "Caricamento...", // Loading text
        closeTxt: "Chiudi", // "Close" button text
        prevTxt: "Successivo", // "Previous" button text
        nextTxt: "Precedente", // "Next" button text
        beforeShow: function(){

            $('iframe').contents().find('#addButton').bind('click', function(e){
                e.preventDefault();
                var formValues = $('iframe').contents().find('form').serialize();
                                
                $.ajax({
                    type : "POST",
                    url : $('iframe').contents().find('#addButton').attr("rel"),
                    data : formValues,
                    success: function(total){

                        $('.carrello-status span').html("Totale € " + total);

                        $('#superbox-overlay').fadeOut('fast');
                        $('#superbox-wrapper').fadeOut('fast', function(){
                            $('.carrello-status').fadeIn('fast');
                        });

                    },
                    error:function(err){
                        alert(err);
                    }
                });

            });
        }
    };

    function setTimer(){
        timer = setInterval(function(){
            setBackground(index + 1);
        }, 5000);
    }

    function setBackground(value){

        if (value > bg_array.length - 1){
            value = 0;
        }

        index = value;

        background.fadeTo(1000, 0, function(){
            background.attr('src', bg_array[index]).fadeTo(1000, 1);
        });
    }

    $('.minified-prev a').click(function(event){
        event.preventDefault();
        if (timer != null){
            clearTimeout(timer);
            setBackground(parseInt($(this).attr('id')));
            setTimer();
        }
    });

    $('#show-text a').click(function(){
        $('html, body').animate({
            scrollTop: $('#more-container').position().top
        },{
            duration: 800,
            easing: 'easeInOutExpo'
        });
    });

    $('#news-go-up').click(function(event){
        event.preventDefault();
        $('html, body').animate({
            scrollTop: 0
        },{
            duration: 800,
            easing: 'easeInOutExpo'
        });
    });

    $(document).scroll(function(){
        
        if ($(window).scrollTop() == 0){
            $('#news-go-up').fadeOut('slow');
        }else{
            if (!($('#news-go-up').is(':visible'))){
                $('#news-go-up').fadeIn('slow');
            }
        }

    });

    $('#dettaglio-nav a').click(function(event){
        event.preventDefault();

        var tab = $($(this).attr('href'));

        if (!(tab).is(':visible')){

            $('#dettaglio-nav a').removeClass('selected');
            $(this).addClass('selected');

            $('.tab:visible').slideUp({
                duration:'1000',
                easing:'easeInOutExpo',
                complete: function(){
                    tab.slideDown({
                        duration:'slow',
                        easing:'easeInOutExpo'
                    });
                }
            });
        }
    });

    $('#color-gallery a').click(function(event){
        event.preventDefault();
        $('#dettaglio-img img').hide();
        $($(this).attr('href')).fadeIn();
    });

    $('.news-button').click(function(e){
        e.preventDefault();
        var p = $(this).parent();
        $(this).fadeOut('slow');
        $('html, body').animate({
            scrollTop: p.position().top - 10
        },{
            duration: 800,
            easing: 'easeInOutExpo',
            complete: function(){
                p.parent().animate({
                    height: $(window).height()
                },{
                    duration: 800,
                    easing:'easeInOutExpo'
                });
                p.find('.news_intro').hide();
                p.find('.article').slideDown({
                    duration: 800,
                    easing:'easeInOutExpo',
                    complete: function(){
                        var close = p.find('.close-article');
                        close.show();
                        close.animate({
                            opacity:0.5
                        }, 800);
                    }
                });
            }
        });
    })

    $('.close-article').click(function(e){
        e.preventDefault();
        var close = $(this);
        close.fadeOut('slow', function(){
            close.hide();
        });
        
        var p = $(this).parent();
        p.find('.article').slideUp({
            duration: 800,
            easing:'easeInOutExpo',
            complete: function(){
                p.find('.news-button').fadeIn('slow');
                
            }
        });

        p.parent().animate({
            height: 300
        },{
            duration: 800,
            easing:'easeInOutExpo'
            
        });
        p.find('.news_intro').show();

       
    })

    $('.close-article').hover(function(){
        $(this).animate({
            opacity:1
        })
    }, function(){
        $(this).animate({
            opacity:0.5
        })
    })
    
/*$('.collezione-box img').css('opacity', 0.7);
    $('.collezione-box img').hover(function(){
        $(this).animate({
            opacity:1
        }, 500);
    }, function(){
        $(this).animate({
            opacity:0.7
        });
    })*/
  $('.collezione-box img').hover(function(e){
    
    $("body").append("<div class='popup'><img src='"+ $(this).attr('rel') +"' alt='prev' /></div>");
    $(".popup")
    .css("top",(e.pageY + yOffset) + "px")
    .css("left",(e.pageX + xOffset) + "px")
    .fadeIn("slow");
  },
  function(){
      $(".popup").remove();
  });

  $(".collezione-box img").mousemove(function(e){
      $(".popup")
      .css("top",(e.pageY + yOffset) + "px")
      .css("left",(e.pageX - xOffset) + "px");
  });

  $('#aggiorna_carrello').click(function(event){
    $('#form_chart').submit();
  });

  $('#submit_registrazione').click(function(event){

      $('#form_registrazione').submit();

  });

   $('#submit_pagamento').click(function(event){
      $('#form_pagamento').submit();
  });

  $('#reset_registrazione').click(function(event){
    $('#form_registrazione').reset();
  });

  $('#submit_login').click(function(event){
    
      $('#form_login').submit();
  });
  
  $('#submit_invio').click(function(event){    


      var formValues = $('#invio_email').serialize();
      
      $.ajax({
          type : "POST",
          url : $('#invio_email').attr("action"),
          data : formValues,
          success: function(msg){

              $('#result_invio').html(msg);

          }
      });
    
  });

  $('#submit_mygat').click(function(event){
      var formValues = $('#invio_mygat').serialize();

      $.ajax({
          type : "POST",
          url : $('#invio_mygat').attr("action"),
          data : formValues,
          success: function(msg){

              $('#result_invio').html(msg);

          }
      });

  });


  $("a[rel^='prettyPhoto']").prettyPhoto({
		animationSpeed: 'fast',
		padding: 40,
		opacity: 0.45,
		showTitle: true,
		allowresize: true,
		counter_separator_label: '/',
		theme: 'light_rounded',
		callback: function(){
		}
	});

  $("input").placeholder();

});


;(function($) {
/**
 * Clear input and textarea placeholder values on focus in and replaces them on focus out if no edit was made
 *
 * @example jQuery("input").placeholder();
 *
 * @lastmod 2011-01-12 23.40
 *
 * @name placeholder
 * @type jQuery
 * @param Object	settings (not used yet)
 *
 * @return jQuery
 * @author Dharma Ferrari (http://www.dharmaferrari.com)
 */
$.fn.placeholder = function(settings)
{
	settings = $.extend({}, $.fn.placeholder.defaults, settings);

	return this.each(function() {
  	var $this = $(this);
    $this.addClass(settings.cssClass);

    $this.data('old', $this.val());

    // On focus in
    $this.focusin(function() {
      if($this.data('old') == $this.val()) {
        $this.val('');
        $this.removeClass(settings.cssClass);
      }
    });

    // On focus out
    $this.focusout(function() {
      if('' == $this.val()) {
        $this.val($this.data('old'));
        $this.addClass(settings.cssClass);
      }
    });

  });

}

$.fn.placeholder.defaults = {
	cssClass : 'placeholder_in'
};

})(jQuery);
