
(function(){

	var picRotation = {



		/**

		 * 轮播图片

		 * @type {Array}

		 */

		pics : [],

		

		/**

		 * 轮播导航

		 * @type {Array}

		 */

		dots : [],

		

		Rotation : null,

		

		picContainer : 'pic_wrap',

		

		dotContainer : 'dot_wrap',

		

		showClassName : 'pic-show',

		

		hideClassName : 'pic-hide',

		/**

		 * 轮播间隔，单位秒

		 * @type {Number}

		 */

		delay : 5,

		

		/**

		 * 轮播初始化方法

		 * @public

		 */

		init : function() {



			var picCon = document.getElementById(picRotation.picContainer);

			var dotCon = document.getElementById(picRotation.dotContainer);

			var wrap = document.getElementById('banner');

			// var pictures = picCon.getElementsByTagName('img');

			var pictures = picCon.children;



			var pics = picRotation.pics;

			var dots = picRotation.dots;



			

			for(var i = 0, len = pictures.length; i < len; i++) {

				pics.push(pictures[i]);

			}

			

			var obj = null;

			var	tmp;

			

			for(var j = 0; (tmp = pics[j]); j++){

				

				obj = document.createElement('button');

				obj.setAttribute('type', 'button');



				obj.className = (tmp.className == picRotation.showClassName) 

								? 'dot dot-cur' : 'dot'; 

				

				(function(j){

					obj.onclick = function(){	

						picRotation._dotClick(j);

					}

				})(j);

				

				obj.onfocus = function() {

					this.blur();

				}

				

				dotCon.appendChild(obj);

				

				dots.push(obj);

					

			}



			picRotation.picContainer = picCon;

			picRotation.dotContainer = dotCon;

			picRotation.banner = wrap;	

			

			picRotation._start();

			

		},

		

		/**

		 * 获取下一次显示的图片序号

		 * @private

		 * @param  {int} num 当前播放序号

		 * @return {int}     下次播放序号

		 */

		_getPicNum : function(num) {

					

			var num = 0;

			var pics = picRotation.pics;

			var len = pics.length;

			

			for(var i = 0; i < len; i++) {

				if(pics[i].className == picRotation.showClassName) {

					num	= i;

					break;

				}	

			}

			

			if(num >= (len - 1)) {

				num = 0;

			} else { 

				num++;

			}

					

			return num;

			

		},

		

		/**

		 * 显示图片

		 * @private

		 * @param  {int} num 显示图片序号

		 */

		_showPic : function(num) {

			

			num == null ? (num = picRotation._getPicNum()) : num;



			var pics = picRotation.pics;

			var dots = picRotation.dots;

			var wrap = picRotation.banner;

			

			for(var i = 0, len = pics.length; i < len; i++){

			

				if(i ==  num){				

					

					pics[i].className = picRotation.showClassName;	

					

					dots[i].className = 'dot dot-cur';



					wrap.className = 'banner' + (i+1);

					

				}else{

					

					pics[i].className = picRotation.hideClassName;	

					

					dots[i].className = 'dot';

				}

			}

		},

		

		/**

		 * 轮播开始方法

		 * @private

		 */

		_start : function(){

			

			if(picRotation.Rotation == null){

			

				picRotation.Rotation = window.setInterval(

					function(){

						picRotation._showPic();

					},

					picRotation.delay * 1000

				);

				

			}

			

		},

		

		/**

		 * 导航点击处理方法

		 * @private

		 * @param  {int} num 播放图片序号

		 */

		_dotClick : function(num){

			

			picRotation._clear();

			

			picRotation._showPic(num);

			

			picRotation._start();

			

		},

		

		/**

		 * 清除轮播

		 * @private

		 * @return {[type]} [description]

		 */

		_clear : function(){

				

			window.clearInterval(picRotation.Rotation);		

			

			picRotation.Rotation = null;

		}

		

	};





	/**

	 * 添加事件

	 * @param {HTMLElement}   el    节点

	 * @param {string}   event 事件名称

	 * @param {Function} cb    事件

	 */

	function addEvent(el, event, cb){

		if(el.addEventListener) {

			el.addEventListener(event, cb, false);

		} else if (el.attachEvent) {

			el.attachEvent('on' + event, cb); 

		} else {

			el['on' + event] = cb;

		}

	}



	/**

	 * 点击统计方法

	 * @param  {string} key   统计关键词

	 * @param  {string} refer 来源页面

	 */

	function clickCount(key, refer){

		var img = new Image();

        var guid = '_QIAO_REQ_' + new Date().getTime() + '_';



        window[guid] = img; 

        img.onload = img.onerror = function () {

            window[guid] = null;

        }

        img.src = URL_COUNT + '&click=' + key + '&source=' + refer + '&guid=' + guid;

        //img = null;

    }



    /**

     * 添加统计事件

     */

	function bindCount(){

		var newEl = document.getElementById('J_qiao_new');

		var oldEl = document.getElementById('J_qiao_old');

		var docEl = document.getElementById('J_qiao_doc');

		var android = document.getElementById('J_qiao_android');

		var classEl = document.getElementById('J_qiao_class');



		addEvent(newEl, 'click', function(){

			clickCount('qiao3.0', 'home');

		});

		addEvent(oldEl, 'click', function(){

			clickCount('qiao2.0', 'home');

		});

		addEvent(docEl, 'click', function(){

			clickCount('qiao-doc', 'home');

		});

		addEvent(android, 'click', function(){

			clickCount('qiao-android', 'home');

		});

		addEvent(classEl, 'click', function(){

			clickCount('qiao-class', 'home');

		});

	}



	/**

	 * 页面初始化方法

	 */

	function init(){

		//初始化轮播

		picRotation.init();



		bindCount();



		var tags = ['a'];

		var atags = null;

		for(var i = 0; i < tags.length; i++){

			atags = document.getElementsByTagName(tags[i]);

			for(var j = 0; j < atags.length; j++){

				atags[j].onfocus = function() {

					this.blur();

				};

			}

		}

	}



	init();



}());