/* 
	Title:		General JS
	Author:		Heston Roberts
	Date:		March 2, 2011
	Notes:
*/

// Set Navigation font family to specific Sacala Sans LF 
Cufon.replace('.scala_ff', {hover:true, fontFamily:'Scala Sans'});

function searchSuppliers( ){
	
	// remove any click events just in case
	$$( '.supplier_letter' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.supplier_letter' ).addEvent( 'click', function( event ) {
		
		letter = this.get( 'title' );
		
		var myRequest = new Request.HTML({
			method:		'get',
			url:		'ajax.html&ajax=search suppliers&letter=' + letter,
			update: 	'supplier_listings',
			onRequest: 	function ( ) {
				
				$( 'supplier_listings' ).set( 'html', '<img src="images/loading_lg.gif" width="220" height="19">' );
				
			},
			onSuccess:	function( html ) {
				
				initiate( );
				
			},
			onFailure:	function( ) {
				
			}
		}).send( );
		
		event.stop( );
	
	});

};

function get_started( ){
	
	// remove any click events just in case
	$( 'get_started_btn' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$( 'get_started_btn' ).addEvent( 'click', function( event ) {

		// clear all errors
		$$( '.field_error' ).removeClass( 'field_error' );

		// validate the form
		var myRequest = new Request.JSON({
			method:		'post',
			url:		'ajax.html&ajax=get_started',
			onRequest: 	function ( ) {
				
				$( 'get_started_btn_container' ).set( 'html', '<img src="images/loading_sm_get_started.gif">' );
				
			},
			onSuccess:	function( json ) {
				
				var valid = 1;
				
				if( json.validName == 0 ){
					
					$( 'name' ).addClass( 'field_error' );
					valid = 0;
					
				};
				
				if ( json.validEmail == 0 ) {
				
					$( 'email' ).addClass('field_error');
					valid = 0;
					
				};
				
				if ( json.validPromo == 0 ) {
				
					$( 'promo_code' ).addClass( 'field_error' );
					$( 'promo_code' ).value = 'Invalid Promo Code...';
					valid = 0;
					
				};
				
				if( valid ){
					
					$( 'Form_Get_Started' ).submit( );
					
				} else {
				
					$( 'get_started_btn_container' ).set( 'html', '<a href="#" id="get_started_btn"><img src="images/get_started_large_btn.png" width="162" height="41"></a>');
					initiate();	
					
				}
				
			},
			onFailure:	function( ) {
				
				$( 'get_started_btn_container' ).set( 'html', '<a href="#" id="get_started_btn"><img src="images/get_started_large_btn.png" width="162" height="41"></a>');
				
			}
		}).send( $( 'Form_Get_Started' ) );

		/*
		var valid = form_validation( );
		
		if (valid) {
		
			$( 'Form_Get_Started' ).submit( );
		
		};
		*/
		
		event.stop( );
	});
	
}

function nextStep( ){
	
	// remove any click events just in case
	$$( '.next_step' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.next_step' ).addEvent( 'click', function( event ) {
		
		event.stop();
		
		theme = this.id;
		step = this.get( 'alt' );

		if( $( 'content_header' ) ) {
			var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
		};
		
		var myRequest = new Request.HTML({
			method:		'get',
			url:		'ajax.html&ajax=step&step=' + step + '&theme=' + theme + '&noCache=',
			update: 	'content_header',
			noCache:	true,
			onRequest: 	function ( ) {
				
				$( 'content_header' ).addClass( 'overlay' );
				$( 'loader' ).removeClass( 'hide' );
				
			},
			onSuccess:	function( html ) {
				
				initiate( );
				
				$( 'content_header' ).removeClass( 'overlay' );
				$( 'loader' ).addClass( 'hide' );
				
			},
			onFailure:	function( ) {
				
			}
		}).send( );
		
		
	});
	
};

function personalizeSite( ){
	
	// remove any click events just in case
	$$( '.personalize_site' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.personalize_site' ).addEvent( 'click', function( event ) {

		event.stop();
		
		if( $( 'content_header' ) ) {
			var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
		};		
		
		var myRequest = new Request.HTML({
			method:		'post',
			url:		'ajax.html&ajax=personalize_site&noCache=',
			update: 	'content_header',
			noCache:	true,
			onRequest: 	function ( ) {
				
				$( 'content_header' ).addClass( 'overlay' );
				$( 'loader' ).removeClass( 'hide' );
				
			},
			onSuccess:	function( html ) {
				
				initiate( );
				
				$( 'content_header' ).removeClass( 'overlay' );
				$( 'loader' ).addClass( 'hide' );
				
			},
			onFailure:	function( ) {
				
			}
		}).send( $( 'personalization' ) );
			
	});
	
};

