/*
ID: basic
Name: Common
Version: 1.0
Description: Commonly used javascript functions.
Dependents: jquery-1.4.1
*/


// 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);

// Subnav Drops
var subnav = function() {
	$( '#navigation ul li' ).bind( "mouseover", function() {
		$( this ).children( '.drops' ).show();
		$( this ).children('a').addClass( 'hover-active' );
	}).bind( "mouseout", function() {		
		$( this ).children( '.drops' ).hide();
		$( this ).children('a').removeClass( 'hover-active' );
	} );
	}
$(document).ready(subnav);


// 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);


/**
 * OnClear (clear form values)
 */
jQuery.fn.extend({
    onClear: function(options) {
        return this.each(function() {
            new jQuery.onClear(this, options);
        });
    }
});
jQuery.onClear = function( el, options )
{
    var def = $.trim($(el).val());
    $(el).addClass(options.className);
    
    $(el).bind('focus', function(e) {
        $(this).removeClass(options.className);
        if( def == $(this).val() ) {
            $(this).val('');
        }
    } )
    .bind('blur', function(e) {
        if( $.trim( $(this).val() ) == '' ) {
            $(this).val(def);
            $(this).addClass(options.className);
        }
    } );
    
    $('form:has(input[name='+ $(el).attr('name') +'])')
    .bind('reset', function(e) {
        $(el).val(def);
        $(el).addClass(options.className);
    } )
    .bind('submit', function(e) {
        if( $(el).val() == def )
            $(el).val('');
    } );
}

$(document).ready( function(){ $('.onclear').onClear({className:'clear-init'}); } );

/**
 * labelClear (Clear form values using labels)
 */
jQuery.fn.extend({
	labelClear : function(options) {
		return this.each(function() {
			new jQuery.labelClear(this, options);
		});
	}
});
jQuery.labelClear = function( el, options )
{
	var field = $(el).siblings(':input[type=text],:input[type=textarea]');
	if( $(field).val() == '' )
		$(el).css('visibility','visible');
	$(el).click( function() {
		$(this).css('visibility','hidden'); $(field).focus();
	});
	$(field).blur(function(){
		if( this.value == '' )
			$(el).css('visibility','visible');
	}).focus(function(){
		$(el).css('visibility','hidden');
	}).keydown(function(e){
		$(el).css('visibility','hidden');
		$(this).unbind(e);
	});
}



