$(document).ready(function()
{
	/* INIT - setup jquery */
	onLoadInit();

	/* do global inits for ajax loaded content
	$('body').ajaxSuccess(function(e, xhr, settings){
	onLoadInit();
	}); */
}); 

function onLoadInit(){
	
	//autoPlacehold - look for title
	$('INPUT.autoPlacehold').each(function(i){
		var title = $(this).attr('title');
		var value = $(this).attr('value');
		if(title){
			$(this).removeAttr('title');
			$(this).placehold({placeholdValue: title,blurClass: "placehold",blankSubmit:true});
		}else{//default, use value
			//$(this).placehold({/* placeholdValue: "ENTER EMAIL",*/blurClass: "placehold",blankSubmit:true});
		}
	});
	
	//$('INPUT.autoPlacehold').placehold({/* placeholdValue: "ENTER EMAIL",*/blurClass: "placehold"});
	
	
	$('INPUT.autoEnter').keypress(function(e){
		if(e.which == 13){$($(this).attr('ref')).click();}
	});
	
	$('A.trackLink').click(function(e){	
		_gaq.push(['_trackPageview', $(this).attr('href')])
	});
	openWindowInit();
	visibleInit();
	
}

function openWindowInit(){
	$('A.openNewWindow').removeClass('openNewWindow').click(function(e){
		winder = window.open($(this).attr('href'), '_blank', 'toolbar=0,menubar=0,location=0,height=500,width=620,scrollbars=1');
		
		leftPos = (screen.width / 2) - (620/2);
		topPos = (screen.height / 2) - (500/2);
		
		winder.moveTo(leftPos,topPos);
		e.preventDefault();
	});
}

function visibleInit(){
	$('SELECT.styled').each(function(i){
		if($(this).data('selectStyled_complete')){return;}
		var width = $(this).outerWidth();
		
		if( !$(this).is(':visible') ){width = $(this).getHiddenDimensions().outerWidth;}
		//alert(width+' '+$(this).outerWidth());
		var selWidth = 23;
		$(this).before('<span class="selectStyler" ref="#searchCarrier">'+$(this).find('OPTION:selected').text()+'</span>')
			.prev().width(width-selWidth);
		$(this).data('selectStyled_complete',true);
		
		$(this).change( function(e){$(this).prev().text( $(this).find('OPTION:selected').text() )} );
	});	
}


//Optional parameter includeMargin is used when calculating outer dimensions
(function($) {
$.fn.getHiddenDimensions = function(includeMargin) {
    var $item = this,
        props = { position: 'absolute', visibility: 'hidden', display: 'block' },
        dim = { width:0, height:0, innerWidth: 0, innerHeight: 0, outerWidth: 0, outerHeight: 0 },
        $hiddenParents = $item.parents().andSelf().not(':visible'),
        includeMargin = (includeMargin == null)? false : includeMargin;

    var oldProps = [];
    $hiddenParents.each(function() {
        var old = {};

        for ( var name in props ) {
            old[ name ] = this.style[ name ];
            this.style[ name ] = props[ name ];
        }

        oldProps.push(old);
    });

    dim.width = $item.width();
    dim.outerWidth = $item.outerWidth(includeMargin);
    dim.innerWidth = $item.innerWidth();
    dim.height = $item.height();
    dim.innerHeight = $item.innerHeight();
    dim.outerHeight = $item.outerHeight(includeMargin);

    $hiddenParents.each(function(i) {
        var old = oldProps[i];
        for ( var name in props ) {
            this.style[ name ] = old[ name ];
        }
    });

    return dim;
}
}(jQuery));



