/*http://www.youtube.com/watch?v=AVyHwbXc4RA*/
jQuery.noConflict();

(function($) {
    $(document).ready(function() {
        
		
		$('.newsitem .readon,.mod_placehere .readon').click(function () {
			var self = this;
			$(self).addClass('isloading');
			$(this).prev().load($(this).attr('href') + " .article-content > *", function (data, status, req) {
				$(self).removeClass('isloading');
				if (status == 'success')
					$(self).remove();
			});
			return false;
		});
		/*
		$('.mod_placehere .readon').click(function () {
			var self = this;
			$(self).addClass('isloading');
			$(this).prev().load($(this).attr('href') + " .article-content > *", function (data, status, req) {
				$(self).removeClass('isloading');
				if (status == 'success')
					$(self).remove();
			});
			return false;
		});
		*/
		var calendarTooltips = function () {
			var contentLoaded = function () {
				var events = $("#events-"+$(this).attr('content'));
				$(this).bubble('header', events.find('h2.day').detach().html());
				return events.html();
			};
			
			var contentLoadedNoEvents = function () {
				$(this).bubble('header', 'Ei tapahtumia t&auml;ll&auml; p&auml;iv&auml;ll&auml;.');
				return '<div class="contentpaneopen_edit"><a class="caladd" href="tapahtumat/editevent/?dates='+$(this).attr('content')+'">Lis&auml;&auml; tapahtuma</a></div>';
			};
			
			var pinRemoved = function () {
				$.ownskit.bubble.block = false;
			};
			
			var pinNoEvents = function () {
				$.ownskit.bubble.block = true;
			}
				
			var pin = function () {
				var self = this;
				
				$.ownskit.bubble.block = true;
				
				if ($(self).data('ajaxified'))
					return;
					
				$(self).bubble('getContent').addClass('isloading');
				$(self).bubble('repositionBubble', 0);
				$(self).bubble('getContent').load($(self).attr('href'),
					{
						ajax_com : '1'
					},
					function (resp, status, req) {
						$(self).bubble('getContent').find('.componentheading').remove();
						$(self).bubble('getContent').removeClass('isloading');
						$(self).data('ajaxified', 1);
						$(self).bubble('width', 550);
						$(self).bubble('repositionBubble', 0);
						$(self).bubble('repositionBubble', 0);
						$(self).bubble('getContent').find('img').load(function() {
							//console.log('loaded!');
							$(self).bubble('repositionBubble', 0);
						});
					});
			};
				
			$('.cal .nonEventDay').bubble({
				openerBindShow : '',
				openerBindHide : '',
				openerBindPin : 'click',
				animationSpeed : 0,
				animationSpeedReposition : 0,
				width: 400,
				offset : 5,
				onPinRemoved : pinRemoved,
				onContentLoaded: contentLoadedNoEvents
			});
			$('.smallcal .eventDay').bubble({ 
				openerBindShow : 'mouseenter',
				openerBindHide : 'mouseleave',
				openerBindPin : 'click',
				animationSpeed : 0,
				animationSpeedReposition : 0,
				width: 400,
				offset : 5,
				onContentLoaded: contentLoaded,
				onPinRemoved : pinRemoved,
				onPin: pin
			});
			$('.bigcal td:not(.isOpen) .eventDay').bubble({ 
				openerBindShow : 'mouseenter',
				openerBindHide : 'mouseleave',
				openerBindPin : '',
				animationSpeed : 0,
				animationSpeedReposition : 0,
				width: 400,
				offset : 5,
				onContentLoaded: contentLoaded
			});
		}
		calendarTooltips();
		
		var calendarNavClick = function () {
			//$('.calmini .caltable a').bubble('destroy');
			$('.cal .modulecontent').load($(this).attr('href'), 
				{ 
					ajax_mod: 'ajax'
				}, 
				function () {
					calendarTooltips();
				}
			);
			
			return false;
		};
		
		// TODO: A bit harsh
		$('.smallcal .calnext,.page_day .calnext,.page_editevent .calnext').live('click', calendarNavClick);
		$('.smallcal .calprev,.page_day .calprev,.page_editevent .calprev').live('click', calendarNavClick);
		$('.smallcal .calcurpast,.page_day .calcurpast,.page_editevent .calcurpast').live('click', calendarNavClick);
		$('.smallcal .calcurfut,.page_day .calcurfut,.page_editevent .calcurfut').live('click', calendarNavClick);
    
        $('a[href^=http://www.youtube.com/watch?v=]').click(function() {
            $.fancybox({
                'padding'		: 0,
                'autoScale'		: false,
                'transitionIn'	: 'none',
                'transitionOut'	: 'none',
                'title'			: this.title,
                'width'		    : 680,
                'height'		: 495,
                'href'			: this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
                'type'			: 'swf',
                'swf'			: {
                    'wmode'		: 'transparent',
                    'allowfullscreen'	: 'true'
                }
            });
            
            return false;
        });
        
        $('a[href$=flv]').click(function() {
            sizem = /size(\d+)x(\d+)/;
            var m = sizem.exec($(this).attr('rel'));
            var width = 640;
            var height= 480;
            if (m != null) {
                width = m[1];
                height = m[2];
            }
            $.fancybox({
                'padding'		: 0,
                'autoScale'		: false,
                'transitionIn'	: 'none',
                'transitionOut'	: 'none',
                'title'			: this.title,
                'width'		    : parseInt(width),
                'height'		: parseInt(height),
                'href'			: '/plugins/content/avreloaded/mediaplayer.swf',
                'type'			: 'swf',
                'swf'			: {
                    'wmode'		: 'transparent',
                    'allowfullscreen'	: 'true',
                    'flashVars': 'autostart=true&amp;file=' + this.href
                }
            });

            return false;
        });
        
        //<object type="application/x-shockwave-flash" id="p_avreloaded0" class="allvideos" data="/plugins/content/avreloaded/mediaplayer.swf" width="400" height="320"><param name="allowscriptaccess" value="always"><param name="seamlesstabbing" value="true"><param name="allowfullscreen" value="true"><param name="wmode" value="window"><param name="bgcolor" value="#FFFFFF"><param name="menu" value="true"><param name="flashvars" value="file=http://uusi.22-pistepirkko.net/images/stories/videos/Roundabout__optimoitu_w768.flv&amp;width=400&amp;height=320&amp;showeq=false&amp;searchbar=false&amp;enablejs=false&amp;autostart=false&amp;showicons=true&amp;showstop=false&amp;showdigits=true&amp;showdownload=false&amp;usefullscreen=false&amp;backcolor=0xFFFFFF&amp;frontcolor=0x000000&amp;lightcolor=0x000000&amp;screencolor=0x000000&amp;overstretch=false"></object>
        
    });
})(jQuery);
