/*--- request function ---*/
function initGall(){
	$('div.page-wrapper').each(function(){
		var _hold = $(this);
		var gall_hold = _hold.find('div.photo-frame-wrapper');
		var btn_prev = _hold.find('div.prev-btn a');
		var btn_next = _hold.find('div.next-btn a');
		if(gall_hold.length && btn_prev.length && btn_next.length){
			var img_list = [];
			var img_url = gall_hold.find('span.json-src').text();
			var list_h = $('<ul></ul>');
			gall_hold.find('span.json-src').replaceWith(list_h);
			var _a = 0;
			$.getJSON( img_url,
				function(data){
					$.each(data, function(_i){
						var t_el = [];
						t_el._el = false;
						t_el._alt = this.alt;
						t_el._src = this.src;
						img_list.push(t_el);
					});
					img_list[_a]._el = $('<li class="active"><img src="'+img_list[_a]._src+'" alt="'+img_list[_a]._alt+'"/></li>');
					list_h.append(img_list[_a]._el);
					btn_prev.click(function(){
						if(_a > 0) changeImg(_a - 1);
						else changeImg(img_list.length - 1);
						return false;
					});
					btn_next.click(function(){
						if(_a < img_list.length - 1) changeImg(_a + 1);
						else changeImg(0);
						return false;
					});
				}
			);
			var _t;
			function changeImg(_ind){
				if(_t) clearTimeout(_t);
				list_h.children().removeClass('active').animate({opacity: 0}, {queue:false, duration:600});
				if(img_list[_ind]._el){
					img_list[_ind]._el.addClass('active').animate({opacity: 1}, {queue:false, duration:600});
				}
				else{
					img_list[_ind]._el = $('<li class="active"><img src="'+img_list[_ind]._src+'" alt="'+img_list[_ind]._alt+'"/></li>');
					img_list[_ind]._el.css('opacity', 0);
					list_h.append(img_list[_ind]._el);
					_t = setTimeout(function(){
						img_list[_ind]._el.addClass('active').animate({opacity: 1}, {queue:false, duration:600});
					}, 1000);
				}
				_a = _ind;
			}
		}
		
	});	
}
/*--- slide menu function ---*/
function slideMenu(){
	var nav_hold = $('#navigation');
	if(nav_hold.length){
		var _btn = nav_hold.find('a');
		var _a = _btn.index(_btn.filter('.active:eq(0)'));
		_btn.removeClass('active').eq(_a).addClass('active');
		_btn.each(function(_i){
			var _box = this.href.substr(this.href.indexOf("#") + 1);
			if(_box){
				_box = $('#'+_box);
				if(_box.length){
					var _scrollTo = 0;
					if(_a == _i){
						_scrollTo = _box.offset().left + (_box.width() - window_w())/2;
						if(_scrollTo < 0) _scrollTo = 0;
						$('body').scrollTo(_scrollTo, 1500);
					}
					this.onclick = function(){
						_scrollTo = _box.offset().left + (_box.width() - window_w())/2;
						if(_scrollTo < 0) _scrollTo = 0;
						$('body').scrollTo(_scrollTo, 1500);
						_btn.eq(_a).removeClass('active');
						$(this).addClass('active');
						_a = _i;
						return false;
					}
				}
			}
		});
	}
	var _header = $('div.header:eq(0)');
	var h_width = _header.width();
	_header.css({marginLeft: ($(window).width()-h_width)/2 + 100});
	$(window).resize(function(){
		_header.css({marginLeft: ($(window).width()-h_width)/2 + 100});
	});
	function window_w(){
		if (window.innerWidth) return window.innerWidth;
		else return document.documentElement.clientWidth;
	}
	
}
function initHeight(){
	var _window = document.documentElement.clientHeight;
	if(_window > 580) {
		$('.wrapper').css('top', (_window - 580)*0.5);
		$('.main-wrapper').css('margin-top', (_window - 580)*0.5);
	}

	setTimeout(function(){
		initHeight();
	}, 10);
}
$(document).ready(function(){
	initGall();
	slideMenu();
	setTimeout(function(){
		initHeight();
	}, 10);
});