// flip through pics in news items for R&A / Open Golf
// Niall King wrote this on 27th March 2008
hp_imgs = new Array();
currentimagelinks = new Array();
anchor = false;

hpprogtimeout = false;
hpcurrent=0;
anchornumber=0;
restartinterval = 4000;
proginterval = dictHP.fadeinterval;
progcount = 0;
galleryanchors = new Array();
currentgallerythumb = 0;

fadehppics=function() {
	clearInterval(hpprogtimeout);
    hpprogtimeout = setInterval(progressbar,proginterval);
	if (hp_imgs.length>0) hp_xfade();
}

function progressbar(){
	progcount = progcount +1
	if (progcount > 85) { 
		progcount=0; 
		fadehppics();
	}
	var progbar = $('progbar');
	progbar.style.width = progcount+'px' ;
}

prephpimgs=function() {
	styleheight='307px';
	stylewidth='496px';

	var parentdiv = $('hptopfeature');
	hp_imgs[0]=$('hpflip1');
	hp_imgs[1]=$('hpflip2');
	hp_imgs[2]=$('hpflip3');
	hp_imgs[3]=$('hpflip4');

	var brotherdiv = $('newstitles');
	var controls = document.createElement('div');
	controls.id = 'hpcontrols';
	brotherdiv.appendChild(controls);
	
	var progbar = document.createElement('span');
	progbar.id = 'progbar';
	progbar.innerHTML = '<!-- progress bar -->'; // don't want a self-closed div
	controls.appendChild(progbar);
	
	var controltext = document.createElement('div');
	controltext.id = 'hpcontroltext';
	controltext.innerHTML = '1 / '+hp_imgs.length;
    controls.appendChild(controltext);
    
    currentimagelinks = brotherdiv.getElementsByTagName('A');
	for (var xx=0; xx<currentimagelinks.length; xx++) {
		currentimagelinks[xx].id='currentimagelink'+xx;
		currentimagelinks[xx].title="DETAYLI BiLGi iCiN TIKLAYINIZ"; //dictionary this!!
		currentimagelinks[xx].innerHTML += "<span>DETAYLI BiLGi iCiN TIKLAYINIZ</span>"; //dictionary this!!
		currentimagelinks[xx].className='';		
//		currentimagelinks[xx].onclick=function() {
//			anchor=true;
//			anchornumber = getNumbers(this.id);
//			clearInterval(hpprogtimeout);
//			setTimeout(hp_xfade,50);
//			return false;
//		}
	}
	
	currentimagelinks[0].className='currentimage';
	
	var controlLeft = document.createElement('a');
	var controlRight = document.createElement('a');
	controlLeft.href='#';
	controlRight.href='#';
	controlLeft.id='hpcontrol_left';
	controlRight.id='hpcontrol_right';
	controls.appendChild(controlLeft);
	controls.appendChild(controlRight);	
	
    controlLeft.onclick = function() {				
        anchor=true;
		anchornumber=hpcurrent-1;	
		clearInterval(hpprogtimeout);
		setTimeout(hp_xfade,50);
		setTimeout(starthpfade,restartinterval);	
		return false;
	}
    controlRight.onclick = function() {
		anchor=true;
		anchornumber=hpcurrent+1;
		clearInterval(hpprogtimeout);
		setTimeout(hp_xfade,50);
		setTimeout(starthpfade,restartinterval);	
		return false;
	}

	if (parentdiv){
		if (hp_imgs.length>0) {	
			hp_imgs[0].style.opacity='1';
			hp_imgs[0].style.filter='(opacity=100)';
			hp_imgs[0].style.display='block';
			hp_imgs[0].xOpacity = 1;
		}	
		for (var hpi=1; hpi<hp_imgs.length; hpi++) {
			hp_imgs[hpi].style.opacity='0';
			hp_imgs[hpi].style.filter='(opacity=0)';
			hp_imgs[hpi].style.display='none';
			hp_imgs[hpi].xOpacity = 0;
		}

		hpcurrent=0;
		if (!anchor) {	
			hpprogtimeout = setInterval(progressbar,proginterval);
		}
	}
	
	var gallerythumbs = $('gallerythumbs');

	thumbanchors = gallerythumbs.getElementsByTagName('A');
	
	galleryimgs=$('gallerypreviewimage')
	galleryanchors=galleryimgs.getElementsByTagName('IMG');	

	gallerycaption = $('gallery_alt_text');
	
	if (galleryanchors.length >0) {
		galleryanchors[0].id='gallerypreview0';
		galleryanchors[0].xOpacity = 1;
		galleryanchors[0].style.display = 'block';
		galleryanchors[0].style.opacity='1';
		galleryanchors[0].style.filter='(opacity=100)';
	}
	for (var yy=1; yy<galleryanchors.length; yy++) {
		galleryanchors[yy].id='gallerypreview'+yy;
		galleryanchors[yy].xOpacity = 0;
		galleryanchors[yy].style.display = 'none';
		galleryanchors[yy].style.opacity='0';
		galleryanchors[yy].style.filter='(opacity=0)';
	}
	for (yy=0; yy<thumbanchors.length; yy++) {
		thumbanchors[yy].className='';
		thumbanchors[yy].id='thumbanchor'+yy;
		thumbanchors[yy].onclick = function() {
			var thumbno = getNumbers(this.id);
			var oldthumb = 'thumbanchor'+currentgallerythumb;
			$(oldthumb).className='';
			this.className = 'current';
			gallery_xfade(thumbno)
			captiondiv = $('gallery_alt_text');
			captiondiv.innerHTML=this.title;
			return false;
		}
	}
	thumbanchors[0].className='current';
	
	
}

