/* Utility */
fooUtil.prototype = {
	
	redirect : function( url, timeout, new_window, title ) {
		if ( timeout==null ) timeout=0;
		if ( new_window==null ) new_window=false;
		if ( title==null ) title="";
		if ( timeout > 0 && !new_window ) {
			setTimeout( "window.location = '"+url+"';", timeout );
		} else if ( timeout > 0 && new_window ) {
			setTimeout( "window.open('"+url+"','"+title+"');", timeout );
		} else if ( new_window ) {
			window.open(url,title);
		} else {
			window.location = url;
		}
	} ,
	
	roundNumber : function( num, dec ) {		
		return Math.round( num * Math.pow(10,dec) ) / Math.pow(10,dec);		
	} ,
	
	formatNumber : function( num, dec ) {		
		var rounded = this.roundNumber( num, dec );
		var formatted = new Number( rounded );
		formatted = formatted.toFixed( dec );
		return formatted;	
	} ,
	
	addCommas : function( str ) {
		str += '';
		var x = str.split('.');
		var x1 = x[0];
		var x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	} ,
	
	inArray : function( arr, val ) {
		
		for( var i=0; i<arr.length; i++ ) {		
			if ( arr[i] == val ) return true ;			
		}
		return false;
		
	} ,
	
	removeKeysFromURL : function( url, remove_keys ) {
		
		var parts = url.split('?');
		var domain = parts[0];
		var query = parts[1];
		
		var new_pairs = new Array();
		var i, key, value;
		var pairs = query.split('&');
		for ( i=0; i<pairs.length; i++ ) {
			parts = pairs[i].split('=');
			key = parts[0];
			value = parts[1];			
			if ( ! foo_util.inArray( remove_keys, key ) )
				new_pairs.push( key + '=' + value );
		}		
		var new_query = new_pairs.join('&');
		
		return domain + '?' + new_query ;
			
	}
	
}

/* Cookies */
fooCookie.prototype = {
	
	create : function( name, value, seconds ) {
		if ( seconds ) {
			var date = new Date();
			date.setTime( date.getTime() + (seconds*1000) );
			var expires = "; expires=" + date.toGMTString() ;
		} else {
			var expires = "";
		}
		document.cookie = name + "=" + value + expires + "; path=/";
	} ,
	
	read : function( name ) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		var i=0;
		for( i=0; i < ca.length; i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return false;
	} ,
	
	erase : function( name ) {
		this.create( name, "" ,-1 );
	}
	
}

/* Ajax */
fooAjax.prototype = {
	
	post : function( url, data, callback ) {
		
		// post data w/ optional callback
		if ( callback == null ) callback = false;	
		$.post( url, data, function( e ) {			
			var response = eval("(" + e + ")");			
			if ( callback ) {
				setTimeout( function(){callback(response)},20 );
			}		  
		});
		
	}
	
}

