/* ****************************************************************************
JAM.BO JavaScript
2009.12.04 KJD: Document created
**************************************************************************** */
var focusedItem = new Object();

$( document ).ready(function(){

	// 2009.12.05 KJD: Add label over
	if( $( '.labelOver' ).length )
	{
		$( '.labelOver' ).labelOver();
	}
	
	// 2009.12.05 KJD: Change theme
	$( '.themeselection A' ).click(function(){
		$( '#pageCSS' ).attr('href','themes/' + $( this ).text() + '/css/style.css');
	});
	
	// 2009.12.05 KJD: Correct empty links
	$( 'A' ).each(function(){
		if( !$(this).attr('href') )
		{
			$( this ).attr('href', "javascript:void(0)" );
		}
	});
	
	$( '.button.signinopen' ).click(function(){
		// var thisLocation = $( this ).position();
		// 2010.02.18 KJD: Use content location
		var thisLocation = $( '.pagecontent' ).position();
		var thisRight	= $( '.pagecontent' ).width();
		
		$( '.popup.signinpopup' )
			.css({'top':thisLocation.top, 'left': (thisLocation.left) }) // 2010.02.18 KJD: Updated
			.show(300, function(){ $( ".signinform input[name='username']" ).focus(); });
	});
	
	$( '.popup.signinpopup .close' ).click(function(){
		$( this ).parent().hide(300);
	});
	
	// 2010.01.21 KJD: Removed .button
	//$( '.button.top10open' ).click(function(){
	$( '.top10open' ).click(function(){
		location.href= "/?use=reasons.html";	// jwg 2010-01-14
		browserClear();												//
		//$( '.popup.top10popup' )						//
		//	.show(300, function(){ 	; });			//
	});
			
	$( '.popup.top10popup .close' ).click(function(){
		$( this ).parent().hide(300);
	});
	
	
	$( '.pagearea .logo img' ).click(function(){
		location.href= "/";
		browserClear();
	});
	
	$( '.button.signupbutton ' ).click(function(){
		$('.errormsg2').show(300);
	});
	

	// 2009.12.28 KJD: Validate login form
	if( $( '.signinform' ).length )
	{
		$( '.signinform' )
			.attr( 'action', '/landing1/login' )
			.validate({
				submitHandler: function(form)
				{
					// 2009.12.28 KJD: Good form, hide & show spinner
					// jwg 2010-06-14 -- not required for new landing page
					//$( '.signinform' ).css({'display':'none'});
					//$( '.signinspinner' ).css({'display':'block'});
					form.submit();
				}
			});
			
		$( '.button.signin' ).click(function(){
			$( '.signinform' ).submit();
		});
	}
	
	$( '.errormsg .close' ).click(function(){
		$( this ).parent().hide(300);
	});	
	$( '.errormsg2 .close' ).click(function(){
		$( this ).parent().hide(300);
	});
	
	
	// 2010.02.03 KJD: New registration
	if( $('.register form').length )
	{
		// 2010.01.06 KJD: set full page
		$('.td_aside').css({'display':'none'});
		$('.td_content')
			.css({'width':'100%'})
			.parents('TABLE')
			.css({'width':'100%'});

		// 2009.12.13 KJD: Use beauty tips
		$( '.register INPUT, .register textarea' ).each(function(){
			if( $( this ).attr( 'rel' ) )
			{
				//if( !$.browser.msie )
				//{
					$( this ).bt( { contentSelector: "$(this).attr('rel')" } );
				//}
			}
		});
	}
	
	// jwg 2010-05-02 .. use diff form name for step 2,3,4..
	if( $('.form_settings').length )
	{
		// step 2
		//$( 'tr.urlinfo a' ).click(DuplicateRow);
		// jwg 2010-05-02 -- beauty tips
		var el = $("#bio");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#bio').attr('rel')" } );
		}
		var el = $("#homepage");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#homepage').attr('rel')" } );
		}
		var el = $("#location");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#location').attr('rel')" } );
		}
		var el = $("#language");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#language').attr('rel')" } );
		}
		var el = $("#timezone");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#timezone').attr('rel')" } );
		}
		var el = $("#tags");
		if (el.length && el.attr('rel')) {
			el.bt( { contentSelector: "$('#tags').attr('rel')" } );
		}
		
		/* -- jwg -- not doing it that way -- use existing approach that works ...
		// step 3
		$( "div.tab input[type='checkbox']" ).click(function(){
			if( $( this ).attr('checked') )
			{
				$( this ).parents( 'li' ).find( 'div.tab, div.info' ).addClass( 'selected' );
			}
			else
			{
				$( this ).parents( 'li' ).find( 'div.tab, div.info' ).removeClass( 'selected' );
			}
		});
		
		if( $( '.colorpicker' ).length )
		{
			$('.colorpicker').ColorPicker({
				flat: true,
				onChange: function (hsb, hex, rgb)
				{
					$( focusedItem ).val( "#" + hex )
						.parent( '.mycolorItem' ).find( '.myColor' ).css({'background-color': "#" + hex });
				}
			});
		}
		
		// Step 4
		
		// 2010.02.02 KJD: Check if someone typed
		$( '.mycolorItem .input' ).keyup(function(){
			$( this ).parent( '.mycolorItem' ).find( '.myColor' ).css({'background-color': $( this ).val() });
		});
		
		// 2010.02.01 KJD: Watch checkboxes
		$( ".mycolorItem input[type='checkbox']" ).click( function(){
			if( $( this ).attr( 'checked' ) )
			{
				$( this ).parent( '.mycolorItem' ).addClass( 'selected' )
					.find( '.input' ).attr( 'disabled', '' ).focus();
					
					focusedItem = $( this ).parent( '.mycolorItem' ).addClass( 'selected' )
					.find( '.input' );
			}
			else
			{
				$( this ).parent( '.mycolorItem' ).removeClass( 'selected' )
					.find( '.input' ).attr( 'disabled', 'disabled' );
			}
		});
		*/
	}
	
	// 2009.12.05 KJD: Add corners (exclude IE - only temporary for IE6)
	//doCorners();
});

// 2010.02.03 KJD: Duplicate row on registration
function DuplicateRow()
{
	$( 'tr.urlinfo:last' ).after("<tr class='urlinfo'>" + $( 'tr.urlinfo:last' ).html() + "</td>" );
	$( 'tr.urlinfo:last a' ).click(DuplicateRow);
}

// 2009.12.05 KJD: Everything that has corners
function doCorners()
{
	// 2010.04.06 KJD: corner errors with opera, so only do if NOT opera
	if( !$.browser.opera )
	{
		$( '.corner' ).corner( '7px');
		$( '.searchfield, .entry input' ).corner("left 7px");
		$( '.button.searchbutton, .button.emailsubmitbutton' ).corner( 'right 7px' );
	}
}

// 2009.12.28 KJD: Clear browser info
function browserClear()
{
	if( $.browser.msie )
	{
		window.event.returnValue=false; return false;
	}
}
