/*
	jQuery Friendly Common File
	---------------------------
*/



// Deactive Dummy "#" Links
var linkBreaker = function() { $('a[@href=#]').click( function(){ return false; } ); }
//	$(document).ready(linkBreaker);


// Set Outbound Links by Class Name
var outLinks = function() { $('a.out').click( function(){ this.target = '_blank'; } ); }
$(document).ready(outLinks);


// Pop-up Windows by Size
var popWindows = function() { $('a.popup').popwindow(); }
jQuery.fn.popwindow = function() {
	return this.each(
		function(){
			var dimensions = this.className.match(/[0-9]{1,4}x[0-9]{1,4}/) + '';
			var width = eval(dimensions.split('x')[0]), height = eval(dimensions.split('x')[1]);
			$(this).bind('click',function() { window.open("" + this.href + "",null,'width=' + width + ', height=' + height); return false; })
		}
	)
}
$(document).ready(popWindows);

// Search form
var sitesearch = {
	init : function()
	{
		var keyword = $('#top-keyword'), def = keyword.val();
		keyword.focus( function(){
			var value = $(this).val() == def ? '' : $(this).val();
			$(this).val(value);
		});
		keyword.blur( function() {
			var value = $(this).val() == '' ? def : $(this).val();
			$(this).val(value);
		});
	}
}
$(document).ready(sitesearch.init);


// Correct PNG Images for IE 6
var pngFix = {
	init : function() {
		var imgs = $("img[@src*=png]");
		imgs.each(
			function() {
				var newSrc = $(this).attr('src'), newID = $(this).attr('id'), newClass = $(this).attr('class'), newTitle = $(this).attr('alt'), newStyle = $(this).attr('style'), newAlign = $(this).attr('align'), newW = $(this).attr('width'), newH = $(this).attr('height'), dfilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true,src='"+ newSrc +"',sizingMethod=image)";
				
				var span = document.createElement('span');
				$(span).attr({ 'id':newID, 'class':newClass, 'title':newTitle, 'style':newStyle });
				$(span).css({'display':'inline-block', 'background':'none', width:newW+'px', height:newH+'px', filter:dfilter});
				if( newAlign ) $(span).css({'float':newAlign});
				if( $(this).parent('a') ) $(span).css({'cursor':'hand'});
				$(this).after(span).remove();
			}
		)
	}
}
if( $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent) )
	$(document).ready(pngFix.init);


// Image Rollovers
var rollovers = function(){ $('img.rollover').rollover(); }
jQuery.fn.rollover = function() {
	return this.each(
		function() {
			var srcString = this.src;
			var ext = srcString.substring(srcString.length - 4, srcString.length), name = srcString.substring(0, srcString.length - 4), overImage = name + '_over' + ext;
			var img = new Image();	img.src = overImage;
			$(this).bind('mouseover',function(){ this.src = overImage; } );
			$(this).bind('mouseout',function(){ this.src = srcString; } );
		}
	)
}
$(document).ready(rollovers);

	
// IE Image Rollovers
var IEroll = {
	init : function() {
		$("span.rollover").hover( IEroll.over, IEroll.out );
	},
	over : function() {
		var geturl = new RegExp(/src='([^ ]+)'/i);
		var filter = this.style.filter, src = geturl.exec(filter)[1];
		var ext = src.substring(src.length - 4, src.length), name = src.substring(0, src.length - 4), newSrc = name +'_over'+ ext;
		var image = new Image();
		image.src = newSrc;
		var dfilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + newSrc + "\', sizingMethod='image')";
		$(this).css({filter:dfilter});
	},
	out : function() {
		var geturl = new RegExp(/src='([^ ]+)'/i);
		var filter = this.style.filter, src = geturl.exec(filter)[1];
		var ext = src.substring(src.length - 4, src.length), name = src.substring(0, src.length - 9), newSrc = name + ext;
		var dfilter = "progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src=\'" + newSrc + "\', sizingMethod='image')";
		$(this).css({filter:dfilter});

	}
}
if( $.browser.msie && /MSIE\s(5\.5|6\.)/.test(navigator.userAgent) )
	$(document).ready(IEroll.init);


// HREF Link
function goTo( url ) {
	location.href = url;
}

// Smooth Scroll Back to Top
var scrollToTop = function(){ $('a[href=#top]').click(function(){ backToTop(); return false;} ) };
function backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}
$(document).ready(scrollToTop);



/*	Form Highlighting Functions
	--------------------------------------------------------	*/
var liveForm = {
	init : function() {
		var form = $('.form');
		if( form.length <= 0 )
			return false;
		var labels = $(form).find('label');
		var inputs = $(form).find(':input');
		// Hovers
		$(labels).hover( function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); } )
		$(inputs).focus( function(){ $(this).parent().addClass('focus'); } )
		$(inputs).blur( function(){ $(this).parent().removeClass('focus'); } )
	}
}
$(document).ready(liveForm.init);