/* Form */
fooForm.prototype = {
	
	currentForm : "" ,
	defaultWidth: 600 ,
	
	send : function( form_name, callback ) {
		
		// init				
		var form = $('#'+form_name);		
		if ( callback == null ) callback = false;
		
		// update button/loading
		form.find('.submit').css( 'display','none' );
		form.find('.loading').css( 'display','block' );
		form.find('.message').css( 'display','none' );
		
		// check for new window
		var new_window = false ;
		var page_title = $("title").text();
		if ( form.attr('target') == '_blank' )
			new_window = true ;
		
		// define form options
		var options = {
			
			'success' : function( e ) {
				
				var response = eval("(" + e + ")");				
				// valid input
				if ( response.valid > 0 ) {					
					// message and redirect
					if ( response.redirect != 0 && response.message != "" && callback==false ) {						
						form.find('.message').html( response.message );
						form.find('.message').css( 'display','block' );
						foo_util.redirect( response.redirect, 1000, new_window, page_title );
					// just redirect
					} else if ( response.redirect != 0 && callback==false ) {
						foo_util.redirect( response.redirect, 0, new_window, page_title );
					// just message
					} else if ( response.message != "" ) {
						form.find('.message').html( response.message );
						form.find('.message').css( 'display','block' );
						if ( callback ) {
							setTimeout( function(){callback(response)},1000 );
						}
					// just callback
					} else if ( callback ) {
						setTimeout( function(){callback(response)},20 );
					}				
				// invalid input
				} else if ( response.message != "" ) {					
					form.find('.message').html( response.message );
					form.find('.loading').css( 'display','none' );
					form.find('.submit').css( 'display','block' );
					form.find('.message').css( 'display','block' );					
				}
				// update button/loading
				if ( response.redirect == 0 ) {				
					form.find('.submit').css( 'display','block' );
					form.find('.loading').css( 'display','none' );					
				}
				
			}
			
		};
		
		// submit form
		form.ajaxSubmit(options); 
		
	} ,
	
	validate : function( type, input, el ) {
		
		// init
		var url = '/validate/validateinput/remote/1';
		var data = { 'type' : type , 'input' : input };
		var el = $(el);
		var update = el.closest('tr').find('.form_message');
		
		// check for object
		if ( el.data('object') ) {
			data['object'] = el.data('object');
		}
		
		// show loading
		this.showLoading( update );
		
		// make request	
		$.post( url, data, function( e ) {
			var response = eval("(" + e + ")");		
			if ( response.valid > 0 ) {
				update.removeClass( 'form_message_bad' );
				update.addClass( 'form_message_good' );
				update.text( 'looks good!' );
			} else {
				update.removeClass( 'form_message_good' );
				update.addClass( 'form_message_bad' );
				update.text( response.message );
			}
		});
		
	} ,
	
	validateConfirm : function( input, el ) {
		
		var el = $(el);
		var update = el.closest('tr').find('.form_message');
		var password = el.closest('table').find('.password').eq(0).val();
		
		// show loading
		this.showLoading( update );
		
		// validate
		if ( password == input ) {
			update.removeClass( 'form_message_bad' );
			update.addClass( 'form_message_good' );
			update.text( 'passwords match' );
		} else {
			update.removeClass( 'form_message_good' );
			update.addClass( 'form_message_bad' );
			update.text( 'passwords do not match' );
		}
		
	} ,
	
	validateNumber : function( input, el ) {
		
		var el = $(el);
		var update = el.closest('tr').find('.form_message');		
		update.removeClass( 'form_message_bad' );
		update.css( 'visibility', 'hidden' );
		
		// validate number
		var int = parseFloat( input );
		if ( isNaN( int ) ) {
			update.removeClass( 'form_message_good' );
			update.addClass( 'form_message_bad' );
			update.text( 'please enter a valid number' );
			update.css( 'visibility', 'visible' );
		}
		
		// validate min
		if ( el.data("min") != undefined ) {
			var input_min = parseFloat( el.data("min") );
			if ( int < input_min ) {
				update.removeClass( 'form_message_good' );
				update.addClass( 'form_message_bad' );
				update.text( 'please enter a number greater than or equal to ' + input_min );
				update.css( 'visibility', 'visible' );
			}
		}
		
		// validate max
		if ( el.data("max") != undefined ) {
			var input_max = parseFloat( el.data("max") );
			if ( int > input_max ) {
				update.removeClass( 'form_message_good' );
				update.addClass( 'form_message_bad' );
				update.text( 'please enter a number less than or equal to ' + input_max );
				update.css( 'visibility', 'visible' );
			}
		}
		
	} ,
	
	validateMax : function( el ) {
		
		el = $(el);
		var input = el.val() ;
		var input_len = input.length;
		var chars_left_el = el.closest('.col_input').find('.chars_left').eq(0);
		var max_chars = parseInt( chars_left_el.data('max') );
		var chars_left = max_chars - input_len;
		chars_left_el.text( chars_left );
		if ( chars_left < 0 ) chars_left_el.addClass('alert');	
		else chars_left_el.removeClass('alert');
		
	} ,
	
	validateRequired : function( input, el ) {
		
		var el = $(el);
		var update = el.closest('tr').find('.form_message');		
		update.removeClass( 'form_message_bad' );
		update.css( 'visibility', 'hidden' );
		
		if ( input.length <= 0 ) {
			update.removeClass( 'form_message_good' );
			update.addClass( 'form_message_bad' );
			update.text( 'this field is required' );
			update.css( 'visibility', 'visible' );
		}
		
	} ,
	
	showLoading : function( el ) {
		
		el.removeClass( 'form_message_bad' );
		el.removeClass( 'form_message_good' );
		el.text( 'validating...' );
		el.css( 'visibility', 'visible' );
		
	} ,
	
	initUpload : function( url, input, update, size_limit, cookie_name, callback ) {
		
		if ( callback==null ) callback=false;
		var user_cookie = foo_cookie.read( cookie_name );
		if ( user_cookie == false ) return false;	
		var data = { 'user_cookie' : user_cookie };
		$('#'+input).uploadify({
			'uploader' : '/_include/javascript/jquery-uploadify.swf',
			'script' : url,
			'scriptData' : data,
			'cancelImg' : '/_images/common/jquery-uploadify/cancel.png',
			'auto' : true,
			'multi' : false,
			'fileExt' : '*.jpg;*.gif;*.png',		
			'fileDesc' : 'Image Files (.JPG, .GIF, .PNG)',
			'sizeLimit' : size_limit * 1024 ,
			'onComplete' : function( event, ID, fileObj, response, data ) {			
				var response = eval("(" + response + ")");					
				if ( response.valid > 0 ) {				
					$('#'+update).css('background-image','url('+response.image+')');
					foo_ajax.post('/login/resetsession/remote/1','');
					if ( callback ) {
						setTimeout( function(){callback(response)},20 );
					}
				} else {
					alert(response.message);
				}
			}
		});
	
	} ,
	
	showForm : function( el, width, allow_close ) {
		
		if ( width == null ) width = this.defaultWidth ;
		if ( allow_close==null ) allow_close = true ;
		var form = $(el);
		var wrapper = $('#lightbox_wrapper');
		var overlay = $('#lightbox_overlay');
		
		if ( ! allow_close ) {
			$('#lightbox_close').css('display','none');
		}
		
		if ( form ) {
			if ( this.currentForm != el ) {
				if ( this.currentForm != "" )
					$(this.currentForm).css('display','none');		
				form.css('display','block');	
				this.currentForm = el;			
			}
			if ( parseInt( wrapper.width() ) != width ) {
				wrapper.css('width',width);
				wrapper.css('margin-left', Math.round( width/2*-1 ));
			}
			var doc_offset = parseInt( $(document).scrollTop() ) + 50;
			var doc_height = $(document).height();
			
			wrapper.css('top',doc_offset);
			wrapper.fadeIn();
			overlay.css('height',doc_height);
			overlay.css('display','block');		
		}
		
	} ,
	
	hideForm : function() {
		$('#lightbox_wrapper').fadeOut();
		$('#lightbox_overlay').css('display','none');
		$('.message').css('display','none');
	}
	
}