function changeColor( ){
	
	// remove any click events just in case
	$$( '.change_color' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.change_color' ).addEvent( 'click', function( event ) {
		
		event.stop();
		
		theme = this.id;
		
		if( $( 'content_header' ) ) {
			var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
		};
		
		var myRequest = new Request.HTML({
			method:		'get',
			url:		'ajax.html&ajax=change_color&theme=' + theme,
			update: 	'color_container',
			onRequest: 	function ( ) {
				
				$( 'content_header' ).addClass( 'overlay' );
				$( 'loader' ).removeClass( 'hide' );
				
			},
			onSuccess:	function( html ) {
				
				initiate( );
				
				$( 'content_header' ).removeClass( 'overlay' );
				$( 'loader' ).addClass( 'hide' );
				
				// change the chosen thumb
				$$( '.thumb' ).removeClass( 'chosen_theme_color' );
				$( theme + '_container' ).addClass( 'chosen_theme_color' );
				
			},
			onFailure:	function( ) {
				
			}
		}).send( );
	
	});
	
};

function notifyClose( ){
	
	// remove any click events just in case
	$$( '.notify_close' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.notify_close' ).addEvent( 'click', function( event ) {
		
		var slideOut = new Fx.Slide( 'notify' ).slideOut( );
		
		event.stop();
		
	});
	
};

function changeAgencyTabs( ){
	
	// remove any click events just in case
	$$( '.agency_tabs' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.agency_tabs' ).addEvent( 'click', function( event ) {
		
		event.stop();
		
		// hide all tabs
		$$( '.mini_panel' ).addClass( 'hide' );
		
		// show this tab
		$( this.get( 'alt' ) ).removeClass( 'hide' );
		
		//show upload if agency tab
		if( this.get( 'alt') == 'agency_info' ){
			
			$( 'agency_uppload_logo' ).removeClass( 'hide' );
			
		};
		
		// change the active status
		$$( '.agency_tabs' ).getParent( ).removeClass( 'active' );
		$$( '.' + this.get( 'alt' ) + '_tab' ).addClass( 'active' );
		
		// scroll back to the top of the panel	
		if( $( 'content_header' ) ) {
			var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
		};
		
	});
	
};

function swapStep3Tabs( ){

	// remove any click events just in case
	$$( '.step_3_tabs' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.step_3_tabs' ).addEvent( 'click', function( event ) {

		event.stop();
		
		// find the tab currently active
		var tabs = $( 'mini_nav_top' ).getChildren( );
		
		tabs.each( function( el ){
			
			if( el.hasClass( 'active' ) ){
				
				makeActive = el.getNext( );
				
			};
			
		});
		
		if( makeActive != null ){
	
			// hide all tabs
			$$( '.mini_panel' ).addClass( 'hide' );
			
			// show the correct tab
			$( makeActive.getFirst( '.agency_tabs' ).get( 'alt' ) ).removeClass( 'hide' );
			
			// change the active tab
			$$( '.tabs' ).removeClass( 'active' );
			$$( '.' + makeActive.getFirst( '.agency_tabs' ).get( 'alt' ) + '_tab' ).addClass( 'active' );
			
			// scroll back to the top of the panel
			if( $( 'content_header' ) ) {
				var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
			};
			
		} else {	
		
			if( $( 'content_header' ) ) {
				var myFxScroll = new Fx.Scroll( window ).toElement( 'content_header' );	
			};
			
			var myRequest = new Request.HTML({
				method:		'post',
				url:		'ajax.html&ajax=personalize_site&noCache=',
				update: 	'content_header',
				noCache:	true,
				onRequest: 	function ( ) {
					
					$( 'content_header' ).addClass( 'overlay' );
					$( 'loader' ).removeClass( 'hide' );
					
				},
				onSuccess:	function( html ) {
					
					initiate( );
					
					$( 'content_header' ).removeClass( 'overlay' );
					$( 'loader' ).addClass( 'hide' );
					
				},
				onFailure:	function( ) {
					
				}
			}).send( $( 'personalization' ) );
			
		};
		
	});
	
};

