(function($) {

var get_parent_url = (function(str){return str.replace(/\/[^/]+\/?$/, '/');}),
	my_url    = $('script:last').attr('src'),
	js_url    = get_parent_url(my_url),
	media_url = get_parent_url(js_url),
	img_url   = media_url + 'images/';

// creates abbr tag on IE6
if ($.browser.msie && $.browser.version < 7)
	document.createElement("abbr");

// website namespace, thx paulirish.com
$.CYBX = {
	// properties
	name: 'Cyberonics.com',
	js_url: js_url,
	media_url: media_url,
	img_url: img_url,
	play_image_url: media_url+'css/images/play_large.png',
	flowplayer_url: js_url + "flowplayer/flowplayer-3.1.5.swf",
	facelift_url: js_url + 'facelift-2.0b3/',

	// helper functions
	get_parent_url: get_parent_url,

	// pages
	common: function(){
		$('body')
		.find('.nice').each(function(){
			$(this)
			.prepend('<span class="ir"><!-- ir --></span>')
			.removeClass('nice')
			.addClass('niced');
		});
		$('#primary .file_download_list li:nth-child(odd), #primary tr:nth-child(odd)').addClass('odd');
		$('#breadcrumbs a:last').addClass('active');
	},
	s_default_www: function(){ // global home
		$('#global_nav a').mouseover(function(e){
			$('span', this).hide().stop(1, 1).fadeIn('fast');
		});

		FLIR.init( { path: $.CYBX.facelift_url } );
		$('#welcome h2, #welcome p').each(function(){
			var $this = $(this),
				text = $this.text();
			FLIR.replace($this.html(text).get(0), new FLIRStyle({ mode:'wrap' }));
		});
		window.slideshow = function(){
			var $img = $('#welcome img.slide'),
				me = window.slideshow,
				slides = me.slides,
				c = (me.current +1) % slides.length;
			me.current = c;
			$('<img src="' + img_url + slides[c] + '" class="slide" />').insertBefore(
				$img.fadeOut(1000, function(){$(this).remove()})
			);
		}
		var slides = [
			'home/01.jpg','home/03.jpg','home/04.jpg'  // removed 02.jpg by request
		];
		window.slideshow.slides = slides;
		window.slideshow.current = 2;
		//preload
		$.map(slides, function(src){
			var img = new Image();
			img.src = src;
		});
		setInterval('slideshow()', 8000);

		return false; // false (default) = don't process "other"
	},
	other: function(){
		$("#lang_region_search").children()
			.hoverIntent(
				function(){$(this).addClass('active')},
				function(){$(this).removeClass('active')}
			);

		$('#content')
		.find("a[href*=.flv]")
			.not(".video-panel a")
				.addClass('video')
				.prepend('<img src="'+$.CYBX.play_image_url+'" />')
				.flowplayer($.CYBX.flowplayer_url, {clip: {scaling: 'fit'}})
				.end()
			.end()
		.find(".video-panel")
			.pinchevideo({
				play_image: $.CYBX.play_image_url,
				fp_url: $.CYBX.flowplayer_url
			})
			.end()
		/* to-do: refine it further */
		.find('a.unfold').click(function(){
				var target = $(this).attr('href');
				$(target).slideToggle();
				return false;
		});

		FLIR.init( { path: $.CYBX.facelift_url } );
		var headingFLIRStyle = new FLIRStyle({ mode:'wrap' });
		$("h1.nice-text,h2,h3,h4,h5.nice-text,h6.nice-text").each(function(){
			var $this = $(this),
				text = $this.text();
			FLIR.replace($this.html(text).get(0), headingFLIRStyle);
		});
		if ($.browser.msie && $.browser.version <= 8) {
			var rounded_corners = '<span class="rc rc-tl"><!-- rounded corner top-left --></span><span class="rc rc-tr"><!-- rounded corner top-right --></span><span class="rc rc-br"><!-- rounded corner bottom-right --></span><span class="rc rc-bl"><!-- rounded corner bottom-left --></span>';
			$('.rounded-corners').append(rounded_corners);
		}
	},
	finalize: function(){
		// .js remains in place
		$.UTIL.removeFlag('loading');
	}
};

$.UTIL = {
	fire: function(funcname, args){
		var namespace = $.CYBX;
		if (funcname !== '' && namespace[funcname] && typeof namespace[funcname] == 'function')
			return namespace[funcname](args);
		return true;
	},
	loadEvents: function(){
		var bodyId = document.body.id, fire=$.UTIL.fire;
		fire('common');
		fire(bodyId) && fire('other');
		fire('finalize');
	},
	removeFlag: function(flag) {
		var H=document.documentElement, c='className', f=new RegExp('\\b' + flag + '\\b');
		H[c]=H[c].replace(f, '');
	},
	setFlags: function() {
		var H=document.documentElement, c='className';
		H[c]=[H[c]].concat($.makeArray(arguments)).join(' ');
	}
};

// loading & js flags
$.UTIL.setFlags('loading js')

// kick it all off here
$(document).ready($.UTIL.loadEvents);

})(jQuery)