jQuery(function($){

if (window.parent !== window.self) {
	return;
}


function ArticleImages(url) {
	this.selected = 0;
	this.node = null;
	this.imageblock = null;
	this.images = null;
	this.controller = null;
	this.closebutton = null;
	this.duration = 800;
	this.init(url);
}
ArticleImages.prototype.init = function(url){
	this.node = $('<div class="article"></div>').prependTo('.content-body');
	this.getContent(url);
	this.open();
}
ArticleImages.prototype.getContent = function(url){
	var _this = this;
	$('<iframe></iframe>').bind('load', function(){
		_this.onload($(this.contentWindow.document.documentElement).html());
	}).attr('src', url).css('position', 'absolute').css('top', '-99999px').appendTo('body');
}
ArticleImages.prototype.select = function(index){
	if (index < 0 || this.images.length -1 < index) {
		return;
	}
	this.selected = index;
	this.indicator.removeClass('stay')
			.find('img').attr('src', 'images/button_indicator_01.png')
			.end()
		.eq(index).addClass('stay')
			.find('img').attr('src', 'images/button_indicator_01_s.png')
	;
	this.imageblock.stop().animate({'margin-left': -1*index*950+'px'}, this.duration, 'easeOutCubic');
}
ArticleImages.prototype.prev = function(){
	this.select(this.selected - 1);
}
ArticleImages.prototype.next = function(){
	this.select(this.selected + 1);
}
ArticleImages.prototype.open = function(){
	var _this = this;
	this.node.stop().animate({'height': '540px'}, this.duration, 'easeOutCubic', function(){
		if (_this.onopened) {
			_this.onopened();
		}
	});
}
ArticleImages.prototype.close = function(){
	var _this = this;
	this.images.add('.article-controller').add('.article-close').stop().animate({opacity: 0}, this.duration, 'easeOutCubic')
	this.node.stop().animate({'height': '0px'}, this.duration, 'easeOutCubic', function(){
		if (_this.onclosed) {
			_this.onclosed();
		}
		_this.node.remove();
		_this.node = null;
	});
	if (this.onclose) {
		this.onclose();
	}
}
ArticleImages.prototype.createController = function(){
	this.controller = $('<ul class="article-controller"></ul>').appendTo(this.node);
	this.controller.append($('<li class="article-prev"><a href="#"><img src="images/button_prev_01.png" width="22" height="22" alt="" /></a></li>'));
	for (var i = 0, n = this.images.length; i < n; i++) {
		this.controller.append($('<li class="article-indicator"><a href="#"><img src="images/button_indicator_01.png" width="12" height="12" alt="'+(i+1)+'" /></a></li>'));
	}
	this.controller.append($('<li class="article-next"><a href="#"><img src="images/button_next_01.png" width="22" height="22" alt="" /></a></li>'));
	this.indicator = this.controller.find('.article-indicator');

	var _this = this;
	this.indicator.find('a').each(function(index,node,array){
		$(node).click(function(e){
			e.preventDefault();
			e.stopPropagation();
			_this.select(index);
		});
	});
	this.controller.find('.article-prev a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_this.prev();
	});
	this.controller.find('.article-next a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_this.next();
	});
}
ArticleImages.prototype.createCloser = function(){
	this.closebutton = $('<div class="article-close"><a href="#"><img src="common/images/icon/close.png" width="66" height="36" alt="" /></a></div>').appendTo(this.node);
	var _this = this;
	this.closebutton.find('a').click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_this.close();
	});
}
ArticleImages.prototype.onload = function(data){
	this.imageblock = $(data).find('.article-images').appendTo(this.node);
	this.images = this.imageblock.find('.article-image');
	this.createController();
	this.createCloser();
	this.select(0);
	this.images.eq(0).add('.article-controller').add('.article-close').css('opacity', 0).animate({opacity: 1}, this.duration/2, 'easeOutCubic');
	if (this.onloaded) {
		this.onloaded();
	}
}

