/*
 * @Date  : 19-04-2007
 * @Desc  : Js coding for open & close the ToolTips
 */

var pipeobj = {
	expanddiv : 0,curdiv : 0,moveobj1 : 0,moveobj2 : 0,expanddivNo : 0,curdivNo : 0,
	start : new Array,end : new Array,disp : 0,pipeheadDest : 0,
	t : 0,duration: 300,per : 0,slideStart : 0,
	pipeheadH : 0,
	expandVal : 90,	
	collapseVal : 20,
	headHArr : new Array(),
	linkH :	70,		//(screen.width > 800)?70:90,
	
	tooltip_init : function(){
		var isIE = !!document.uniqueID;
		var htmltxt = '',pipeDivId = '';
		var pipediv = null,pipeHeading = '',pipeCaption = '';		
		var trimcharCount =	pipeobj.getTrimCharNo()-4;			//+3;
		var eachpipeW = (isIE)?eval(Math.floor(screen.width * 0.18)-13):eval(Math.floor(screen.width * 0.18)-8);
		var captiontrimchar	= function(){
			var charcount;
			if(screen.width >= 1400)
				charcount = 22;
			else if(screen.width >= 1280)
				charcount = 18;
			else if(screen.width >= 1152)
				charcount = 14;
			else if(screen.width >= 1024)
				charcount = 12;
			else if(screen.width >= 800)
				charcount = 10;
			else
				charcount = 12;
			return(charcount);
		}();

		if(document.location.href.search(/\/events\/?$/) > 1){
			trimcharCount -= 6;	
		}
		
		document.getElementById('pipehead').style.borderWidth = '1px';
		document.getElementById('pipehead').style.borderStyle = 'solid';
		document.getElementById('pipehead').style.borderColor = tooltipConfig.pipedivBorder;
		//document.getElementById('pipehead').style.position = 'absolute';

		
		for(var i=0;i<tooltipConfig.pipeCount;i++){	
			pipeDivId = 'pipe' + eval(i+1);
			pipeDivNo = i+1; 
			pipeHeading = tooltipConfig.pipeHeading[i];
			pipeCaption = tooltipConfig.pipeImgCaption[i];
			//--------------------------------------
			
			var temppipeHead = '',temppipeCaption = '';
			temppipeHead = pipeobj.strtobr(trimcharCount,pipeHeading);			
			
			// Currently The Logic is Not Used.
			/*pipeCaptionArr = pipeCaption.split('<br>');			
			for(var count=0;count<pipeCaptionArr.length;count++){
				temppipeCaption += pipeobj.strtobr(captiontrimchar,pipeCaptionArr[count]);
				temppipeCaption += '<br>';
			}
			temppipeCaption = temppipeCaption.substr(0,temppipeCaption.length-4);			

			
			if(temppipeCaption.split('<br>').length > 5){
				var x1 = eval(70 + (((temppipeCaption.split('<br>')).length - 5) * 10));				
			}*/
			
			/*if(pipeobj.linkH < x1)
				pipeobj.linkH = x1;*/

			//--------------------------------------

			//###### SET THE Pipeheading Array ######
				//pipeobj.headHArr[i] = (pipeHeading.length > trimcharCount)?30:20;
				pipeobj.headHArr[i] = eval(8 + ((temppipeHead.split('<br>')).length) * 12);
				//alert(pipeobj.headHArr[i]);

			//#######################################			

			if(i)
				htmltxt = '<div style="position:absolute;"><table cellspacing="0" cellpadding="2" width="100%" border="0" align="center" style="border-top:1pt solid ' + tooltipConfig.pipeBorder + ';">';
			else
				htmltxt = '<div style="position:absolute;"><table cellspacing="0" cellpadding="2" width="100%" border="0" align="center">';
			htmltxt += '<tr class="morecard" height="'+pipeobj.headHArr[i]+'px"><td align="left" width="14" valign="top">';
			htmltxt += '<img src="http://img.123greetings.com/images/plus.gif" id="opencloseimg'+eval(i+1)+'" border="0" width="14" height="14" title="Expand" onClick="pipeobj.easeDiv(\''+pipeDivNo+'\');">';
			htmltxt += '</td>';
			htmltxt += '<td align="left" colspan="2"><a href="'+tooltipConfig.pipeImgLinks[i]+'" style="font-weight:bold;" title="'+ tooltipConfig.pipeHeading[i] +'">' + temppipeHead + '</a></td></tr>';
			htmltxt += '<tr height="'+eval(pipeobj.linkH)+'px" valign="middle" class="morecard">';
			htmltxt += '<td>&nbsp;</td>';
			htmltxt += '<td width="60px" valign="middle" align="left">';
			htmltxt += '<a href="'+tooltipConfig.pipeImgLinks[i]+'" title="'+ tooltipConfig.pipeImgAltTxt[i] +'">';
			htmltxt += '<img src="' + tooltipConfig.pipeImageName[i] + '" id="pipeimg'+i+'" border="0" width="60px" height="60px" alt="'+ tooltipConfig.pipeImgAltTxt[i] +'">';
			htmltxt += '</a>';
			htmltxt += '</td>';
			htmltxt += '<td width="80px" align="center" valign="middle"><a href="'+tooltipConfig.pipeImgLinks[i]+'" style="font-weight:normal;">' + pipeCaption + '</td></tr>';
			htmltxt += '</table></div>';

			pipediv = document.createElement('div');
			pipediv.setAttribute('id',pipeDivId);
			pipediv.id = pipeDivId;
			pipediv.style.position = 'relative';
			//pipediv.style.width = '';
			pipediv.style.height = pipeobj.headHArr[i] + 'px';			
			pipediv.style.overflow = 'hidden';			
			pipediv.innerHTML = htmltxt;
			document.getElementById('pipehead').appendChild(pipediv);
		}
		pipeobj.firstexpandDiv(parseInt(tooltipConfig.pipeOpenNo));
	},

	strtobr : function(charLimt,str){
	//var str = '123456 7891234567891234 5678912345678 912';
		var str_arr = str.split(' ');
		var x = 0;
		for(var i=0;i<str_arr.length;i++){
			var temp_str = str_arr[i]; 
			//alert(temp_str.length);

			x += temp_str.length;
			//alert(x);
			
			if(x>charLimt){		
				var br_str = (i) ? '<br>' : '';
				if(temp_str.length > charLimt){			
					str_arr[i] = br_str + temp_str.substr(0,charLimt) + '-<br>' + temp_str.substr(charLimt,temp_str.length-charLimt);
					x = temp_str.length-charLimt;
				}else{
					
					str_arr[i] = br_str + temp_str;	
					x = temp_str.length;
				}		
				//alert(str_arr[i]);		
				//alert(x);
			}
			else{
				var space_str = (i) ? ' ' : '';
				str_arr[i] = space_str + temp_str;
				//alert(str_arr[i]);		
			}
			x++;
		}
		return(str_arr.join(''));
		//alert(str_arr.join(''));

	},


	firstexpandDiv : function(){
		var id = parseInt(arguments[0]);
		var tempH = 0;
		if(id>0){
			document.getElementById('pipe'+id).style.height = pipeobj.headHArr[id-1] + pipeobj.linkH + 'px';
			document.getElementById('opencloseimg'+id).src = 'http://img.123greetings.com/images/minus.gif';
			document.getElementById('opencloseimg'+id).title = 'Collapse';
			
			for(var i=0;i<tooltipConfig.pipeCount;i++)
				tempH += pipeobj.headHArr[i];
			
			document.getElementById('pipehead').style.height = eval(tempH + pipeobj.linkH + 2) + 'px';
			pipeobj.expanddiv = 'pipe'+id;
			pipeobj.expanddivNo = id;
		}
	},

	easeDiv : function(){
		pipeobj.curdivNo = parseInt(arguments[0]);

		if(!pipeobj.t){
			if(pipeobj.expanddiv == 0){
				pipeobj.fun(pipeobj.curdivNo,0);	//all pipes are closed,clicked pipe will be opened.
				pipeobj.expanddivNo = pipeobj.curdivNo;		

			}else if(pipeobj.expanddivNo == pipeobj.curdivNo){
				pipeobj.fun(pipeobj.curdivNo,0);	//close the pipe that is open now.
				pipeobj.expanddivNo = 0;

			}else if(pipeobj.expanddivNo != pipeobj.curdivNo){
				pipeobj.fun(pipeobj.curdivNo , pipeobj.expanddivNo);	//open a closed pipe & close the current opend pipe.
				pipeobj.expanddivNo = pipeobj.curdivNo;

			}
			pipeobj.changeActImg(pipeobj.expanddivNo);
		}
	},

	fun : function(d1,d2){
		var tempH = 0;
		for(var i=0;i<tooltipConfig.pipeCount;i++)
			tempH += pipeobj.headHArr[i];

		var curdivH = document.getElementById('pipe'+d1).offsetHeight;
		pipeobj.pipeheadH = document.getElementById('pipehead').offsetHeight;

		if(curdivH == eval(pipeobj.headHArr[d1-1]+pipeobj.linkH)){
			pipeobj.disp = -parseInt(pipeobj.linkH);
			pipeobj.start[0] = pipeobj.headHArr[d1-1]+pipeobj.linkH;
			pipeobj.end[0] = pipeobj.headHArr[d1-1];
			pipeobj.start[1] = 0;			
			pipeobj.end[1] = 0;
	
			pipeobj.pipeheadDest = parseInt(tempH);

		}else{
			pipeobj.disp = parseInt(pipeobj.linkH);
			pipeobj.start[0] = pipeobj.headHArr[d1-1];
			pipeobj.end[0] = pipeobj.headHArr[d1-1]+pipeobj.linkH;
			if(parseInt(d2)){
				pipeobj.start[1] = pipeobj.headHArr[d2-1]+pipeobj.linkH;
				pipeobj.end[1] = pipeobj.headHArr[d2-1];
			}else{
				pipeobj.start[1] = 0;
				pipeobj.end[1] = 0;

			}
			pipeobj.pipeheadDest = parseInt(eval(tempH + pipeobj.linkH + 2));
		}

		pipeobj.moveobj1 = d1;
		if(parseInt(d2)) pipeobj.moveobj2 = d2; else pipeobj.moveobj2;
		pipeobj.per = Math.PI/(2 * pipeobj.duration);
		pipeobj.slideStart = (new Date()).getTime();
		pipeobj.t = setInterval("pipeobj.fun1()",3);
		

	},

	fun1 : function(){

		
		
		var elapse = (new Date()).getTime() - pipeobj.slideStart;		
		if(elapse <= pipeobj.duration){						
			pipeobj.disp = (pipeobj.end[0] - pipeobj.start[0]) * Math.sin(this.per * elapse);	
			pipeobj.disp = Math.round(pipeobj.disp);
			document.getElementById('pipe'+pipeobj.moveobj1).style.height = eval(pipeobj.start[0]+pipeobj.disp) + 'px';			
			
			if(parseInt(pipeobj.moveobj2)){
				document.getElementById('pipe'+pipeobj.moveobj2).style.height = eval(pipeobj.start[1]-pipeobj.disp) + 'px';}
			else
				document.getElementById('pipehead').style.height = eval(pipeobj.pipeheadH + pipeobj.disp) + 'px'; 
		}else{			
			pipeobj.disp = 0;
			clearInterval(pipeobj.t);
			pipeobj.t=0;
			document.getElementById('pipe'+pipeobj.moveobj1).style.height = pipeobj.end[0] + 'px';
			if(parseInt(pipeobj.moveobj2)){
				document.getElementById('pipe'+pipeobj.moveobj2).style.height = pipeobj.end[1] + 'px';
				pipeobj.moveobj2 = 0;
			}else{
				document.getElementById('pipehead').style.height = pipeobj.pipeheadDest + 'px'; 
			}
		}
	},


	changeActImg : function(){
		obj = 'pipe'+arguments[0];
		for(var i=1;i<=tooltipConfig.pipeCount;i++){
			if(obj == 'pipe'+i){
				document.getElementById('opencloseimg'+i).src = 'http://img.123greetings.com/images/minus.gif';
				document.getElementById('opencloseimg'+i).title = 'Collapse';
			}else{
				document.getElementById('opencloseimg'+i).src = 'http://img.123greetings.com/images/plus.gif';
				document.getElementById('opencloseimg'+i).title = 'Expand';
			}
		}
	},

	
	getTrimCharNo : function(){
		var charcount;
		if(screen.width >= 1400)
			charcount = 37;
		else if(screen.width >= 1280)
			charcount = 34;
		else if(screen.width >= 1152)
			charcount = 30;
		else if(screen.width >= 1024)
			charcount = 26;
		else if(screen.width >= 800)
			charcount = 22;
		else
			charcount = 22;
		return(charcount);
	}
};




