// JavaScript Document

var openEffect = null;
var closeEffects = new Array(1);
var lockBoxes = false;

var openNavEffect = null;
var closeNavEffects = new Array();

function initHome(anim,mode) {
	var anim = anim ? anim:false;
	
	runNews();
	$('boxC1').onmouseover = function() { openBox(1); };
	$('boxC2').onmouseover = function() { openBox(2); };
	$('boxC3').onmouseover = function() { openBox(3); };
	$('boxC2').setAttribute('open','true');
	if (mode == 'publish') { $('boxC3').absolutize(); }
	//$('boxC3').style.position = 'absolute';

	fixPNGbg();

	// IE PNG Fix
	if (Prototype.Browser.IE == true && /MSIE 6/.test(navigator.userAgent) == true) {
		for(var i=0; i<document.images.length; i++) {
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
	
	/*if (anim == true) {
		setTimeout("handAnim();",1000);
	} else {
		lockBoxes=false;
		$('handContainer').style.display='none';
		setTimeout("techAnim();",1000);
	}*/
} //end function

function handAnim() {
	new Effect.Morph($('hand'),{style:'top:0px',duration:1.0, afterFinish: function() { 
		setTimeout("new Effect.Morph($('hand'),{style:'top:385px',duration:1.0, afterFinish: function() { lockBoxes=false; $('handContainer').style.display='none'; techAnim(); }});",1000);
	} });
}


function initSub() {
	fixPNGbg();
	
	subnav = $('leftNav').childElements();
	for (var i=0;i<subnav.length;i++) {
		if (subnav[i].tagName == 'DIV') {
			subnav[i].onmouseover = function() { openNav(this); return false; };
			subnav[i].style.height = subnav[i].childElements()[0].getHeight() + 'px';
			if (subnav[i].className == 'on') { openNav(subnav[i]); }
		}
	} //end for
	
} //end function


function fixPNGbg() {

	if (Prototype.Browser.IE == true && /MSIE 6/.test(navigator.userAgent) == true) {
		$('outsideContainer').style.backgroundImage = 'url(http://67.217.101.166/images/spacer.gif)';
		$('footerShadow').style.backgroundImage = 'url(http://67.217.101.166/images/spacer.gif)';
		$('outsideContainer').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://67.217.101.166/images/shadow_bg.png', sizingMethod='scale')";
		$('footerShadow').style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='http://67.217.101.166/images/shadow_footer.png', sizingMethod='scale')";
		
		//alert($$('.qmshadow'));
		/*var navFooters = $('qm0').descendants();
		//alert(navFooters);
		for (var i=0;i<navFooters.length;i++){
			if (navFooters[i].tagName == 'DIV') {
				//alert(navFooters[i].innerHTML);
				navFooters[i].style.backgroundImage = 'url(images/spacer.gif)';
				navFooters[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='images/shadow_footer.png', sizingMethod='scale')";
			}
			//alert(navFooters[i].style.background);
		}*/
	}
}


function fixIE6PNG() {
	// IE PNG Fix
	if (Prototype.Browser.IE == true && /MSIE 6/.test(navigator.userAgent) == true) {
		for(var i=0; i<document.images.length; i++) {
			var img = document.images[i]
			var imgName = img.src.toUpperCase()
			
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
				i = i-1
			}
		}
	}
}

function searchString(method) {
	if (method == 'blur' && $('search').value == '') { $('search').value = 'Search'; }
	if (method == 'focus' && $('search').value == 'Search') { $('search').value = ''; }
} //end function


var newsTimeout = null;

