$(function(){
	/* Make some calculations for the cause box (sign-up-blurb.html > "Mike's Car Fund" box); */
	calcImageSide();
	
	/* Validate image selecting for the signup pages */
	$('body.new_skyraise input#img-selecter').live('click', function(){
		/*var _file = $('body.new_skyraise input#file-input').val();
		var _check = $('body.new_skyraise input[name="img-select"]').is(':checked');
		var _certify = $('body.new_skyraise input#certify-field');
		if ( _file=='' && _check==false ){
			$('body.new_skyraise #file-upload .error-msg').hide().html('You should choose an image or upload your own. ').slideDown();
			return false;
		} else if ( _file!=='' && _certify.is(':checked')==false) {
			$('body.new_skyraise #file-upload .error-msg').hide().html('Please certify that you have the right to display and distribute this picture and that it does not violate the SkyRaise Terms of Use.').slideDown();
			return false;
		} else {
			
		}*/
	});
	
	/* This hides the blurb.  */
	$('body.new_skyraise a.close-btn').click(function(){
		$(this).parents('body.new_skyraise .sign-in:eq(0)').slideToggle(function() {
			$(this).remove();
		});
	});
	
	/* Plugin for custom selects */
	$("body.new_skyraise select").selectbox();
	
	/* Test if font exists */
	if ($.font.test('"Lucida Grande"') == false) {
		$('head').append('<link rel="stylesheet" href="/css/style-verdana.css" type="text/css" media="all" />');
	};
	
	/* Blinking text fieds  */
	$('body.new_skyraise .blink').focus(function () {
		if ($(this).val() == $(this).attr('title')) 
		{
			$(this).css('color', '#000').val('');
		}
	}).blur(function () {
		if ($(this).val() == '') {
			$(this).css('color', '#acacac').val($(this).attr('title'));
		}
	});
	
	if ($.browser.msie && $.browser.version > 6) {
		$('body.new_skyraise input.field').css('margin-top', '-1px');
	};
	
	/* Toggle the more information for the cause box (sign-up-blurb.html > "Mike's Car Fund" box); */
	$col_height = $('body.new_skyraise .info-col').height();
	$('body.new_skyraise .info-col .see-more').live('click', function(){
		if ($(this).hasClass('active')) {
			$(this).parents('body.new_skyraise .info-col:eq(0)').css('height', $col_height);
			$(this).html('SEE MORE').removeClass('active');
		} else {
			$(this).parents('body.new_skyraise .info-col:eq(0)').css('height', 'auto');
			$(this).html('SHOW LESS').addClass('active');
		};
		return false;
	});
	$('body.new_skyraise a.show-less').click(function(){
		$(this).parent().slideUp();
		$('body.new_skyraise a.see-more').show();
		return false;
	});
	
	/* Plugin for password strength */
	$("body.new_skyraise input#password-field").passStrength({
		userid:	"#user_id"
	});

	/* Mail validation */
	if ($('body.new_skyraise .mail-validation').length) {
		var valid_mail = new LiveValidation('email-field');
		valid_mail.add( Validate.Presence );
		valid_mail.add( Validate.Email );	
	};	
	
	/* Toggle class on active fields */
	$('body.new_skyraise input[type=text], body.new_skyraise input[type=password], body.new_skyraise textarea.texarea-field').focus(function () {
		$(this).addClass('active-input');
	}).blur(function () {
		$(this).removeClass('active-input');	
	});
	$('body.new_skyraise .search-form2 input[type=text]').focus(function () {
		$(this).parents('div:eq(0)').addClass('active-input');
	}).blur(function () {
		$(this).parents('div:eq(0)').removeClass('active-input');
	});
	
	
	/* Validating the first form */
	$('body.new_skyraise input.first-submit').click(function(){
		var flag = 0;
		$('body.new_skyraise .required').each(function(){
			if($(this).val()==""){
				flag = 1;
			}
		});
		if($('body.new_skyraise span.testresult').hasClass('shortPass')){
			flag = 2;
		}
		if($('body.new_skyraise span.LV_validation_message').hasClass('LV_invalid')){
			flag = 3;
		}
		if(flag){
			switch(flag){
				case 1: $('body.new_skyraise p.error-msg').text('Please fill the required elements !!!'); break;
				case 2: $('body.new_skyraise p.error-msg').text('Your password is too short !!!'); break;
				case 3: $('body.new_skyraise p.error-msg').text('Your e-mail is not correct!!!'); break;
			}
			$('body.new_skyraise p.error-msg').slideDown();
			return false;
		}
		
		else {
			submit_skyraise_query();
			var pageurl = "/codeigniter/views/"+getNextPage()+".php";
			//makeCall(pageurl);
			return false;
		}
	});
	
	
	/* Validating all forms */
	$('body.new_skyraise input.next-submit').live('click', function(){
		if($(this).hasClass('has-required'))
		{
			flag = 0;
			$('body.new_skyraise .required').each(function(){
				if($(this).val()==""){
					flag = 1;
				}
			});
			
			if(flag){
				$('body.new_skyraise p.error-msg').text('Please fill the required elements !!!');
				$('body.new_skyraise p.error-msg').slideDown();
				return false;
			}
			else {
				submit_skyraise_query();
				//makeCall($(this).attr('name'));
				return false;
			}
		}
		
		if($(this).hasClass('has-required-radio')){
			flag=0;
			$('body.new_skyraise input.radio-field').each(function(){
				if($(this).is(":checked")){
					flag = 1;
				}
			});
			
			if(!flag){
				$('body.new_skyraise p.error-msg').text('Please select an option !!!');
				$('body.new_skyraise p.error-msg').slideDown();
				return false;
			}
			
			else {
				submit_skyraise_query();
				//makeCall($(this).attr('name'));
				return false;
			}
		}
		
		else {
			submit_skyraise_query();
			//makeCall($(this).attr('name'));
			return false;
		}
	});
	
});

