var userAgent = navigator.userAgent.toLowerCase();
var config = new Object;
var stats_url = '/service/statistic/statcollector.do';
var ie = false;
var ie6 = false;
var ie7 = false;

if (userAgent.indexOf('msie') > -1) {
	ie = true;
	if (userAgent.indexOf('msie 6.0') > -1) {
		ie6 = true;
	}
	if (userAgent.indexOf('msie 7.0') > -1) {
		ie7 = true;
	}

}
else if (userAgent.indexOf('firefox') > -1) {
	firefox = true;
}

if (ie6) {
	_('<link rel="stylesheet" type="text/css" href="/common/v1/css/ie6.css" />');
}

if (ie7) {
	_('<link rel="stylesheet" type="text/css" href="/common/v1/css/ie7.css" />');
}

/*
try {
	_('<script type="text/javascript" src="/time/?mod=common"></script>');
}
catch (e) {}
*/

_('<script type="text/javascript" src="/common/v1/js/ddtabmenu.js"></script>');
_('<script type="text/javascript" src="/common/v1/js/ad.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/yahoo-dom-event/yahoo-dom-event.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/element/element-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/container/container_core-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/paginator/paginator-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/tabview/tabview-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/yahoo/yahoo-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/connection/connection-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/carousel/carousel-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/yui/animation/animation-min.js"></script>');
_('<script type="text/javascript" src="/common/v1/js/fbfuncs.js"></script>');
_('<script type="text/javascript" src="/common/v1/js/boxover.js"></script>');


function $(eid) {
	return document.getElementById(eid);
}

function _(str) {
	document.write(str);
}

function search_on_focus(field) {
	if(field.value=='Tìm kiếm'){ field.value = ''; }
}

function search_on_blur(field) {
	if(field.value.split(' ').join('') == ''){ field.value='Tìm kiếm';}
}

function bookmark(obj) {
	var url = 'http://baodanang.vn';
	if (ie) {
		obj.style.behavior = 'url(#default#homepage)';
		obj.setHomePage(url);
	}
	else if (firefox) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel('baodanang.vn', url, '');
	}
}

 function CreateBookmarkLink() {

	 title = "Báo Đà Nẵng"; 
	 url = "http://baodanang.vn";
		if (window.sidebar) { 
			window.sidebar.addPanel(title, url,"");
		} else if( window.external ) { // IE Favorite
			window.external.AddFavorite( url, title); }
		else if(window.opera && window.print) { // Opera Hotlist
			return true; }
 }


function get_url() {
	return window.location.href;
}


function get_parentcate_menu() {
	pcate = $('parentcate').getElementsByTagName('div');
	
	strHtml = '<ul>';
	strHtml += '<li class="home"><a rel="ct1" href="/"><span class="active-right"><span class="active"></span></span></a></li>';
	for (var i = 0; i < pcate.length; i++ ) {
		if(i == pcate.length -1 ) {
		strHtml += '<li class="last"><a rel="ct' + (i +2) + '" href="/channel/' + pcate[i].id + '/"><span class="active-right"><span class="active">' + pcate[i].innerHTML + '</span></span></a></li>';
		}
		else{
		strHtml += '<li><a rel="ct' + (i +2) + '" href="/channel/' + pcate[i].id + '/"><span class="active-right"><span class="active">' + pcate[i].innerHTML + '</span></span></a></li>';
	}
	}
	strHtml += '</ul>';

	_(strHtml);
}

function get_subcate_menu() {
	strHtml = '';
	pcate = $('parentcate').getElementsByTagName('div');
	scate = $('subcate').getElementsByTagName('div');
	strHtml += '<div id="ct1" class="tabcontent"><div id="home"><div id="now" class="date left"></div><script type="text/javascript">try{VO22105();}catch (e){}</script></div></div>';
		for (var i=0; i< pcate.length ; i++) {
			strHtml += '<div id="ct' + (i +2) + '" class="tabcontent"><ul>';
			var k = 0;
			for (var j = 0; j < scate.length; j++ ) {
					if(scate[j].className == pcate[i].id) {
						if (k == 0) {
							strHtml += '<li class="first"><a href="/channel/' + scate[j].id + '/" class="first">' + scate[j].innerHTML + '</a></li>';
						}
						else {
							strHtml += '<li><a href="/channel/' + scate[j].id + '/">' + scate[j].innerHTML + '</a></li>';
						}
						k++;
					}
				}
			strHtml += '</ul></div>';
	}
	_(strHtml);
	
}

function menu_select() {
	try {
		
		cate = window.location.href.split('/')[4];
		pcate = $('parentcate').getElementsByTagName('div');
		scate = $('subcate').getElementsByTagName('div');
		for (var i=0; i< pcate.length ; i++) {
			for (var j = 0; j < scate.length; j++ ) {
				if((cate == pcate[i].id) || (scate[j].className == pcate[i].id && cate == scate[j].id)) {
					return (i+1);
				}
			}
		}
		return 0;
	}
	catch (e) {
		return 0;
	}
}


