function initAccordions() {
	$('div.accordion > ul').accordion({
		active: ".selected",
		autoHeight: false,
		header: ".opener",
		collapsible: true,
		changestart: function(event, ui) {
			ui.newHeader.parent().addClass('active');
		},
		change: function(event, ui) {
			ui.oldHeader.parent().removeClass('active');
		},
		event: "click"
	});
}

function initSlideshow() {
	$('div.portfolio').slideShow({
		slideEl:'ul.popup-slideset img',
		numElementLink:'ul.preview a',
		autoSlideShow:false,
		switchTime:3000,
		duration:500,
		event:'click'
	});
}

function initPopups() {
	$('a.open-popup').simpleLightbox({
		lightboxContentBlock: '.lightbox',
		faderOpacity: 0.7,
		topPosition: 90,
		faderBackground: '#000',
		closeLink:'div.close > a',
		href:true,
		onClick: null
	});
}

function initSliders() {
	$('div.scale-slider').each(function(){
		var _holder = $(this);
		_holder.append('<input type="hidden" />');

		var _min = 0;
		var _max = 1;
		var _step = 1;
		var _value = 1;

		if(_holder.hasClass('.long-range')) {
			_min = 50;
			_max = 500;
			_step = 50;
			_value = 150;
		}
		if(_holder.hasClass('.slide-triple-toggle')) {
			_min = 0;
			_max = 2;
			_step = 1;
			_value = 3;
		}

		var _sliderField = _holder.find('input[type="hidden"]');
		_holder.slider({
			value:_value,
			min: _min,
			max: _max,
			step: _step,
			slide: function(event, ui) {
				_sliderField.val(ui.value);
			}
		});
	});

	$('div.scale-slider-toggle').each(function(){
		var _holder = $(this);
		_holder.append('<input type="hidden" />');
		var _sliderField = _holder.find('input[type="hidden"]');
		_holder.slider({
			value:1,
			min: 0,
			max: 1,
			slide: function(event, ui) {
				_sliderField.val(ui.value);
			}
		});
	});

}

function initLayout() {
	var _wrapper = $('#container');
	function fixLayout() {
		if (document.documentElement.clientHeight > _wrapper.height()) {
			if ($.browser.msie && $.browser.version == '6.0') {
				_wrapper.css({height: document.documentElement.clientHeight})
			} else {
				_wrapper.css({minHeight: document.documentElement.clientHeight})
			}
		}
	}
	$(window).resize(fixLayout);
}

function initColorSelector() {
	$('.runtime-color-selector').each(function(){
		var _holder = $(this);
		var _preview = _holder.find('.runtime-color-preview');
		var _switcherList = _holder.find('.runtime-color-list');

		_switcherList.each(function(){
			var _list = $(this);
			var _switchers = _list.find('.runtime-color-switcher');
			_switchers.click(function(){
				var _color = $(this).css('backgroundColor');

				// change fore color / bg color
				if(_list.hasClass('fore-color')) {
					_preview.css('color',_color);
				} else {
					_preview.css('backgroundColor',_color);
				}

				// set/remove active class
				if(_list.hasClass('active-parent')) {
					_switchers.parent().removeClass('runtime-active').removeClass('active').removeClass('hover');
					$(this).parent().addClass('runtime-active');
				} else {
					_switchers.removeClass('runtime-active');
					$(this).addClass('runtime-active');
				}
				return false;
			});
		});
	});
}

function initListSliders() {
	$('div.gallery').each(function(){
		var _slideSpeed = 200;
		var _gallery = $(this);
		var _listHolder = _gallery.find('div.holder');
		var _listHolderWidth = _listHolder.width();
		var _slider = _listHolder.find(' > ul');
		var _sliderItems = _slider.find(' > li');
		var _itemsCount = _sliderItems.length;
		var _itemWidth = _sliderItems.eq(0).outerWidth(true);
		var _btnPrev = _gallery.find('.prev');
		var _btnNext = _gallery.find('.next');
		var _sliderWidth = _itemWidth*_itemsCount;
		var _currentIndex = 0;
		var _visibleCount = Math.round(_listHolderWidth/_itemWidth);




		_btnNext.click(function(){
			if(_currentIndex < _itemsCount - _visibleCount) {
				_currentIndex++;
				switchSlide();
			}
			return false;
		});
		_btnPrev.click(function(){
			if(_currentIndex > 0) {
				_currentIndex--;
				switchSlide();
			}
			return false;
		});

		function switchSlide() {
			_slider.animate({marginLeft:-_currentIndex*_itemWidth},{duration:_slideSpeed,queue:false});


		}
	});
}

$(document).ready(function() {
	initLayout();
	initPopups();
	initAccordions();
	initSlideshow();
	initSliders();
	initListSliders();
	initColorSelector();
	
	//search handle
	$('#txtSearchKey').keydown(function(e){
	
	    if(e.keyCode==13)
	    {
	        searchTheSite();
	        return false;
	    }
	
	});
});

/********************************************** By MaHongTao ***********************************************************/

function searchTheSite()
{
    var q=escape($('#txtSearchKey').val());
    
    if(q!='')
        location.href='SearchResult.aspx?q='+q;
}