//
// Note: This file depends on the MooTools library.
//

// Module pattern:
// http://yuiblog.com/blog/2007/06/12/module-pattern/
var FORMALIZE = (function(window, document, undefined) {
  // Private constants.
  var PLACEHOLDER_SUPPORTED = 'placeholder' in document.createElement('input');
  var AUTOFOCUS_SUPPORTED = 'autofocus' in document.createElement('input');
  var WEBKIT = 'webkitAppearance' in document.createElement('select').style;
  var IE6 = Browser.ie6;
  var IE7 = Browser.ie7;

  // Expose innards of FORMALIZE.
  return {
    // FORMALIZE.go
    go: function() {
      for (var i in FORMALIZE.init) {
        FORMALIZE.init[i]();
      }
    },
    // FORMALIZE.init
    init: {
      // FORMALIZE.init.detect_webkit
      detect_webkit: function() {
        if (!WEBKIT) {
          return;
        }

        // Tweaks for Safari + Chrome.
        $$('html')[0].addClass('is_webkit');
      },
      // FORMALIZE.init.full_input_size
      full_input_size: function() {
        if (!IE7 || !$$('textarea, input.input_full').length) {
          return;
        }

        // This fixes width: 100% on <textarea> and class="input_full".
        // It ensures that form elements don't go wider than container.
        $$('textarea, input.input_full').each(function(el) {
          new Element('span.input_full_wrap').wraps(el);
        });

      },
      // FORMALIZE.init.ie6_skin_inputs
      ie6_skin_inputs: function() {
        // Test for Internet Explorer 6.
        if (!IE6 || !$$('input, select, textarea').length) {
          // Exit if the browser is not IE6,
          // or if no form elements exist.
          return;
        }

        // For <input type="submit" />, etc.
        var button_regex = /button|submit|reset/;

        // For <input type="text" />, etc.
        var type_regex = /date|datetime|datetime-local|email|month|number|password|range|search|tel|text|time|url|week/;

        $$('input').each(function(el) {
          // Is it a button?
          if (el.getAttribute('type').match(button_regex)) {
            el.addClass('ie6_button');

            /* Is it disabled? */
            if (el.disabled) {
              el.addClass('ie6_button_disabled');
            }
          }
          // Or is it a textual input?
          else if (el.getAttribute('type').match(type_regex)) {
            el.addClass('ie6_input');

            /* Is it disabled? */
            if (el.disabled) {
              el.addClass('ie6_input_disabled');
            }
          }
        });

        $$('textarea, select').each(function(el) {
          /* Is it disabled? */
          if (el.disabled) {
            el.addClass('ie6_input_disabled');
          }
        });
      },
      // FORMALIZE.init.autofocus
      autofocus: function() {
        if (AUTOFOCUS_SUPPORTED || !$$('[autofocus]').length) {
          return;
        }

        $$('[autofocus]')[0].focus();
      },
      // FORMALIZE.init.placeholder
      placeholder: function() {
        if (PLACEHOLDER_SUPPORTED || !$$('[placeholder]').length) {
          // Exit if placeholder is supported natively,
          // or if page does not have any placeholder.
          return;
        }

        FORMALIZE.misc.add_placeholder();

        $$('[placeholder]').each(function(el) {
          var text = el.get('placeholder');

          el.addEvents({
            focus: function() {
              if (el.value === text) {
                el.set('value', '').removeClass('placeholder_text');
              }
            },
            blur: function() {
              FORMALIZE.misc.add_placeholder();
            }
          });

          // Prevent <form> from accidentally
          // submitting the placeholder text.
          el.getParent('form').addEvents({
            'submit': function() {
              if (el.value === text) {
                el.set('value', '').removeClass('placeholder_text');
              }
            },
            'reset': function() {
              setTimeout(FORMALIZE.misc.add_placeholder, 50);
            }
          });
        });
      }
    },
    // FORMALIZE.misc
    misc: {
      // FORMALIZE.misc.add_placeholder
      add_placeholder: function() {
        if (PLACEHOLDER_SUPPORTED || !$$('[placeholder]').length) {
          // Exit if placeholder is supported natively,
          // or if page does not have any placeholder.
          return;
        }

        $$('[placeholder]').each(function(el) {
          var text = el.get('placeholder');

          if (!el.value || el.value === text) {
            el.set('value', text).addClass('placeholder_text');
          }
        });
      }
    }
  };
// Alias window, document.
})(this, this.document);

// Automatically calls all functions in FORMALIZE.init
$(document).addEvent('domready', function() {
  FORMALIZE.go('radio');
});

/**
 * @version		0.5
 * @package		Scroller
 * @author		JoomlaConvert http://www.joomlaconvert.com
 * @copyright	Copyright (c) 2011 JoomlaConvert.com. All rights reserved.
 * @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
 */

//when the dom is ready
window.addEvent('domready',function() {
  //smooooooth scrolling enabled
  //new SmoothScroll({ options }, window);
  new SmoothScroll({ duration:1500 }, window); //500 milliseconds to get there
});
// End


//*Lazy loading of Images * //

		
		var LazyLoad = new Class({
 
			Implements: [Options,Events],
 
			/* additional options */
			options: {
				range: 200,
				resetDimensions: true,
				elements: 'img',
				container: window,
				fireScroll: true, /* keeping for legacy */
				mode: 'vertical',
				startPosition: 0
			},
 
			/* initialize */
			initialize: function(options) {
 
				/* vars */
				this.setOptions(options);
				this.container = document.id(this.options.container);
				this.elements = $$(this.options.elements);
				var axis = (this.options.mode == 'vertical' ? 'y': 'x');
				this.containerDimension = this.container.getSize()[axis];
				this.startPosition = 0;
 
				/* find elements remember and hold on to */
				this.elements = this.elements.filter(function(el,i) {
					/* reset image src IF the image is below the fold and range */
					if(el.getPosition(this.container)[axis] > this.containerDimension + this.options.range) {
						el.store('oSRC',el.get('src')).set('src',this.options.image);
						if(this.options.resetDimensions) {
							el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''});
						}
						return true;
					}
				},this);
 
				/* create the action function */
				var action = function() {
					var cpos = this.container.getScroll()[axis];
					if(cpos > this.startPosition) {
						this.elements = this.elements.filter(function(el) {
							if((cpos + this.options.range + this.containerDimension) >= el.getPosition(this.container)[axis]) {
								
								if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); }
								if(this.options.resetDimensions) {
									el.set({ width: el.retrieve('oWidth'), height: el.retrieve('oHeight') });
								}
								this.fireEvent('load',[el]);
								return false;
							}
							return true;
						},this);
						this.startPosition = cpos;
					}
					this.fireEvent('scroll');
					/* remove this event IF no elements */
					if(!this.elements.length) {
						this.container.removeEvent('scroll',action);
						this.fireEvent('complete');
					}
				}.bind(this);
 
				/* listen for scroll */
				this.container.addEvent('scroll',action);
				if(this.options.fireScroll) { action(); }
			}
		});
		
		/* do it! */
		window.addEvent('domready',function() {
			var lazyloader = new LazyLoad({
				onLoad: function(img) {
					setTimeout(function(){img.setStyle('opacity',0).fade(1);},500);
				}
			});
		});
		
	/*End */