/* This function is called when you go to the next step in the signup page. */
function makeCall(content) {
	$('body.new_skyraise .content').fadeOut(function(){				
		$(this).html("");
		$('body.new_skyraise .loader').fadeIn('fast');
		
		$('body.new_skyraise .content').load(content, function(){
			$('body.new_skyraise .loader').fadeOut(function(){
				$('body.new_skyraise .content').fadeIn();
				$("body.new_skyraise select").selectbox();
				
				$('body.new_skyraise input.field, body.new_skyraise textarea.texarea-field').focus(function () {
					$(this).addClass('active-input');
				}).blur(function () {
					$(this).removeClass('active-input');	
				});
			});
		});
	});
}

/* Check the active radio buttons */
$('body.new_skyraise .price-box-holder body.new_skyraise .radio-check-btns').each(function() {
	var checked = $(this).is(':checked');
	if (true) {};
})

/*
function asd(elem) {
	var checked = $(this).is(':checked');
	var big_box = $(this).parents('.price-box-holder');
	var row_holder = $(this).parents('.price-row');
	var check_big_box = big_box.hasClass('big-box');
	if (checked) {
		if (check_big_box) {
			if (big_box.hasClass('last-row')) {
				big_box.addClass('active-price');
			} else {
				big_box.addClass('active-price');
			};
		} else {
			if (row_holder.hasClass('last-row')) {
				row_holder.addClass('active-last-row');
			} else {
				row_holder.addClass('active-row');
			};	
		}
	} else {
		if (check_big_box) {
			if (big_box.hasClass('last-row')) {
				big_box.removeClass('active-price');
			} else {
				big_box.removeClass('active-price');
			};
		} else {
			if (row_holder.hasClass('last-row')) {
				row_holder.removeClass('active-last-row');
			} else {
				row_holder.removeClass('active-row');
			};	
		}
	};	
}
*/

function calcImageSide() {
	var image_width = $('body.new_skyraise #image-side img').width();
	var image_side = $('body.new_skyraise #image-side');
	var info_side = $('body.new_skyraise #info-side');
	var input = $('body.new_skyraise #url-input');
	var max_width = 428;
	var max_space = 60;
	var whole_width = 641;
	var padding = (max_width - image_width)/2;
	if (padding > max_space) {
		padding = max_space;
	};
	var new_image_side_width = image_width + 2*padding;
	image_side.css('width', new_image_side_width);
	
	var new_input_width = new_image_side_width - 97;
	input.css('width', new_input_width);
	
	var new_info_side_width = whole_width - new_image_side_width;
	info_side.css('width', new_info_side_width);
	
	$('body.new_skyraise #image-side img').css('padding', padding);
	
	/*
	var image_height = parseInt(image_side.find('.image').height()) - 16;
	if (image_height < 219) {
		info_side.find('.info-col').css('height', 219);
	} else {
		info_side.find('.info-col').css('height', image_height);
	};
	*/
	
}