/* Misc */
fooMisc.prototype = {
	
	toggleRows : function( ) {
		
	}
	
}

/* Campaign */
swCampaign.prototype = {

	imageCallback : function( response ) {
		
		setTimeout( "foo_form.hideForm();", 1000 );
		
		var main_image = $('#campaign_gallery_main');
		var image_list = $('#campaign_gallery_list');
		var sub_images = image_list.children('li');
		var image_count = parseInt( sub_images.length );
		
		if ( main_image.css('display')=='none' ) {			
			main_image.css('background-image','url("'+response["image_600"]+'")');
			main_image.css('display','block');			
		}
		
		sub_images.each(function(index,el){
			if ( $(el).css('display')=='none' ) {
				$(el).css('background-image','url("'+response["image_200"]+'")');
				$(el).css('display','block');
				$(el).data('image',response["image_600"]);
				$(el).data('id',response["id"]);
				if ( index >= image_count-1 ) {
					$('#gallery_button').css('display','none');
				}
				return false;				
			}
		});
		
	} ,
	
	showImage : function( index_selected ) {
		
		var main_image = $('#campaign_gallery_main');
		var image_list = $('#campaign_gallery_list');
		var sub_images = image_list.children('li');
		sub_images.removeClass('selected');
		
		sub_images.each(function(index,el){
			if ( index==index_selected ) {
				$(el).addClass('selected');
				main_image.css('background-image','url("'+$(el).data('image')+'")');
				main_image.data('id',$(el).data('id'));
			}
		});
		
	} ,
	
	removeCurrentImage : function( campaign_name_url ) {
		
		var main_image = $('#campaign_gallery_main');
		var image_list = $('#campaign_gallery_list');
		var sub_images = image_list.children('li');
		
		var id = main_image.data('id');
		var url = '/campaign/removeimage/' + campaign_name_url;
		var data = 'image_id='+id+'&remote=1';
		var active_index = false;
		
		// delete image
		foo_ajax.post( url, data );
		
		// hide image thumbnail
		sub_images.each(function(index,el){
			if ( id == $(el).data("id") ) {
				$(el).css("display","none");
			}
			if ( active_index===false && $(el).css("display")!="none" ) {
				active_index = index;
			}
		});
		
		// show image or hide everything
		if ( active_index===false ) {
			main_image.css("display","none");
		} else {
			this.showImage( active_index );
		}
		
		// show add button
		$('#gallery_button').css('display','block');
		
	} ,
	
	postComment : function( campaign_id ) {
		
		var url = '/comment/submitauto/campaign/' + campaign_id;
		var data = 'remote=1';		
		foo_ajax.post( url, data );
		
	}
	
}

