jQuery.fn.opacity = function($opacity) {
	if (this.css('position') != 'absolute') return;
   if (jQuery.browser.msie) return;

	$position = this.offset();
	$id = this.css('id') + this.css('class') + 'opacity';

	this.before("<div class=\"" + $id + "\" style=\"position: absolute; width: " + this.innerWidth() + "px; height: " + this.innerHeight() + "px; top: " + $position.top + "px; left: " + $position.left + "px; background: " + this.css('background') + "; background-image: " + this.css('background-image') + "; background-color: " + this.css('background-color') + "; opacity: " + $opacity + "; filter: alpha(opacity=" + ($opacity  * 100) + ");\"></div>");
	this.css('background', 'none');
	this.filter('.' + $id).remove();
}

jQuery.email = function($value) {
	return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test($value);						
}


jQuery.fn.mousehold = function(timeout, callback) {
	if (timeout && typeof timeout == 'function') {
		callback = timeout;
		timeout = 100;
	}

	if (callback && typeof callback == 'function') {
		var timer = 0;
		var step = 0;

		return this.each(function() {
			jQuery(this).mousedown(function() {
				step = 1;

				var counter = 0;
				var scope = this;

				timer = setInterval(function() {
					counter++;
					callback.call(scope, counter);
					step = 2;
				}, timeout);
			})

			var reset = function() {
				clearInterval(timer);
				if (step == 1) callback.call(this, 1);
				step = 0;
			}

			jQuery(this).mouseout(reset);
			jQuery(this).mouseup(reset);
		})
	}
}


$.widget('ui.scrollable', {

	_init: function() {
   	var $this = this;

   	this.element.find('.button1, .button2').click(function() {
         $this.change(($(this).hasClass('button1') ? '-1' : '1'));
      });
      
      this._setData('index', 1);
      this._setData('enabled', true);
   },


   change: function(offset)
   {
   	if (!this._getData('enabled')) return;
      
      this._setData('enabled', false);

   	var $this = this;
      var vertical = (this._getData('type') == 'vertical');
   	var index = this._getData('index');
      var items = this.element.find('.items ul li');
      var item = items.eq(0);
      var length = (vertical ? item.height() : item.width());
      var spacing = parseInt(item.css((vertical ? 'marginBottom' : 'marginRight')), 10);
		var count = Math.floor(((vertical ? this.element.find('.items').height() : this.element.find('.items').width()) + spacing) / (length + spacing));
      var total = Math.ceil(items.length / count);
      var data = {};

      switch (offset) {
      	case '-1': index = (index == 1 ? total : index - 1); break;
         case '1': index = (index == total ? 1 : index + 1); break;
      }
      
      if (vertical) {
         data.marginTop = -((length + spacing) * count) * (index - 1);
      }
      else {
         data.marginLeft = -((length + spacing) * count) * (index - 1);
      }

      this._setData('index', index);
      this.element.find('.items ul').animate(data, 1000, function() {
      	$this._setData('enabled', true);
     	});
   }

});


$.widget('ui.validate', {
	
	_init: function() {
		var $this = this;
		
		$(this.element).submit(function() {
			return $this.validate();										  
		});
	},
	
	validate: function() {
		$elements = $(this.element).find('input, textarea, select');
		$result = true;

		for ($i = 0; $i < $elements.length; $i++) {
			$element = $elements.eq($i);
			$value = $element.val();
			$classes = $element.attr('class').split(" ");
			$class = null;
			
			for ($j = 0; $j < $classes.length; $j++) {
				if ($classes[$j].slice(0, 8) == 'validate') {
					$class = $classes[$j].slice(9, -1).split(',');
					break;	
				}
			}

			if ($class != null)
			{
				$label = $element.prev('label').eq(0);

				switch ($class[0])
				{
					case 'email': $valid = jQuery.email($value); break;
					default:  $valid = ($value.length >= 1);
				}

				if ($valid)
				{
					$element.removeClass('error');
					$label.removeClass('error');
				}
				else
				{
					$element.addClass('error');
					$label.addClass('error');

					if (!$valid) $result = false;
				}
			}
		}

		return $result;
	}
	
});


