/**
 *
 * 全局JS脚本文件
 *
 * @FileName: global.js
 * @Auther: Pandao
 * @QQ: 272383090
 * @CreateTime: 2013-08-19 14:46:43 
 * @UpdateTime: 2013-08-21 15:06:01 
 * Copyright@2013 泉州市科伟信息咨询有限公司版权所有，禁止非法用于商业用途，否则后果自负!
 */

function _$(selector) {
	return document.querySelectorAll(selector);
}

function $id(id) {
	return document.getElementById(id);
}

function $tag(tagName) {
	return document.getElementsByTagName(tagName);
}

function $class(className) {
	return document.getElementsByClassName(className);
}

function isSupportHTML5() {
	return Boolean(navigator.userAgent.match(/AppleWebKit/i));
}

function isOperaMobile() {
	return Boolean(navigator.userAgent.match(/Opera Mobi/i));
}

function isiOS4() {
    // 判断是否为iPhone/iPad/iPod
    if( (navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPod/i)) ) {        
        return ( Boolean(navigator.userAgent.match(/OS [5-9]_\d[_\d]* like Mac OS X/i)) ) ? false : true; //判断系统版本号是否大于4

    } else {
		return false;
    } 
}

function getCurrentStyle(obj, prop) { 
	 //W3C标准浏览器
	if (window.getComputedStyle) {
		var propprop = prop.replace(/([A-Z])/g, "-$1");
		propprop = prop.toLowerCase();
		return document.defaultView.getComputedStyle(obj, null)[propprop];
	}

	return null;
}

var header, footer;

window.addEventListener('DOMContentLoaded', function(){ 
	footer = $tag("footer")[0];
	header = $tag("header")[0];

	header.addEventListener('touchmove', function(e){ 
		e.preventDefault();
	});

	footer.addEventListener('touchmove', function(e){ 
		e.preventDefault();
	});
	
	//console.log(_$("section")[0]);
	//$tag("section")[0].innerHTML = "isiOS4=>"+isiOS4()+"<br/>"+navigator.userAgent+"<br/>screen.width=>"+window.screen.width+", screen.height=>"+window.screen.height;

	//禁用拖动viewport
	document.ontouchstart = document.ontouchmove = function(e) {
		//e.preventDefault();
	} 	
});

window.addEventListener('load', function() { 

	//隐藏地址栏 & 处理事件的时候，防止滚动条出现
	setTimeout(function(){ window.scrollTo(0, 1); }, 100); 

	if(isiOS4 || isOperaMobile) {// || !isSupportHTML5
		window.onscroll = document.ontouchstart = document.ontouchmove = document.ontouchend = function(){
			var scrollTop = document.documentElement.scrollTop; 
			footer.style.bottom = scrollTop + document.documentElement.clientHeight - footer.offsetHeight;  
		};
	}

	var searchBtn = $id("search-btn");

	searchBtn.onclick = searchBtn.ontouchstart = searchBtn.ontouchmove = searchBtn.ontouchend = function(e) { 
		var searchBox = $id("search-box");
		var keyword = $id("search-keyword"); 

		if(e.touches) {
			var touch = e.touches[0];
			var x = touch.clientX;
			var y = touch.clientY;
		}		

		if(keyword.value == "") {
			searchBox.style.display = (getCurrentStyle(searchBox, 'display') == "none") ? "block" : "none"; 
		}

		if(keyword.value != "") {
			location.href= searchBox.attributes["action"].nodeValue + "?keyword=" + keyword.value;
		} 
		e.preventDefault();
	};

	searchBtn.ontouchmove = function(e){ 
		//e.preventDefault();
	};
});

/*$(document).ready(function() {
	//conHeight();
	//$(window).resize(conHeight);
	$(window).on("touchmove", function() {
		var top = $(window).scrollTop();
		$("footer").css({top : window.screen.height + top - $("footer").outerHeight() });    
	}).on("touchstop", function() {
		var top = $(window).scrollTop();
		$("footer").css({top : window.screen.height + top - $("footer").outerHeight() });  
	});

	//禁用(类似)ipad的uiwebview下拉/上拉更新动作
	$("header, footer").on('touchmove',function(e){
		e.preventDefault();
	});
});*/
/*
$(function() {
	$(document).on('touchmove',function(e){
		e.preventDefault();
	});
	//uses body because jquery on events are called off of the element they are
	//added to, so bubbling would not work if we used document instead.
	$('body').on('touchstart','.scrollable',function(e) {
		if (e.currentTarget.scrollTop === 0) {
			e.currentTarget.scrollTop = 1;
		} else if (e.currentTarget.scrollHeight === e.currentTarget.scrollTop + e.currentTarget.offsetHeight) {
			e.currentTarget.scrollTop -= 1;
		}
	});
	//prevents preventDefault from being called on document if it sees a scrollable div
	$('body').on('touchmove','.scrollable',function(e) {
		e.stopPropagation();
	});
});*/