var loadCSS = function(file) {
	var link = document.createElement('link');
	link.href = file;
	link.rel = 'stylesheet';
	link.type = 'text/css';
	document.getElementsByTagName('head')[0].appendChild(link);
};

var loadJS = function(file) {
	var script = document.createElement('script');
	script.src = file;
	script.type = 'text/javascript';
	document.getElementsByTagName('head')[0].appendChild(script);
};

function getStyleObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
		return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
		return document.layers[objectId];
    } else {
		return false;
    }
} // getStyleObject

function getObject(objectId) {
    if(document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
    } else if (document.all && document.all(objectId)) {
		return document.all(objectId);
    } else {
		return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.visibility = newVisibility;
		if(newVisibility == 'visible') {
		    styleObject.display = 'block';
		} else {
		    styleObject.display = 'none';
		}
		return true;
    } else {
		return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
		styleObject.left = newXCoordinate+"px";
		styleObject.top = newYCoordinate+"px";
		return true;
    } else {
		return false;
    }
} // moveObject

function nop() {}

function sendSearchTerm(formid) {
	if (formid == '') {
       formid = 'quicksearch';
    }
	if (document.getElementById(formid) && document.getElementById(formid).search_str) {
        urchinTracker('/searchterms/' + document.getElementById(formid).search_str.value);
    }
}

function showPopup (targetObjectId, eventObj) {
	return showPopupPos(targetObjectId, eventObj,0);
}

function showPopupPos (targetObjectId, eventObj, yOff) {
    if(eventObj) {
		hideCurrentPopup();
		eventObj.cancelBubble = true;
		var windowWidth = (document.body.clientWidth) ? document.body.clientWidth : document.body.offsetWidth;
		//Calculate Coordinates to show popup
		var newXCoordinate = (eventObj.pageX) ? eventObj.pageX : eventObj.x;
		var newYCoordinate = (eventObj.pageY) ? eventObj.pageY : eventObj.y;
		//Get Width of popup to show
		getStyleObject(targetObjectId).display='block';
		var targetObjectWidth = getObject(targetObjectId).offsetWidth;
		var origXCoordinate = (eventObj.pageX) ? eventObj.pageX : eventObj.x;
		//Resposition popup object to avoid offscreen display
		if((origXCoordinate+targetObjectWidth+20) > windowWidth) {
		    newXCoordinate -= origXCoordinate+targetObjectWidth+20-windowWidth;
		}
		//Move Object to new position
		moveObject(targetObjectId, newXCoordinate, newYCoordinate - yOff);
		if( changeObjectVisibility(targetObjectId, 'visible') ) {
		    window.currentlyVisiblePopup = targetObjectId;
		    return true;
		} else {
		    return false;
		}
    } else {
		return false;
    }
}

function hideCurrentPopup() {
    if(window.currentlyVisiblePopup) {
		changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
		window.currentlyVisiblePopup = false;
    }
}

window.onload = initializeHacks;

document.onclick = hideCurrentPopup;

function initializeHacks() {
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
		&& (navigator.platform.indexOf('Mac') != -1)
		&& getStyleObject('blankDiv')) {
		window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    createFakeEventObj();
}

function createFakeEventObj() {
    if (!window.event) {
		window.event = false;
    }
}

function resizeBlankDiv() {
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
		&& (navigator.platform.indexOf('Mac') != -1)
		&& getStyleObject('blankDiv')) {
		getStyleObject('blankDiv').width = document.body.clientWidth - 20;
		getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}

function explorerMacResizeFix () {
    location.reload(false);
}

function showInfoPopup(name) {
    infopopup = window.open(name,"InfoPopup",'top=200,left=250,height=500,width=590,innerHeight=500,innerWidth=500,resizable=yes,directories=no,scrollbars=yes,status=no,menubar=no,toolbar=no');
    infopopup.focus();
}

function hideInfoPopup() {
	infopopup.close();
}

function showFeaturePopup(name) {
    featurepopup = window.open("/charsetfeature-" + name + ".html","CharsetPopup",'top=200,left=250,width=500,height=180,scrollbars=yes,innerHeight=180,innerWidth=500,resizable=yes,directories=no,scrollbars=yes,status=no,menubar=no,toolbar=no');
    featurepopup.focus();
}

function hideFeaturePopup() {
    featurepopup.close();
}

