// ---------------------------------------------------------------
// C O D E   D E S C R I P T I O N
// ---------------------------------------------------------------
// project: IDS Scheer
// ---------------------------------------------------------------
// name:    helper.js
// ---------------------------------------------------------------
// label:   JavaScript Helper Functions
// ---------------------------------------------------------------
// descr:   common helper functions for
//          - generating dynamic service links
//          - popup window opener
//  ---------------------------------------------------------------
// issues:  dependencies with lib.js (general DOM functions)
//          requires string constants like SERVICE_LINK_PRINT_PAGE
//           usage on page bottom, will be processed by
//          global event manager in lib.js
// --------------------------------------------------------------
// version: 1.0
// ---------------------------------------------------------------
// charset: UTF-8
// ---------------------------------------------------------------
// author:  Aperto AG | Felix Lucke
//  ---------------------------------------------------------------
// edit:    2007/01/11 - initial release
// ---------------------------------------------------------------

// initialize vars
// ---------------------------------------------------------------
var bol_hasPrintFeature = false;	// possibility to open print dialog per js in browser
var bol_supportsW3CDOM = false;		// browser supports DOM tree and other W3C stuff
var bol_popupMode = false;			// set true if functions runs in popup windows (different procedure)
var bol_systemValuesOK = false;		// check system string values (like Footer-ID)
var bol_debug = false;			    // will cause alert messages for debugging purposes

// check environment
// ---------------------------------------------------------------
var ie = 0;
var agent = navigator.userAgent.toLowerCase();
var isMac = (agent.indexOf('mac')) ? true : false;
if (navigator.appName.indexOf("Explorer") > -1) {
	if (navigator.appVersion.indexOf("MSIE 5") > -1) { ie = 5 }
	else { ie = navigator.appVersion.substring(0,1) }
}

// print feature
if (window.print) {
	bol_hasPrintFeature = true;
}

// DOM feature by emperic testing
if (document.createElement && document.createTextNode && document.insertBefore && document.appendChild) {
	bol_supportsW3CDOM = true;
}

// check system values
if ((SERVICE_LINK_SYS_FOOTER_ID != '') && (SERVICE_LINK_SYS_SERVICE_UL_ID != '')) {
	// if no footer link - page mode is popup
	if (document.getElementById(SERVICE_LINK_SYS_FOOTER_ID) == null) {
		bol_popupMode = true;
	}
	bol_systemValuesOK = true;
}


// go process if everything okay
// ---------------------------------------------------------------
if (bol_systemValuesOK && bol_supportsW3CDOM) {

	if (bol_popupMode) {
		// popup processing
		// ---------------------------------------------------------------
		if (bol_debug) alert ('page mode: popup');

		// get ul for link stuff
		dom_ServiceList = document.getElementById(SERVICE_LINK_SYS_SERVICE_UL_ID);
		if (bol_debug) alert ('dom_ServiceList: ' + dom_ServiceList);

		if (dom_ServiceList) {

			// render print link if print feature enabled
			if (bol_hasPrintFeature) {

				// build print link
				dom_li_print = document.createElement('li');
				dom_li_print.innerHTML = SERVICE_LINK_PRINT_POPUP;
				if (bol_debug) alert ('dom_li_print: ' + dom_li_print);

				// insert print link as first list item
				dom_ServiceList.appendChild(dom_li_print);
			}

			// render close window link
			dom_li_close = document.createElement('li');
			dom_li_close.innerHTML = SERVICE_LINK_CLOSE_WINDOW_POPUP;
			if (bol_debug) alert ('dom_li_close: ' + dom_li_close);

			// insert close link as list item
			int_ListPosition = SERVICE_LINK_SYS_POSITION_POPUP;
            dom_ServiceList.appendChild(dom_li_close);

		}


	} else {
		// default page processing
		// ---------------------------------------------------------------
		if (bol_debug) alert ('page mode: default');

		// get ul for link stuff
		dom_ServiceList = document.getElementById(SERVICE_LINK_SYS_SERVICE_UL_ID);
		if (bol_debug) alert ('dom_ServiceList: ' + dom_ServiceList);

		if (dom_ServiceList) {

			// render print link if print feature enabled
			if (bol_hasPrintFeature) {

				// build print link
				dom_li_print = document.createElement('li');
				dom_li_print.innerHTML = SERVICE_LINK_PRINT_POPUP;
				dom_li_print.appendChild(document.createTextNode(SERVICE_LINK_DIVIDER_FOOTER));
				if (bol_debug) alert ('dom_li_print: ' + dom_li_print);

				// insert print link as first list item
				int_ListPosition = SERVICE_LINK_SYS_POSITION_FOOTER;
				dom_ServiceList.insertBefore(dom_li_print, dom_ServiceList.childNodes[int_ListPosition]);
			}
		}
	}



} else {
	// error: to be definied
}



/*
# ---------------------------------------------------------------
# need refactoring still old code !!!!
# ---------------------------------------------------------------
*/