function runNews(direction) {
	dir = direction ? direction : null;
	newsArea = $('newsBar');
	newsStories = $(newsArea).descendants();
	
	if (newsStories.length > 7) {
		
		if (direction == 'last') {
			clearTimeout(newsTimeout);

			try { 

				Effect.Pulsate(newsStories[6], { duration:0.5, from:1.0, pulses:1, afterFinish:function(){
					new Effect.Morph(newsStories[6], { style:'text-indent:-900px;', duration:1.1, afterFinish:function() { 
						newsStories[6].style.visibility='hidden'; 
						newsStories[6].style.textIndent='0px';
						
						//alert(newsStories[6]);
						Element.insert(newsStories[6], { 'before': newsStories[newsStories.length-1] });
						newsStories = $(newsArea).descendants();
						
						/*	var test = '';
							for (var i=0;i<newsStories.length;i++) {
								test += i + ': ' + newsStories[i] + '\n';
							}
						alert(test);*/
						if (newsStories[6].innerHTML.length > 130) { newsStories[6].innerHTML = shortenText(newsStories[6].innerHTML,127); };
						newsStories[6].style.filter='alpha(opacity=0)';
						newsStories[6].style.opacity=0; 
						newsStories[6].style.visibility=''; 
						newsStories[6].style.width='790px';
						newsStories[6].style.overflow='hidden';
						newsStories[6].style.whiteSpace='nowrap'; 
						new Effect.Opacity(newsStories[6], {duration: 1.2, from:0.0, to:1.0, afterFinish: function() {   }});
						runNews(); } 
					});
				}});
				
			} catch(e) {
				alert(e.description);
			}

		
		} else {
			clearTimeout(newsTimeout);
			newsAction = "Effect.Pulsate(newsStories[6], { duration:0.5, from:1.0, pulses:1, afterFinish:function(){ newsStories[7].style.visibility='hidden'; new Effect.Morph(newsStories[6], { style:'text-indent:-900px;', duration:1.1, afterFinish:function() { Element.insert(newsArea, newsStories[6]); newsStories[6].style.textIndent='0px'; if (newsStories[7].innerHTML.length > 130) { newsStories[7].innerHTML = shortenText(newsStories[7].innerHTML,127); }; newsStories[7].style.filter='alpha(opacity=0)'; newsStories[7].style.opacity=0; newsStories[7].style.visibility=''; newsStories[7].style.width='790px';newsStories[7].style.overflow='hidden';newsStories[7].style.whiteSpace='nowrap'; new Effect.Opacity(newsStories[7], {duration: 1.2, from:0.0, to:1.0, afterFinish: function() {   }}); runNews(); } }); 	}});";
			if (dir == 'next') { eval(newsAction); } else { newsTimeout = setTimeout(newsAction,5000); }
		}
	} //end if
}


function openBox(boxId) {
	if (lockBoxes == false && $('boxC'+boxId).readAttribute('open') != 'true') {
		if ($('boxC'+boxId).style.position=='absolute') { $('boxC'+boxId).style.position=''; }
		if (openEffect != null) { openEffect.cancel(); for (i=0;i<closeEffects.length;i++) { closeEffects[i].cancel(); } };
		openEffect = new Effect.Morph($('boxC'+boxId),{ style: 'width:472px;',duration: 0.4});
		$('boxC'+boxId).setAttribute('open','true');
		var closeCount = 0;
		for (i=1;i<=3;i++) {
			
			if (i != boxId && $('boxC'+i).getWidth() > 232) {
				if (i==3) { 
					closeEffects[closeCount] = new Effect.Morph($('boxC'+i),{ style: 'width:232px;',duration: 0.4, afterFinish: function() { 
						$('boxC3').style.position='absolute';
					}});
				} else {
					closeEffects[closeCount] = new Effect.Morph($('boxC'+i),{ style: 'width:232px;',duration: 0.4});	
				}
				$('boxC'+i).setAttribute('open','false');
				closeCount++;
			} //end if
		} //end for
	} //end if
} //end function


function openNav(nav) {

	//alert($(nav).childElements()[0].getHeight());
	try {
		if ($(nav).getHeight() < $(nav).childElements()[0].getHeight()+2) { //30) { 
			if (openNavEffect != null) { openNavEffect.cancel(); for (i=0;i<closeNavEffects.length;i++) { closeNavEffects[i].cancel(); } };
			if ($(nav).select('div').length > 0) {
				var navHeight = $(nav).select('div')[0].getHeight();
				openNavEffect = new Effect.Morph($(nav),{ style: 'height:' + ($(nav).childElements()[0].getHeight()+navHeight)  + 'px;',duration: 0.4});
			}
		} //end if
	} catch(e) { alert(e.description); }

	var closeCount = 0;
	for (var i=0;i<subnav.length;i++) {	
		if (subnav[i] != nav && subnav[i].tagName == 'DIV' && subnav[i].getHeight() > subnav[i].childElements()[0].getHeight()) {//29) {
			//alert(subnav[i].innerHTML);	
			closeNavEffects[closeCount] = new Effect.Morph($(subnav[i]),{ style: 'height:' + subnav[i].childElements()[0].getHeight() + 'px;',duration: 0.4});
			//$(subnav[i]).setAttribute('open','false');
			//$(subnav[i]).onclick = function() { openNav(this); return false; };
			closeCount++;
		} //end if
	} //end for
} //end function


function openDemo(url,width,height) {
	var opts = 'width=' + width + ',height=' + height + ',resizable=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,fullscreen=no,dependent=no';
    win=window.open(url, "demo", opts );
   win.focus();
}

function techAnim() {
	$('tech2').style.opacity='1.0';
	$('tech2').style.filter='alpha(opacity=100)';
	new Effect.Opacity($('tech1'),{ from:1.0, to:0.0, duration:1.0, afterFinish: function() {
		setTimeout("new Effect.Opacity($('tech2'),{ from:1.0, to:0.0, duration:1.0, afterFinish: function() { setTimeout(\"new Effect.Opacity($('tech1'),{ from:0.0, to:1.0, duration:1.0, afterFinish: function() { setTimeout(\\\"techAnim();\\\",1000); }});\",1000); }});",1000);	
	}});	
}


