$(function () {
	videoListEffects();
	
	$('.slider').cycle({
		next:'.next',
		prev:'.prev',
		timeout:0
	});
	
	$('.class_youtube').mouseover(function(){$(this).animate({height:'552px'},{queue:false,duration:300})});
	$('.class_youtube').mouseout(function(){$(this).animate({height:'527px'},{queue:false,duration:300})});
	
	$('#share_menu').mouseover(function(){ $('#share_menu ul').show(); });
	$('#share_menu').mouseout(function(){ $('#share_menu ul').hide(); });
	
	if ($('#videolist img').length>0){
		$('#videolist img').parent().parent().css({backgroundImage:'url("assets/gif_load.gif")',backgroundPosition:'center center',backgroundRepeat:'no-repeat'});
		$('#videolist img').parent().css({top:'220px'});
		$('#videolist img').hide();
	}
	
	if ($('.slider img').length>0){
		$('#slider_wrap').css({backgroundImage:'url("assets/gif_load.gif")',backgroundPosition:'center center',backgroundRepeat:'no-repeat'});
		$('#slider_wrap ul li img').css({opacity:0});
	}
	
});

$(window).bind("load", function() {
	if ($('#videolist img').length>0) {
		$('#videolist img').resize({width:232,height:130});
		int = setInterval('loadImage("#videolist img")',100);
	}
	if ($('#slider_wrap ul li img').length>0){
		$('#slider_wrap ul li img').resize({width:460,height:260});
		$('#slider_wrap ul li img').animate({opacity:1});
		$('#slider_wrap').css({backgroundImage:'none'});
	}
});

var i = 0;
var int=0;

function loadImage(elements) {
	var images = $(elements).length;
	if (i >= images) clearInterval(int);
	var el = $(elements+':hidden').eq(0);
	var grandpar = $(el).parent().parent();
	var par = $(el).parent();
	$(el).show();
	$('#videolist img').css({opacity:.5});
	$(par).animate({top:'0px'},500,function(){ $(grandpar).css({backgroundImage:'none'}); });
	i++;
}


function galleryFullSize(but) {
	
	if ($(but).val()==0) {
		
		$('#slider_wrap').css({width:'940px',height:'525px'});
		$('#textcontent .img_nav').css({width:'940px'});
		
		$('#slider_wrap ul li img').resize({width:940,height:525});
		$('#left').hide();
		$(but).val(1);
		$(but).removeClass("resize");
        $(but).addClass("resize_active");
		
		var targetOffset = $('#slider_wrap').offset().top;
		$('html,body').animate({scrollTop: targetOffset}, 1000);
		
	}else{
		
		$('#slider_wrap').css({width:'460px',height:'260px'});
		$('#textcontent .img_nav').css({width:'460px'});
		
		$('#slider_wrap ul li img').resize({width:460,height:260});
		$('#left').show();
		$(but).val(0);
		$(but).removeClass("resize_active");
        $(but).addClass("resize");
		
	}
	
}


function filterVideos(list,id,el) {
	
	var list_el = list+' li';
	$(el+' .selected').removeClass('selected');
	$('#filter_'+id).addClass('selected');
	
	
	$(list).animate({opacity:0},300,function(){
		if (id<0) {
			$(list_el).show();
			$(callback).html('Displaying '+$(list_el).length+' of '+$(list_el).length+' videos');
		}else{
			var showCount = 0;
			$(list_el).each(function(){ if ($(this).find('.tagval').val().indexOf(id)<0) { $(this).hide(); }else{ showCount++; $(this).show(); } });
			$(el+' .count').html('Displaying '+showCount+' of '+$(list_el).length+' videos');
		}
		$(this).animate({opacity:1},300);
	});
}



function videoListEffects (){
	$('#videolist li').mouseover(function(){ $(this).find('img').animate({opacity:1},{queue:false, duration:300})  });
	$('#videolist li').mouseout(function(){ $(this).find('img').animate({opacity:.5},{queue:false, duration:300})  });
	return false;
	$('#videolist li').mouseover(function(){
									var selected = this;
									$('#videolist li').each(function(){ if (this!=selected) $(this).animate({opacity:'.5'},{queue:false, duration:300}); });
									});
	$('#videolist').mouseout(function(){ $(this).find('li').animate({opacity:'1'},{queue:false, duration:300}); });
}



(function($) {
    $.fn.resize = function(options) {
 
        var settings = $.extend({
            width: null,
			height: null,
			frame: "max"
        }, options);
 
        return this.each(function() {
			
			if(this.tagName.toLowerCase() != "img") { return $(this); } // Only images can be resized

			var width = this.naturalWidth;
			var height = this.naturalHeight;
			
			// IE fix
			if (!width || !height) {
				var img = document.createElement('img');
				img.src = this.src;
				width = img.width;
				height = img.height;
			}
			
			var diff = 1;
			var newWidth = 1;
			var newHeight = 1;
			
			var eq = (width/height <= settings.width/settings.height);
			if (settings.frame=="min") eq = (width/height >= settings.width/settings.height);
			
			if (eq) {
				diff = height/width;
				newWidth = settings.width;
				newHeight = settings.width*diff;
			}else{
				diff = width/height;
				newHeight = settings.height;
				newWidth = settings.height*diff;
			}
			
			var posX = Math.round((settings.width/2)-(newWidth/2));
			var posY = Math.round((settings.height/2)-(newHeight/2));
			
			return $(this)
				.attr("width", newWidth)
				.attr("height", newHeight)
				.css({ position:'absolute',top:posY,left:posX });
			
        });
    }
})(jQuery);