$.widget('ui.listings', {
	
	_init: function() {
		var $this = this;

		$slider = this.element.find('.scrollbar .track');
		
		if ($slider.length == 0) return;

		$slider.slider({ orientation: 'vertical', min: 0, max: 100, value: 100, animate: true });
		$slider.bind('slidechange slide', function() { $this.update(); });

		this._setData('slider', $slider);
		this.element.find('.scrollbar .button1, .scrollbar .button2').mousehold(100, function() {
			$slider = $(this).closest('.scrollbar').find('.track');
			$this.update(($(this).attr('class') == 'button1' ? -1 : 1));
		});	
	},

	update: function($offset) {
		$slider = this._getData('slider');
      $container = this.element.find('.scrollable');
		$limit = $container.height();
		$items = $container.find('.scrollable-content');
      $spacing = Math.max(0, $items.innerHeight() - $limit);

		if (!isNaN($offset)) $slider.slider('value', $slider.slider('value') + (-$offset * (2000 / $spacing)));

		$position = (100 - $slider.slider('value')) / 100;
		$items.css('marginTop', -$position * $spacing);
	}
	
});


$.widget('ui.media', {

	_init: function() {
		var $this = this;

		this._setData('speed', 500);
		this._setData('disabled', false);
		this.element.find('.control .button1, .control .button2').click(function() {
			$this.change(($(this).hasClass('button1') ? '-1' : '1'));																			  
		});
		
		$index = this._getData('index');
		if (isNaN($index) || $index == 0) $index = 1;
		
		this._setData('index', 0);
		this.change($index);
	},
	
	change: function($offset) {
		if (this._getData('disabled')) return;
		
		$data = this._getData('data');
		$index = Number(this._getData('index'));

		switch ($offset) {
			case '-1': $index = ($index == 1 ? $data.length : $index - 1); break;
			case '1': $index = ($index == $data.length ? 1 : $index + 1); break;
			default: $index = $offset;	
		}

		if ($index == this._getData('index')) return;

      this.element.find('a.link').hide();
      this.element.find('a.link' + $index).show();

		this._setData('index', $index);
		this._setData('disabled', true);
		this.fade($index);
		this._trigger('change', 0, $index);
	},

	fade: function($index) {
		$this = this;
      
      $value = this._getData('data')[this._getData('index') - 1];

      if (typeof $value == 'string') {
      	document.location = $value;
      	return;
      }
		
		this.element.prepend("<div class=\"preloader\"></div>");
		
		$preloader = this.element.find('.preloader');
		$preloader.fadeTo(0, 0);
		$preloader.fadeTo(500, 1);

		$speed = this._getData('speed');
		$content = this.element.find('.images');
		$content.append("<img src=\"" + $value.url + "\" />");
		$images = $content.find('img');
		$image = $($images[$images.length - 1]);
		$image.fadeTo(0, 0);
		$image.load(function() {
			$preloader.stop(true);
			$preloader.fadeTo(500, 0);

			$image.fadeTo($speed, 1, function() {
				$images = $content.find('img');
				if ($images.length > 1) $images.eq(0).remove();

				$this._setData('disabled', false);
			});
		});
	}

});