function shortenText(str,len) {
	var len = len ? len : 125;
	var newStr = str.substring(0,len+1);
	var words = newStr.split(' ');
	words.splice(words.length-1,1);
	newStr = words.join(' ') + '...';
		
	return newStr;
}

/*function browserVersion(dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	}*/
	
function popUpVideo(e) {
	var video = $(e).readAttribute('video');
	var vidWidth = $(e).readAttribute('vidWidth');
	var vidHeight = $(e).readAttribute('vidHeight');
	if (vidWidth == '' || isNaN(vidWidth)) { vidWidth = 696; }
	if (vidHeight == '' || isNaN(vidHeight)) { vidHeight = 450; }
	
	vidBoxWidth = parseInt(vidWidth) + 68;
	vidBoxHeight = parseInt(vidHeight) + 68;

//alert('Video: ' + video);
	if (!$('blackout')) {
		
		$('body').insert('<div id="blackout" style="z-index:20000;position:absolute;top:0px;left:0px;width:100%;height:1px;background-color:#000000;opacity:0;filter:alpha(opacity=0);">&nbsp;</div>');

		var viewport = document.viewport.getDimensions();
		var width = viewport.width;
		var height = viewport.height;		
		var width2 = $('body').getWidth();
		var height2 = $('body').getHeight();		
		$('blackout').style.width=Math.max(width,width2)+'px';
		$('blackout').style.height=Math.max(height,height2)+'px';
		
		Effect.Fade('blackout', { duration:0.4, from:0, to:0.70, afterFinish: function() {
		//width="' + vidBoxWidth + '" height="' + vidBoxHeight + '"
		//style="width:130px;height:75px;"
			$('body').insert('<div id="videoWin" style="z-index:20001;position:absolute;top:80px;width:' + vidBoxWidth + 'px;height:' + vidBoxHeight + 'px;display:block;"><table cellpadding="0" cellspacing="0" border="0" width="' + vidBoxWidth + '" height="' + vidBoxHeight + '"><tr><td align="right"><table cellpadding="0" cellspacing="0" border="0" id="videoBox"><tr><td width="34" height="34"><img src="/images/popup/popup_tl.png" width="34" height="34" border="0" style="display:block;" /></td><td background="/images/popup/popup_t.gif"></td><td width="34" height="34"><img src="/images/popup/popup_tr.png" width="34" height="34" border="0" style="display:block;" /><div id="popUpX" style="width:34px;height:34px;cursor:pointer;opacity:0;filter:alpha(opacity=0);position:absolute;top:0px;right:0px;" onclick="closeVideo();"><img src="/images/popup/popup_x.png" width="34" height="34" border="0"></div></td></tr><tr><td width="34" background="/images/popup/popup_l.gif">&nbsp;</td><td bgcolor="#ffffff"><div id="videoContent" style="width:130px;height:75px;">&nbsp;</div></td><td width="34" background="/images/popup/popup_r.gif">&nbsp;</td></tr><tr><td width="34" height="34"><img src="/images/popup/popup_bl.png" width="34" height="34" border="0" style="display:block;" /></td><td background="/images/popup/popup_b.gif" height="34">&nbsp;</td><td width="34" height="34"><img src="/images/popup/popup_br.png" width="34" height="34" border="0" style="display:block;" /></td></tr></table></td><tr></table></div>');
			fixIE6PNG();
			
			$('videoWin').style.left = Math.ceil((Math.max(width,width2)-vidBoxWidth)/2) + 'px';
			new Effect.Morph('videoContent', { 
				style: 'width:'+ vidWidth + 'px;height:'+ vidHeight + 'px;',
				duration: 0.6,
				afterFinish: function() {
					$('popUpX').fade({duration:0.5, from:0, to:1});
					$('videoContent').innerHTML = '<a href="http://www.starentnetworks.com' + video + '" id="popupVideo" style="display:block;width:'+ vidWidth + 'px;height:'+ vidHeight + 'px;"></a>';
			
					flowplayer("popupVideo", "/flash/flowplayer-3.1.0.swf", {
							wmode: 'opaque',
							key: '#@a621a50bbc00b1f1f52',
							clip: { 
							autoPlay: true, 
							autoBuffering: false
						},
						plugins: { 
							controls: { 
								play:true, 
								volume:true, 
								mute:false, 
								time:true, 
								stop:true, 
								playlist:false, 
								fullscreen:false,
								height: 24, 
								backgroundGradient: 'low'
							} 
						}
					});					
			
				} //end finish
			});
			
		}});

	}
} //end function

function closeVideo() {
	if ($('blackout')) {	
		$('videoWin').remove();
		$('blackout').remove();
	} //end if
}

