Warning = {
	init: function() {
		var container = $('div.warning-popup');
		container.find('.cover').css({height: $(window).height()}).next().css({top: ($(window).height() - container.find('.container').height()) / 2});
		container.find('.close, .cover').click(function(){ container.hide(); });
	}
}

$.fn.youtube = function(){
	var self = $(this);
		self.rel = self.attr('rel');
	
	self.click(function(){
		if (self.off != 1) {
			self.top = self.parent().offset().top - 228;
			self.left = self.parent().offset().left + self.width() + 50;
			self.off = 1;
			$('div.youtube-popup').remove();
			var block = $('<div class="youtube-popup"><a class="close"></a><span>' +
					  '<object width="424" height="344">' +
					  	'<param name="movie" value="http://www.youtube.com/v/' + self.rel + '&amp;hl=en_US&amp;fs=1"></param>' +
					  	'<param name="allowFullScreen" value="true"></param>' +
					  	'<param name="allowscriptaccess" value="always"></param>' +
					  	'<embed src="http://www.youtube.com/v/' + self.rel + '&amp;hl=en_US&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="424" height="344"></embed>' +
					  '</object></span></div>');
			block.appendTo('body');
			self.left = (self.left + block.width() > $(window).width() ? $(window).width() - block.width() - 10 : self.left);
			block.css({left: self.left, top: self.top});
			block.find('.close').click(function(){
				block.remove();
				$(document).unbind('click');
				self.off = 0;
				self.on = false;
			});
			$(document).click(function(e){
				var target = $(e.target);
				if (!target.hasClass('youtube-popup') && !target.parent().hasClass('youtube-popup') && !target.parent().parent().hasClass('youtube-popup') && self.on == true) {
					if (!target.is('object') && !target.is('embed')) {
						$(document).unbind('click');
						self.on = false;
						block.remove();
						self.off = 0;
					}
				} else self.on = true;
			});
		}
	});
}



/* Can wait, not important to execute on document.ready event */
$(window).bind('load', function () {
	
	/*
	 * Search
	 */
	$('#search')
		.focus(function () {
			$(this).parents('form').addClass('search-notempty');
		})
		.blur(function () {
			var val = $(this).val().replace(/^\s+|\s+$/g, '');
			if (!val) {
				$(this).parents('form').removeClass('search-notempty');
			}
		})
		.val('');
	
});

$(function(){
	Warning.init();
	$('span.youtube-link a').each(function(){ $(this).youtube(); });
});



/*
 * Execute callback when object is available
 * 
 * Usage:
 *   $.available(find, callback, options);
 *   $.available(find, callback);
 *   $.available(find, options);
 * 
 * Example:
 * 	.available("PersonInfo.persons", function () {...});
 * 
 * Arguments:
 *   Find
 *     String which will try to find in context (dot separated)
 *     Case sensitive
 * 
 *   Callback
 *     Callback function if 'find' is found in context
 *     Arguments are: [find string, options, object which was found]
 * 
 *   Options (all optional):
 *     timeout        number of milliseconds before stop and execute 'error' callback
 *     context        context in which search for 'find', default is window
 *     error          callback function if 'find' is not found in context in 'timeout'
 *     success        callback function if 'find' is found in context, same as 'callback'
 */
(function ($) {
	
	function check (find, context) {
		var context = context;
		for(var i=0,ii=find.length; i<ii; i++) {
			if (context && (find[i] in context)) {
				context = context[find[i]];
			} else {
				return false;
			}
		}
		
		return context;
	}
	
	$.available = function (str, fn, options) {
		//Get arguments
		var fn = (typeof fn == 'function' ? fn : null);
		var options = (typeof fn == 'object' && !options ? fn : options || {});
		
		if (!fn && 'success' in options) fn = options.success;
		
		//Timeout data
		var timeout = ('timeout' in options ? options.timeout || 999999 : 5000);
		var delay = 25;
		
		//Find what in what
		var context = options.context || window;
		var find = str.split('.');
		
		setTimeout(function (interval) {
			timeout -= 25;
			
			var result = check(find, context);
			if (result) {
				if (typeof fn == 'function') {
					fn(str, options, result);
				}
				return;
			}
			
			if (timeout >= 0) {
				setTimeout(arguments.callee, delay);
			} else {
				//Timed out
				if ('error' in options) {
					if (typeof options.error == 'function') {
						options.error(str, options);
					}
				}
			}
		}, delay);
	};
	
})(jQuery);


/*
 * jQuery reflow plugin
 */
jQuery.fn.reflow = function (delay, include_ie7) {
	var version = (include_ie7 ? 7 : 6);
	
	if (!jQuery.browser.msie || parseFloat(jQuery.browser.version) > version) return false;
	
	var delay = delay || 0;
	var self = this;
	
	var fn = function () {
		jQuery(self).each(function () {
			
			var props = ['marginLeft', 'marginTop', 'marginRight', 'marginBottom', 'paddingLeft', 'paddingTop', 'paddingRight', 'paddingBottom'];
			
			for(var i=0,j=props.length; i<j; i++) {
				var val = jQuery(this).css(props[i]);
				if (parseFloat(val) == 0) {
					if (val.indexOf('px') !== -1) {
						jQuery(this).css(props[i], '0em');
					} else {
						jQuery(this).css(props[i], '0px');
					}
					
					return;
				}
			}
			
		});
	};
	
	if (delay != -1) {
		setTimeout(fn, delay);
	} else {
		fn();
	}
	
	return true;
};

/*
 * Logging plugin
 */
jQuery.log = (function () {
	if (typeof console == 'object' && 'log' in console) {
		var fn = $.proxy(console.log, console);
		fn.error = $.proxy(console.error, console);
		fn.debug = $.proxy(console.debug, console);
	} else {
		var fn = function () {};
		fn.error = fn.debug = function () {};
	}
	
	jQuery.log = fn;
	jQuery.log.apply(jQuery, arguments);
});

/*
 * UA for IE6
 */
jQuery.browser.msie6 = !!($.browser.msie && parseInt($.browser.version) <= 6);

/*
 * CSS classnames for each browser
 */
$(function () {
	if ($.browser.msie) {
		$(document.body).addClass('IE' + parseInt($.browser.version));
	} else if ($.browser.mozilla){
		$(document.body).addClass('FF');
	} else if ($.browser.webkit) {
		$(document.body).addClass('WebKit');
	}
});

/*
 * IE6 background image flicker fix 
 */
if (jQuery.browser.msie6) {
	try {
		document.execCommand("BackgroundImageCache", false, true);
	} catch (e) {}
}