function plan_pricing( ){
	
	var planSavings;
	var selectedPlan = "starter";
	var selectedPlanTerm = "monthly";
	var baseStarterPrice = "17.95";					
	var baseAdvantagePrice = "29.95";
	var basePremiumPrice = "49.95";					
	var selectedPlanTermNum = 1;
	var semiannualDiscount = .10;
	var annualDiscount = .15;
	var setPackagePlanStatus = true;
	var submenuSelectedStatus = false;
	
	function setPackagePlan(selectedPlan){
		$$(".price_chart.top .col, .price_chart.mid .col").removeClass('selected');
		
		if(!submenuSelectedStatus){
			$$(".core_features").addClass("hide_menu");							
		} else {
			submenuSelectedStatus = false;
		}
								
		$$(".price_chart.top .col." + selectedPlan + ", .price_chart.mid .category_col." + selectedPlan + ", .price_chart.mid .col." + selectedPlan).addClass('selected');
								
		$$(".pricing_options li").removeClass("selected");
		$$(".pricing_options li.monthly").addClass("selected");
		selectedPlanTerm = "monthly";
		selectedPlanTermNum = 1;
		Cufon.refresh();
		setPackagePlanStatus = false;
		setPackagePlanPricing();
	}
	
	function setPackagePlanPricing(){						
		switch(selectedPlan){
			case "starter":
				basePlanPrice = parseFloat(baseStarterPrice);
				break;
			case "advantage":
				basePlanPrice = parseFloat(baseAdvantagePrice);
				break;
			case "premium":	
				basePlanPrice = parseFloat(basePremiumPrice);
				break;				
		}
		
		basePrice = basePlanPrice;
		
		if(selectedPlanTerm == "semi-annual"){
			planSavings = semiannualDiscount;
			savingFrequency = 'semi-annually';
		} else if(selectedPlanTerm == "annual"){
			planSavings = annualDiscount;
			savingFrequency = 'annually';
		} else {
			planSavings = 1;
			savingFrequency = 'monthly';
		}
					
		if(planSavings != 1){	
			discountAmount = (basePlanPrice * selectedPlanTermNum) * planSavings;	
			basePlanPrice = (basePlanPrice * selectedPlanTermNum) - discountAmount;
		} else {
			basePlanPrice = basePlanPrice * selectedPlanTermNum;
		}
		
		approxPrice = basePlanPrice / selectedPlanTermNum;
		approxPrice = approxPrice.toFixed(2);
		
		monthlyTotal = basePrice * selectedPlanTermNum;
		savingValue = monthlyTotal - basePlanPrice;
		savingValue = savingValue.toFixed(2);
		
		basePlanPrice = basePlanPrice.toFixed(2);
		basePlanPriceStr = basePlanPrice.toString();						
		basePlanPriceArr = basePlanPriceStr.split(".");
		
		if (savingFrequency == 'monthly') {
		
			$$(".approx").addClass( 'hide' );
			$$(".pricing .value").set('html', '<strong>$</strong>' + basePlanPriceArr[0] + '<strong>' + basePlanPriceArr[1] + '</strong>');
		
		} else {
		
			$$(".approx").removeClass( 'hide' );
			$$(".per_month_details .value").set('html', '$' + approxPrice);
			$$(".pricing .value").set('html', '<strong>$</strong>' + basePlanPriceArr[0] + '<strong>' + basePlanPriceArr[1] + '</strong>');
			
		};

		if( savingValue > 0 ){
		
			$( "pricing_plan_savings" ).removeClass( "hide" );
				
		} else {
				
			$( "pricing_plan_savings" ).addClass( "hide" );
			
		};
				
		$$(".pricing_plan_details .value").set('html', '$' + savingValue);
		$$(".pricing_plan_details #pricing_plan_frequency").set('html', savingFrequency);
		Cufon.refresh();
	}
	
	$$(".select_plan_btn").addEvent("click", function(event){
		event.preventDefault();
		selectedPlan = this.getProperty('alt');
		setPackagePlan(selectedPlan);
		// add value to form
		$( 'chosenPlan' ).value = selectedPlan;
		$( 'chosenFrequency' ).value = 'monthly';
		// scroll to options
		var myFxScroll = new Fx.Scroll( window ).toElement( 'pricing_options_chart' );
	});
	
	$$(".cf_link_btn").addEvent("click", function(event){
		event.preventDefault();
		
		if(this.getParent(".core_features").hasClass('hide_menu')){
			$$(".core_features").addClass("hide_menu");
			
			this.getParent(".core_features").removeClass("hide_menu");
		} else {
			$$(".core_features").addClass("hide_menu");
		}
		
		selectedPlan = this.getProperty('alt');
		submenuSelectedStatus = true;
		
		if(setPackagePlanStatus){
			setPackagePlan(selectedPlan);
		} else {
			setPackagePlanStatus = true;
		}
	});
	
	$$(".pricing_options li").addEvent("click", function(event){
		$$(".pricing_options li").removeClass("selected");
		this.addClass("selected");
		
		if(this.hasClass("monthly")){
			selectedPlanTerm = "monthly";
			selectedPlanTermNum = 1;
		} else if(this.hasClass("semi-annual")){
			selectedPlanTerm = "semi-annual";
			selectedPlanTermNum = 6;
		} else if(this.hasClass("annual")){
			selectedPlanTerm = "annual";
			selectedPlanTermNum = 12;
		}
		
		$( 'chosenFrequency' ).value = selectedPlanTerm;
		
		setPackagePlanPricing(); 
	});
					
};


