// Function for stripping html tags out of the tiny mce for correct compration.
function stripHtml(stringValue) {
        var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
        return stringValue.replace(regexp, "");
}

// function to select all checkboxes within a elementId
function selectAll(el, id) {
	var isChecked = el.checked ? true : false;
	var els = document.getElementById(id).getElementsByTagName("input");
	var j = els.length;
	
	for(var i=0; i<j; i++) {
		els[i].checked = isChecked;
	}
}

// Controls the size of textarea
function controlLimit(textarea, maxlimit) {
    if (textarea.value.length > maxlimit) {
        textarea.value = textarea.value.substr(0, maxlimit);
    }
}



// Check wheither string contains some value
jQuery.fn.contains = function(txt) {
    var regexp = '/' + txt + '/';
    return jQuery(this).search(regexp);
}

// Replaces something with something
jQuery.fn.strReplace = function(replaceWhat, replaceWith) {
    return jQuery(this).replace(replaceWhat, replaceWith);
}

// Limits text length
jQuery.fn.limitLength = function() {
    var limit = null;

    // Our pattern
    regexp = /limit-length-/;

    // Looping through the array
    this.each(
        function(i) {
            offset = this.className.search(regexp);

            // So we split the string "limit-length-xxxx" to get only the xxxx value
            limit = this.className.substring(offset + 'limit-length-'.length, this.className.length);

            // We explode the array in order to get rid of classes, which possibly following
            limit = limit.split(' ');
            limit = limit[0];

            // If limit is greater than 0, limiting the textarea length
            if (limit > 0) {
                jQuery(this).keydown(function() {
                    controlLimit(this, limit);
                });

                jQuery(this).keyup(function() {
                    controlLimit(this, limit);
                });
            }
        }
    );

    return jQuery(this);
}


jQuery(document).ready(function() {
	
	browser.identify();

	jQuery('#tabs h2.tab').click(function() {
		jQuery("#tabs").attr('class', jQuery(this).attr('id'));
	});
	
	
	
	jQuery('#recommendation-rating span').click(function() {
		var rating = jQuery(this).attr("class").substr(-1);
		jQuery("#recommendation-rating").attr('class', 'stars_'+rating);
		jQuery("#memberRating").attr('value', rating);
	});
	
	
    jQuery('a[rel="external"]').attr('target', '_blank'); // Adding target _blank to all external links

    jQuery('#millennium-block img').attr('alt', ''); // Removing alt attributes of the images in millennium block

    jQuery('#millennium-block a[class="hover-button"]').tooltip({showURL: false}); // Adding tooltips
    jQuery('a.tooltip2').tooltip(); // Adding tooltips
    jQuery('#member-goals a[class="hover-button"]').tooltip(); // Adding tooltips
    
	jQuery('#openinviter input[name="toggle_all"]').click(function() {
		jQuery('#openinviter input[type="checkbox"]').attr('checked', jQuery(this).attr('checked'));
	});

    
    // Project page text message
    jQuery('#ProjectsPublicMessageForm textarea[name="text"]').Watermark(jQuery('#write_something').attr('value'), '#000');

	jQuery('#public-message').Watermark(jQuery('#public-message-user').attr('value') + jQuery('#public-message-default').attr('value'), '#000');
	jQuery('#MembersPublicMessageForm').submit(function() {
		if (jQuery('#public-message').attr('value') == (jQuery('#public-message-user').attr('value') + jQuery('#public-message-default').attr('value'))) {
			jQuery('#public-message').focus();
			return false;
		}
	});

    // Catching project page text message form submit
    jQuery('#ProjectsPublicMessageForm').submit(function() {
		if (jQuery('#ProjectsPublicMessageForm textarea[name="text"]').attr('value') == (jQuery('#write_something').attr('value'))) {
			jQuery('#ProjectsPublicMessageForm textarea[name="text"]').focus();
			return false;
		}
	});

    // Limiting text length of text-areas. All textarea tags with class="limit-length-xxxx" will be
    // limited, where xxxx is number of chars to limit.
    jQuery('textarea').limitLength();

    jQuery('.weblog div').truncate({max_length: 500, more: 'Toon meer', less: 'Toon minder'});    
   
    jQuery('.help').hover(
    	function(){
    		var el = jQuery(this).parent().find(".helpcontent");
    		el.fadeIn(250);
    	},
    	function () {
    		var el = jQuery(this).parent().find(".helpcontent");
	    	el.fadeOut(1000);
    	}
    );
});


