//=================
var currentPage = 0;
var delay = 9000, fadeDelay = 100;
var percent=5;
var ptechData =  new Array();
var appeared = false;
var ptechProd= new Array();
var currentNews = 0;
var newsList= new Array();

function bottom(showPtechProducts){
	var bh = 0;
	if (window.innerHeight) bh = window.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight != 0) bh = document.documentElement.clientHeight;
	else if (document.body) bh = document.body.clientHeight;

	var footer=document.getElementById("footer");
	var contentWrapperHeight = document.getElementById("content-wrapper").offsetHeight;
	var headerHeight = document.getElementById("header").offsetHeight+document.getElementById("newslist").offsetHeight+21;
	var contentHeight=contentWrapperHeight+headerHeight;//100=header
	var desiredOffset = bh - (contentHeight+footer.offsetHeight);

	if(desiredOffset > 0) {
		footer.style.top = desiredOffset + 'px';
	}
	else footer.style.top ='0px';
	if(showPtechProducts) ptechProduct();
	pNewsList();
}

function popupNewWindow(imgURL, title) {
	var img = new Image();
	img.src = imgURL;
	var w =
	popupDialog(title,imgURL,0,0,800,600);
}

function ptechProduct() {
	if(ptechProd.length==0 || (typeof(newsList)!="undefined" && newsList.length==0)){
		var uri = "/ProductAdvServlet?pg="+currentPage+"&ajax=1";
		var ajax =new Ajax(sucessfullyDisplayPtechProdDetails,failToDisplayPtechProdDetails,null);
		ajax.get(uri);
	}else {
		showPtechProduct();
		if(ptechProd.length>0){
			if(currentPage==ptechProd.length-1) currentPage=0;
			else currentPage+=1;
		}
		if(typeof(newsList)!="undefined" && newsList.length>0){
			if(currentNews>=newsList.length-1) currentNews=0;
			else currentNews+=1;
		}else currentNews=0;
		return;
	}
}

function showPtechProduct() {
	var vmarquee = document.getElementById("vmarquee");
	var html ="<h3><a href='"+ptechProd[currentPage].url+"'>"+ptechProd[currentPage].name+"</a></h3>";
	if(ptechProd[currentPage].image !="")
		html+= "<center><img src='/index_images/ptech_products/"+ptechProd[currentPage].image+"' align='center' width='90%' height='200'></center><br/>";
	html+="<p>"+ptechProd[currentPage].desc+"<a href='"+ptechProd[currentPage].url+"'>Read More&raquo;</a></p>";
	if(vmarquee!=null && typeof(vmarquee)!="undefined") {
		vmarquee.innerHTML = html;
		vmarquee.style.MozOpacity = 10;
		vmarquee.style.opacity = 1;
		vmarquee.style.filter = 'alpha(opacity=100)';
	}
	if(typeof(newsList)!="undefined" && newsList.length>0 && typeof(newsList[currentNews])!="undefined"){
		var newslistDiv = document.getElementById("newslist");
		var para ="NEWS(<font size='1''>Click to read more</font>) - <a href='/news/news-details.jsp?id="+newsList[currentNews].id+"'>"+newsList[currentNews].name+"</a>";
		newslistDiv.innerHTML = para;
		newslistDiv.style.MozOpacity = 10;
		newslistDiv.style.opacity = 1;
		newslistDiv.style.filter = 'alpha(opacity=100)';
	}
	setTimeout("fade(\"newslist\",\"vmarquee\")",delay);
}

function sucessfullyDisplayPtechProdDetails(ajax,response,params) {
	//alert(response);
	var obj = eval('('+response+')');
	ptechProd=obj.products;
	newsList=obj.news;
	showPtechProduct();
	currentPage+=1;
	currentNews+=1;
}

function failToDisplayPtechProdDetails(ajax,response,params){}

function pNewsList(){
	if(newsList.length==0){
		var uri = "/ProductAdvServlet?news=1&ajax=1";
		var ajax =new Ajax(sucessfullyDisplayNewsList,failToDisplayNewsList,null);
		ajax.get(uri);
	}else {
		showNewsList();
		//alert(currentNews+ "leng: "+ newsList.length);
		if(currentNews==newsList.length-1) {currentNews=0;}
		else currentNews+=1;
		return;
	}
}
function sucessfullyDisplayNewsList(ajax,response,params) {
	//alert(response);
	var obj = eval('('+response+')');
	newsList=obj.news;
	showNewsList();
	currentNews+=1;
}

function failToDisplayNewsList(ajax,response,params){}

function showNewsList() {
	var newslist = document.getElementById("newslist");
    if(newsList!=null && typeof(newsList)!="undefined" && typeof(newsList[currentNews])!="undefined"){
        var html = "NEWS(<font size='1''>Click to read more</font>) - <a href='/news/news-details.jsp?id="+newsList[currentNews].id+"'>"+newsList[currentNews].name+"</a>";
        //alert(html);
        newslist.innerHTML = html;
        newslist.style.MozOpacity = 10;
        newslist.style.opacity = 1;
        newslist.style.filter = 'alpha(opacity=100)';
    }
  //setTimeout("fade('newslist')",delay);
}
/** FADE OUT EFFECT
 */
function fade(domId1, domId2) {
	var obj1 = document.getElementById(domId1);
	var obj2 = document.getElementById(domId2);
	if(obj1.style.display == "none" && obj2.style.display == "none") return false; //Return false if the element is already hidden
	var alpha = 10;
  function f(){ //Internal function
    alpha--;
		if(obj1!=null && typeof(obj1)!="undefined"){
			obj1.style.MozOpacity = alpha;
			obj1.style.opacity = alpha/10;
			obj1.style.filter = 'alpha(opacity=' + alpha*10 + ')';
		}
		if(obj2!=null && typeof(obj2)!="undefined"){
			obj2.style.MozOpacity = alpha;
			obj2.style.opacity = alpha/10;
			obj2.style.filter = 'alpha(opacity=' + alpha*10 + ')';
		}
    if(alpha > 1) setTimeout(f, fadeDelay);
    else {ptechProduct();}
  }
  setTimeout(f, fadeDelay); //This is where we call the f() function for the first time
}
/** FADE IN EFFECT
 */
function appear(domId){
  obj = document.getElementById(domId);
  if(obj.style.display != "none") return false; //Return if it is already being displayed
  obj.style.display = "block";
  var alpha = 0;
  function a(){ //Internal function
	  alpha++;
    obj.style.MozOpacity = alpha;
	  obj.style.opacity = alpha/10;
		obj.style.filter = 'alpha(opacity=' + alpha*10 + ')';
	  if(alpha < 11) setTimeout(a, fadeDelay);
		else { }
  }
  setTimeout(a, fadeDelay); //This is where we call the a() function for the first time
}