/* -- Flash Embed -- */
var embed = {
	video : function(type,src,options,target) {
		
		switch( type ) {
			case 'flv' :
			case 'swf' :
				classid = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
				codebase = 'http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0';
				pluginspage = 'http://www.macromedia.com/go/getflashplayer';
				apptype = 'application/x-shockwave-flash';
				srcname = 'movie';
				break;
			case 'mov' :
			case 'm4v' :
			case 'mv4' :
				classid = 'clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B';
				codebase = 'http://www.apple.com/qtactivex/qtplugin.cab#version=6,0,2,0';
				pluginspage = 'http://www.apple.com/quicktime/download/';
				apptype = 'application/video-quicktime';
				srcname = 'src';
				break;
			case 'wmv' :
				classid = 'CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6';
				codebase = 'http://activex.microsoft.com/activex/controls/ mplayer/en/nsmp2inf.cab#Version=5,1,52,701';
				pluginspage = 'http://www.microsoft.com/Windows/MediaPlayer/';
				apptype = 'application/x-oleobject';
				srcname = 'FileName';
				break;
		}
		
		// Build Object String
		var objectStr = '<' + 'object classid="' + classid + '" codebase="' + codebase + '" width="' + options.width + '" height="' + options.height + '">' + "\n" + '<' + 'param name="'+ srcname +'" value="' + src + '" />' + "\n";
		for( var key in options ) {
			objectStr += '<' + 'param name="'+ key +'" value="'+ options[key] +'" />' + "\n";
		}
		
		// Build Embed String
		var embedStr = '<' + 'embed src="' + src + '"';
		for( var key in options ) {
			embedStr += ' '+ key + '="' + options[key] + '"';
		}
		embedStr += ' />' + "\n";
		
		objectStr += embedStr + '</' + 'object>' + "\n";
		$(target).html(objectStr);
//		document.write(objectStr);
		
	}
}

/*	| Slider Navigation
	---------------------------------	*/
var blob = {
	init : function() {
		blob.nav = $('#navigation');
		blob.links = $('#navigation li');
		blob.current = $('#navigation .current');
		if( blob.current.length == 0 )
			blob.current = $('#navigation a:first').addClass('current');
		blob.currID = blob.current.parent().attr('id');
		blob.slider = $('<li class="back"><div class="left"></div></li>').appendTo(blob.nav);
		$(blob.links).hover(blob.move,blob.goback);
		$(blob.current).each(blob.move);
	},
	move : function() {
		$(blob.slider).stop().animate({ width:this.offsetWidth, left:this.offsetLeft}, 'fast');
	//	$(blob.slider).animate({ width:this.offsetWidth, left:this.offsetLeft}, 'fast');
		blob.timer = clearTimeout(blob.timer);
	},
	goback : function() { blob.timer = setTimeout( blob.moveback, 500 ); },
	moveback : function() {
		var e = document.getElementById(blob.currID);
		$(blob.slider).animate({ width:e.offsetWidth, left:e.offsetLeft}, 'fast');
	}
}
$(document).ready(blob.init);




/**
 * tips
 *
 * Pop-up tooltips. Requires styling in CSS file.
 */
jQuery.fn.extend({
	popTip: function( options ) {
		return this.each(function() {
			new jQuery.popTip( this, options );
		});
	}
});
jQuery.popTip = function( el, options )
{
	//	Define defaults.
	var timer;
	var defTitle	= $(el).attr('title');
	var url			= $(el).attr('href');
	defaults = {
		xAdjust		: 10,
		yAdjust		: 10,
		opacity		: 0.85,
		inDelay		: 0,
		outDelay	: 0,
		inSpeed		: 'fast',
		outSpeed	: 'fast',
		showURL		: true,
		className	: 'tooltip'
	};
	
	options = $.extend(defaults, options);

	//	Create our box.
	var box = document.createElement('div');
	
	$(el).bind('mouseover', function( event ) {
		
		timer = clearTimeout(timer);
		//	Empty title tag to stop browser tips.
		$(this).attr('title','');
		
		//	Append box and box contents.
		$('body').append(box);
		$(box).html('<p>'+ defTitle +'</p>');
		if( options.showURL == true )
			$(box).append('<em>'+ url +'</em>');
		
		//	Add box class.
		$(box).addClass( options.className );
		
		//	Position box and adjust for boundaries.
		var x = event.pageX, y = event.pageY;
		x = ( options.x == 'center' ) ? (x - ($(box).width() / 2)) : (x + options.x);
		y = ( options.y == 'above' ) ? (y - $(box).height() - $(this).height()) : (y + options.y);
		var outeredge = x + $(box).width(), bodywidth = $('body').width();
		x = ( x < 0 ) ? 0 : ( ( outeredge > bodywidth ) ? x - (outeredge - bodywidth) : x );
		$(box).css({'position':'absolute', 'opacity':options.opacity, 'left':x, 'top':y});
		
		//	Display box.
		timer = setTimeout( function(){
			$(box).stop().fadeIn( options.inSpeed );
		}
		, options.inDelay );
		
	}).bind('mouseout', function(e) {
		//	Put title back.
		$(this).attr('title', defTitle );
		
		//	Hide box.
		timer = clearTimeout(timer);
		timer = setTimeout( function() {
			$(box).stop().fadeOut( options.outSpeed, function(){ 
				$(this).remove();
			});
			timer = clearTimeout(timer);
		}, options.outDelay );
	});
}

//	Load tool tips.
$(document).ready( function(){ 
	$('a.tip').popTip({
		x		: 'center',
		y		: 20,
		showURL	: false,
		opacity	: 0.85,
		inDelay	: 270,
		outDelay: 600,
		className : 'tooltip'
	});
});


 