/*
	ImageUploader - Handles functionality for uploading images through the control panel
*/
var ImageUploader = new Class({
	
	options: {
		
		// Set up available options
		LogoFileInput:			'logoFile',							// The ID of the file uploader for the logo in the agency control panel
		UploadLogoBtn: 			'upload_logo_button',				// The button element that starts the upload process
		UploadLogoForm: 		'upload_logo_form',					// The form element submitting the logo
		SiteLogo:				'SiteLogo',							// The Site Logo image id
		SecondsPerRequest:		3,									// The number of seconds per request
		MaxNumberOfRequests:	10,									// The max number of requests to attempt
		UploadButtonPath:		'/images/upload_logo_btn.png',		// The relative path to the upload button
		LoadingButtonPath:		'/images/loading_sm.gif'			// The relative path to the loafing icon
	},
	
	initialize: function( options ) {
	
		this.LogoFileInput 			= $( this.options.LogoFileInput );
		this.UploadLogoBtn 			= $( this.options.UploadLogoBtn );
		this.UploadLogoForm 		= $( this.options.UploadLogoForm );
		this.SiteLogo 				= $( this.options.SiteLogo );
		this.SecondsPerRequest		=    this.options.SecondsPerRequest * 1000;
		this.MaxNumberOfRequests	=    this.options.MaxNumberOfRequests;
		this.UploadButtonPath		=    this.options.UploadButtonPath;
		this.LoadingButtonPath		=    this.options.LoadingButtonPath;
		this.RequestCounter			= 0;
		
		this.LogoRequest 			= new Request.JSON({
										url:			'/ajax.html&ajax=validate_uploaded_photo&',
										noCache:		true,
										initialDelay:	1000,
										delay:			this.SecondsPerRequest,
										onComplete:		function() {
															this.RequestCounter++;
															if( this.RequestCounter >= this.MaxNumberOfRequests ) {
																this.RequestCounter = 0;
																this.LogoRequest.stopTimer();
																this.LogoFileInput.removeClass( 'hide' );
																this.UploadLogoBtn.setProperty( 'src', this.UploadButtonPath );
																alert( 'Image upload failed.\n\nIt\'s possible that:\n\n\t- The image is an unsupported format (.jpg, .gif and .png only)\n\t- The image is too large\n\t- The image is corrupted\n\t- The request timed-out\n\nYou can:\n\n\t- Try again\n\t- Try a new image\n\t- Contact a site administrator' );
															};
														}.bind( this ),
										onSuccess:		function( json ) {
															if( json.result != '' ) {
																this.SiteLogo.setProperty( 'src', json.result + '?rn=' + Math.floor( Math.random() * 999 ) );
																this.RequestCounter = 0;
																this.LogoRequest.stopTimer();
																this.LogoFileInput.removeClass( 'hide' );
																this.UploadLogoBtn.setProperty( 'src', this.UploadButtonPath );
															};
														}.bind( this )
									});
		
		// Add file input change event
		this.LogoFileInput.addEvent( 'change', function( event ){
			this.validateFile();
		}.bind( this ) );
		
	},
	
	validateFile: function() {
		if( this.LogoFileInput.value != '' ) {
			this.UploadLogoBtn.setProperty( 'src', this.LoadingButtonPath );	// show the loading icon	
			this.LogoFileInput.addClass( 'hide' ); 								// remove the file uploader (no duplicate input)
			this.UploadLogoForm.submit(); 										// submit the form
			this.LogoRequest.startTimer(); 										// start the request
		};
		
	}

});
ImageUploader.implement( new Options );