var openedPopups = new Array();
var defaults = new Array();
defaults['url'] = ""; defaults['name'] = "popup"; defaults['width'] = 600; defaults['height'] = 400;
function openPopup (url,name,w,h,switches,x,y) {
	if (!url) { url = defaults['url'] }
	if (!name) { name = defaults['name'] }
	if (!w) { w = defaults['width'] }
	else { w = parseInt(w) }
    if (ie && isMac){ w -= 10 }
    if (ie == 5) { w += 20 }
	if (!h) { h = defaults['height'] }
	else { h = parseInt(h) }
    if (ie && ie < 5) { h -= 20; }
	if (switches && switches.indexOf("f") > -1 && window.screen) {
        /*w = "outerWidth=" + window.screen.availWidth;
          h = ",outerHeight=" + window.screen.availHeight; */
		w = window.screen.availWidth;
		h = window.screen.availHeight;
	}

	var xpos; 	var ypos;
	if (x > -1 && y > -1) {
        xpos = x;
		ypos = y;
	} else {
		if (switches && window.screen) {
            if (switches.indexOf("c") > -1) {
                xpos = parseInt((window.screen.availWidth - w)/2);
				ypos = parseInt((window.screen.availHeight - h)/2);
			}
			if (switches.indexOf("f") > -1) {
                xpos = 0;
				ypos = 0;
			}
		}
	}
	var params = popupParams(w,h,switches,xpos,ypos);
	var newwin;
	if (openedPopups[name] && openedPopups[name].close && !openedPopups[name].closed) {
        openedPopups[name].close();
        openedPopups[name] = "";
    }
    newwin = window.open(url, name, params);
    if (switches && switches.indexOf('b') > -1 && window.moveTo && window.resizeTo && window.focus) {
        newwin.blur();
        newwin.opener.focus();
        newwin.resizeTo(w,h);
        newwin.moveTo(xpos,ypos);
	}
	if (window.focus) { newwin.focus() }
	openedPopups[name] = newwin;
    return false;
}
function popupParams (w,h,switches,xpos,ypos) {
	var width = "width=" + w;
	var height = ",height=" + h;
	var parent = ",dependent=no";
    var dirbar = ",directories=no";
	var fullscreen = ",fullscreen=0";
    var hotkeys = ",hotkeys=yes";
    var locbar = ",location=no";
	var menubar = ",menubar=no";
	var resizable = ",resizable=no";
	var scrollbars = ",scrollbars=no";
	var statusbar = ",status=no";
	var toolbar = ",toolbar=no";
	if (switches) {
		if (switches.indexOf("p") > -1 ) { parent = ",dependent=yes"	}
		if (switches.indexOf("d") > -1 ) { dirbar = ",directories=yes"	}
		if (switches.indexOf("h") > -1 ) { hotkeys = ",hotkeys=no"	}
		if (switches.indexOf("l") > -1 ) { locbar = ",location=yes"	}
		if (switches.indexOf("m") > -1 ) { menubar = ",menubar=yes"	}
		if (switches.indexOf("r") > -1 ) { resizable = ",resizable=yes"	}
		if (switches.indexOf("s") > -1 ) { scrollbars = ",scrollbars=yes" }
		if (switches.indexOf("u") > -1 ) { statusbar = ",status=yes"	}
		if (switches.indexOf("t") > -1 ) { toolbar = ",toolbar=yes"	}
		if (switches.indexOf("k") > -1 || switches.indexOf("b") > -1 ) { fullscreen = ",fullscreen=1" }
	}
	var pos = "";
	if (xpos > -1 && ypos > -1) { pos = ",left=" + xpos + ",top=" + ypos }
		return width + height + parent + dirbar + hotkeys + locbar + menubar + resizable + scrollbars + statusbar + toolbar + pos + fullscreen;
}

function targetOpener(link)
{
	window.opener.focus();
	window.opener.location.href=link.href;
	window.close();
}
// Externe Links �ffnen in neuem Fenster

window.onload = externalLinks;
function externalLinks() {
     if(!document.getElementsByTagName) return;
     var links,a,i;
     links = document.getElementsByTagName("a");
     for(i=0;i<links.length;i++) {
         a = links[i];
         if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("external") > -1) {
             a.onclick = function() {
                 window.open(this.href);
                 return false;
             }
         }
         if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("openBigPictureXXL") > -1) {
             a.onclick = function() {
                 openPopup (this.href, "BigPicture", 790, 782, "prsu");
                 return false;
             }
         }
        if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("openBigPictureGallery") > -1) {
             a.onclick = function() {
                 openPopup (this.href, "BigPicture", 590, 782, "prsu");
                 return false;
             }
         }
          if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("openBigPictureLandscape") > -1) {
             a.onclick = function() {
                 openPopup (this.href, "BigPicture", 500, 675, "prsu");
                 return false;
             }
         }
          if(a.getAttribute("rel") && a.getAttribute("rel").indexOf("openBigPicturePortrait") > -1) {
             a.onclick = function() {
                 openPopup (this.href, "BigPicture", 415, 782, "prsu");
                 return false;
             }
         }
     }
}