function starthpfade() {
    anchor=false;
    progcount = 0;
    hpprogtimeout = setInterval(progressbar,proginterval);
}

function stophpfade(d){ 
    var i = (d.id);
    if (i.indexOf('hpvid') > -1){
        anchor=true;
		anchornumber = getNumbers(i)-1;
        if (hpprogtimeout) clearInterval(hpprogtimeout);
		setTimeout(hp_xfade,50);
    }
}

var currenthpnews = false;
function hphighlightthis(obj) {

	var litop = obj.offsetTop - 147; // find where to draw the highlight
	var daddy = obj.parentNode; // find the menu that contains this menu item
	daddy.style.backgroundPosition = '-1px '+litop+'px'; // set background position to draw the highlight under this li
	var kiddy = obj.getElementsByTagName('UL')[0];
	if (currenthpnews) {currenthpnews.style.display = 'none'}
	if (kiddy) {
		kiddy.style.display = 'block';
		currenthpnews = kiddy;
	}
}

function setOpacity(obj) {
	if(obj.xOpacity>.99) {
		obj.xOpacity = 1;
	}
	if(obj.xOpacity<0) {
		obj.xOpacity = 0;
	}
	obj.style.opacity = obj.xOpacity;
	obj.style.filter = "alpha(opacity=" + (obj.xOpacity*100) + ")";
	if(obj.xOpacity==1) {
		return;
	}
}	

function hp_xfade() {
		var progbar = $('progbar');
		if (!anchor) {
			nIndex = hpcurrent+1;
		} else {
			nIndex = anchornumber;
		}

		var maxcount = 4;
		if (nIndex==maxcount) { nIndex=0 ;}
		if (nIndex<0) {nIndex=3;}
		if (anchor) {
			progbar.style.width = '85px';
		}	
		currentimagelinks[hpcurrent].className='';
		currentimagelinks[nIndex].className='currentimage';

		cOpacity = hp_imgs[hpcurrent].xOpacity;
		nOpacity = hp_imgs[nIndex].xOpacity;
		cOpacity-=.1; 
		nOpacity+=.1;
		hp_imgs[nIndex].style.display = "block";
		hp_imgs[hpcurrent].xOpacity = cOpacity;
		hp_imgs[nIndex].xOpacity = nOpacity;
		setOpacity(hp_imgs[hpcurrent]); 
		setOpacity(hp_imgs[nIndex]);
		var controltext = document.getElementById('hpcontroltext');
		controltext.innerHTML = 1+nIndex+' / '+hp_imgs.length;
		if(cOpacity<=0) {	
			hp_imgs[hpcurrent].style.display = "none";
			hpcurrent = nIndex;
		} else {

			setTimeout("hp_xfade()",50);
		}

}

function gallery_xfade(nIndex) {
	cOpacity = galleryanchors[currentgallerythumb].xOpacity;
	nOpacity = galleryanchors[nIndex].xOpacity;
	cOpacity-=.1; 
	nOpacity+=.1;
	galleryanchors[nIndex].style.display = "block";
	galleryanchors[currentgallerythumb].xOpacity = cOpacity;
	galleryanchors[nIndex].xOpacity = nOpacity;
	setOpacity(galleryanchors[currentgallerythumb]); 
	setOpacity(galleryanchors[nIndex]);
	if(cOpacity<=0) {
		galleryanchors[currentgallerythumb].style.display = "none";
		currentgallerythumb = nIndex;
	} else {
		setTimeout('gallery_xfade('+nIndex+')',50);
	}
}