/* Comment */
swComment.prototype = {
	
	toggleReply : function( uid ) {
		$('#list_reply_'+uid).slideToggle();
	} ,
	
	submitReply : function( uid ) {
		
		var input_el = $('#list_reply_input_'+uid);
		var insert_el = input_el.closest('li');
		var content = input_el.val();
		if ( content=='' || content==input_el.data('default') ) return ;
		
		var url = '/comment/submit/' + uid;
		var data = {
			'content' : content ,
			'remote' : 1	
		};
		
		$.post( url, data, function( e ) {			
			var response = eval("(" + e + ")");			
			if ( response['content'] != undefined ) {
				var new_item = $('<li>',{'style':'display:none'}).html(response['content']);
				insert_el.before(new_item);
				new_item.slideDown();
				input_el.val( input_el.data('default') );
			}
		});
	}
	
}

/* Donation */
swDonation.prototype = {
	
	steps : 2 ,
	
	showDonation : function( item_id ) {
		
		// init
		var title_el = $('#donation_title_item');
		var title = title_el.data('default');
		var item_input = $('#form_donation_item_id');
		
		// set item to zero
		item_input.val('0');
		
		// check for id
		if ( item_id==null ) item_id = false;
		if ( item_id ) {
			var item_el = $('#item_'+item_id);
			item_input.val(item_id);
			title = item_el.data('title');
		}
		
		// update title
		title_el.text( title );
		
		// go to step 1
		this.goToStep( 1 );
		
		// show form
		foo_form.showForm('#donation_popup');
		
	} ,
	
	goToStep : function( step ) {
		var i, el;
		var steps = this.steps;
		for( i=1; i<=steps; i++ ) {
			el = $('#form_donation_step'+i);
			if ( step==i ) el.css('display','block');
			else el.css('display','none');
		}
		if ( step==2 ) this.updateFee();
	} ,
	
	updateFee : function() {
		
		var donation_amount = parseInt( $('input:radio[name=amount]:checked').val() );		
		if ( donation_amount<= 0 ) donation_amount = parseInt( $('#donation_amount_other').val() );
		if ( isNaN( donation_amount ) || donation_amount <= 0 ) return ;
		
		var fee = $('#donation_fee');
		var fee_percent = parseInt( fee.data('percent') );
		var fee_amount = donation_amount * fee_percent / 100 ;
		fee_amount = foo_util.formatNumber( fee_amount, 2 );
		
		fee.text( fee_amount );
		
	} ,
	
	checkHash : function() {
		
		if ( ! window.location.hash ) return;
		
		var hash = window.location.hash;
		if ( hash == '#donate' ) this.showDonation();
		else if ( hash.length > 1 ) {
			var item_id = hash.replace('#','');
			this.showDonation( item_id )
		}
		
	} ,
	
	updateState : function() {	
		var country = $('#form_donation_country').val();
		var state = $('#form_donation_state_row');
		var state_other = $('#form_donation_state_other_row');		
		if ( country == "US" ) {		
			state.css('display','');
			state_other.css('display','none');			
		} else {			
			state_other.css('display','');
			state.css('display','none');			
		}		
	}
	
}

