var banner = {
	load : function() {
		var speed = 1000;
		var interval = 5000;	
		var banner = jQuery('#banner_image');
		banner.css('position', 'relative');
		var elements = banner.children('img');
		elements.css({
			position : 'absolute',
			top : 0,
			left : 0,
			'z-index' : 1
		}).hide().eq(0).show();
		var counter = 0;
		setInterval(function() {
			if(counter < elements.size() - 1)
				counter++;
			else
				counter = 0;
			elements.filter(':visible').css('z-index', 1);
			elements.eq(counter).css('z-index', 2).fadeIn(speed, function() {
				elements.not(jQuery(this)).hide();
			});
		}, interval);		
	}
};
var navigation = {
	load : function() {
		var navigation = jQuery('#navigation');
		var menu = navigation.find('.subelements');
		var background = navigation.find('.background');
		if(menu.children().size() == 0) {
			menu.hide();
		} else {
			if(menu.find('.active_element').size() !== 0) {
				background.css('opacity', .9).show();
				menu.css('opacity', .9).show();
			} else {
				var speed = 1000;
				var easing = 'easeOutQuint';
				background.show();
				this.slide(background, speed, easing, 0, 225, 0, .9);
				this.slide(menu, speed, easing, 0, 225, 0, 1);
			};
		};
	},
	slide : function(object, speed, easing, sPos, ePos, sOpa, endOpa) {
		object.css({
			left : sPos,
			opacity : sOpa
		}).animate({
			left : ePos,
			opacity : endOpa
		}, speed, easing);	
	}
};
var shorten = {
	byId : function(id, length, closure) {
		shorten.truncate(jQuery('#' + id), length, closure);
	},
	byClass : function(className, length, closure) {
		jQuery('.' + className).each(function() {
			shorten.truncate(jQuery(this), length, closure);
		});
	},
	truncate : function(obj, length, closure) {
		if(obj.text().length > length) {
			var value = obj.text().slice(0, length);
			if(value.substring(length - 1, length) == ' ')
				value = value.substring(0, length - 1);
			if(closure)
				value += closure;
		};
		obj.html(value);
	}
};
var logo = {
	load : function() {
		var elements = jQuery('.logo_gallery .element img');
		elements.each(function() {
			this.on = logo.replaceFormat(jQuery(this).attr('src'), 'logo')
			this.off = logo.replaceFormat(jQuery(this).attr('src'), 'logo_grayscale');
			jQuery(this).attr('src', this.off);
		}).hover(function() {
			jQuery(this).attr('src', this.on);
		}, function() {
			jQuery(this).attr('src', this.off);
		});
	},
	replaceFormat : function(src, format) {
		var q = '&format=';
		return src.substring(0 , src.indexOf(q)) + q + format;
	}
};
var news = {
	check : function(id) {
		var container = jQuery('#' + id);
		var title = container.children('.title');
		var contents = container.children('.contents');
		if(contents.children().size() == 0)
			title.hide();
	}
};
var search = {
	load : function() {
		var node = jQuery('#search input.text');
		var value = node.val();
		node.focus(function() {
			if(node.val() == value)
				jQuery(this).val('');
		}).blur(function() {
 			if(node.val() == '')
				jQuery(this).val(value);
		});
	},
	noResults : function(n, elements) {
		if(n == 0) {
			var page = jQuery('#search_page');
			elements.each(function(i) {
				jQuery(i).remove();
			});
		};
	}
};
var flash = {
	movie : function(id, player, skin, skinColor, movie, fWidth, fHeight, pWidth, pHeight, autoStart) {
		jQuery(function() {
			jQuery('#' + id).empty().flash({
				src : player,						
				width : fWidth,
				height : fHeight,
				flashvars : {
					PlayerSkinColor : skinColor,
					PlayerVideoSource : movie,
					PlayerSkinSource : skin,
					PlayerWidth : pWidth,
					PlayerHeight : pHeight,
					PlayerAutoPlay : autoStart
				}
			});
		});
	}
};
jQuery(function() {
	banner.load();
	logo.load();
	search.load();
});