/**
* hoverIntent r5 // 2007.03.27 // jQuery 1.1.2+
* <http://cherne.net/brian/resources/jquery.hoverIntent.html>
* @param  f  onMouseOver function || An object with configuration options
* @param  g  onMouseOut function  || Nothing (use configuration options object)
* @author    Brian Cherne <brian@cherne.net>
*/
(function($){$.fn.hoverIntent=function(f,g){var cfg={sensitivity:7,interval:100,timeout:0};cfg=$.extend(cfg,g?{over:f,out:g}:f);var cX,cY,pX,pY;var track=function(ev){cX=ev.pageX;cY=ev.pageY;};var compare=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);if((Math.abs(pX-cX)+Math.abs(pY-cY))<cfg.sensitivity){$(ob).unbind("mousemove",track);ob.hoverIntent_s=1;return cfg.over.apply(ob,[ev]);}else{pX=cX;pY=cY;ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}};var delay=function(ev,ob){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);ob.hoverIntent_s=0;return cfg.out.apply(ob,[ev]);};var handleHover=function(e){var p=(e.type=="mouseover"?e.fromElement:e.toElement)||e.relatedTarget;while(p&&p!=this){try{p=p.parentNode;}catch(e){p=this;}}if(p==this){return false;}var ev=jQuery.extend({},e);var ob=this;if(ob.hoverIntent_t){ob.hoverIntent_t=clearTimeout(ob.hoverIntent_t);}if(e.type=="mouseover"){pX=ev.pageX;pY=ev.pageY;$(ob).bind("mousemove",track);if(ob.hoverIntent_s!=1){ob.hoverIntent_t=setTimeout(function(){compare(ev,ob);},cfg.interval);}}else{$(ob).unbind("mousemove",track);if(ob.hoverIntent_s==1){ob.hoverIntent_t=setTimeout(function(){delay(ev,ob);},cfg.timeout);}}};return this.mouseover(handleHover).mouseout(handleHover);};})(jQuery);


/* Document Ready Scripts */
jQuery(document).ready(function() {
	
	// BxSlider
	$('#NewsTicker ul').bxSlider({
		pager: false,
		speed: 800,
		pause: 3000,
		mode: 'fade',
		auto: true
	});

	// Mega Dropdown Menu
	function megaHoverOver(){
		$(this).find(".Menu").stop().fadeTo('fast', 1).show();	
		(function($) { // Calculate width of all ul's
			$.fn.calcSubWidth = function() {
				rowWidth = 0;
				$(this).find(".Col").each(function() { // Calculate row				
					rowWidth += $(this).width(); 
				});	
			};
		})($); 
		if ($(this).find(".Row").length > 0 ) { // If row exists...
			var biggestRow = 0;	
			$(this).find(".Row").each(function() {	// Calculate each row					   
				$(this).calcSubWidth();
				if(rowWidth > biggestRow) { // Find biggest row
					biggestRow = rowWidth;
				}
			});
			$(this).find(".Menu").css({'width':biggestRow}); // Set width
			$(this).find(".Row:last").css({'margin':'0'});
		} else { // If row does not exist...
			$(this).calcSubWidth(); // Set Width
			$(this).find(".Menu").css({'width' : rowWidth});
		}
	}
	function megaHoverOut(){ 
	  $(this).find(".Menu").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	var config = {    
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		interval: 0, // number = milliseconds for onMouseOver polling interval    
		over: megaHoverOver, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: megaHoverOut // function = onMouseOut callback (REQUIRED)    
	};
	$("#Navi #TopNav li .Menu").css({'opacity':'0'});
	$("#Navi #TopNav li").hoverIntent(config);
	
	
	// Twitter Feed
	$(".Tweet").tweet({
		join_text: "auto",
		username: "twictivity",
		avatar_size: 0,
		count: 5,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "Tweets inladen..."
	});
	
	// Contact Dropdown Menu
	function megaHoverOver2(){
		$(this).find(".Contact").stop().fadeTo('fast', 1).show();	
		(function($) { // Calculate width of all ul's
			$.fn.calcSubWidth = function() {
				rowWidth = 0;
				$(this).find(".Col").each(function() { // Calculate row				
					rowWidth += $(this).width(); 
				});	
			};
		})($); 
		if ($(this).find(".Row").length > 0 ) { // If row exists...
			var biggestRow = 0;	
			$(this).find(".Row").each(function() {	// Calculate each row					   
				$(this).calcSubWidth();
				if(rowWidth > biggestRow) { // Find biggest row
					biggestRow = rowWidth;
				}
			});
			$(this).find(".Contact").css({'width':biggestRow}); // Set width
			$(this).find(".Row:last").css({'margin':'0'});
		} else { // If row does not exist...
			$(this).calcSubWidth(); // Set Width
			$(this).find(".Contact").css({'width' : rowWidth});
		}
	}
	function megaHoverOut2(){ 
	  $(this).find(".Contact").stop().fadeTo('fast', 0, function() {
		  $(this).hide(); 
	  });
	}
	var config = {    
		sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)    
		interval: 0, // number = milliseconds for onMouseOver polling interval    
		over: megaHoverOver2, // function = onMouseOver callback (REQUIRED)    
		timeout: 0, // number = milliseconds delay before onMouseOut    
		out: megaHoverOut2 // function = onMouseOut callback (REQUIRED)    
	};
	$("#Intro #ContactBtn li .Contact").css({'opacity':'0'});
	$("#Intro #ContactBtn li").hoverIntent(config);
	
	// Gravity Forms 
	jQuery('.gform_body input[type="text"], .gform_body textarea').addClass("idleField");
	jQuery('.gform_body input[type="text"], .gform_body textarea').focus(function() {
		jQuery(this).removeClass("idleField").addClass("focusField");
		if (this.value == this.defaultValue){ 
			this.value = '';
		}
		if(this.value != this.defaultValue){
			this.select();
		}
	});
	jQuery('.gform_body input[type="text"], .gform_body textarea').blur(function() {
		jQuery(this).removeClass("focusField").addClass("idleField");
		if (jQuery.trim(this.value) == ''){
			this.value = (this.defaultValue ? this.defaultValue : '');
		}
	});
	
});