function addNewUser( ){
	
	// execute the clicked event	
	$( 'add_new_user' ).addEvent( 'click', function( event ){

		// clone the form fields
		var clone = $( 'prototype_user_row' ).clone( ).inject( 'admin_users', 'bottom' );
		
		event.stop( );
		
	});
	
};


function showDomains( ){
	
	// execute the clicked event	
	$( 'assign_domains' ).addEvent( 'click', function( event ){

		if( this.checked ){
			
			$( 'show_domains' ).removeClass( 'hide' );
			
		} else {
			
			$( 'show_domains' ).addClass( 'hide' );
			
		};
		
	});
	
};

function addNewDomain( ){
	
	// execute the clicked event	
	$( 'add_new_domain' ).addEvent( 'click', function( event ){

		// clone the form fields
		var clone = $( 'prototype_domain_name' ).clone( ).inject( 'my_domains', 'bottom' );
		
		event.stop( );
		
	});
	
};

function checkDefaultDomain( ){
	
	// remove any click events just in case
	$( 'default_site_address' ).removeEvents( 'blur' );
	$( 'start_trial' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$( 'default_site_address' ).addEvent( 'blur', function( event ) {
		
		var field = this;
		var name = this.value;
		
		// remove any non-alphanumeric characters
		name = name.replace(/[^a-zA-Z0-9-]+/g,'');
		
		var myRequest = new Request.HTML({
			method:		'get',
			url:		'ajax.html&ajax=check_default_site&name=' + name,
			update: 	'notice',
			onRequest: 	function ( ) {

				field.value = name;
				
				$( 'notice' ).set( 'html', '<img src="images/loading_sm.gif">Checking availability...' );
				
			},
			onSuccess:	function( html ) {
				
				initiate( );
				
			},
			onFailure:	function( ) {
				
			}
		}).send( );
		
		event.stop( );
	
	});
	
	// execute the clicked event	
	$( 'start_trial' ).addEvent( 'click', function( event ) {

		// validate form fields
		var valid = form_validation( );

		if ( valid ) {

			var field = $('default_site_address');
			var name = field.value;
			
			// remove any non-alphanumeric characters
			name = name.replace(/[^a-zA-Z0-9-]+/g, '');
			
			var jsonRequest = new Request.JSON({
				url: 'ajax.html&ajax=check_default_site&result=json&name=' + name,
				onRequest: function( ){

					$( 'get_started_action' ).addClass( 'hide' );
					$( 'content_header' ).addClass( 'opaque' );
					//$( 'billing_notification' ).set( 'html', '' );
					//$( 'billing_notification' ).addClass( 'hide' );
					
				},
				onSuccess: function(json){

					field.value = name;
					
					if (json.result == 1) {
						
						/*
						// validate card
						var myRequest = new Request.JSON({
							method:		'post',
					 		url:		'ajax.html&ajax=validate_card',
					 		onSuccess:	function( json ) {
					 			 
					 			if ( json.valid == -1 ) {
								
									$( 'billing_notification' ).set( 'html', 'There was an error connecting to the credit card verification service. Please try submitting this form again.' );
									
								} else if ( json.valid == 2 || json.valid == 3 || json.valid == 4 ){
	
									$( 'billing_notification' ).set( 'html', 'There was an error processing your card: ' + json.message + ' Please try again.' );
									
								}
								
								if (json.valid != 1) {
								
									$( 'billing_notification' ).removeClass( 'hide' );
									$('get_started_action').removeClass( 'hide' );
									$( 'content_header' ).removeClass( 'opaque' );
									
								} else {
									
									$( 'content_header' ).removeClass( 'opaque' );
									$( 'builder' ).addClass( 'hide' );
									$( 'building' ).removeClass( 'hide' );
									$( 'review' ).submit( );
									
								}
					 
					 		},
							onFailure: function( ){
								
								$( 'get_started_action' ).removeClass( 'hide' );
								$( 'content_header' ).removeClass( 'opaque' );
								
							}
					 	}).send( $( 'review' ) );
					 	*/
						
						$( 'content_header' ).removeClass( 'opaque' );
						$( 'builder' ).addClass( 'hide' );
						$( 'building' ).removeClass( 'hide' );
						$( 'review' ).submit( );

					} else {
					
						$( 'get_started_action' ).removeClass( 'hide' );
						$( 'content_header' ).removeClass( 'opaque' );
						//$( 'billing_notification' ).set( 'html', 'The default site address you are trying to use is already in use by another Agent Studio site. Please try a different default domain.' );
						//$( 'billing_notification' ).removeClass( 'hide' );
						
					}
					
				},
				onFailure: function( ){

					$( 'get_started_action' ).removeClass( 'hide' );
					$( 'content_header' ).removeClass( 'opaque' );
					
				}
			}).get();
			
		};

		event.stop( );
	
	});
	
};

function sameAs( ){
	
	// remove any click events just in case
	$( 'sameAs' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$( 'sameAs' ).addEvent( 'click', function( event ) {
		
		if( this.checked ){

			var jsonRequest = new Request.JSON({
				url:		'ajax.html&ajax=sameAs',
				onSuccess:	function( json ) {
					
					// update the form fields with the stored values
					$( 'billing_address' ).value 		= json.address;
					$( 'billing_city' ).value 			= json.city;
					$( 'billing_region' ).value 		= json.region;
					$( 'billing_postal_code' ).value 	= json.postal_code;
					$( 'billing_phone' ).value 			= json.phone;
					$( 'billing_email' ).value 			= json.email;
					
				}
			}).get( );
			
		};		
		
	});
	
};

function expandDestinations( ){
	
	
	// remove any click events just in case
	$$( '.chosen_destinations' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.chosen_destinations' ).addEvent( 'click', function( event ) {

		var destination = this.value;
		
		if( this.checked ){

			$( 'destination_' + destination + '_children' ).removeClass( 'hide' );
			
		} else {

			$( 'destination_' + destination + '_children' ).addClass( 'hide' );
			
			// uncheck all the children
			$( 'destination_' + destination + '_children' ).getElements( '.chosen_destinations' ).each( function( el ){
				
				el.checked = false;
			
			});
			
		};
	
	});
		
};

function selectAllDestinations( ){
	
	// remove any click events just in case
	$$( '.select_all_destinations' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.select_all_destinations' ).addEvent( 'click', function( event ) {

		var destination = this.id;
		$( 'destination_' + destination + '_children' ).getElements( '.chosen_destinations' ).each( function( el ){

			$( 'destination_' + el.value + '_children' ).removeClass( 'hide' );
			
			el.checked = true;
			
		});
		
		event.stop( );
	
	});
	
};