function ArticleList(node) {
	this.node = null;
	this.banner = null;
	this.rows = [];
	this.items = null;
	this.selected = -1;
	this.currentArticle = null;
	this.currentRowNumber = -1;
	this.duration = 800;
	this.delay = 400;
	this.delayTimer = null;
	this.init(node);
}
ArticleList.prototype.init = function(node){
	this.node = node;
	this.loader = $('<div class="loader"></div>');
	this.banner = $('.content-aside .support');
	this.items = this.node.find('li');
	for (var i = 0, n = this.items.length; i < n; i++) {
		if (i % 5 == 0) {
			this.items.eq(i).css('clear', 'left');
		}
		this.getRowByIndex(i).push(this.items.eq(i));
	}

	var _this = this;
	this.items.click(function(e){
		e.preventDefault();
		e.stopPropagation();
		_this.onclick(e.currentTarget);
	});
}
ArticleList.prototype.getMaxRowNumber = function(){
	return this.rows.length - 1;
}
ArticleList.prototype.getRowNumber = function(index){
	return parseInt(index / 5, 10);
}
ArticleList.prototype.getRow = function(rowNumber){
	if (!this.rows[rowNumber]) {
		this.rows[rowNumber] = [];
	}
	return this.rows[rowNumber];
}
ArticleList.prototype.getRowByIndex = function(index){
	return this.getRow(this.getRowNumber(index));
}
ArticleList.prototype.articleOpen = function(url){
	this.loader.prependTo('.content-body');
	if (!this.currentArticle) {
		this.loader.stop().animate({'height': '560px',opacity:1}, this.duration/2, 'easeOutCubic');
	} else {
		this.loader.stop().animate({opacity:1}, this.duration/2, 'easeOutCubic');
	}
	this.currentArticle = new ArticleImages(url);
	var _this = this;
	this.currentArticle.onloaded = function(){
		_this.loader.animate({opacity:0}, this.duration/2, 'easeOutCubic', function(){ $(this).remove(); });
	}
	var _article = this.currentArticle;
	this.currentArticle.onclose = function(){
		if (_this.currentArticle === _article) {
			_this.loader.stop().css('opacity', 0).css('height', 0);
		}
		_this.setRowPosition(-1);
		_this.setBannerPosition(-1);
		_this.setSelectedIndex(-1);
	}

	var hash = (url.match(/\/?([\w]+)\.html/) || ['',''])[1];
	location.hash = '/' + hash;
}
ArticleList.prototype.articleClose = function(){
	if (this.currentArticle) {
		this.currentArticle.close();
	}
}
ArticleList.prototype.setArticlePosition = function(rowNumber){
	var top = (252 * rowNumber + 2);
	var bottom = (252 * (this.getMaxRowNumber() - rowNumber + 1));
	this.loader.css('bottom', bottom + 'px').data('posTop', top);
	this.currentArticle.node.css('bottom', bottom + 'px').data('posTop', top);
	$($.browser.webkit ? document.body : document.documentElement).stop().animate({'scrollTop': top + $('.category-header').outerHeight(true) + $('.content-header').outerHeight(true)}, this.duration, 'easeOutCubic');
}
ArticleList.prototype.setRowPosition = function(rowNumber){
	if (this.currentRowNumber != rowNumber) {
		if (this.currentRowNumber >= 0) {
			var row = this.getRow(this.currentRowNumber);
			for (var i = 0, n = row.length; i < n; i++) {
				row[i].stop().animate({'padding-top':'0'}, this.duration, 'easeOutCubic');
			}
		}
		if (this.currentArticle && this.currentRowNumber >= 0 && this.currentRowNumber < rowNumber) {
			this.currentArticle.node.css('top', this.currentArticle.node.data('posTop') + 'px');
		}
		if (rowNumber >= 0) {
			var row = this.getRow(rowNumber);
			for (var i = 0, n = row.length; i < n; i++) {
				row[i].stop().animate({'padding-top':'540px'}, this.duration, 'easeOutCubic');
			}
		}
		this.currentRowNumber = rowNumber;
	}
	if (rowNumber < 0) {
		this.currentArticle = null;
	}
}
ArticleList.prototype.clearTimer = function(){
	if (this.delayTimer) {
		clearTimeout(this.delayTimer);
		this.delayTimer = null;
	}
}
ArticleList.prototype.setBannerPosition = function(rowNumber){
	var pos = (rowNumber >= 0) ? (252 * rowNumber + 540) : 0;
	var _this = this;
	this.banner.stop();
	this.delayTimer = setTimeout(function(){
		_this.banner.stop().animate({'top': pos + 'px'}, _this.duration, 'easeOutCubic');
	}, this.delay);
}
ArticleList.prototype.setPosition = function(rowNumber){
	this.setArticlePosition(rowNumber);
	this.setRowPosition(rowNumber);
}
ArticleList.prototype.setSelectedIndex = function(index){
	if (this.selected >= 0) {
		var img = this.items.eq(this.selected).find('img');
		img.attr('src', img.data('default_src'));
	}
	this.selected = index;
	if (this.selected >= 0) {
		var img = this.items.eq(this.selected).find('img');
		if (!img.data('default_src')) {
			img.data('default_src', img.attr('src'));
		}
		//img.attr('src', 'images/by_job_' + img.data('default_src').match(/_(\d+)/)[1] + '_s.jpg');
		img.attr('src', 'common/images/category/list_' + img.data('default_src').match(/_(\d+)/)[1] + '_s.png');
	}
	if (this.selected < 0) {
		location.hash = '';
	}
}
ArticleList.prototype.selectNode = function(node){
	var url = $(node).find('a').attr('href');
	var rowNumber = -1;
	for (var i = 0, n = this.items.length; i < n; i++) {
		if (this.items.get(i) === node) {
			rowNumber = this.getRowNumber(i);
			if (this.selected == i) {
				this.articleClose();
				return;
			} else {
				this.setSelectedIndex(i);
			}
			break;
		}
	}

	var _this = this;
	if (this.currentArticle) {
		this.currentArticle.onclosed = function(){
			_this.articleOpen(url);
			_this.setPosition(rowNumber);
			_this.currentArticle.onopened = function(){
				_this.setBannerPosition(rowNumber);
			}
		}
		this.currentArticle.onclose = null;
		this.articleClose();
		this.setRowPosition(rowNumber);
	} else {
		this.articleOpen(url);
		this.setPosition(rowNumber);
		this.currentArticle.onopened = function(){
			_this.setBannerPosition(rowNumber);
		}
	}
}
ArticleList.prototype.onclick = function(node){
	this.selectNode(node);
}