/* apply to task lightbox */
function applyToTask(id) {

	window.addEvent('domready', function() {            
	                
	    var thelink = $("applytotask");
	    if(thelink != null) {                
	        thelink.setAttribute('href', '/doen/apply-to-task?id='+id);
	        thelink.setAttribute('rel', "{handler:'iframe',size:{x:470,y:392},iframePreload:true}");
	        
	        SqueezeBox.initialize({
	            size: {x: 350, y: 358}
	        });
	        SqueezeBox.assign(thelink, { parse: 'rel'});
	    }
	});
}

function initLightBox(el) {

jQuery(document).ready(function() {
	    var thelink = $(el);
	    if(thelink != null) {                
	        SqueezeBox.initialize({
	            size: {x: 350, y: 358}
	        });
	        SqueezeBox.assign(thelink, { parse: 'rel'});
	    }
	});
}


/************
*	Browser detectie
************/
var browser =
{
	identify : function()
	{
		var t = document.getElementsByTagName("html");
		var a = navigator.userAgent.toString().toLowerCase();
		var b;

		// todo: "opera" en de render engiens "ff gecko", "chrome webkit", "ie ie6 trident"
		switch(true)
		{
			case (a.indexOf("msie 6") != -1)	:	b = "ie ie6";		break;
			case (a.indexOf("msie 7") != -1)	:	b = "ie ie7";		break;
			case (a.indexOf("msie 8") != -1)	:	b = "ie ie8";		break;
			case (a.indexOf("firefox") != -1)	:	b = "ff";			break;
			case (a.indexOf("chrome") != -1)	:	b = "chrome";		break;	
			case (a.indexOf("safari") != -1)	:	b = "safari";		break;
			default								:	b = "undefined";
		}
		t[0].className = b;

		return b;
	}	
}



/************
*	Expertises naar je profiel verplaatsen
************/
function shiftOptions(sourceId, targetId)
{
 
    
	var sourceSelect = document.getElementById(sourceId);
	var targetSelect = document.getElementById(targetId);
	var num = sourceSelect.options.length - 1; //  
	
	var opts = new Array();
   
	if(num == -1)
		return false;
	
   
	for(var i=num; i>=0; i--)
	{
	    
		var opt = sourceSelect.options[i];
	
		if(opt.selected == true)
		{
			try {
				targetSelect.options.add(opt, null);
			}
			catch(e) {
				targetSelect.options.add(new Option(opt.text, opt.value), 0);
				sourceSelect.options.remove(i);
			}
		}
	}
	
	sortSelectBox(sourceSelect);
	sortSelectBox(targetSelect);
	
	return false;
}


/************
*	Het sorteren van de expertises selectboxes
************/
function sortSelectBox(el)
{
	var arrTexts = new Array();
	var num = el.length;
	
	for(i=0; i<num; i++)
		arrTexts[i] = el.options[i].text+':'+el.options[i].value; 
	
	arrTexts.sort();
	
	for(i=0; i<num; i++)
	{
		opt = arrTexts[i].split(':'); 
		el.options[i].text = opt[0];
		el.options[i].value = opt[1];
		el.options[i].selected = false;
	}
}

/************
*	Alle expertises selecteren als het form ge-submit word
************/
function selectAllExpertises() {
	var expertises = document.getElementById("b");
	var num = expertises.options.length;
	
	for(var i=0; i<num; i++)
		expertises.options[i].selected = true;
		
	return true;
}





