﻿
var _Lib = {}

_Lib.ie7 = navigator.appVersion.indexOf("MSIE 7")!=-1 ? true:false;


/* <documentation about="_Lib.addOpenInLinksNewWindow" type="specific function">
	<summary>Adds functionality that all links with type='new-window' will be opened in a new window</summary>
	<namespace>_Lib</namespace>
	<param type="string" descr="Id of parent element; defaults to document if not supplied">parentElementId</param>
</documentation> */
_Lib.addOpenInLinksNewWindow = function (parentElementId) {
	var container = "";
	if(parentElementId && parentElementId.length != 0) { container = "#" + parentElementId + " "; }
	var hyperlinks = jQuery(container + "a");
	
	for (var i=0; i < hyperlinks.length; i++) {
		if (hyperlinks[i].getAttribute("type") != "new-window") { continue; }
		
		//hyperlinks[i].setAttribute("title", "deze link opent in een nieuw venster"); 
		jQuery(hyperlinks[i]).click(function () { window.open(this.href); return false; });		
	}
}

 /* <documentation about="_Lib.resizeIFrame" type="specific function">
<summary>Resizes iframe</summary>
<namespace>_Lib</namespace>
</documentation> */
_Lib.resizeIFrame = function (frameId, newHeight) {
   jQuery("#" + frameId).animate({ height:newHeight}, 500);
} 

/* <documentation about="_Lib.limitCharactersTextAreas" type="specific function">
	<summary>Limits ammount of charactesr in text area</summary>
	<namespace>_Lib</namespace>
</documentation> */
_Lib.limitCharactersTextAreas = function ()	{ 
    var maxLength = 500;
    
    var limitText = function(textarea)
        {
            if(jQuery(textarea).attr("class").indexOf("max") != -1) { maxLength = parseInt(jQuery(textarea).attr("class").replace("max", "")); }
            
            //linebreaks take 2 chracters in back-end only 1 in front-end so we have to add them here            
            var lines = jQuery(textarea).val().split('\n');
            var breaks = lines.length;
            // check for /r at the end of the lines (IE)
            for (var i=0; i<lines.length; i++) {
                var line = lines[i];                
                if (line.charCodeAt(line.length-1) == 13)
                breaks++;
            }
            
            //singelquotes are doubled because of dynamic stored procedures, so 1 single quotes should be counted as 2
            var singleQuotes = jQuery(textarea).val().match(/'/g);
            var countSingleQuotes = 0;
            if(singleQuotes) { countSingleQuotes = singleQuotes.length; }            
            
            var length =  jQuery(textarea).val().length + breaks + countSingleQuotes;    
            var maxlength =  maxLength - breaks - countSingleQuotes;     
                 
            if(length > maxLength) {  
                //cut it
                jQuery(textarea).val(jQuery(textarea).val().substring(0, maxlength)); 
                
                //show it
                jQuery(textarea).attr("scrollTop", jQuery(textarea).attr("scrollHeight"));
                jQuery(textarea).css( "backgroundColor", "#ffcccc");
                var id = jQuery(textarea).attr("id");
                window.setTimeout('jQuery("#' + id + '").css( "backgroundColor", "#fff");', 1000);
            }
        };
    
    jQuery("textarea").blur(function() { limitText(this);  });    
    jQuery("textarea").keyup(function() { limitText(this); });        	   
}

_Lib.scrollToPosition = function(y) {
    jQuery.scrollTo(y, 1000);
}

/* <documentation about="_Lib.findElementPosition" type="general function">
	<summary>Find position of a HTML element relative to window</summary>
	<namespace>_Lib</namespace>
	<param type="object" descr="HTML element">elem</param>
	<returns>array [left (integer), top (integer)]</returns>
</documentation> */
_Lib.findElementPosition = function (elem){
	var curleft = curtop = 0;
	if (elem.offsetParent) {
	
		curleft = elem.offsetLeft
		curtop = elem.offsetTop
		while (elem = elem.offsetParent) {
			curleft += elem.offsetLeft
			curtop += elem.offsetTop
		}
	}
	return [curleft,curtop];
}

/* <documentation about="_Lib.addOnFocusInputFields" type="specific function">
<summary>Adds color change on focus on input type = text fields + empties field value</summary>
<namespace>_Lib</namespace>
</documentation> */
_Lib.addOnFocusInputFields = function (partId) {
    if (partId) { partId = "#" + partId + "Form "; }
    else { partId = ""; }

    var inputFields = jQuery(partId + "input.text[title]");
    for (var i = 0; i < inputFields.length; i++) {
        if (inputFields[i].type != "text" || inputFields[i].title.length == 0) { continue; }
        //on load
        if (inputFields[i].value.length == 0) {
            inputFields[i].value = inputFields[i].title;
            jQuery(inputFields[i]).addClass("no-focus");
            //continue;
        }
        if (inputFields[i].value == inputFields[i].title) { jQuery(inputFields[i]).addClass("no-focus"); }

        jQuery(inputFields[i]).focus(function () { if (this.value == this.title) { this.value = ""; jQuery(this).removeClass("no-focus"); } });
        jQuery(inputFields[i]).blur(function () { if (this.value.length == 0) { this.value = this.title; jQuery(this).addClass("no-focus"); } });
    }
}


/* <documentation about="_Lib.resizeTextArea" type="specific function">
<summary>resize text area</summary>
<namespace>_Lib</namespace>
</documentation> */
_Lib.addResizeTextArea = function () {
    var resize = function (textarea) {
        var id = "Shadow" + jQuery(textarea).attr("id");                        //get id of shadow box
        jQuery("#" + id).html(jQuery(textarea).val().replace(/\n/g, "<br />")); //replace line breaks

        var myHeight = jQuery("#" + id)[0].offsetHeight +20;                   //get height of shadow
        if (myHeight <= 50) { myHeight = 50; }                                   //if 30 or smaller don't bother
        jQuery(textarea).height(myHeight);                                      //set height
    }

    //get textarea
    var selector = "textarea";
    var textareas = jQuery(selector);
    for (var i = 0; i < textareas.length; i++) {
        //add shadow for measurement 
        var div = jQuery("<div class='textarea' style='width:" + jQuery(textareas[i]).width() + "px; position:absolute; top:0; left:-9999em;' id='Shadow" + jQuery(textareas[i]).attr("id") + "'>gg</div>");
        jQuery(textareas[i]).after(div);

        div.html(jQuery(textareas[i]).val());  //value in shadow
        resize(textareas[i]); //resize
    }

    //resize on page load
    textareas.keyup(function () { resize(this); });
}

/* <documentation about="_Lib.getScrollY" type="general function">
<summary>Get scrolling distance from the top of the window in pixels</summary>
<namespace>_Lib</namespace>
<returns>Scrolling distance in pixels (integer)</returns>
</documentation> */
_Lib.getScrollY = function () {
    var scrOfY = 0;
    if (typeof (window.pageYOffset) == 'number') {
        //Netscape compliant
        scrOfY = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        //DOM compliant
        scrOfY = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
    }
    return scrOfY;
}