/* Item */
swItem.prototype = {
	
	newItem : function( campaign_name_url ) {
		
		$('#form_item .message').css('display','none');
		$('#form_item_object').attr('name','campaign');
		$('#form_item_object').val(campaign_name_url);
		$('#form_item_title').val('');
		$('#form_item_value_total').val('');
		foo_form.showForm('#item_popup');
		
	} ,

	editItem : function( item_name_url ) {
		
		var list_item = $('#item_'+item_name_url);
		
		$('#form_item .message').css('display','none');
		$('#form_item_object').attr('name','item');
		$('#form_item_object').val(item_name_url);
		$('#form_item_title').val(list_item.data('title'));
		$('#form_item_value_total').val(list_item.data('value'));
		foo_form.showForm('#item_popup');
		
	} ,
	
	removeItem : function( item_name_url ) {
		
		// delete item
		var url = '/item/remove/' + item_name_url;
		var data = 'remote=1';		
		foo_ajax.post( url, data );
		
		// remove from list
		var list_item = $('#item_'+item_name_url);
		list_item.slideUp();
		
	} ,
	
	itemCallback : function( response ) {
		
		var list_item = '#item_' + response['id'] ;
		setTimeout( "foo_form.hideForm();", 1000 );
		
		if ( response["is_new"]<=0 ) {

			$(list_item).data('title',response['item']['title']);
			$(list_item).data('value',response['item']['value_total']);
			$(list_item).find('.item_title').text(response['item']['title']);
			$(list_item).find('.item_bar_copy span').text(response['item']['value_left_f']);
			$(list_item).find('.item_bar').css('width',response['item']['value_percentage']+'%');
			
		} else {
			
			var new_item = $('<li>', { 'id' : 'item_' + response['id'] , 'style':'display:none' });
			new_item.data('title',response['item']['title']);
			new_item.data('value',response['item']['value_total']);
			new_item.html(response['content']);
			new_item.appendTo('#campaign_item_list');
			new_item.slideDown();
			
		}
		
	}
	
}

/* Lightbox */
swLightbox.prototype = {
	
	uid : '' ,
	baseURL : '' ,
	codePattern : "<script type=\"text/javascript\">document.write(unescape('%3Cscript src=\"[url]\" type=\"text/javascript\"%3E%3C/script%3E').replace(/\'/,'%27'));</script>" ,
	
	init : function( uid, base_url ) {
		
		this.uid = uid;
		this.baseURL = base_url;
		
	} ,
	
	updateCode : function() {
		
		var button_position = $('#form_lightbox_button_position').val();
		var button_title = $('#form_lightbox_button_title').val();
		var title = $('#form_lightbox_title').val();
		var share = $('#form_lightbox_share').val();
		
		var url = this.baseURL ;
		url += '?share=' + share;
		url += '&title=' + encodeURIComponent(title);
		url += '&button_position=' + button_position;
		url += '&button_title=' + button_title;
		
		// build code
		var code = this.codePattern ;
		code = code.replace( '[url]', url );
		
		// update
		$('#form_lightbox_code').text( code );
		
	} ,
	
	updateButton : function() {
		
		var position = $('#form_lightbox_button_position').val();
		var title = $('#form_lightbox_button_title').val();
		var button = $('#'+this.uid+'-side_button_container');
		var button_link = $('#'+this.uid+'-side_button_link');
		
		// update position
		button.removeClass(this.uid+'-side_button_w '+this.uid+'-side_button_e '+this.uid+'-side_button_nw '
							+this.uid+'-side_button_ne '+this.uid+'-side_button_sw '+this.uid+'-side_button_se');							
		button.addClass(this.uid+'-side_button_'+position);
		
		// update image
		button_link.css( 'background-image' , 'url(/_images/_repot/Widget/button_'+title+'_'+position+'.png)' );
		
		this.updateCode();
		
	} ,
	
	updateTitle : function( input ) {
		
		$('#form_widget_frame').contents().find(".widget_header").eq(0).text( input );
		
		this.updateCode();
		
	}
	
}

