// JavaScript Document
var jx, slideShow, imgdir, subLinks, backToTop, resizingDiv;
function loadPage(f, t, _d){
	if(subLinks){
		misc.removeElement(subLinks);
		subLinks = '';
	};
	
    var d=$(_d), welcome=$('welcome'), h=$('heading'), jx = new AjaxTicker();
    welcome.style.display = 'none';
    
    d.innerHTML = '<table border="0" align="center" cellpadding="3" cellspacing="0"><tr><td>&nbsp;</td><td height="70">&nbsp;</td></tr><tr><td><img src="../lib1.5/imgs/srch-loading.gif" width="16" height="16" /></td><td>Loading '+t+', Please Wait....</td></tr></table>';
    
    if(t)h.innerHTML = t;
    
    jx.url = f;
    jx.loadInto = function(html){
        d.innerHTML = html;
        
        switch(t.toUpperCase()){
			case "CONTACTS":
				loadPage('contacts/pakistan.htm', '', 'contactsPort');
			break;
			
			case "CUSTOMER CARE":
				loadPage('customerCare/genuineParts.htm', '', 'subPort');
			break;
			
            case "2 WHEELERS":
                loadPage('products/2Wheelers/70cc.htm', '2 Wheelers - 70cc', 'productPort');
                Link.onMouseUp($('linkProducts'));
            break;
            
            case "3 WHEELERS":
                loadPage('products/3Wheelers/175cc.htm', '3 Wheelers - 175cc', 'productPort');
                Link.onMouseUp($('linkProducts'));
            break;
            
            case "4 WHEELERS":
				loadPage('products/4Wheelers/fourWheelers.htm', '4-Wheelers', 'productPort');
                Link.onMouseUp($('linkProducts'));
            break;
            
            case "AUTO PARTS":
				loadPage('products/autoParts/sheetMetal.htm', 'Auto Parts - Sheets Metal', 'productPort');
                Link.onMouseUp($('linkProducts'));
            break;
			
			case "NEWS & EVENTS":
			case "GALLERY":
				var imgid = 'image';
				//var imgdir = 'newsEvents/nbp/fullsize';
				var imgext = '.jpg';
				var thumbid = 'thumbs';
				var auto = true;
				var autodelay = 5;
				
				
				
				slideShow=function(){
					var bxs,bxe,fxs,fxe,ys,ye,ta,ia,ie,st,ss,ft,fs,xp,yp,ci,t,tar,tarl;
					ta=document.getElementById(thumbid); ia=document.getElementById(imgid);
					t=ta.getElementsByTagName('li'); ie=document.all?true:false;
					st=3; ss=3; ft=10; fs=5; xp,yp=0;
					return{
						init:function(){
							document.onmousemove=this.pos; window.onresize=function(){setTimeout("slideShow.lim()",500)};
							ys=this.toppos(ta); ye=ys+ta.offsetHeight;
							len=t.length;tar=[];
							for(i=0;i<len;i++){
								var id=t[i].value; tar[i]=id;
								t[i].onclick=new Function("slideShow.getimg('"+id+"')");
								if(i==0){this.getimg(id)}
							}
							tarl=tar.length;
						},
						scrl:function(d){
							clearInterval(ta.timer);
							var l=(d==-1)?0:(t[tarl-1].offsetLeft-(ta.parentNode.offsetWidth-t[tarl-1].offsetWidth)+10)
							ta.timer=setInterval(function(){slideShow.mv(d,l)},st);
						},
						mv:function(d,l){
							ta.style.left=ta.style.left||'0px';
							var left=ta.style.left.replace('px','');
							if(d==1){
								if(l-Math.abs(left)<=ss){
									this.cncl(ta.id); ta.style.left='-'+l+'px';
								}else{ta.style.left=left-ss+'px'}
							}else{
								if(Math.abs(left)-l<=ss){
									this.cncl(ta.id); ta.style.left=l+'px';
								}else{ta.style.left=parseInt(left)+ss+'px'}
							}
						},
						cncl:function(){clearTimeout(ta.timer)},
						getimg:function(id){
							if(auto){clearTimeout(ia.timer)}
							if(ci!=null){
								var ts,tsl,x;
								ts=ia.getElementsByTagName('img'); tsl=ts.length;x=0;
								for(x;x<tsl;x++){
									if(ci.id!=id){var o=ts[x]; clearInterval(o.timer); o.timer=setInterval(function(){slideShow.fdout(o)},fs)}
								}
							}
							if(!document.getElementById(id)){
								var i=document.createElement('img');
								ia.appendChild(i);
								i.id=id; i.av=0; i.style.opacity=0;
								i.style.filter='alpha(opacity=0)';
								i.src=imgdir+'/'+id+imgext;
							}else{
								i=document.getElementById(id); clearInterval(i.timer);
							}
							i.timer=setInterval(function(){slideShow.fdin(i)},fs);
						},
						nav:function(d){
							var c=0;
							for(key in tar){if(tar[key]==ci.id){c=key}}
							if(tar[parseInt(c)+d]){
								this.getimg(tar[parseInt(c)+d]);
							}else{
								if(d==1){
									this.getimg(tar[0]);
								}else{this.getimg(tar[tarl-1])}
							}
						},
						auto:function(){ia.timer=setInterval(function(){slideShow.nav(1)},autodelay*1000)},
						fdin:function(i){
							if(i.complete){i.av=i.av+fs; i.style.opacity=i.av/100; i.style.filter='alpha(opacity='+i.av+')'}
							if(i.av>=100){if(auto){this.auto()}; clearInterval(i.timer); ci=i}
						},
						fdout:function(i){
							i.av=i.av-fs; i.style.opacity=i.av/100;
							i.style.filter='alpha(opacity='+i.av+')';
							if(i.av<=0){clearInterval(i.timer); if(i.parentNode){i.parentNode.removeChild(i)}}
						},
						lim:function(){
							var taw,taa,len; taw=ta.parentNode.offsetWidth; taa=taw/4; 
							bxs=slideShow.leftpos(ta); bxe=bxs+taa; fxe=bxs+taw; fxs=fxe-taa;
						},
						pos:function(e){
							xp=ie?event.clientX+document.documentElement.scrollLeft:e.pageX; 
							yp=ie?event.clientY+document.documentElement.scrollTop:e.pageY;
							if(xp>bxs&&xp<bxe&&yp>ys&&yp<ye){
								slideShow.scrl(-1);
							}else if(xp>fxs&&xp<fxe&&yp>ys&&yp<ye){
								slideShow.scrl(1);
							}else{slideShow.cncl()}
						},
						leftpos:function(t){
							var l=0;
							if(t.offsetParent){
								while(1){l+=t.offsetLeft; if(!t.offsetParent){break}; t=t.offsetParent}
							}else if(t.x){l+=t.x}
							return l;
						},
						toppos:function(t){
							var p=0;
							if(t.offsetParent){
								while(1){p+=t.offsetTop; if(!t.offsetParent){break}; t=t.offsetParent}
							}else if(t.y){p+=t.y}
							return p;
						}
					};
				}();
				
				slideShow.init();
				slideShow.lim();
			break;
        };
    };
    jx.initialize();
};