var Caty = function(paging, list, rowsperpage, url) {
	this.paging = paging;
	this.list = list;
	this.url = url;
	this.rowsperpage = rowsperpage;

	this.render = function() {
		var list = this.list;
		var paging = this.paging;
		var rowsperpage = this.rowsperpage

		var handleSuccess = function(o) {

			if (o.responseText !== undefined) {
				var blah = document.createElement('div');
				blah.innerHTML = o.responseText;

				var items = blah.getElementsByTagName('blockquote');
				var data = new Array();

				for (var i = 0; i < items.length; i++) {
					data[i] = items[i].innerHTML;
				}

				if (data.length < 1) {
					YAHOO.util.Dom.get('list-cate-next').parentNode.style.display = 'none';
					document.getElementById('pagy').style.display="none";
				}

				var handlePagination = function (state) {
					var startIndex = state.recordOffset;
					var recs = data.slice(startIndex, startIndex + state.rowsPerPage);
					list.start = startIndex + 1;
					list.innerHTML = recs.join('');
					paginator.setState(state);
				};

				var paginator = new YAHOO.widget.Paginator({
					rowsPerPage: rowsperpage,
					totalRecords: data.length,
					containers: ['pagy'],
					template: '{PreviousPageLink}{NextPageLink}',
					nextPageLinkLabel: 'Trang sau',
					previousPageLinkLabel: 'Trang trước',
					nextPageLinkClass : 'yui-pg-next next',
					previousPageLinkClass : 'yui-pg-previous prev'
				});
				paginator.subscribe('pageChange', function() {
	/*				if(paginator.getCurrentPage() >= 10) {
						YAHOO.util.Dom.get('pagy').innerHTML = '<a href="/channel/5475/?key=cateid:' + $('cate-id').innerHTML + '&start=' + parseInt(11) + '" class="xemtatca">Xem tất cả</a>';
					}*/
					window.location.href = '#header';
				});

				paginator.subscribe('changeRequest', handlePagination);
				paginator.render();
				handlePagination(paginator.getState());
			}
		};

		var handleFailure = function(o) {
			YAHOO.util.Dom.get('list-cate-next').parentNode.parentNode.style.display = 'none';
			if (o.responseText !== undefined) {return null;}
		};

		var callback = {
			success: handleSuccess,
			failure: handleFailure
		};

		var request = YAHOO.util.Connect.asyncRequest('GET', this.url, callback);
	};
};


function clientSideInclude(id, url){
	
	var handleSuccess = function(o) {
		document.getElementById('boxtieudiem').innerHTML = o.responseText;
	};

	var handleFailure = function(o) {
		if (o.responseText !== undefined) {
			document.getElementById('boxtieudiem').style.display="none";
			return null;
		}
	};

	var callback = {
		success: handleSuccess,
		failure: handleFailure
	};
	var request = YAHOO.util.Connect.asyncRequest('GET', url, callback);

}

function changeWeatherInfo(strInfo) {
	var wImage = strInfo.split("|")[0];
	var wLow = strInfo.split("|")[1];
	var wHigh = strInfo.split("|")[2];
	var wDescription = strInfo.split("|")[3];
	var wCity = strInfo.split("|")[4];

		$('w-sky').src =  wImage;
		$('w-desc').innerHTML =wDescription;
		$('w-temp').innerHTML = wLow + '-' + wHigh + '<sup>o</sup>C';
	
}


function init_weather()
{
	var selObj = document.getElementById("location");
	changeWeatherInfo(selObj.value);

	// Init Onchange
	selObj.onchange = function() {
		changeWeatherInfo(this.value);
	}

}


function Ie6CountDown() {
	try{
		if ((ie6) || (ie7)){
			var newdiv = document.createElement('div');
			var divIdName = 'isFloat';
			newdiv.setAttribute('id', divIdName);
			newdiv.style.clear = "both";
			newdiv.style.cursor = "pointer";
			newdiv.style.background = "#ffd url(/common/v1/image/warn.gif) no-repeat 5px center";
			newdiv.style.padding = "5px 10px 5px 30px";
			newdiv.style.marginBottom = "10px";
			newdiv.style.borderBottom = "1px solid black"; 
			newdiv.style.position = "relative";
			newdiv.style.zIndex = "1";
			newdiv.style.top="0";
			newdiv.style.font ="10pt Tahoma";
			newdiv.style.color="#000";
			newdiv.onmouseover= linkHover;
			newdiv.onmouseout = linkOut;
			newdiv.onclick = linkClick;
			newdiv.innerHTML="Trình duyệt Internet Explorer của bạn đã lỗi thời. Bấm vào đây để tải về bản mới, nhanh và an toàn hơn.";
			document.body.insertBefore(newdiv, document.getElementById('main'));
		}

	}
	catch (e){}
}
function linkClick() {
	window.open('http://windows.microsoft.com/en-us/internet-explorer/products/ie/home');
} 

function linkHover() {
	this.style.backgroundColor='#316ac5'; 
	this.style.color='white';
} 

function linkOut() {
	this.style.backgroundColor='#ffd'; 
	this.style.color='black';
}