/* Widget */
swWidget.prototype = {
	
	codePattern : '<iframe name="donation" src="[url]" frameborder="0" style="border:none;overflow:auto;[style]" allowTransparency="true"></iframe>' ,
	paddingW : 60 ,
	paddingH : 80 ,
	fixedWidth : 0 ,
	
	setFixedWidth : function( width ) {
		
		this.fixedWidth = width ;
		
	} ,
	
	getFrameWidth : function() {
		
		if ( this.fixedWidth > 0 ) return this.fixedWidth ;
				
		return $('#form_widget_frame').contents().find(".widget_wrapper").eq(0).outerWidth() + this.paddingW;
		
	} ,
	
	getFrameHeight : function() {
		
		return $('#form_widget_frame').contents().find(".widget_wrapper").eq(0).outerHeight() + this.paddingH;
		
	} ,
	
	adjustFrame : function( ) {
		
		var frame = $('#form_widget_frame');		
		var width = this.getFrameWidth();
		var height = this.getFrameHeight();
		
		if ( this.fixedWidth > 0 ) {
			width = this.fixedWidth ;
		}
		
		frame.css('width',width);
		frame.css('height',height);
		
		this.updateCode();
		
	} ,
	
	updateTitle : function( title ) {
		
		// update it in widget
		$('#form_widget_frame').contents().find('#widget_title_content').text(title);
		
		// store it
		$('#form_widget_frame').data('title',title);
		
		this.updateCode();
		
	} ,
	
	updateWidth : function( width, min_width ) {
		
		width = parseInt( width );
		if ( isNaN( width ) ) return ;
		if ( width < min_width ) width = min_width ;
		
		$('#form_widget_frame').css('width',width);
		$('.iframe_preview_bg').css('width',width);
		
		this.setFixedWidth( width );
		this.updateCode();
		
	} ,
	
	updateCode : function() {
		
		var code = this.codePattern ;
		
		// build url
		var url = $('#form_widget_frame').attr('src');		
		var keys = [ 'title', 'width' ];
		url = foo_util.removeKeysFromURL( url, keys );
		
		var title = $('#form_widget_frame').data('title');
		url += '&title=' + encodeURIComponent(title);
		
		// build styles
		var width = this.getFrameWidth();
		var height = this.getFrameHeight();
		var style = 'width:'+width+'px;height:'+height+'px;';		
		url += '&width=' + width ;
		
		// build code
		code = code.replace( '[url]', url );
		code = code.replace( '[style]', style );
		
		// update
		$('#form_widget_code').text( code );
		
	}
	
}