function expandSuppliers( ){
	
	// ***** SUPPLIER CATEGORIES *****
	
	// remove any click events just in case
	$$( '.chosen_supplier_categories' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.chosen_supplier_categories' ).addEvent( 'click', function( event ) {

		var category = this.value;
		
		if( this.checked ){

			$( 'supplier_category_' + category + '_suppliers' ).removeClass( 'hide' );
			
		} else {

			$( 'supplier_category_' + category + '_suppliers' ).addClass( 'hide' );
			
			// uncheck all the children
			$( 'supplier_category_' + category + '_suppliers' ).getElements( '.chosen_suppliers' ).each( function( el ){
				
				el.checked = false;
			
			});
			
		};
	
	});
	
	
	// ***** SUPPLIERS *****
	
	// remove any click events just in case
	$$( '.chosen_suppliers' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.chosen_suppliers' ).addEvent( 'click', function( event ) {

		var supplier = this.value;

		if( this.checked ){
			
			$( 'supplier_' + supplier + '_children' ).removeClass( 'hide' );
			
		} else {

			$( 'supplier_' + supplier + '_children' ).addClass( 'hide' );
			
			// uncheck all the children
			$( 'supplier_' + supplier + '_children' ).getElements( '.chosen_suppliers' ).each( function( el ){
				
				el.checked = false;
			
			});
			
		};
	
	});
		
};

