window.addEvent('domready', function() {

    var hClicked = false; // checking if any link is clicked
    //var pid = '1348';
	//var hostUrl = 'http://bobo/~minze/projects/t3Antenne/index.php?id=1347';
	//var pWidth = pWidth;
	//var pHeight = pHeight;

    // class adds events to all anchors //
    var Popup = new Class({
        addEvents: function() {
                $$('body a').each(function(el)
                    {
						
					
                   	// if already onlick do nothing
                    	if (!el.onclick) {
							el.addEvent('click', function(evt, actionId)
								{
									new Event(evt).stop() // Cancel link click action
									this.setClicked(el.getProperty('href'), el) // Run method and pass in clicked link's Id
								}.bind(this));
                        }
                    }, this)
        },
        checkForms: function() {
	        $$('body form').each(function(el)
                    {
							el.addEvent('submit', function(evt, actionId)
								{
									//new Event(evt).stop() // Cancel link click action
									hClicked = true;
									//this.setClicked(el.getProperty('href'), el) // Run method and pass in clicked link's Id
								}.bind(this));
                    }, this)
        },
        onBrowserClose: function(event)  {
            if (!hClicked) {
                if (!Cookie.get('pbsurvey[' + pid + '][rid]')) {
                    window.open(hostUrl, 'popupvenster', 'width=' + pWidth +', height=' + pHeight + ', resizable=' + pResizable + ', menubar=no, scrollbars=' + pScroll + ', status=no, toolbar=no');
                }
            }
        },
        setClicked: function(property, el) {

            // clicked url
            hClicked = true;
            
            var url = el.href;
            var atarget = el.target;
            
            //alert(atarget.contains('_blank'));
            if(url.contains(window.location.hostname) && atarget.contains('_blank')) { 
            	window.location.href = el.href; // redirecting to local
            } else {
				// checking  for internal or external link
				if (url.contains('http:')) {
					// checking for local
					if (url.contains(window.location.hostname)) {
						//new Event(event).stop() // Cancel link click action
						window.location.href = el.href; // redirecting to local
					} else {
						if (!Cookie.get('pbsurvey[' + pid + '][rid]')) {
							window.open(hostUrl, 'popupvenster', 'width=' + pWidth +', height=' + pHeight + ', resizable=no, menubar=no, scrollbars=no, status=no, toolbar=no');
						}
						window.location.href = el.href;
					}
				} else {
					// leaving site show popup
					// external without http://
				if (!Cookie.get('pbsurvey[' + pid + '][rid]')) {
						window.open(hostUrl, 'popupvenster', 'width=' + pWidth +', height=' + pHeight + ', resizable=no, menubar=no, scrollbars=no, status=no, toolbar=no');
					}
					window.location.href = el.href;
				}
			} 
        }
    });
    
    
    // getting popup var from outside this class, window
    if (popup == true) {
		var oPopup = new Popup();
		// adding events to forms, to exclude submit
		oPopup.checkForms();
		// adding events to anchors
		oPopup.addEvents();
		// checking before exiting, ie, firefox only
		window.addEvent('beforeunload', function() { oPopup.onBrowserClose();});
		//window.addEvent('unload', function() { oPopup.onBrowserClose();});
    }
    
});
