/* -- FOXCRIME INIZIALIZE JAVASCRIPTS & EFFECTS -- */

/* -- CROPPING IMAGES --*/

$(window).load(function() {
	
	$(function() {
		
		$("img.gallery620x400").scaleImage({
			parent: '.gallery620x400Cont',
			center: false,
			scale: "fill"
		});
				
		$("img.thumb270x200").scaleImage({
			parent: '.thumb270x200Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb270x150").scaleImage({
			parent: '.thumb270x150Cont a',
			center: false,
			scale: "fill"
		});
				
		$("img.thumb200x140").scaleImage({
			parent: '.thumb200x140Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb200x110").scaleImage({
			parent: '.thumb200x110Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb133x73").scaleImage({
			parent: '.foto a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb130x80").scaleImage({
			parent: '.thumb130x80Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb180x80").scaleImage({
			parent: '.thumb180x80Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb60x60").scaleImage({
			parent: '.thumb60x60Cont a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumb70x50").scaleImage({
			parent: '.thumb70x50Cont a',
			center: false,
			scale: "fill"
		});

		$("img.thumb").scaleImage({
			parent: '.slides-nav a',
			center: false,
			scale: "fill"
		});
		
		$("img.fotoEvidenza").scaleImage({
			parent: '.foto a',
			center: false,
			scale: "fill"
		});
		
		$("img.thumbRing").scaleImage({
			parent: '.ringThumb a',
			center: false,
			scale: "fill"
		});
		
    });

	
});

$(document).ready(function(){

  
  var tdUp = function(){
    $('#tendina').slideUp('slow',function(){
      $('#tendina').html('');
      $(".btn-slide").removeClass("selected");
    });

  };
  
	
	/* -- Tendina --*/
			
	$(".btn-slide").click(function(){

    if($(this).hasClass('selected')){

        tdUp();
  
    } else {

      $("#tendina").slideDown("slow",function(){
        if($('#tendina').html().replace(/^\s+|\s+$/g,"") == ''){
           $.ajax({
            url: "/tendina",
            context: document.body,
            success: function(data){
              $(data).appendTo($('#tendina')).fadeIn('slow', function(){
                $('.btn-close').click(function(){
                  tdUp();
                }); 
                initCufon();
                Cufon.now();
              });
            }
          });
        }

      });
      $(this).addClass("selected");

    }
    return false;
	});
	
	/* -- Tooltip sulle thumb del visore --*/
	
	$(".slide-thumb-0").mouseover(function(){$(".slide-info-0").removeClass("displayNone");return false;});
	$(".slide-thumb-1").mouseover(function(){$(".slide-info-1").removeClass("displayNone");return false;});
	$(".slide-thumb-2").mouseover(function(){$(".slide-info-2").removeClass("displayNone");return false;});
	$(".slide-thumb-3").mouseover(function(){$(".slide-info-3").removeClass("displayNone");return false;});
	$(".slide-thumb-4").mouseover(function(){$(".slide-info-4").removeClass("displayNone");return false;});
	
	$(".slide-thumb-0").mouseout(function(){$(".slide-info-0").addClass("displayNone");return false;});
	$(".slide-thumb-1").mouseout(function(){$(".slide-info-1").addClass("displayNone");return false;});
	$(".slide-thumb-2").mouseout(function(){$(".slide-info-2").addClass("displayNone");return false;});
	$(".slide-thumb-3").mouseout(function(){$(".slide-info-3").addClass("displayNone");return false;});
	$(".slide-thumb-4").mouseout(function(){$(".slide-info-4").addClass("displayNone");return false;});
	
	/*-- Tooltip sulle icone del video --*/
	
	$(function(){
		$('img.captify').captify({
		// all of these options are... optional
		// ---
		// speed of the mouseover effect
		speedOver: 'fast',
		// speed of the mouseout effect
		speedOut: 'fast',
		// how long to delay the hiding of the caption after mouseout (ms)
		hideDelay: 100,	
		// 'fade', 'slide', 'always-on'
		animation: 'slide',		
		// text/html to be placed at the beginning of every caption
		prefix: '',		
		// opacity of the caption on mouse over
		opacity: '0.7',					
		// the name of the CSS class to apply to the caption box
		className: 'caption-bottom',	
		// position of the caption (top or bottom)
		position: 'bottom',
		// caption span % of the image
		spanWidth: '100%'
	});
	});
	
	$("img.captify").mouseover(function(){
  		$(this).removeClass("opacity80").closest("a").find("#foto").addClass("opacity100");
  		return false;
	});
	$("img.captify").mouseout(function(){
  		$(this).removeClass("opacity100").closest("a").find("#foto").addClass("opacity80");
  		return false;
	});
	
	/* -- Comment form --*/
			
	$(".btn-commenti").click(function(){
		$(".commentForm").slideDown("slow");
		$(this).addClass("selected");
		return false;
	});
	
	
	$(".btn-close-commenti").click(function(){
		$(".commentForm").slideUp("slow");
		$(".btn-commenti").addClass("selected");
		return false;
	});
		
	/* -- Datepicker 
	
	$(function() {
		$( "#datepicker" ).datepicker({
			showButtonPanel: true,
			showOn: "button",
			buttonImage: "/images/jquery.ui.foxcrime/calendario.png",
			buttonImageOnly: true
		});
	});
  --*/
		 
});

