var DialogWindows = null;

var loader = new YAHOO.util.YUILoader({
    require: ["yuiloader-dom-event", "connection", "container", "json", "selector"],
    base: YAHOO.config.BASE_URL + '/js/yui/build/',
    loadOptional: true,
    onSuccess: function(o) {
        DialogWindows = new Dialogs();
    }
});

loader.insert();

function run_script(_div) {
    if (_div) {
    	var divContent = _div.childNodes;
    	if (divContent) {
        	for (var i = 0; i < divContent.length; i++) {
        		var requestItem = divContent.item(i);
        		if (requestItem.tagName) {
                    if (requestItem.tagName.toUpperCase() == 'SCRIPT') eval(requestItem.text);
                    else run_script(requestItem);
        		}
        	}
    	}
    }
}
//================================================
function is_safari(){
	if(navigator.userAgent.indexOf("Safari") != -1) return 1;
	return 0;
}

//====================================================
var Dialogs = function () {
    this.initialize();
}

Dialogs.prototype = {
    loadingWindow: null,

    
    initialize: function () {
        this.loadingWindow = new YAHOO.widget.Panel("wait",  
			{ width:"240px", 
			  fixedcenter:true, 
			  close:false, 
			  draggable:false, 
			  zindex:4,
			  modal:false,
			  visible:false
			} 
		);

        this.loadingWindow.setHeader(YAHOO.strings.PLEASE_WAIT);
        this.loadingWindow.setBody('<img src="' + YAHOO.config.IMAGES_PATH + '/loading.gif" />');
        this.loadingWindow.render(document.body);
        
    },
    
    showLoadingWindow: function () {
        this.loadingWindow.show();
    },
    
    hideLoadingWindow: function () {
        this.loadingWindow.hide();
    },
    
    show: function (width, url, id, title) {
        var callback = {
            success: function(o) {
                var window = new YAHOO.widget.Panel(id,  
                    { width: width + 'px', 
                        fixedcenter: true, 
                        close: true, 
                        draggable: true, 
                        zindex: 4,
                        visible: false
                    } 
                );
                window.setHeader(title);
                window.setBody(o.responseText);
                window.render(document.body);
                window.show();
                
                run_script(document.getElementById(id + '_c'));
                
                DialogWindows.hideLoadingWindow();
            },
            failure: function(o) {
                alert('Connection failed...');
                DialogWindows.hideLoadingWindow();
            }
        }
        
//        if(document.getElementById(id + '_c')) {
//            document.getElementById(id + '_c').style.visibility = 'visible';
//        } else {
            DialogWindows.showLoadingWindow();
            var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
//        }
    },
    
    set: function (toContainer, url, startCallback, endCallback,indicatoroff,postdata) {
        var callback = {
            success: function(o) {
                toContainer.innerHTML = o.responseText;
                run_script(toContainer);
                
                if (endCallback) {
                    endCallback.call();
                }
            },
            failure: function(o) {
                toContainer.innerHTML = '<div align="center">error...</div>';
                alert('Connection failed...');
            }
        }
        if(!indicatoroff){
        toContainer.innerHTML = '<div align="center"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + YAHOO.strings.LOADING_PLEASE_WAIT + '</div>';
		}
        if (startCallback) {
            startCallback.call();
        }
        var conn=null;
        if(postdata){
        	conn = YAHOO.util.Connect.asyncRequest("POST", url, callback,postdata);
        }else{
        	conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
        }
        
        return false;
    },
    
    sendHidden: function (url) {
        var conn = YAHOO.util.Connect.asyncRequest("GET", url);
        return false;
    },
    
    appendFirst: function (toContainer, url, startCallback, endCallback) {
        var currentContent = toContainer.innerHTML;
        var callback = {
            success: function(o) {
                toContainer.innerHTML = o.responseText + currentContent;
                run_script(toContainer);
                
                if (endCallback) {
                    endCallback.call();
                }
            },
            failure: function(o) {
                toContainer.innerHTML = '<div align="center">error...</div>';
                alert('Connection failed...');
            }
        }
        
        toContainer.innerHTML = '<div align="center"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + YAHOO.strings.LOADING_PLEASE_WAIT + '</div>' + currentContent;
        if (startCallback) {
            startCallback.call();
        }
        var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
        
        return false;
    }, 
    
    appendLast: function (toContainer, url, startCallback, endCallback) {
        var currentContent = toContainer.innerHTML;
        var callback = {
            success: function(o) {
                toContainer.innerHTML = currentContent + o.responseText;
                run_script(toContainer);
                
                if (endCallback) {
                    endCallback.call();
                }
            },
            failure: function(o) {
                toContainer.innerHTML = '<div align="center">error...</div>';
                alert('Connection failed...');
            }
        }
        
        toContainer.innerHTML = currentContent + '<div align="center"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + YAHOO.strings.LOADING_PLEASE_WAIT + '</div>';
        if (startCallback) {
            startCallback.call();
        }
        var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
        
        return false;
    },
    
    deleteItem: function (item, url, startCallback, endCallback) {
        var callback = {
            success: function(o) {
                item.parentNode.removeChild(item);
                
                if (endCallback) {
                    endCallback.call();
                }
            },
            failure: function(o) {
                item.innerHTML = '<div align="center">error...</div>';
                alert('Connection failed...');
            }
        }
        
        item.innerHTML = '<div align="center"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + YAHOO.strings.DELETE_PLEASE_WAIT + '</div>';
        if (startCallback) {
            startCallback.call();
        }
        var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
        
        return false;
    },
    /*
	 * send the action to be performed to the server, then hide the
	 * fromElement element and show the toElement.
	 * optionally, the endcallback function can be executed to perform
	 * any cleaning up ; for example refresh the list
	 * added endCallback to allow refreshing the list of prospective jobs
	 * after removal -- Razaq
	 */	 
    replaceItem: function (url, fromElement, toElement, endCallback) {
        fromElement.style.display = 'none';
        toElement.style.display = 'none';

        var parent = fromElement.parentNode;
        var parentHTML = parent.innerHTML;
        
        var callback = {
            success: function(o) {
                parent.innerHTML = parentHTML;
                $(toElement.id).style.display = 'block';
				
				if (endCallback) {
                        endCallback.call();
                    }
            },
            failure: function(o) {
                parent.innerHTML = parentHTML;
                $(fromElement.id).style.display = 'block';
            }
        }
        
        parent.innerHTML = '<div align="center" id="please_wait_message"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + YAHOO.strings.SAVING + '</div>';
        
        var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);
        
        return false;
    },
    
    submitForm: function (form, toContainer, url, startCallback, endCallback, needUpload, pseudoForm) {
		 
        if (!needUpload) {
            var callback = {
                success: function(o) {
                    toContainer.innerHTML = o.responseText;
                    run_script(toContainer);
                    
                    if (endCallback) {
                        endCallback.call();
                    }
                },
                failure: function(o) {
                    toContainer.innerHTML = '<div align="center">error...</div>';
                    alert('Connection failed...');
                }
        	}
        } else {
			var callback = {
                upload: function(o) {
                    toContainer.innerHTML = o.responseText;
                    run_script(toContainer);
                    if (endCallback) {
                        endCallback.call();
                    }
                },
                failure: function(o) {
                    toContainer.innerHTML = '<div align="center">error...</div>';
                    alert('Connection failed...');
                }
        	}
			
        }
        
        if (startCallback) {
            startCallback.call();
        }
        
        if (form.method.toLowerCase() == 'post') {
            var string = YAHOO.strings.SAVING_PLEASE_WAIT;
        } else {
            var string = YAHOO.strings.LOADING_PLEASE_WAIT;
        }
        
        if (!needUpload) {
            YAHOO.util.Connect.setForm(form);
            toContainer.innerHTML = '<div align="center"><img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + string + '</div>';
        } else {
            YAHOO.util.Connect.setForm(form, true);
            if (pseudoForm) {
                pseudoForm.style.display = 'none';
            } else {
				//if(!is_safari())
               form.style.display = 'none';
            }
            var waitElement = document.createElement('div');
			waitElement.setAttribute('id','wait');
            waitElement.innerHTML = '<img src="' + YAHOO.config.IMAGES_PATH + '/indicator.gif" /> ' + string;
            if (pseudoForm) {
                pseudoForm.parentNode.appendChild(waitElement);
            } else {
                form.parentNode.appendChild(waitElement);
            }
        }
        var conn = YAHOO.util.Connect.asyncRequest(form.method, url, callback);
        return false;
    },
    
    checkCredits: function (url, indicator, button) {
        var callback = {
            success: function(o) {
                var response = YAHOO.lang.JSON.parse(o.responseText);
                if (!response.error) {
                    YAHOO.util.Dom.addClass(button, 'active');
                    button.alt = '';
                    button.title = '';
                    button.disabled = false;
                } else {
                    button.alt = response.error;
                    button.title = response.error;
                    button.disabled = true;
                }
                indicator.style.display = 'none';
            },
            failure: function(o) {
                indicator.style.display = 'none';
                alert('Connection failed...');
            }
        }
        indicator.style.display = 'block';
        var conn = YAHOO.util.Connect.asyncRequest("GET", url, callback);        
        return false;
    }
}