function toFarenheight(celcius) {
	return Math.round(celcius*(9/5))+32
}

function toCelcius(farenheight) {
	return Math.round((farenheight-32)*(5/9));
}

prepTemp = function() {
	currenttemp = 'c';
	temp1 = $('openhp_leftlinks').getElementsByTagName('SPAN')[0];
	temp2 = $('openhp_leftlinks').getElementsByTagName('SPAN')[1];
	var tempchange = $('openhp_leftlinks').getElementsByTagName('SPAN')[2].getElementsByTagName('A')[0];
	tempchange.onclick=function() {
		var o = '&#176;';
		this.innerHTML = o+'F to '+o+'C';
		var temp1v=temp1.innerHTML.replace(/[^0-9]/g,'');
		var temp2v=temp2.innerHTML.replace(/[^0-9]/g,'');
		if (currenttemp=='c') {
			this.innerHTML = o+'F TO '+o+'C';
			temp1.innerHTML = toFarenheight(temp1v)+o;
			temp2.innerHTML = toFarenheight(temp2v)+o;
			currenttemp = 'f';
		} else {
			this.innerHTML = o+'C TO '+o+'F';
			temp1.innerHTML = toCelcius(temp1v)+o;
			temp2.innerHTML = toCelcius(temp2v)+o;
			currenttemp = 'c';
		}
	}
}





function prepSlidingTabs() {
	slidingTabPanel = $('slidetabpanel');
	slidingTabs=[];
	iw = 133;
	tw = 454;
	ti = 35;
	if (slidingTabPanel) {
		var sT = slidingTabPanel.getElementsByTagName('DIV');
		currentSlidingTab = 2; // the third one is default zero based.
		y=0;
		for (var x=0; x<sT.length; x++) {
			if (sT[x].className.indexOf('slidetab')>-1) {
				slidingTabs[y]=sT[x]
				sT[x].id='st'+y;
				y++
				sT[x].onclick=function() {
					var me = (this.className.indexOf('current') > -1);
					if (!me) {
						activateTab(this);
					}
				}	
			}
		}
	}
}

function growtabs(newtabid, oldtabid) {
	newtab=$(newtabid);
	oldtab=$(oldtabid);
	var nw = newtab.offsetWidth+ti;
	var ow = oldtab.offsetWidth-ti;
	var repeat = true;
	if (nw>tw || ow<iw){
		nw = tw;
		ow = iw;
		repeat = false;
	}
	newtab.style.width = nw+'px';
	oldtab.style.width = ow+'px';
	if (repeat) {
		setTimeout('growtabs("'+newtab.id+'","'+oldtab.id+'")',20);
	} else {
		newtab.className='slidetab current';
		oldtab.className='slidetab';
	}
}

function activateTab(tab) {

	for (var x=0; x<slidingTabs.length; x++) {
		if(slidingTabs[x].className.indexOf('current') > -1) {
			current=slidingTabs[x];
		}
	}
	growtabs(tab.id, current.id);
}

//addLoadEvent(prepTemp);
addDOMLoadEvent(prephpimgs);
addDOMLoadEvent(prepSlidingTabs);
addDOMLoadEvent(didyouknowdevice);

function switchdyk(num){
    var dyk = $('dykdevice');
    if (dyk) {
        dyk.style.backgroundImage = 'url(/styles/images/open/dykdevice'+num+'.jpg)';
        var as=dyk.getElementsByTagName('A')[1];
        if (num==1) {
            as.style.top='301px';
            as.style.left='50px';
        }
        if (num==2) {
            as.style.top='290px';
            as.style.left='65px';
        }
        if (num==3) {
            as.style.top='289px';
            as.style.left='515px';
        }
        if (num==4) {
            as.style.top='288px';
            as.style.left='68px';
        }
    }
}

function didyouknowdevice() {
    var dyk = $('dykdevice');
    if (dyk) {
        var rand_no = Math.floor(4*Math.random())+1;
        switchdyk(rand_no);
        var as=dyk.getElementsByTagName('A');
        as[0].onclick=function(){
             var num=dyk.style.backgroundImage.replace(/[^0-9]/g,'');
             num=num-1;
             if (num<1) num=4;
             switchdyk(num);
             
             return false;
        }
        as[2].onclick=function(){
             var num=parseInt(dyk.style.backgroundImage.replace(/[^0-9]/g,''));
             num=num+1;
             if (num > 4) num = 1;
             switchdyk(num);
             return false;
        }      
    }
}






