$(document).ready(function() {
	form_replace();
	showSuccess();
	
	even_divs($('.item')); // Even out floated un-even divs
	$('.item:odd').css({'margin-right':'0px'});
	
	/* Target _BLANK for non inside urls (and correct code) */
	//$('a[href^=http://]:not([href^=http://' + location.host + ']):not([href^=http://www.' + location.host + ']):not([href^=http://' + location.host.substr(4) + '])').live(
	$('a[href^=http://]:not(#weblinks_content a, #weblinks a, #TABLE1 a,[href^=http://' + location.host + '])').live(
	   'click',
	   function() {
	       window.open($(this).attr('href'), $(this).text());
	       return false;
	   }
	);
	
	/* If there is an iframe watch for its size to be reported */
	if ( $('h2#iframe_height').length != 0 ) {
		parent.parent.sizeIframe( $('h2#iframe_height').html() );
	}
	
	/* Ie fixes */
	if ( $.browser.msie ) {
		/*Round Corners*/
		$('.item').corner("cc:#E6F1FA");
		//$('#navigation li').corner('cc:#fff round 10px top');
		$('#navigation li:first').corner('cc:#E1E1E1 round 10px tl').corner("cc:#fff round 10px tr");
		$('#navigation li:not(:first)').corner('cc:#fff round 10px top');
		//$('#navigation li').corner2({tl: { radius: 8 }, tr: { radius: 8 }, bl: { radius: 8 }, br: { radius: 8 }, antiAlias: true});
		$('#calendar, #sandbox, #sandbox2, #sandbox3, #left-nav').corner('cc:#E6F1FA');
		$('#navigation li ul li').uncorner();
	}
	
	// Watch index slides for clicks
	$('#viewing ul li a').each(function(i) {
		$(this).click(function() { 
			$('#index-upper').attr('rel', 'stop');
			watchSlides(this, i); 
			return false;
		});
	});
	
	// send form
	$("#form_auto_send").submit(function(){
		$.ajax({url: "/scripts/emailForm.php",
				type: "POST",
				cache: false,
				data: $("#form_auto_send").serialize(),
				dataType: "html",
				success: function(msg){ 
					if($("#redirect_url").val()>"") window.location = $("#redirect_url").val();
					else if($("#thankyou_message").val()>""){
						alert($("#thankyou_message").val());
						location.reload(true);
					} else {
						alert("Thank you. Form submitted successfully.");
						location.reload(true);
					}
				}
			});	
		return false;
	});
	
	
	// startes the timer on the index "slides"
	setTimeout(function() { timeSlides(0); }, 5000);
});

function sizeIframe(h) {
	h = (h*1) + 20;
	$('#weblinks').animate({height:h+'px'}, 500);
}

function form_replace() {
	$('form').find('input:text, textarea').each(function() {
		if ( $('body').attr('class') == 'admin' ) {} else {
			$(this).focus(function() { 
				value = $(this).val(); $(this).val(''); 
			});
			$(this).blur(function() {
				if ( $.trim($(this).val()) == '' ) { $(this).val(value); }
			})
		}
	});
}

function rmPix(varin) {
	out = varin.replace("px", "") * 1;
	return out;
}

function showSuccess() {
	$('#success').fadeTo(2000, 1, function() {
		$(this).animate({top:"-3px"}, 500).fadeTo(10000, 1, function() {
			$(this).animate({top:"-100px"}, 500);
		});
	});
}

function even_divs(obj) { // I am only used for .items so I can be modified. 
	big = 0;
	$(obj).each(function() {
		var height = $(this).height();
		if ( height >= big ) { big = height; } // Get the largest object
	});
	$(obj).each(function() { 
		current = rmPix($(this).css('padding-bottom')); //Current padding
		height = $(this).height();
		var padding = (big - height) + current;//new padding based on need to expand. 
		$(this).delay(125).animate({paddingBottom: padding}, 250).delay(125);
	});
}

function timeSlides(target) {
	if ( $('#index-upper').attr('rel') == 'timer') {
		target = target + 1;
		if ( (target) == 4 ) { target = 0; }
		watchSlides($('#viewing ul li a:eq('+target+')'), target);
		setTimeout(function() { timeSlides(target); }, 5000);
	}
}

/* 	Slides	*/
function watchSlides(object, index) {
	// Turn off timer
	// Calculate what Slide were on
	// Prepare for a slide switch
	// Switch Slides
	// Reset Classes and such
	// IE needs to not fade text...
	
	var target 	= $(object).attr('rel');
	if ( ($('#frame-image .live').index() + 1) == target ) { return	}
	
	//Arrange the images
	$('#frame-image img:eq('+((target*1)-1)+')').css({'z-index':'100'});
	$('#frame-image .live').fadeTo(150, 0, function() {
		//Change Classes and re-set the CSS
		$(this).removeClass('live').css({'z-index':'10', 'opacity':'1'})
		$('#frame-image img:eq('+((target*1)-1)+')').addClass('live').css({'z-index':'1000'})
		$("#frame-link").attr("href", $('#frame-image img:eq('+((target*1)-1)+')').attr('rel'));
	});
	
	if ( !$.browser.msie ) {
		$('.slide:visible').fadeTo(150, 0, function() {
			$(this).removeClass('live').css({'display':'none'});
			$('.slide:eq('+((target*1)-1)+')').fadeTo(100, 1, function() {
				$(this).addClass('live');
			});
		});
	} else {
		$('.slide:visible').css({'display':'none'}).removeClass('live');
		$('.slide:eq('+((target*1)-1)+')').css({'display':'block'}).addClass('live');
	}
	
	$('#viewing ul li a img:not(:eq(4))').each(function() {
		$(this).attr('src', path+'/images/viewing/viewing-off.png').attr('rel', 'off');
	})
	$(object).find('img').attr('src', path+'/images/viewing/viewing-on.png').attr('rel', 'on');
	
	return false;
}



$(document).ready(function(){
	$('#memberAppForm').submit(function(ev){
		var that = this;
		ev.preventDefault();
		$.ajax({
			url: "/form/"+$("#fpurl").val(),
			type: "POST",
			cache: false,
			data:({info:$(this).serializeArray()}),
			success: function(json){
				if ($('#Payment_method').val() == "check") {
					$("#thank_you_area").css('display', 'block');
					$("#memberAppForm").css('display', 'none');
					window.location = '#';
					return false;
				} 
				$('[name=x_login]').val(json.loginID);
				$('[name=x_amount]').val(json.amount);
				$('[name=x_description]').val(json.description);
				$('[name=x_invoice_num]').val(json.invoice);
				$('[name=x_fp_sequence]').val(json.sequence);
				$('[name=x_fp_timestamp]').val(json.timeStamp);
				$('[name=x_fp_hash]').val(json.fingerprint);
				$('[name=x_test_request]').val(json.testMode);
				$(that).attr('action',json.url);
				$('#memberAppForm').unbind('submit').submit();
			}
		});
		return false;
	});
});
