String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}

$(function(){
    $(".carrousel ul").cleanWhitespace();
    $("#nav ul").cleanWhitespace();
    $(".pry-gallery").cleanWhitespace();	

	$("#nav span").vcenter("margin-top");
    $(".landing-footer-text p").vcenter("margin-top");
	$('.carrousel IMG').vcenter('top');
	
	$('.proyecto-img-nav-next A').click(function(){
		var scrl = $(this).parents('.pry-gallery-frm').find('.pry-gallery');
		scrl.stop(true, true).animate({scrollLeft: scrl.width()}, {duration: 500, complete: function(){
			$('>DIV:first', scrl).insertAfter($('>DIV:last', scrl) );
			scrl.scrollLeft(0);
		}} ) ;
		return false;
	}).attr('href', '#').each(function(){
		var scrl = $(this).parents('.pry-gallery-frm').find('.pry-gallery');
		if($('>DIV', scrl).length <= 1 ){
			$(this).remove();
	    }
	});

    $('#nav A').each(function(){
        var a = window.location + '';
        var b = $(this).attr('href');
        //alert(a + '\n' + b + '\n' + a.endsWith(b));
        if(a.endsWith(b)){
            $(this).addClass('active');
        }
    })

});

jQuery.fn.vcenter = function(method){
	var nds = $(this);
	for(var i=0; i < nds.length; i++){
		var node = $(nds[i]);
		var ldfn = function(){
			if($(this).outerHeight() <= 0) return;
			var mt = ($(this).parent().height() - $(this).outerHeight())/2;
			$(this).css(method, Math.round(mt) + 'px' );
		}
		ldfn.call(node);
		if($('IMG', node).length){
			$('IMG', node).data('node', node);
			$('IMG', node).load(function(){
				if($(this).outerHeight() <= 0) return;
				var mt = ($(this).data('node').parent().height() - $(this).data('node').outerHeight())/2;
				$(this).data('node').css(method, Math.round(mt) + 'px' );
			});
		}else{
			node.load(ldfn);
		}
	}
	return $(this);
}

function rm_white_spaces_ie(node){
	var ns = node.contents().filter(function() { return (node.nodeType != 3 || /\S/.test(node.nodeValue)); });
	var fns = [];
	for(var i=0; i< ns.length; i++){
		var cns = $(ns[i]).contents();
		for(var j=0; j< cns.length; j++)				
		   detach(cns[j]);

		var s = $.trim(ns[i].outerHTML);
		$(ns[i]).remove();
		var n = $(s);
		for(var j=0; j< cns.length; j++) if(cns[j]!=null){
			$(cns[j]).appendTo(n);
		}
		fns.push(n);
	}
	node.empty();
	for(var i=0; i< fns.length; i++){
		fns[i].appendTo(node);
	}
}

jQuery.fn.cleanWhitespace = function() {
	if($.browser.msie && parseInt($.browser.version, 10) < 8 ){
		$(this).each(function(){rm_white_spaces_ie($(this));});
	}else{
		textNodes = this.contents().filter(
			function() { return (this.nodeType == 3 && !/\S/.test(this.nodeValue)); })
			.remove();
	}
    return this;
}