function selectAllSuppliers( ){
	
	// ***** SUPPLIER CATEGORIES *****
	
	// remove any click events just in case
	$$( '.select_all_category_suppliers' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.select_all_category_suppliers' ).addEvent( 'click', function( event ) {

		var category = this.id;

		$( 'supplier_category_' + category + '_suppliers' ).getElements( '.chosen_suppliers' ).each( function( el ){

			$( 'supplier_' + el.value + '_children' ).removeClass( 'hide' );
			
			el.checked = true;
			
		});
		
		event.stop( );
	
	});
	
	
	// ***** SUPPLIERS *****
	
	// remove any click events just in case
	$$( '.select_all_suppliers' ).removeEvents( 'click' );
	
	// execute the clicked event	
	$$( '.select_all_suppliers' ).addEvent( 'click', function( event ) {

		var supplier = this.id;

		$( 'supplier_' + supplier + '_children' ).getElements( '.chosen_suppliers' ).each( function( el ){

			$( 'supplier_' + el.value + '_children' ).removeClass( 'hide' );
			
			el.checked = true;
			
		});
		
		event.stop( );
	
	});
	
};

function choosePackage( ){
	
	// execute the clicked event	
	$$( '.package_type' ).addEvent( 'click', function( event ){

		// clone the form fields
		$( 'site_package' ).value = this.get( 'title' );
		
		event.stop( );
		
	});
	
};

function showToolTips( ){
	
	//store titles and text
	$$( 'a.tipz' ).each( function( element, index ){
		
		var content = element.get( 'title' ).split( '::' );
		element.store( 'tip:title', content[0] );
		element.store( 'tip:text', content[1] );

	});
							
	//create the tooltips
	var tipz = new Tips( '.tipz',{
	
		className: 	'tipz',
		//fixed:	 	true,
		hideDelay: 	50,
		showDelay: 	50
	
	});
	
	/*				
	//fading effect
	tipz.addEvents({
	
		'show': function(tip) {
			
			tip.fade('in');
			
		},
	
		'hide': function(tip) {
	
			tip.fade('out');
							
		}
	
	});
	*/
	
};

function initiate( ){
	
	if( $$( '.supplier_letter' ) ) {
	
		searchSuppliers( );
		
	};
	
	if( $$( '.next_step' ) ) {
		
		nextStep( );
		
	};
	
	if( $$( '.notify_close' ) ) {
		
		notifyClose( );
		
	};
	
	if( $$( '.change_color' ) ){
		
		changeColor( );
		
	};
	
	if( $$( '.agency_tabs' ) ){
		
		changeAgencyTabs( );
		
	};
	
	if( $( 'upload_logo_form' ) ){
		
		myUploadLogo = new ImageUploader();
		
	};
	
	if( $$( '.personalize_site' ) ){
		
		personalizeSite( );
		
	};
	
	if( $( 'add_new_user' ) ){
		
		addNewUser( );
		
	};
	
	if( $( 'assign_domains' ) ){
		
		showDomains( );
		
	};
	
	if( $( 'add_new_domain' ) ){
		
		addNewDomain( );
		
	};
	
	if( $( 'default_site_address' ) ){
		
		checkDefaultDomain( );
		
	};
	
	if( $( 'sameAs' ) ){
		
		sameAs( );
		
	};
	
	if( $$( '.chosen_destinations' ) ){
		
		expandDestinations( );
		
	};
	
	if( $$( '.select_all_destinations' ) ){
		
		selectAllDestinations( );
		
	};
	
	if( $$( '.chosen_supplier_categories' ) ){
		
		expandSuppliers( );
		
	};
	
	if( $$( '.select_all_category_suppliers' ) ){
		
		selectAllSuppliers( );
		
	};
	
	if( $$( '.step_3_tabs' ) ){

		swapStep3Tabs( );
		
	};
	
	if( $( 'get_started_btn' ) ){
		
		get_started( );
		
	};
	
	if( $$( '.package_type' ) ){
		
		choosePackage( );
		
	};
	
	if ($$('.select_plan_btn')) {
	
		plan_pricing( );
		
	};
	
	if( $$( 'a.tipz' ) ){
		
		showToolTips( );
		
	};
	
	Mediabox.scanPage( );
	
};

window.addEvent( 'domready', function( ){
	
	initiate( );
	
});