function showImagePopup(url, title, width, height) {
    popup=window.open('','ImagePopup','width='+width+',height='+height+',resizable=no,scrollbars=no,status=no,menubar=no,toolbar=no,location=no');
    var doc = popup.document;
    doc.write('<html><head><title>'+title+'</title></head>');
    doc.write('<body style="margin:0;">');
    doc.write('<a href="javascript:self.close();"><img src="'+url+'" alt="'+title+'" title="'+title+'" border="0"></a>');
    doc.write('</body></html>');
    doc.close();
    return false;
}

function openSamplerWindow(artikelid, sessid) {
	sWindow = window.open('/catalog/fontsampler/?PHPSESSID=' + sessid + '&id=' + artikelid, 'fontSampler', 'width=820,height=600,resizable=yes,scrollbars=yes');
	sWindow.focus();
}

function openEcardWindow(artikelid) {
	eWindow = window.open('/fontcard/index.php?font=' + artikelid, 'fontcard', 'width=600,height=630,scrollbars=no,resizable=no,status=no,menubar=no,toolbar=no,location=no');
	eWindow.focus();
}

var adjust = 0;
function setAdjust(val) {
	adjust = val;
}

function getAdjust() {
	return adjust;
}

function setSampleSize(def){
	// get viewport dimensions
	if (window.innerWidth) { 
		breit = window.innerWidth; 
	} else { 
		breit = document.body.clientWidth; 
	}
	// correct against the standard resolution
	breit = def + (breit - 1024);
	// if too small, reset
	if (breit < def) { breit = def; }
	res = 1 + breit - (getAdjust() + 1);
	// alert(getAdjust() + ':' + def + ':' + res + ':' + breit);
	return res;
}

function selectTab(src,str,index,link) { 
	if (link != '' && document.main) {
		spinWheel('on');
		document.main.subviewmode.value = link;
		document.main.submit();
		return true;
	}
	if (src == 0) {
		if (document.getElementById(str + 'tab_' + index)) {
			src = document.getElementById(str + 'tab_' + index).firstChild;
		} else {
			return;
		}
	}
	parentItem = src.parentNode;
	listContainer = parentItem.parentNode;
	tabCollection = listContainer.getElementsByTagName('li');

	for (j=0; j < tabCollection.length; j++) {
		if (document.getElementById(str + j)) {
			document.getElementById(str + j).style.display = 'none';
		}
		if (document.getElementById('selected_' + str + j)) {
			document.getElementById('selected_' + str + j).value = 0;
		}
	}
	for (j=0; j < tabCollection.length; j++) {
		if (parentItem == tabCollection[j]) {
			tabCollection[j].className = 'activeTab'; 
			try {
				tabCollection[j].blur();
			} catch (e) {
				// nothing
			}
			if (document.getElementById(str + j)) {
				document.getElementById(str + j).style.display = 'block';
				if (document.main) {
					document.main.selindex = j;
				}
			}
			if (document.getElementById('selected_' + str + j)) {
				document.getElementById('selected_' + str + j).value = 1;
			}
		} else {
	  		tabCollection[j].className = ''; 
		}
	}
	if (link == '') spinWheel('off');
}

function switchDisplay(what) {
	if (document.getElementById('panel_' + what)) {
		if (document.getElementById('panel_' + what).style.display == 'none') {
			if(document.getElementById('panel_' + what).tagName == 'TABLE' && !document.all) {
				document.getElementById('panel_' + what).style.display = 'table';
			} else {
				document.getElementById('panel_' + what).style.display = 'block';
			}
			document.getElementById('img_' + what).src = '/gif/symbol_arrow_up.gif';
			if (document.getElementById('selected_' + what)) {
				document.getElementById('selected_' + what).value = 1;
			}
		} else {
			document.getElementById('panel_' + what).style.display = 'none';
			document.getElementById('img_' + what).src = '/gif/symbol_arrow_down.gif';
			if (document.getElementById('selected_' + what)) {
				document.getElementById('selected_' + what).value = 0;
			}
		}
	}
}

function switchDisplayLoop(what,cnt) {
	for (i=0;i<cnt;i++) {
		switchDisplay(what + '_' + i);
	}
}

function spinWheelHeight(what, height) {
	if (document.getElementById('wheel')) {
        document.getElementById('wheel').style.height = height;
    }
	spinWheel(what);
}

function spinWheel(what) {
	if (document.getElementById('wheel')) {
		document.getElementById('wheel').style.display = (what == 'off') ? 'none' : 'block'; 
	}
} 

function tryIt(what) {
	item = document.getElementById(what);
	document.main.samplestr.value   = item.samplestr.value;
	document.main.subviewmode.value = item.subviewmode.value;
	document.main.what.value        = 'samplestr';
	document.main.submit();
	return false;
}

spinWheel();
