/*
 * Lightbox.js
 *
 * Controls lightbox functionality and contains functions for creating lightbox content
 *
 */

//create lightbox class
var Lightbox = new Class({
	'top_offset': 30,
	'fadetime': 270,	//time in milliseconds to fade in or out a lightbox

	//called when the lightbox class is instantiated
	'initialize': function(customClass){
		var lightbox = this;
		lightbox.locked = false;

		//create lightbox objects
		lightbox.dim = new Element('div', {'id': 'dim'});
		lightbox.lb = new Element('div', {'id': 'lightbox', 'class': customClass});
		lightbox.content = new Element('div', {'id': 'lightbox_content'});

		lightbox.close = new Element('div', {'id': 'lightbox_close', 'events': {'click': function(){ lightbox.hide(); }}}).grab(
			new Element('img', {'src': baseUrl + 'images/lightbox_close.png'}).setStyles({'width': 66, 'height': 17})
		);

		lightbox.spacer = {
			'top': new Element('div', {'class': 'lightbox_spacer'}),
			'bottom': new Element('div', {'class': 'lightbox_spacer'})
		};

		//construct the lightbox, including rounded corners and close button
		lightbox.lb.adopt(
			new Element('div', {'id': 'lightbox_border_top', 'class': 'lightbox_border'}).adopt(
				new Element('div', {'class': 'lightbox_corner_nw lightbox_corner'}),
				lightbox.spacer.top,
				new Element('div', {'class': 'lightbox_corner_ne lightbox_corner'}),
				lightbox.close
			),
			lightbox.content,
			new Element('div', {'id': 'lightbox_border_bottom', 'class': 'lightbox_border'}).adopt(
				new Element('div', {'class': 'lightbox_corner_sw lightbox_corner'}),
				lightbox.spacer.bottom,
				new Element('div', {'class': 'lightbox_corner_se lightbox_corner'})
			)
		);

		//add an event to the window so that the lightbox and dimmer are repositioned to account for changes in window size
		window.addEvents({
			'resize': function(){
				lightbox.setCenter();
			}
		});

		//inject the dimmer and lightbox into the dom
		$(document.body).grab(lightbox.dim);
		$(document.body).grab(lightbox.lb);
	},

	'setCenter': function(){
		this.lb.morph({
			'left': (window.getSize().x - this.lb.getSize().x) / 2,
			'top': this.top_offset + window.getScroll().y
		});
	},

	'setup': function(data){
		this.lb.setStyles(data.lb);

		if ($chk(data.spacer)){
			this.setSpacer(data.spacer.width);
		}
		else {
			var width_int = data.lb.width.toInt();
			if (width_int){
				this.setSpacer(width_int + 44);
			}
		}

		if ($chk(data.close)){
			this.close.setStyles(data.close);
		}
	},

	'setSpacer': function(data){
		this.spacer.top.setStyle('width', data);
		this.spacer.bottom.setStyle('width', data);
	},

	//sets up the lightbox content so that it can be shown
	'show': function(args){
		if ( ! this.locked){
			this.locked=true;

			//empty any old lightbox content still in the lightbox
			this.content.empty();

			//choose the model to populate the lightbox with
			if (args.mode.substring(0,6) == 'upload'){
				this['upload']();
			}
			else if (args.mode.substring(0,6) == 'voting'){
				this['voting']();
			}
			else if (args.mode.substring(0,5) == 'login'){
				this['login']();
			}
			else if (args.mode.substring(0,8) == 'register'){
				this['signup']();
			}
			else {
				this[args.mode](args.data);
			}
		}
	},

	//does the actual showing of the lightbox
	'update_lb': function(el, foc){
		this.content.grab(el);

		//set the dimmer to fill the screen, show itself, and have transparency
    var document_height = Math.max(
      Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
      Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
      Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );

		this.dim.setStyles({
			'width': window.getScrollSize().x,
			'height': document_height,
			'display': 'block',
			'opacity': .7
		});

		//set the lightbox to be centered horizontally, a set amount down the screen vertically, and transparent
		this.lb.setStyles({
			'left': (window.getSize().x - this.lb.getSize().x) / 2,
			'top': this.top_offset + window.getScroll().y,
			'opacity': 0,
			'visibility': 'visible'
		});

		//fade in the lightbox and unlock it for future animation
		new Fx.Tween(this.lb, {'duration': this.fadetime}).start('opacity', '1').chain(function(){lb.locked = false;});

		if ($chk(foc)){
			foc.focus();
		}
	},

	//hide the lightbox
	'hide': function(){
		if ( ! this.locked){
			var lightbox = this;
			this.locked = true;

			//fade out the lightbox, hide the dimmer, and unlock animations
			new Fx.Tween(this.lb, {'duration': this.fadetime}).start('opacity', '0').chain(function(){lightbox.locked = false;});
			this.dim.setStyle('display', 'none');
		}
	},

	//set up close buttons
	'close_button': function(){
		$$('.close_button').addEvent('click', function(){
			lb.hide();
		});
	},


	/*
	 *
	 *=-=-=-=-=- Lightbox Model Functions -=-=-=-=-=
	 *
	 */


	/*=-=-=-=-=- Preview Lightbox -=-=-=-=-=*/

	'preview': function(data){

    // console.log(data);
		this.setup({
			'lb': {'width': 720, 'height': 'auto', 'min-height': 284, 	'padding': '30px 0px', 'background': '#fff' },
			'spacer': {'width': 704},
			'close': { 'top': 3, 'right': 3 }
		});


		// Ajax request to load add to cart form
		function load_add_to_cart()
		{
  		new Request({
  			'url': baseUrl + 'request/add_to_cart_lb/' +data.log_id.replace('/', '--') +'/' +data.shape +'/preview',
  			'onComplete': function(data){
          // Remove lock and loading
      		if (typeof ft != 'undefined')
      		{
      			ft.mode.locked = false;
      			ft.set_loading();
      		}

          // Update lightbox
          var el = new Element('div', {'html' : data});
  				lb.update_lb(el);

          // Set preview image to preloaded one
  				$$('#cart_form .preview_image').setProperty('src', im.src);

          // Change the background
          $$('#change_background a').addEvent('click', function(){
            var img_wrapper = $$('.img_wrapper')[0];
            img_wrapper.style.backgroundImage = this.style.backgroundImage;

            $$('#change_background a').removeClass('current');
            this.addClass('current');

            _gaq.push(['_trackEvent', 'Preview', 'Change Background', this.style.backgroundImage]);

            return false;
          });
  			}
  		}).send();
    }

    // If this is editor then lock editor
		if (typeof ft != 'undefined')
		{
  		ft.mode.locked = true;
  		ft.set_loading();
    }

		// Preload Image
		var im = new Image();
		im.onload = function(){
			load_add_to_cart(data);
		}

		//load the correct thumbnail based on if it needs to be built
		if (data.no_rebuild)
		{
			im.src = baseUrl + 'request/image/' + data.log_id + '/preview_save/' + new Date().getTime();
		}
		else
		{
			im.src = baseUrl + 'request/compile_image/' + data.log_id.replace('/', '--') + '/preview/' + new Date().getTime();
		}
	},


	/*=-=-=-=-=- Save Lightbox -=-=-=-=-=*/

	'save_process': function(data){
		this.setup({
			'lb': { 'width': 678, 'height': 'auto', 'min-height': 0, 'padding': '22px 30px' },
			'close': { 'top': 8, 'right': 8 }
		});

		var el = new Element('div', {'id': 'save_lb'});
		$(document.body).grab(el);

		//ajax request to save the image in memory, or request registration/login if necessary
		new Request.HTML({
			'url':baseUrl + 'request/save_lb',
			'update': 'save_lb',
			'onComplete': function(){
				lb.save_setup();
				lb.close_button();
				lb.update_lb(el, $('save_name'));
			}
		}).send();
	},

	'save_setup': function(){

		if($('save_row')){
			var save_proc = function(){
				//validate the save name if necessary and call the save function
				var name = $('save_name').get('value');
				if( ! name.length){
					name = 'Untitled';
				}
				ft.save({'step': 2, 'name': name});

				//wait for the save to finish and update the lightbox
				lb.lb.addEvent('saveFinished', function(){
					$('save_lb').empty().adopt(
						new Element('h1', {'text': 'Save Your Fracture'}),
						new Element('p', {'html': 'Your Fracture has been saved and added to your <a href="' + baseUrl +'my_fractures">"My Fractures"</a> page.'})
					)
				});
			}

			//save button is clicked
			$('save_button').addEvent('click', function(){
				save_proc();
			});

			if($('save_name')){
				$('save_name').addEvent('keydown', function(e){
					if(e.key == 'enter'){
						save_proc();
					}
				});
			}
		}
		else {
			//set up links that show the correct form based on if the user wants to login or register
			$$('.signup_link').addEvent('click', function(){
				$('signup_row').setStyle('display', 'block');
				$('login_row').setStyle('display', 'none');
				$('signup_email').focus();
			});

			$$('.login_link').addEvent('click', function(){
				$('signup_row').setStyle('display', 'none');
				$('login_row').setStyle('display', 'block');
				$('login_email').focus();
			});

			lb.login_setup('editor');
			lb.signup_setup('editor');
		}
	},


	/*=-=-=-=-=- Add to Cart Lightbox -=-=-=-=-=*/

	'add_to_cart': function(data)
	{
    // console.log(data);
		this.setup({
			'lb': {'width': 720, 'height': 'auto', 'min-height': 284, 	'padding': '30px 0px', 'background': '#fff' },
			'spacer': {'width': 704},
			'close': { 'top': 3, 'right': 3 }
		});


		// Ajax request to load add to cart form
		function load_add_to_cart()
		{
  		new Request({
  			'url': baseUrl + 'request/add_to_cart_lb/' +data.log_id.replace('/', '--') +'/' +data.shape +'/',
  			'onComplete': function(data){
          // Remove lock and loading
      		if (typeof ft != 'undefined')
      		{
      			ft.mode.locked = false;
      			ft.set_loading();
      		}

          // Update lightbox
          var el = new Element('div', {'html' : data});
  				lb.update_lb(el);

          // Set preview image to preloaded one
  				$$('#cart_form .preview_image').setProperty('src', im.src);


          // Change the background
          $$('#change_background a').addEvent('click', function(){
            var img_wrapper = $$('.img_wrapper')[0];
            img_wrapper.style.backgroundImage = this.style.backgroundImage;

            $$('#change_background a').removeClass('current');
            this.addClass('current');
            return false;
          });

          // Choose a size
          $$('#choose_a_size .size_option').addEvent('click', function()
          {
            // Set current item
            $$('#choose_a_size .size_option').removeClass('current_size');
            this.addClass('current_size');
            var _this = this;
            
            if (this.getElements('img').length > 0)
            {
              setTimeout(function(){
                $('choose_a_size').addClass('hide');
                $('image_confirmation').removeClass('hide');
              }, 150);
            }
            else
            {
              setTimeout(function(){
                $('choose_a_size').addClass('hide');
                $('choose_a_mount').removeClass('hide');
                $('path_size_options').removeClass('hide');
              }, 150);
            }
          });


          // Image confirmation
          $$('#choose_smaller_size').addEvent('click', function()
          {
            $('path_size_options').addClass('hide');
            $('choose_a_mount').addClass('hide');
            $('image_confirmation').addClass('hide');
            $('choose_a_size').removeClass('hide');
            return false;
          });

          $$('#confirm_image_size').addEvent('click', function()
          {
            $('choose_a_size').addClass('hide');
            $('image_confirmation').addClass('hide');
            $('choose_a_mount').removeClass('hide');
            $('path_size_options').removeClass('hide');
            return false;
          });


          // Choose a mount type
          $$('#choose_a_mount .mount_option').addEvent('click', function()
          {
            // Set current item
            $$('#choose_a_mount .mount_option').removeClass('current_mount');
            this.addClass('current_mount');

            // Show and hide correct elements
            setTimeout(function()
            {
              $('confirm_size').innerHTML = $$('#choose_a_size .size_option.current_size')[0].id.replace('size_', '');
              $('confirm_shape').innerHTML = $$('.preview_image')[0].alt.replace(/^\w/, function($0) { return $0.toUpperCase(); });
              $('confirm_mount').innerHTML = $$('.mount_option.current_mount')[0].title;
              
              $('path_size_options').addClass('hide');
              $('path_mount_options').removeClass('hide');
              $('choose_a_mount').addClass('hide');
              $('cart_confirm_div').removeClass('hide');
            }, 150);
          });

          // Choose a mount type on mouse over
          $$('#choose_a_mount .mount_option').addEvent('mouseover', function()
          {
            var el = this.id.replace('mount_', '') + '_' + $$('.preview_image')[0].alt.replace(/^\w/, function($0) {return $0;}) + '_' + $$('#choose_a_size .size_option.current_size')[0].id.replace('size_', '');

            $$('#' + el).removeClass('hide');
            //$$('#' + el).fadeToggle("slow", "linear");

            //alert(el );
          });
          
          // Choose a mount type on mouse out
          $$('#choose_a_mount .mount_option').addEvent('mouseout', function()
          {
            var el = this.id.replace('mount_', '') + '_' + $$('.preview_image')[0].alt.replace(/^\w/, function($0) {return $0;}) + '_' + $$('#choose_a_size .size_option.current_size')[0].id.replace('size_', '');
            
            $$('#' + el).addClass('hide');
            //$$('#' + el).fadeToggle("slow", "linear");


            //alert(el );
          });


          // Paths
          $$('#path_size_options').addEvent('click', function(){
            $('path_size_options').addClass('hide');
            $('choose_a_mount').addClass('hide');
            $('choose_a_size').removeClass('hide');
            return false;
          });

          $$('#path_mount_options').addEvent('click', function(){
            $('path_mount_options').addClass('hide');
            $('path_size_options').removeClass('hide');

            $('cart_confirm_div').addClass('hide');
            $('add_to_cart1').addClass('hide');
            $('cart_confirm').removeClass('hide');
            $('choose_a_mount').removeClass('hide');
            return false;
          });

          // Confirm button
          $$('#cart_confirm').addEvent('click', function()
          {
            $('cart_confirm').addClass('hide');
            $('add_to_cart1').removeClass('hide');
          });

          // Add to cart
          $$('#add_to_cart1').addEvent('click', function(){ add_to_cart_event(); return false; });

          // Add to cart done -> upload button          
          $$('#add_to_cart_upload').addEvent('click', function(){
              lb.close_button();
              lb.show({'mode': 'upload'});
              return false;
          });
  			}
  		}).send();
    }


    // Add to cart function 
    function add_to_cart_event()
    {
  	  log_id = data.log_id;
  	  
			var count = 0;
			var form_data = new Array();

			form_data[0] = {
			 'shape': data.shape,
			 'size': $$('#choose_a_size .size_option.current_size')[0].id.replace('size_', ''),
			 'mount': $$('#choose_a_mount .mount_option.current_mount')[0].id.replace('mount_', ''),
			 'file': log_id,
			 'qty': 1
			};

  		// Ajax request to execute the add to cart
      new Request({
          'url': baseUrl + 'request/add_to_cart/' + (data.shape ? data.shape : '0'),
          'onComplete': function(data){
            
            $('cart_confirm_div').addClass('hide');
            $('path_mount_options').addClass('hide');
            $('add_to_cart_done').removeClass('hide');            
          }
      }).send({'data': {'data': JSON.encode(form_data)}});
    }


		// Preload Image
		var im = new Image();
		im.onload = function(){
			load_add_to_cart(data);
		}

    // If this is editor then lock editor
		if (typeof ft != 'undefined')
		{
  		ft.mode.locked = true;
  		ft.set_loading();
    }

		//load the correct thumbnail based on if it needs to be built
		if (data.no_rebuild)
		{
			im.src = baseUrl + 'request/image/' + data.log_id + '/preview_save/' + new Date().getTime();
		}
		else
		{
			im.src = baseUrl + 'request/compile_image/' + data.log_id.replace('/', '--') + '/preview/' + new Date().getTime();
		}
	},


	/*=-=-=-=-=- Upload Lightbox -=-=-=-=-=*/

	'upload': function(){
		this.setup({ 'lb': { 'width': 448, 'min-height': 0, 'padding': '22px 30px', 'background': '#fff' } })

		var el = new Element('div', {'id': 'upload_lb'});
		$(document.body).grab(el);

		//load the upload lightbox content from a file
		new Request.HTML({
			'url':baseUrl + 'request/upload_lb',
			'update': 'upload_lb',
			'onComplete': function(){
				//add a hidden iframe to upload the image to and keep track of the progress bar status
				$('upload_form').grab(
					new Element('iframe', {
						'id': 'progress_iframe',
						'name': 'progress_iframe',
						'src': '/js/progress_bar.php'
					}).setStyle('display', 'none')
				);

				//hide the real upload form and set the styled box's text to copy the upload form's text
				$$('#upload_float').setStyles({
					'opacity': 0,
					'visibility': 'visible'
				}).addEvent('change', function(){
					$('browse_box').set('text', this.get('value'));
				});

				//move the real browse box underneath the mouse as the mouse moves through the styled upload form
				$('browse_box').addEvent('mousemove', function(e){
					var pos = $('lightbox').getCoordinates();
					this.getPrevious().setStyles({
						'right': pos.right - e.page.x - 30,
						'top': e.page.y - pos.top - 10
					});
				});
				/*
				//when the upload form is submitted, set up and start the progress bar
				$('upload_form').addEvent('submit', function(e){

					//construct the progress bar and insert it into the lightbox
					var p_bar = new Element('div', {'id': 'progress_bar'});
					var p_pcnt = new Element('p', {'id': 'progress_percent', 'text': '0%'});
					var p_msg = new Element('p', {'id': 'progress_message'});

					this.setStyle('display', 'none');
					this.getParent().adopt(
						new Element('div', {'id': 'progress_bar_wrapper'}).grab( p_bar ),
						new Element('div', {'id': 'progress_status'}).adopt(
							p_msg, p_pcnt,
							new Element('input', {'type': 'hidden', 'id': 'progress_start', 'value': $('upload_key').get('value')})
						)
					);

					var old_progress = 0;
					var step = 1;

					var p_bar_tween = new Fx.Tween(p_bar, {'duration': 1000});

					//this function executes at an interval to pole for changes in the upload progress and update the progress bar
					var update_progress = function(){

						//fetch the progress from the iframe, and if there is a problem with it, revert it to its previous value
						var progress = window.frames['progress_iframe'].progress;

						if ( ! $chk(progress) || progress.length > 3){
							progress = old_progress;
						}

						old_progress = progress;

						//animate the progress bar and update the progress percentage text
						p_bar_tween.cancel().start('width', ( progress > 98 ? 98 : progress ) * 2);
						p_pcnt.set('text', progress + '%');

						//animate the moving dots
						if (step == 4){
							step = 1;
						}
						else {
							var dots = '...'.substring(0, step);
							p_msg.set('text', 'Uploading' + dots);
							step++;
						}
					}

					update_progress();
					update_progress.periodical(500);
				});
				*/
				lb.update_lb(el);


        // Add this click to google analytics
        if (typeof _gaq != "undefined")
        {
          _gaq.push(['_trackEvent', 'Uploads', 'Lightbox']);
        }
        $$('#upload_button').addEvent('click', function(){
          if (typeof _gaq != "undefined")
          {
            _gaq.push(['_trackEvent', 'Uploads', 'Upload Request']);
          }
        });
			}
		}).send();
	},


	/*=-=-=-=-=- Login Lightbox -=-=-=-=-=*/

	'login': function(){
		this.setup({ 'lb': { 'width': 350, 'height': 'auto', 'min-height': 100, 'padding': '22px 30px', 'background': '#fff' } });

		var el = new Element('div', {'id': 'login_lb'});
		$(document.body).grab(el);

		//load the lightbox content for the login from a file
		new Request.HTML({
			'url': baseUrl + 'request/login_lb',
			'update': 'login_lb',
			'onComplete': function(){
				lb.login_setup('front');
				lb.update_lb(el, $('login_email'));
			}
		}).send();
	},

	'login_setup': function(mode){
		//login button is clicked, process and create an upload form
		$$('#login_button').addEvent('click', function(){
			lb.login_process(mode);
		});

		//allow the enter key to submit the form
		$$('#login_row input').addEvent('keydown', function(e){
			if(e.key == 'enter'){
				lb.login_process(mode);
			}
		});

		$$('#login_lb input').addEvent('keydown', function(e){
			if(e.key == 'enter'){
				lb.login_process(mode);
			}
		});

		lb.close_button();
	},

	'login_process': function(mode){
		if (mode == 'front'){
			var update = 'login_lb';
		}
		else if (mode == 'editor'){
			var update = 'save_lb';
		}
		else if (mode == 'checkout'){
			var update = 'checkout_lb';
		}

		new Request.HTML({
			'url': baseUrl + 'request/login_user/' + mode,
			'update': update,
			'onComplete': function(){
				if (mode == 'front'){
					var utype = $('login_utype');

					if ($('login_email'))
					{
					 $('user_email').innerHTML = '(' + $('login_email').value + ')';
					}

					if ($chk(utype)){
						$('menu_logged_out').addClass('hidden');
						$('menu_logged_in').removeClass('hidden');
						lb.hide();
					}
					else if ($('login_email'))
					{
						lb.login_setup('front');
						$('login_email').focus();
					}

					if (document.location.href.indexOf('/shop/cart', 0) != -1)
					{
						document.location.reload();
					}
				}
				else if (mode == 'editor'){
					lb.save_setup();
					if ($chk($('save_name'))){
						$('save_name').focus();
					}
					lb.close_button();
				}
				else if (mode == 'checkout'){
					if($('logged_in')){
						document.location = baseUrl + 'shop/checkout_shipping';
					}
					else {
						lb.checkout_setup();
						lb.close_button();
					}
				}

				if (mode != 'editor'){
					new Request({
						'url': baseUrl + 'request/get_cart_count',
						'onComplete': function(resp){
							$('cart_count').set('text', 'cart (' + resp +')');
						}
					}).send();
				}

        // If activaition link is returned, add click event to it
				if ($('send_activation'))
				{
        	$$('#send_activation').addEvent('click', function(){ lb['activation'](this.href, update); return false; });
				}
			}
		}).send({
			'data': {
				'login_email': $('login_email').get('value'),
				'login_password': $('login_password').get('value')
			}
		});
	},

	'activation': function(url, update)
	{
    new Request.HTML({
  		'url': url,
  		'update': update
  	}).send();
  	return false;
	},


	/*=-=-=-=-=- Signup Lightbox -=-=-=-=-=*/

	'signup': function(){
		this.setup({
			'lb': { 'width': 300, 'height': 'auto', 'min-height': 0, 'padding': '22px 30px' },
			'close': { 'top': 8, 'right': 8 }
		});

		var el = new Element('div', {'id': 'signup_lb'});
		$(document.body).grab(el);

		//load the lightbox content for the login from a file
		new Request.HTML({
			'url': baseUrl + 'request/signup_lb',
			'update': 'signup_lb',
			'onComplete': function(){
				lb.signup_setup('front');
				lb.update_lb(el, $('signup_email'));
			}
		}).send();
	},

	'signup_setup': function(mode){
		$$('#signup_button').addEvent('click', function(){
			lb.signup_process(mode);
		});

		//allow the enter key to submit the form
		$$('#signup_row input').addEvent('keydown', function(e){
			if(e.key == 'enter'){
				lb.signup_process(mode);
			}
		});

		$$('#signup_lb input').addEvent('keydown', function(e){
			if(e.key == 'enter'){
				lb.signup_process(mode);
			}
		});

		lb.close_button();
	},

	'signup_process': function(mode){
		if (mode == 'front'){
			var update = 'signup_lb';
		}
		else if (mode == 'editor'){
			var update = 'save_lb';
		}
		else if (mode == 'checkout'){
			var update = 'checkout_lb';
		}

		//do the registration request and reinitialize the lightbox
		new Request.HTML({
			'url': baseUrl + 'request/signup_user/' + mode,
			'update': update,
			'onComplete': function(){
				if(mode == 'front'){
					lb.signup_setup('front');
					if ($('signup_email'))
					{
					 $('signup_email').focus();
					}
				}
				else if (mode == 'editor'){
					lb.save_setup();
				}
				else if (mode == 'checkout'){
					lb.checkout_setup();
				}

        // If activaition link is returned, add click event to it
				if ($('send_activation'))
				{
        	$$('#send_activation').addEvent('click', function(){ lb['activation'](this.href, update); return false; });
				}
			}
		}).send({
			'data': {
				'signup_email': $('signup_email').get('value'),
				'signup_pw': $('signup_pw').get('value'),
				'signup_confirm_pw': $('signup_confirm_pw').get('value')
			}
		});
	},


	/*=-=-=-=-=- Checkout Lightbox -=-=-=-=-=*/

	'checkout': function(){
		this.setup({
			'lb': { 'width': 678, 'height': 'auto', 'min-height': 0, 'padding': '22px 30px' },
			'close': { 'top': 8, 'right': 8 }
		});

		var el = new Element('div', {'id': 'checkout_lb'});
		$(document.body).grab(el);

		new Request.HTML({
			'url':baseUrl + 'request/checkout_lb',
			'update': 'checkout_lb',
			'onComplete': function(){
				lb.checkout_setup();
				lb.close_button();
				lb.update_lb(el);
			}
		}).send();
	},

	'checkout_setup': function(){
		//set up links that show the correct form based on if the user wants to login or register
		$$('.signup_link').addEvent('click', function(){
			$('signup_row').setStyle('display', 'block');
			$('login_row').setStyle('display', 'none');
			$('signup_email').focus();
		});

		$$('.login_link').addEvent('click', function(){
			$('signup_row').setStyle('display', 'none');
			$('login_row').setStyle('display', 'block');
			$('login_email').focus();
		});

		lb.login_setup('checkout');
		lb.signup_setup('checkout');
	},

	/*=-=-=-=-=- Contact Us -=-=-=-=-=*/

	'contact': function(){
		this.setup({
			'lb': { 'width': 374, 'height': 'auto', 'min-height': 0, 'padding': '22px 30px' },
			'close': { 'top': 8, 'right': 8 }
		});

		var el = new Element('div', {'id': 'contactus_lb'});
		$(document.body).grab(el);

		new Request.HTML({
			'url':baseUrl + 'request/contactus_lb',
			'update': 'contactus_lb',
			'onComplete': function(){
				lb.close_button();
				lb.update_lb(el);
			}
		}).send();
	},


	/*=-=-=-=-=- Email Lightbox -=-=-=-=-=*/

	'email_from_editor': function(data){
		this.setup({
			'lb': {'width': 678, 'height': 'auto', 'min-height': 0, 'padding': '22px 30px' },
			'close': { 'top': 8, 'right': 8 }
		});

		var el = new Element('div', {'id': 'email_lb'});
		$(document.body).grab(el);

		new Request.HTML({
			'url': baseUrl + 'request/email_lb',
			'update': 'email_lb',
			'onComplete': function(){
				var email_process = function(){
					ft.mode.locked = true;
					ft.set_loading();

					//ajax request to send the email
					new Request.HTML({
						'url': baseUrl + 'request/email_friend',
						'update': 'email_lb',
						'onComplete': function(){
							ft.mode.locked = false;
							ft.set_loading();
							email_setup();
							$('send_to_address').focus();
						}
					}).send({'data': {
						'email' : $('send_to_address').get('value'),
						'logid' : data
					}});
				}

				var email_setup = function(){
					//send email button is clicked
					$('save_button').addEvent('click',function() {
						email_process();
					});

					$('send_to_address').addEvent('keydown', function(e){
						if(e.key == 'enter'){
							email_process();
						}
					});

					lb.close_button();
				}

				email_setup();
				lb.update_lb(el, $('send_to_address'));
			}
		}).send();
	}
});

window.addEvent('domready',function(){
	lb = new Lightbox();

	$$('.lb_show').addEvent('click', function(){
		lb.show({'mode': this.get('id')});
	});
});


window.onerror = function(desc,page,line,chr){
	new Request({'url': baseUrl + 'request/js_error/'})
    .send("error=Desc: " + desc + "\nPage: " + page + "\nLine: " + line + "\nChr: " + chr + "\nLocation: " + location.href + "\nBrowser: " + navigator.userAgent);
  return true;
};