var Link = {
	activeLink : '',
	onMouseOver : function(td){
		if(td.id.toUpperCase()=="LINKPRODUCTS"){
			if(subLinks)return;
			var lnk = misc.getCoords(td);
			var mh = $('movieHolder');
			var _mh = misc.getCoords(mh);
			subLinks = new Div();
			
			with(subLinks.style){
				top = (lnk.exactTop+lnk.height+2) + 'px';
				left = _mh.exactLeft + 'px';
				width = _mh.width + 'px';
				height = '35px';
				background = 'transparent url(images/jpg/buttonSeparator.jpg)';
				position = 'absolute';
			};
			
			subLinks.onmousemove = function(e){
				if(!e)e = window.event;
				var el = e.srcElement || e.target;
				
				if(misc.isDescendant(el, this))return;
				
				misc.removeElement(this);
				subLinks = '';
			};
			
			var tbl = new Table(1,7);
			with(tbl){
				//align = 'left';
				width = _mh.width-20;
				with(rows[0]){
					with(style){
						fontWeight = 'bold';
						backgroundImage = 'url(images/jpg/buttonSeparator.jpg)';
						style.cursor = 'default';
					};
					
					with(cells[0]){
						align = 'center';
						//vAlign = 'bottom';
						innerHTML = '2 Wheelers';
						height = 28;
						style.cursor = 'pointer';						
						onmouseup = function(){
							loadPage('products/2Wheelers/index.htm', '2 Wheelers', 'viewPort');
							misc.removeElement(subLinks);
							subLinks = '';
						};
					};
					
					with(cells[1]){
						align = 'center';
						//vAlign = 'bottom';
						width = 2;
						innerHTML = '|';
					};
					
					with(cells[2]){
						align = 'center';
						//vAlign = 'bottom';
						innerHTML = '3 Wheelers';
						style.cursor = 'pointer';						
						onmouseup = function(){
							loadPage('products/3Wheelers/index.htm', '3 Wheelers', 'viewPort');
							misc.removeElement(subLinks);
							subLinks = '';
						};
					};
					
					with(cells[3]){
						align = 'center';
						//vAlign = 'bottom';
						width = 2;
						innerHTML = '|';
					};
					
					with(cells[4]){
						align = 'center';
						//vAlign = 'bottom';
						innerHTML = '4 Wheelers';
						style.cursor = 'pointer';						
						onmouseup = function(){
							loadPage('products/4Wheelers/index.htm', '4 Wheelers', 'viewPort');
							misc.removeElement(subLinks);
							subLinks = '';
						};
					};
					
					with(cells[5]){
						align = 'center';
						//vAlign = 'bottom';
						width = 2;
						innerHTML = '|';
					};
					
					with(cells[6]){
						align = 'center';
						//vAlign = 'bottom';
						innerHTML = 'Auto Parts';
						style.cursor = 'pointer';						
						onmouseup = function(){
							loadPage('products/autoParts/index.htm', 'Auto Parts', 'viewPort');
							misc.removeElement(subLinks);
							subLinks = '';
						};
					};
				};
			};
			
			subLinks.appendChild(tbl);
			mh.appendChild(subLinks);
		}
		else{
			if(subLinks)misc.removeElement(subLinks);
			subLinks = '';
		};
		
		if(td==this.activeLink)return;
		
		td.className = "linkHover";
		
		var alpha = 40;
		with(td.style){
			filter = 'progid:DXImageTransform.Microsoft.alpha(opacity=\''+ alpha +'\')';
			opacity = alpha/100;
		};
		
		var timer = setInterval(function(){
			if(!td.style){
				clearInterval(timer);
				return;
			};
			
			with(td.style){
				filter = 'progid:DXImageTransform.Microsoft.alpha(opacity=\''+ alpha +'\')';
				opacity = alpha/100;
				alpha+=10;
				if(alpha>100)clearInterval(timer);
			};
		}, 50);
	},
	
	onMouseOut : function(td){
		if(td==this.activeLink)return;
		
		td.className = "linkNormal";
		td.removeAttribute("style");
	},
	
	onMouseUp : function(td){
		if(td!=this.activeLink){
			if(this.activeLink)this.activeLink.className = "linkNormal";
			td.className = "linkActive";
			td.removeAttribute("style");
			this.activeLink = td;
		};
		
		var top;
		if(td.id.toUpperCase()=="LINKPRODUCTS")top = misc.getCoords($('scrollProducts')).exactTop;
		else top = misc.getCoords($('scrollDefault')).exactTop;
		
		Window.scrollTo(top-5);
	}
};

window.onload = function(){
  backToTop = $('backToTop');
  resizingDiv = $('resizingDiv');
  
  backToTop.innerHTML = '\u25B2'+"Top";
  backToTop.isClicked = false;
  
  backToTop.onmouseup = function(){
    this.isClicked = true;
    this.style.display = 'none';
    Window.scrollTo(0);
  };
  
	with(Link){
    if(!activeLink){
      with(activeLink = $('defaultLink')){
        className = "linkActive";
        removeAttribute("style");
      };
    };
  };
};

window.onscroll = function(){
  if(document.body.scrollTop<500){
    backToTop.isClicked = false;
    backToTop.style.display = 'none';
    return;
  };
  if(backToTop.isClicked)return;
  resizingDiv.style.height = (document.body.scrollTop) + 'px';
  misc.removeAttribute(backToTop.style, 'display');
};
