/*
This file contains other functions and logic to perform
*/

// exclude old browsers
if (!document.getElementById){
	top.location.replace('http://www.iht.com/info/browser.html');
}

// disallow the page to be framed
if (window.self != window.top){ 
	top.location.replace(window.location.pathname); 
}

// generic popup window
function pop_me_up(pURL,name,features){
	new_window = window.open(pURL,name,features);
	new_window.focus();
}

// rollover image function
function changeImage(image_name,image_src) {
   	document.images[image_name].src = image_src;
}


// event handler used for the navigation rollovers
sfHover = function(){
	var sfEls = document.getElementById("nav").getElementsByTagName("li");
	for (var i = 0; i < sfEls.length; i++){
		sfEls[i].onmouseover = function(){
			this.className += " sfhover";
		}
		sfEls[i].onmouseout = function(){
			this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);

// pops up window for the ultralingua translation service
function ulPopUp(url){
	var templang = document.ultralingua.language[ultralingua.language.selectedIndex].value;
	//var templang = document.getElementById("ultralingua").language[ultralingua.language.selectedIndex].value;
	var valarray = templang.split("|");
	var lang = valarray[0];
	var langfull = valarray[1];
	var ulurl;
	if (lang == "whatisthis"){
		ulurl = "http://www.ultralingua.com/onlinedictionary/services/webtrans/iht/what.html";
	} else {
		var ulurlstart = "http://www.ultralingua.com/onlinedictionary/services/webtrans/iht/iht-";
		var ulurlend = "-enable.cgi?service=english2" + langfull + "&location=http://www.iht.com/bin/print_ipub.php?file=" + url;
		ulurl = ulurlstart + lang + ulurlend;
	}
	window.open(ulurl);
}

// Displays the google ad results.
function google_ad_request_done(google_ads) {
	// Proceed only if we have ads to display!
	if (google_ads.length == 0){
		return;
	// only display text ads
	} else {
        var str = '<div id="ad_google">\n';
        str += '<div id="ad_google_header">Ads by Google</div>\n';
        
        var textstyle = "";
    	if (google_ads[0].bidtype == "cpm"){
    		textstyle = "text2_google_cpm";
    	} else {
    		textstyle = "text2_google";
    	}
        
        // For text ads, display each ad in turn.
    	if (google_ads[0].type == 'text') {
    		for (var i = 0; i < google_ads.length; i++) {
                str += '<div class="ad_google_item">\n';
                str += '\t<div class="ad_google_item_title"><a href="' +  google_ads[i].url + '">' + google_ads[i].line1 + '</a></div>\n';
                str += '\t<div class="' + textstyle + '">' + google_ads[i].line2 + ' ' + google_ads[i].line3 + '</div>\n';
                str += '\t<div><a href="' + google_ads[i].url + '">' + google_ads[i].visible_url + '</a></div>\n';
                str += '</div>\n';
            }
        }        
        str += '</div>\n';
        document.write(str);
	}		
}

// text resizing
var currentTextSize = null;
var currentLineHeight = null;
// check for cookie
if (get_text_cookie("ihtfontsize")) {
    currentTextSize = parseInt(get_text_cookie("ihtfontsize"));
} else {
    currentTextSize = 13;
    createCookie("ihtfontsize",currentTextSize,1000);
}
currentLineHeight = currentTextSize + 5;

function textSize(dir) {
    if (dir == 'up') {
        if (currentTextSize <= 17) {
            currentTextSize += 2;
        }
    } else if (dir == 'down') {
        if (currentTextSize >= 11) {
            currentTextSize -= 2;
        }
    }
    currentLineHeight = currentTextSize + 5;
    document.getElementById('bodyText').style.fontSize = currentTextSize + 'px';
    document.getElementById('bodyText').style.lineHeight = currentLineHeight + 'px';
    // write/rewrite cookie
    createCookie("ihtfontsize",currentTextSize,1000);
}

function get_text_cookie ( cookie_name )
{
  var results = document.cookie.match ( cookie_name + '=(.*?)(;|$)' );

  if ( results ) {
    return ( unescape ( results[1] ) );
  }
  else { return null; }
}

// open form submission in popup window
function form2pop(formID, w, h) {
	var myForm = document.getElementById(formID);
	var attribStr = 'width=' + w + ',height=' + h;
	var popup = window.open(myForm.action, myForm.target, attribStr);
	myForm.submit();
}