function $(id) {
    return document.getElementById(id);
}
function $jtget(id) {
    return document.getElementById(id);
}
function callBlock(event, str) {
	var popUp = $("popUp");
	popUp.innerHTML = "<table width=\"100%\"><tr><td>"  +str + "</td></tr></table>";

	var viewH;
	var viewW;
	var offset;

	if (navigator.appName == "Microsoft Internet Explorer") {
		viewW = document.body.clientWidth;
		viewH = document.body.clientHeight;
		offset = document.body.scrollTop;
	}
	else {
		viewW = document.documentElement.clientWidth;
		viewH = document.documentElement.clientHeight;
		offset  = self.pageYOffset;
	}

	var x = event.clientX;
	var y = event.clientY;

	if(x+500>viewW) x = viewW - 520;
	if(offset) y = y + offset;

	popUp.style.top = y + "px";
	popUp.style.left = x + "px";
}

function hidePopUpBlock() {
    var popUp = $("popUp");
	popUp.style.top = "-500px";
	popUp.style.left = "-500px";
}

function createHiddenElement(name, value) {
    var element = null;
    element = document.createElement('input');
    element.setAttribute('type', 'hidden');
    element.setAttribute('name', name);
    element.setAttribute('value', value);
    return element;
}

YAHOO.data.copyToClipboard = function(s)
{
 if (window.clipboardData) 
   {
   
   // the IE-manier
   window.clipboardData.setData("Text", s);
   
   // waarschijnlijk niet de beste manier om Moz/NS te detecteren;
   // het is mij echter onbekend vanaf welke versie dit precies werkt:
   }
   else if (window.netscape) 
   { 
	   var currDoc = window._content.document; // gets you the current document
	   if (currDoc.location.host != "edujob")
	     return false;
   // dit is belangrijk maar staat nergens duidelijk vermeld:
   netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

   // maak een interface naar het clipboard
   var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
   if (!clip) return;
   
   // maak een transferable
   var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
   if (!trans) return;
   
   // specificeer wat voor soort data we op willen halen; text in dit geval
   trans.addDataFlavor('text/unicode');
   
   // om de data uit de transferable te halen hebben we 2 nieuwe objecten nodig   om het in op te slaan
   var str = new Object();
   var len = new Object();
   
   var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
   
   var copytext=s;
   
   str.data=copytext;
   
   trans.setTransferData("text/unicode",str,copytext.length*2);
   
   var clipid=Components.interfaces.nsIClipboard;
   
   if (!clip) return false;
   
   clip.setData(trans,null,clipid.kGlobalClipboard);
   
   }
   return false;
}
YAHOO.example.panels = function(){
//	DialogWindows.showModalCarousel();
//	 panel_five = new YAHOO.widget.Panel("panel_five",{} );
//	 panel_five.render();
	 };