// JavaScript Document
var panel, content, nav;
var page = 0;

App = function () { 

return {
		init: function (){
			getHot();
			nav = getEl('navigate');
			nav.setOpacity(0);
		}
	}
}();


YAHOO.ext.EventManager.onDocumentReady(App.init, App, true);

var listen = function (url) {
	
	var success = function (obj)
	{
		//alert(obj.responseText);
		//document.getElementById('ans'+id).innerHTML = obj.responseText;
		 var xml = obj.responseXML;
		 document.getElementById('content').innerHTML = "";
		
         channel = xml.getElementsByTagName('channel')[0];
         items = channel.getElementsByTagName('item');
		
		 for (i = 0; i < items.length; i++) {
			 
			 idEl  = items[i].getElementsByTagName('target')[0];
			 did = idEl.firstChild.nodeValue;
			
			 textEl  = items[i].getElementsByTagName('text')[0];
			 text = textEl.firstChild.nodeValue;
			  
			 codeEl = items[i].getElementsByTagName('code')[0];
			 codes =  codeEl.firstChild.nodeValue;
			 
			 try {
				 document.getElementById(did).innerHTML = text;
				 eval(codes);
				
			 } catch (e) { 
			 			alert("Invalid type of XML responce recieved.\nPlease try again!");
						} 			  
		 }
		  
	}
	
	var failure = function (obj)
	{
		alert("Fail to connect server!");	
	}
	
	YAHOO.util.Connect.asyncRequest('POST', url , {success:success , failure:failure} , 'none');
	  document.getElementById('content').innerHTML = "<br><center>Please wait...</center>";
}

var getHot = function () {
	listen('hot.php');
	setTimeout(getHot, 60000);
}

function intro () {
// introduction	
	size = getPageSize();
	middle = size[0]/2;
	sd = getEl('shade');
	panel = getEl('outer');
	content = getEl('inner');
	nav = getEl('navigate');
	nav.setOpacity(0);
	contdom = content.dom;
	panel.setOpacity(1);
	//drow shade
	sd.setOpacity(0);
	sd.setBox({x:0,y:0,width:size[0], height:size[1]});
	sd.setOpacity(.7, true, .7 , function () {
				//load page 1
				
				contdom.innerHTML = document.getElementById('p1').innerHTML;
				//drow box				
				panel.setBox({x:middle,y:20,width:0, height:size[3]-80});
				panel.setBox({x:middle-300,y:20,width:600, height:size[3]-80}, true, true, .7, function () {
									//navigate
									pdim = panel.getBox();
									cdim = content.getBox();
									nav.setBox({x:(middle-95),y:(pdim.height+20),width:395, height:40});
									nav.setOpacity(1);});
				});
	
	page = 1;
	
}

function movePage () {
	// scroll data
	pdim = panel.getBox();
	cdim = content.getBox();
	
	lift = cdim.height - pdim.height;
	pos = Math.abs(cdim.y - pdim.y);
	//alert(cdim.y+" "+pdim.y);
	if (pos > 10) {
		// move donword
		
		content.setY(pdim.y);;
	}
	else {
		content.setY(pdim.y-lift);
	}
	
}

function introPage ($page) {
	// moving to in-between pages	
	// shrink panel
	page++;
	if (page > 7) {
		closeIntro();
		return;
	}
	contdom.innerHTML = document.getElementById('p'+page).innerHTML;
	pdim = panel.getBox();
	cdim = content.getBox();
	content.setY(pdim.y);
	el = getEl('updown');
	if (pdim.height < cdim.height) {		
		el.setOpacity(1);
	}
	else {
		el.setOpacity(0);
	}
	
	return;
	size = getPageSize();
	middle = size[0]/2;
	panel.setBox({x:middle,y:20,width:0, height:size[3]-80}, true, true, .7, function () {
					//expand panel
					contdom.innerHTML = document.getElementById('p'+page).innerHTML;
					panel.setBox({x:middle-300,y:20,width:600, height:size[3]-40}, true, true);															   
																					   });
	
	
}

function closeIntro() {
	// close introduction window
	sd = getEl('shade');
	panel = getEl('outer');
	size = getPageSize();
	middle = size[0]/2;
	nav.setOpacity(0);
	nav.setBox({x:0,y:0,width:0, height:0});
	panel.setBox({x:middle,y:20,width:0, height:size[3]-80}, true, true, .7,  function () {
				panel.setBox({x:0,y:0,width:0, height:0});			
				sd.setOpacity(0, true, .7 , function () { 
							sd.setBox({x:0,y:0,width:0, height:0});	
							panel.setOpacity(0);
													   });
								});
	/*,*/
}
	


// Returns array with page width, height and window width, height
// get page size array
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;}
	