function ArticlePage(node) {
	this.node = null;
	this.items = null;
	this.selectedIndex = -1;
	this.currentList = null;
	this.isReady = false;
	this.duration = 800;
	this.init(node);
}
ArticlePage.prototype.init = function(node){
	var _this = this;
	this.defaultpage = (location.hash.match(/^#?\/(\d+)/) || ['',''])[1];
	this.node = node;
	this.items = node.find('li');
	this.items.find('a').each(function(index, node, array){
		$(node).click(function(e){
			e.preventDefault();
			e.stopPropagation();
			_this.select(index);
		});
	});
	if (!this.items.filter(function(index, node, array){if ($(node).is('.stay')) {_this.selectedIndex = index;_this.currentList = new ArticleList($('.article-list'));_this.onready();return true;}}).length) {
		this.select(0);
	}
}
ArticlePage.prototype.select = function(index){
	if (this.selectedIndex == index) {
		return;
	}
	if (this.selectedIndex >= 0) {
		var img = this.items.eq(this.selectedIndex).removeClass('stay').find('img');
		img.attr('src', img.attr('src').replace('_s.', '.'));
	}
	this.selectedIndex = index;
	if (this.selectedIndex >= 0) {
		var img = this.items.eq(this.selectedIndex).addClass('stay').find('img');
		img.attr('src', img.attr('src').replace(/(_s)?\./, '_s.'));
		this.getContent(this.items.eq(this.selectedIndex).find('a').attr('href'));
	}
}
ArticlePage.prototype.getContent = function(url){
	var _this = this;
	$('.article-list').slideUp(this.duration, 'easeOutQuad', function(){$(this).remove()});
	if (this.currentList) {
		if (this.currentList.currentArticle) {
			this.currentList.currentArticle.node.hide();
		}
		this.currentList.articleClose();
	}

	$('<iframe></iframe>').bind('load', function(){
		_this.onload($(this.contentWindow.document.documentElement).html());
	}).attr('src', url).css('position', 'absolute').css('top', '-99999px').appendTo('body');
}
ArticlePage.prototype.onload = function(data){
	this.currentList = new ArticleList($(data).find('.article-list').hide().prependTo('.content-body').slideDown(this.duration, 'easeOutQuad'));
	if ($.browser.msie && $.browser.version < 8) {
		this.currentList.node.find('li').css('opacity', '1');
	}
	if (!this.isReady) {
		var items = this.currentList.node.find('li');
		var length = items.length;
		items.css('opacity', 0).each(function(index, node){
			setTimeout(function(){
				$(node).animate({opacity:1}, 400, 'easeOutQuad');
			}, (length - index) * 80);
		});
		this.onready();
	}
}
ArticlePage.prototype.onready = function(){
	this.isReady = true;
	var articleNum = this.defaultpage;
	if (articleNum) {
		var itemNode = this.currentList.items.find('a[href$="'+ articleNum +'.html"]').eq(0).parent().get(0);
		if (itemNode) {
			this.currentList.selectNode(itemNode);
		}
	}
	this.defaultpage = '';
}

new ArticlePage($('.Nav'));

});