// Homepage Slideshow
function slideSwitch() {
	var $active = $('#Slideshow img.Active');
	if ( $active.length == 0 ) $active = $('#Slideshow img:last');
	var $next =  $active.next().length ? $active.next()
		: $('#Slideshow img:first');
	$active.addClass('Last-Active');
	$next.css({opacity: 0.0})
		.addClass('Active')
		.animate({opacity: 1.0}, 1600, function() {
			$active.removeClass('Active Last-Active');
		});
}
jQuery(function() {
	setInterval("slideSwitch()", 1650 );
});


/*
Copyright 2011 : Simone Gianni <simoneg@apache.org>
Released under The Apache License 2.0 
http://www.apache.org/licenses/LICENSE-2.0
*/

(function() {
    function createPlayer(jqe, video, options) {
        var ifr = $('iframe', jqe);
        if (ifr.length === 0) {
            ifr = $('<iframe scrolling="no">');
            ifr.addClass('player');
        }
        var src = 'http://www.youtube.com/embed/' + video.id;
        if (options.playopts) {
            src += '?';
            for (var k in options.playopts) {
                src+= k + '=' + options.playopts[k] + '&';
            }  
            src += '_a=b';
        }
        ifr.attr('src', src);
        jqe.append(ifr);  
    }
    
    function createCarousel(jqe, videos, options) {
        var car = $('div.carousel', jqe);
        if (car.length === 0) {
            car = $('<div>');
            car.addClass('carousel');
            jqe.append(car);
            
        }
        $.each(videos, function(i,video) {
            options.thumbnail(car, video, options); 
        });
    }
    
    function createThumbnail(jqe, video, options) {
        var imgurl = video.thumbnails[0].url;
        var img = $('img[src="' + imgurl + '"]');
        if (img.length !== 0) return;
        img = $('<img>');    
        img.addClass('thumbnail');
        jqe.append(img);
        img.attr('src', imgurl);
        img.attr('title', video.title);
        img.click(function() {
            options.player(options.maindiv, video, $.extend(true,{},options,{playopts:{autoplay:1}}));
        });
    }
    
    var defoptions = {
        autoplay: false,
        user: null,
        carousel: createCarousel,
        player: createPlayer,
        thumbnail: createThumbnail,
        loaded: function() {},
        playopts: {
            autoplay: 0,
            egm: 1,
            autohide: 1,
            fs: 1,
            showinfo: 0
        }
    };
    
    
    $.fn.extend({
        youTubeChannel: function(options) {
            var md = $(this);
            md.addClass('youtube');
            md.addClass('youtube-channel');
            var allopts = $.extend(true, {}, defoptions, options);
            allopts.maindiv = md;
            $.getJSON('http://gdata.youtube.com/feeds/users/' + allopts.user + '/uploads?alt=json-in-script&format=5&callback=?', null, function(data) {
                var feed = data.feed;
                var videos = [];
                $.each(feed.entry, function(i, entry) {
                    var video = {
                        title: entry.title.$t,
                        id: entry.id.$t.match('[^/]*$'),
                        thumbnails: entry.media$group.media$thumbnail
                    };
                    videos.push(video);
                });
                allopts.allvideos = videos;
                allopts.carousel(md, videos, allopts);
                allopts.player(md, videos[0], allopts);
                allopts.loaded(videos, allopts);
            });
        } 
    });
    
})();
        
jQuery(function() {
    jQuery('#player').youTubeChannel({user:'Ictivity'});
});