/* -- Cufon -- */

var initCufon = function(){
  Cufon.replace('.Futura', {  hover: 'true', fontFamily: 'Futura' });
  Cufon.replace('.Garamond', {  hover: 'true', fontFamily: 'Adobe Garamond Pro' });
}

initCufon();

/* -- Scrollbar --*/
	
$(function()
	{
		$('.scroll-pane').jScrollPane({
			showArrows: true,
			verticalDragMaxHeight:94,
			verticalGutter: 0
		});
	}
);

/* -- Mousewheel -- */

(function($) {

var types = ['DOMMouseScroll', 'mousewheel'];

$.event.special.mousewheel = {
    setup: function() {
        if ( this.addEventListener ) {
            for ( var i=types.length; i; ) {
                this.addEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = handler;
        }
    },
    
    teardown: function() {
        if ( this.removeEventListener ) {
            for ( var i=types.length; i; ) {
                this.removeEventListener( types[--i], handler, false );
            }
        } else {
            this.onmousewheel = null;
        }
    }
};

$.fn.extend({
    mousewheel: function(fn) {
        return fn ? this.bind("mousewheel", fn) : this.trigger("mousewheel");
    },
    
    unmousewheel: function(fn) {
        return this.unbind("mousewheel", fn);
    }
});


function handler(event) {
    var orgEvent = event || window.event, args = [].slice.call( arguments, 1 ), delta = 0, returnValue = true, deltaX = 0, deltaY = 0;
    event = $.event.fix(orgEvent);
    event.type = "mousewheel";
    
    // Old school scrollwheel delta
    if ( event.wheelDelta ) { delta = event.wheelDelta/120; }
    if ( event.detail     ) { delta = -event.detail/3; }
    
    // New school multidimensional scroll (touchpads) deltas
    deltaY = delta;
    
    // Gecko
    if ( orgEvent.axis !== undefined && orgEvent.axis === orgEvent.HORIZONTAL_AXIS ) {
        deltaY = 0;
        deltaX = -1*delta;
    }
    
    // Webkit
    if ( orgEvent.wheelDeltaY !== undefined ) { deltaY = orgEvent.wheelDeltaY/120; }
    if ( orgEvent.wheelDeltaX !== undefined ) { deltaX = -1*orgEvent.wheelDeltaX/120; }
    
    // Add event and delta to the front of the arguments
    args.unshift(event, delta, deltaX, deltaY);
    
    return $.event.handle.apply(this, args);
}

})(jQuery);

/* -- Migliora il mousewheel --*/

(function($){

var mwheelI = {
	pos: [-260, -260]
	},
	minDif 	= 3,
	doc 	= document,
	root 	= doc.documentElement,
	body 	= doc.body,
	longDelay, shortDelay
;

function unsetPos(){
	if(this === mwheelI.elem){
		mwheelI.pos = [-260, -260];
		mwheelI.elem = false;
		minDif = 3;
	}
}

$.event.special.mwheelIntent = {
	setup: function(){
		var jElm = $(this).bind('mousewheel', $.event.special.mwheelIntent.handler);
		if( this !== doc && this !== root && this !== body ){
			jElm.bind('mouseleave', unsetPos);
		}
		jElm = null;
        return true;
    },
	teardown: function(){
        $(this)
			.unbind('mousewheel', $.event.special.mwheelIntent.handler)
			.unbind('mouseleave', unsetPos)
		;
        return true;
    },
    handler: function(e, d){
		var pos = [e.clientX, e.clientY];
		if( this === mwheelI.elem || Math.abs(mwheelI.pos[0] - pos[0]) > minDif || Math.abs(mwheelI.pos[1] - pos[1]) > minDif ){
            mwheelI.elem = this;
			mwheelI.pos = pos;
			minDif = 250;
			
			clearTimeout(shortDelay);
			shortDelay = setTimeout(function(){
				minDif = 10;
			}, 200);
			clearTimeout(longDelay);
			longDelay = setTimeout(function(){
				minDif = 3;
			}, 1500);
			e = $.extend({}, e, {type: 'mwheelIntent'});
            return $.event.handle.apply(this, arguments);
		}
    }
};
$.fn.extend({
	mwheelIntent: function(fn) {
		return fn ? this.bind("mwheelIntent", fn) : this.trigger("mwheelIntent");
	},
	
	unmwheelIntent: function(fn) {
		return this.unbind("mwheelIntent", fn);
	}
});

$(function(){
	body = doc.body;
	//assume that document is always scrollable, doesn't hurt if not
	$(doc).bind('mwheelIntent.mwheelIntentDefault', $.noop);
});
})(jQuery);

/* -- mousewhell --*/
$(document).ready(function(){
	$(".colorbox").colorbox({iframe:true, innerWidth:620, innerHeight:400});
});

/**
 * ScroolPager
 *
 */
var scrollPager = {

  page: 0,
  totalPage: 0,
  caller: false,
  container: null,
  containerId: null,
  url: '',

  setUrl: function(url){
    this.url = url;
  },

  getUrl: function(){
    return this.url;
  },

  setContainer: function(id){
    this.containerId = id;
  },

  getContainer: function(){
    if(this.containerId != null) return $(this.containerId);
    return false;
  },

  getContainerId: function(){
    if(this.containerId != null) return this.containerId;
    return false;
  },

  setPage: function(page){
    this.page = page
  },

  getPage: function(){
    return this.page;
  },

  getTotalPage: function(){
    return this.totalPage;
  },

  setTotalPage: function(tot){
    this.totalPage = tot;
  },

  setCaller: function(flag){
    this.caller = flag;
  },

  getCaller: function(){
    return this.caller;
  }

}




/* Episodes scroll paging */

var episodeScrollPager = $.extend({
  
  init: function(){
    $(window).scroll(function(){

      if( episodeScrollPager.getPage() < episodeScrollPager.getTotalPage())
      {
          if ($(window).scrollTop() >= episodeScrollPager.getContainer().height() && !episodeScrollPager.getCaller()) {
            episodeScrollPager.setCaller(true);
            jQuery.get(episodeScrollPager.getUrl() + "/page/" + ( episodeScrollPager.getPage() + 1 ), '',function(data){
                jQuery(episodeScrollPager.getContainerId() + ' ul').append(data);
                episodeScrollPager.setPage(episodeScrollPager.getPage() + 1);
            }).complete(function(){
              episodeScrollPager.setCaller(false);
            });
          }
      } 
    });

  }
}, scrollPager);



/**
 * videoScrollPager
 *
 */
var videoScrollPager = $.extend({

  init: function(){
    $(window).scroll(function(){

      if( videoScrollPager.getPage() < videoScrollPager.getTotalPage())
      {
          if ($(window).scrollTop() >= videoScrollPager.getContainer().height() + 800  && !videoScrollPager.getCaller()) {
            videoScrollPager.setCaller(true);
            jQuery.get(videoScrollPager.getUrl() + "?page=" + ( videoScrollPager.getPage() + 1 ), '',function(data){
                jQuery(videoScrollPager.getContainerId() + ' ul').append(data);
                videoScrollPager.setPage(videoScrollPager.getPage() + 1);
            }).complete(function(){
              videoScrollPager.setCaller(false);
            });
          }
      }
    });

  }
}, scrollPager);

/**
 * SearchScrollPager
 *
 */
var searchScrollPager = $.extend({

  query: null,

  setQuery: function(val){
    this.query = val;
  },

  getQuery: function(){
    return this.query;
  },

  init: function(){
    $(window).scroll(function(){


      if( searchScrollPager.getPage() < searchScrollPager.getTotalPage())
      {
          if ($(window).scrollTop() >= searchScrollPager.getContainer().height()  - 800 && !searchScrollPager.getCaller()) {
            searchScrollPager.setCaller(true);
            jQuery.get(searchScrollPager.getUrl() + "?query=" + searchScrollPager.getQuery() + "&page=" + ( searchScrollPager.getPage() + 1 ), '',function(data){
                jQuery(searchScrollPager.getContainerId() + ' ul').append(data);
                searchScrollPager.setPage(searchScrollPager.getPage() + 1);
            }).complete(function(){
              searchScrollPager.setCaller(false);
            });
          }
      }
    });

  }
}, scrollPager);

/**
 * CommentScrollPager
 *
 */
var commentScrollPager = $.extend({

  query: null,

  setQuery: function(val){
    this.query = val;
  },

  getQuery: function(){
    return this.query;
  },

  init: function(){
    $(window).scroll(function(){


      if( commentScrollPager.getPage() < commentScrollPager.getTotalPage())
      {
          if ($(window).scrollTop() >= commentScrollPager.getContainer().height() + 700 && !commentScrollPager.getCaller()) {
            commentScrollPager.setCaller(true);
            jQuery.get(commentScrollPager.getUrl() + "/" + ( commentScrollPager.getPage() + 1 ), '',function(data){
                jQuery(commentScrollPager.getContainerId()).append(data);
                commentScrollPager.setPage(commentScrollPager.getPage() + 1);
            }).complete(function(){
              commentScrollPager.setCaller(false);
            });
          }
      }
    });

  }
}, scrollPager);

/** FB ACTIONS  **/

$(document).ready(function(){

  var stile = "top=10, left=10, width=870, height=600, status=no, menubar=no, toolbar=no scrollbars=no";

  var action = function(btn, stile){
    window.open(btn.attr('rel'), "", stile);
  }

  /* Refactoring ?!?!??! */

  var btn = $('.fb-button');
  btn.click(function(){
    action(btn,stile);
  })


  var btnTwitter = $('.fb-twitter');
  btnTwitter.click(function(){
    action(btnTwitter,stile);
  })

});

/* Searc Engine input form*/


$(document).ready(function(){
  $('.query').focusin(function(){
    $(this).val('');
  })
})

/* Video Show Filtering*/

var changeOption = function(el, url)
{
  $(el).change(function(){
    el = $(this);

    if(el.val() != '')
    {
      path  = url + "/" + $(this).val();

      window.location = path;

    }
  })
};