jQuery(document).ready(function() {

	/* Form Validation Listeners */
	$('form .email').blur( function() { foo_form.validate( 'email', $(this).val(), this ); });
	$('form .name').blur( function() { foo_form.validate( 'name', $(this).val(), this ); });	
	$('form .password').blur( function() { foo_form.validate( 'password', $(this).val(), this ); });
	$('form .confirm').blur( function() { foo_form.validateConfirm( $(this).val(), this ); });
	$('form .url').blur( function() { foo_form.validate( 'url', $(this).val(), this ); });	
	$('form .int').blur( function() { foo_form.validateNumber( $(this).val(), this ); });
	$('form .dec').blur( function() { foo_form.validateNumber( $(this).val(), this ); });
	/* $('form .string').blur( function() { foo_form.validate( 'string', $(this).val(), this ); }); */
	$('form .required').blur( function() { foo_form.validateRequired( $(this).val(), this ); });
	$('form .max_check').keyup( function() { foo_form.validateMax( this ); });
	$('form .max_check').each( function() { foo_form.validateMax( this ); });
	
	/* datepicker */
	$( ".datepicker" ).datepicker();
	
	/* html area */
	$('.htmlarea').htmlarea({
		toolbar: [ "html" , "|" , "bold", "p" ,"link", "unlink", "unorderedlist", "orderedlist" ]
	});
	
	/* input focus */
	$('.focus').each( function(){
		var default_val = $(this).data('default');
		$(this).val(default_val);
	});
	$('.focus').focus( function(){
		var default_val = $(this).data('default');
		var val = $(this).val();
		if ( val==default_val ) $(this).val('');
	} );
	$('.focus').blur( function(){
		var default_val = $(this).data('default');
		var val = $(this).val();
		if ( val=='' ) $(this).val(default_val);	
	} );
	
	/* forms */
	$('.form').submit( function(e) {
		var id = $(this).attr("id");
		var callback = null;
		if ( $(this).data("callback") != undefined )
			callback = eval( $(this).data("callback") );
		e.preventDefault();
		foo_form.send( id, callback );
	});
	
	/* form submit link */
	$('.form_submit_link').click( function(e) {
		e.preventDefault(); 
		var form = $(this).attr('href');
		$(form).submit();
	});
	
	/* toggle rows */
	$('.toggle').change( function() {
		var value = $(this).val();
		$('.toggle_row').css('display','none');
		$('.row_'+value).css('display','');
	});
	
	/* links */
	$('.div_link').click( function(){
		var url = $(this).data('href');
		foo_util.redirect( url );
	});	
	$('.div_link_new').click( function(){
		var url = $(this).data('href');
		var title = $(this).attr('title');
		foo_util.redirect( url, 0, true, title );
	});
	
	/* lightbox */
	$('.lightbox_link').click( function(e) {
		e.preventDefault();
		var el = $(this).attr('href');
		foo_form.showForm( el );
	});
	
	/* donation stuff */
	$('#donation_amount_other').focus( function(){
		$('#donation_amount_other_radio').attr('checked','checked');
	});
	$('#form_donation_country').change( function() {
		sw_donation.updateState();
	});
	$('#donation_amount_other').blur( function() {
		var min_value = parseFloat( $(this).data('min') );
		var max_value = parseFloat( $(this).data('max') );
		var value = parseFloat( $(this).val() );
		if ( isNaN( value ) ) alert('this value must be a number');
		else if ( value < min_value ) alert('donation must be at least $'+min_value);
		else if ( value > max_value ) alert('donation must be under $'+max_value);
	});
	$('#form_donation_card_id').change( function(){
		var value = $(this).val();
		if ( value <= 0 ) {
			$('#form_donation_card_rows').css('display','');
		} else {
			$('#form_donation_card_rows').css('display','none');
		}
	});

});

/* declare & init */
function fooUtil(){}
function fooCookie(){}
function fooAjax(){}
function fooForm(){}
function fooMisc(){}
function swCampaign(){}
function swComment(){}
function swDonation(){}
function swItem(){}
function swWidget(){}
function swLightbox(){}
var foo_util = new fooUtil();
var foo_cookie = new fooCookie();
var foo_ajax = new fooAjax();
var foo_form = new fooForm();
var foo_misc = new fooMisc();
var sw_campaign = new swCampaign();
var sw_comment = new swComment();
var sw_donation = new swDonation();
var sw_item = new swItem();
var sw_lightbox = new swLightbox();
var sw_widget = new swWidget();