$.widget('ui.gallery', {

	_init: function() {
		var $this = this;

		this._setData('speed', 500);
		this._setData('disabled', false);
		
		if (this._getData('data') == null) this._setData('data', JSON.parse(this.element.find('.images textarea').val()))

		this.element.find('.control .button1, .control .button2').click(function() {
			$this.change(($(this).hasClass('button1') ? '-1' : '1'));																			  
		});

		if (this.element.find('.images').find('img').length == 0) this.change(1);
		else this._setData('index', 1);
	},

	change: function($offset) {
		if (this._getData('disabled')) return;
		
		$data = this._getData('data');
		$index = this._getData('index');

		switch ($offset) {
			case '-1': $index = ($index == 1 ? $data.length : $index - 1); break;
			case '1': $index = ($index == $data.length ? 1 : $index + 1); break;
			default: $index = $offset;	
		}

		if ($index == this._getData('index')) return;

		this._setData('index', $index);
		this._setData('disabled', true);
		this.fade($index);
		this._trigger('change', 0, $index);
	},

	fade: function($index) {
		var $this = this;
		
		this.element.prepend("<div class=\"preloader\"></div>");
		
		$preloader = this.element.find('.preloader');
		$preloader.fadeTo(0, 0);
		$preloader.fadeTo(500, 1);

		$speed = this._getData('speed');
		$content = this.element.find('.images');
		$content.append("<img src=\"" + this._getData('data')[this._getData('index') - 1] + "\" />");
		$images = $content.find('img');
		$image = $($images[$images.length - 1]);
		$image.fadeTo(0, 0);
		$image.load(function() {
			$preloader.stop(true);
			$preloader.fadeTo(500, 0);
			
			$this.element.find('.control span').html($this._getData('index') + ' of ' + $this._getData('data').length);

			$image.fadeTo($speed, 1, function() {
				$images = $content.find('img');
				if ($images.length > 1) $images.eq(0).remove();

				$this._setData('disabled', false);
			});
		});
	}

});


function interactive() {
	$i = 2;
	$timer = setInterval(function() {
		if ($i == 11) $i = 1;

		$parent = $('#interactive .open');
		$items = $parent.find('span');
		$item = $items.eq($items.length - 1);

		if ($i % 4 == 0 && !$item.hasClass('item1')) {
			$parent.append("<span class=\"item1\"></span>");
			$i++;
		}
		else {
			$parent.append("<span class=\"item" + $i + "\"></span>");
			$i++;
		}
		
		$items = $parent.find('span');
		$item = $items.eq($items.length - 1);
		$item.fadeTo(0, 0);
		$item.fadeTo(500, 1, function() {
			$parent.find('span').eq(0).remove();				
		});

	}, 3000);
}


function soundUpdate($url) {
	$('#sounds-flash')[0].update($url);
}


$().ready(function() {
	$image = $('img.background').eq(0);
	$image.fadeTo(0, 0);
	
	if ($image.attr('complete'))
	{
		$image.fadeTo(500, 1);
	}
	else
	{
		$image.load(function() {
			$image.fadeTo(500, 1);							
		});	
	}

	$('#interactive .open').click(function() {
   	soundUpdate('interactive');
		$('#interactive').css('z-index', 5);
		$(this).animate({ marginTop: '111px' }, 300, function() {
				$('#interactive .menu').animate({ marginTop: '0px' }, 500);
			}
		);
	});

	$('#interactive .close').click(function() {
   	soundUpdate('interactive');
		$('#interactive .menu').animate({ marginTop: '111px' }, 500, function() {
				$('#interactive .open').animate({ marginTop: '87px' }, 300, function() {
					$('#interactive').css('z-index', '');																							
				});
			}
		);
	});
	
	if ($('#interactive.visible').length == 0)
	{
		interactive();
	}
	else
	{
		$interval = setTimeout(function() {
			$('#interactive.visible .close').trigger('click');
			interactive();
		}, 3000);
		
		$('#interactive').mouseover(function() {
			clearTimeout($interval);
		});
	}

	$('#listings').listings();
   
   $flash = new SWFObject("http://www.libertypark.com/assets/swf/sounds.swf", 'sounds-flash', 1, 1, '8', '#fff9ec');
	$flash.addParam('allowScriptAccess', 'always');
	$flash.addParam('allowFullScreen', 'true');
	$flash.write('sounds');
   
   $('a.chat').not(':last').each(function() {
   	$(this).attr('href', "https://server.iad.liveperson.net/hc/11695874/?cmd=file&file=visitorWantsToChat&site=11695874&byhref=1&imageUrl=https://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/");
  		$(this).attr('target', "chat11695874");
      $(this).attr('onclick', "javascript:window.open('https://server.iad.liveperson.net/hc/11695874/?cmd=file&file=visitorWantsToChat&site=11695874&imageUrl=https://server.iad.liveperson.net/hcp/Gallery/ChatButton-Gallery/English/General/1a/&referrer='+escape(document.location),'chat11695874','width=475,height=400,resizable=yes');return false;");
   });

	$('.alert').each(function() {
		alert($(this).html());								  
	});
});