var CSSZibaldone = new function () {
    
    var that = this;
    
    
    this.execute = function(callback) {
        
	
	return callback.method(callback.parameters);
    };
    
    
    var elementExists = function(element) {
    
        if(document.getElementById(element) !== null) {
	    return true;
	} else {
	    return false;
	}
    };
    
    var checkElementLang = function(element) {
    
        element = $(element);
	
	var lang = element.attr('lang');
	
	return lang;
    
    
    };
    
    
    
    
    this.setImageWidth = function() {
    
        if($('img').size()) {
        
	return this.execute({
	    parameters: $('img').not('a img'),
	    method: function(parameters) {
	    
	       parameters.each(function() {
	       
	           var $img = $(this);
		   var parentWidth = $img.parent().width();
		   var imgWidth = $img.width();
		   
		   if($img.hasClass('wide')) {
		   
		       $img.removeClass('wide');
		   
		   }
		   
		   if(imgWidth >= parentWidth) {
		   
		       if(!$img.hasClass('wide')) {
		       
		           $img.addClass('wide');
		       
		       }
		   
		   
		   } else {
		   
		       return;
		       
		   }
	       
	       
	       });
	    
	       
	    }
	});
	
	} else {
	
	    return;
	    
	}
	
    };
    
    
    
    this.normalizeBreadCrumbs = function() {
    
        var html = '';
    
        if(elementExists('breadcrumbs')) {
	    
	    
	    
	    var a = $('#breadcrumbs')[0].getElementsByTagName('a');
	    
	    for(var i=0; i<a.length; i++) {
	    
	      
	    
	       var href = a[i].getAttribute('href');
	       var text = a[i].firstChild.nodeValue;
	       
	       html += '<a href="' + href + '">' + text + '</a>';
	       
	       
	    
	    }
	    
	    var $strong = $('#breadcrumbs strong').text();
	    
	    html += '<strong>' + $strong + '</strong>';
	    
	    $('#breadcrumbs').replaceWith('<div id="breadcrumbs">'+html+'</div>');
	
	} else {
	
	    return;
	    
	}
    
    
    
    };
    
    this.createTableFromList = function() {
    
        if(elementExists('resource-list')) {
	
	    var html = '<table id="resource-list">';
	    
	    $('#resource-list dt').each(function() {
	    
	        var $dt = $(this);
		var $a = $dt.html();
		var $dd = $dt.next().html();
		
		html += '<tr><td class="link">'+$a+'</td><td class="desc">'+$dd+'</td></tr>';
		
	    
	    });
	    
	    html += '</table>';
	    
	    $('#resource-list').replaceWith(html);
	
	} else {
	
	    return;
	    
	}
    
    };
    
    this.createFooterWrapper = function() {
    
        return $('#site-info address').wrapInner('<span></span>');
    
    };
    
        
    this.wrapAcronymsWithLinks = function() {
    
	
	return this.execute({
	
	
	    parameters: $('acronym'),
	    method: function(parameters) {
	    
	        parameters.each(function() {
		
		
		    var $acronym = $(this);
		    var text = $acronym.text().toUpperCase();
		    
		    if(text != '(X)HTML') {
		    
		        $acronym.wrap('<a href="http://en.wikipedia.org/wiki/' + text + '" class="acronym"></a>');
			
		    } else {
		    
			$acronym.wrap('<a href="http://en.wikipedia.org/wiki/XHTML" class="acronym"></a>');
		    
		    }
		
		
		});
	    
	    
	    }
	
	
	});
    
    
    };
    
    this.wrapSpansWithLinks = function() {
    
        if($('span.language').size()) {
	
	
	    return this.execute({
	    
	    
	        parameters: $('span.language'),
		method: function(parameters) {
		
		    parameters.each(function() {
		    
		    
		        var $language = $(this);
			var text = $language.text();
			$language.wrap('<a href="http://en.wikipedia.org/wiki/' + text + '" class="language-link"></a>');
			
		    
		    
		    });
		
		
		}
	    
	    
	    });
	    
	
	} else {
	
	    return;
	    
	}
    
    
    };
    
    this.stylizeFirstParagraph = function() {
    
        return this.execute({
	
	    parameters: $('#main-content h2').not('#main-content div h2').eq(0),
	    method: function(parameters) {
	    
	        parameters.nextAll('p').eq(0).addClass('first-para');
	    
	    }
	
	});
    
    
    };
    
    this.addTableStripes = function() {
    
        $('table:not([class]):not([id]) tr:nth-child(odd)').addClass('alt');
    
    };
    
    this.setOpacityOnCooperationImage = function() {
    
        if(elementExists('cooperation')) {
	
	    $('#cooperation').find('img').css('opacity', '0.7');
	    
	    $('#cooperation div img').hover(function() {
	    
	        $(this).css('opacity', '1');
		
	    }, function() {
	    
	        $(this).css('opacity', '0.7');
		
	    });
	
	
	} else {
	
	    return;
	    
	}
    
    };
    
    this.submitContactForm = function() {
    
        if(elementExists('contact-form')) {
	
	    $('#contact-form').submit(function(e) {
	    
	        var name = $('#contact-form #name').val();
		var email = $('#contact-form #email').val();
		var subject = $('#contact-form #subject').val();
		var message = $('#contact-form #message').val();
		var antispam = $('#contact-form #antispam').val();
		
		var data = 'name=' + name + '&email=' + email + '&subject=' + subject + '&message=' + message +
		            '&antispam=' + antispam;
			    
		$.ajax({
		
		    type: 'POST',
		    url: 'http://www.css-zibaldone.com/gabriele/invio-email/index.php',
		    data: data,
		    success: function(html) {
		    
		        $('#main-content #contact-form').replaceWith(html);
		    
		    }
		
		});
		
	        e.preventDefault();
	    });
	
	} else {
	
	
	    return;
	    
	}
    
    
    };
    
    this.hideShowContactElements = function() {
    
      if(elementExists('contact-form') && elementExists('card')) {
      
         $('#contact-form, #card').hide();
	 
	 $('#contact-actions li a').each(function() {
	 
	     var href = $(this).attr('href');
	     
	     if(href.indexOf('contact-form') != -1) {
	     
	         $(this).click(function(e) {
		 
		     $('#contact-form').slideDown(400);
		     
		     e.preventDefault();
		     
		  });
	     
	     } else {
	     
	        $(this).click(function(e) {
		
		    $('#card').slideDown(400);
		    e.preventDefault();
		    
		});
	     }
	 
	 });
      
      
      } else {
      
          return;
	  
      }
    
    
    };

    this.removeElements = function(content) {
       
       $('div.res ul li').each(function() {
       
         var $li = $(this);
         var text = $li.find('a').text();
         
         if(text == content) {
         
           $li.remove();
         
         
         }
       
       
       }); 
    
    };    
    
    this.loader = function(page) {
    
        page = $(page).attr('id');
	
	return this.execute({
	
	    parameters: page,
	    method: function(parameters) {
	        switch(parameters) {
		    
		    case 'altro':
		    case 'appunti':
		    case 'articles':
		    case 'articoli':
		    case 'author':
		    case 'code':
		    case 'demo':
		    case 'test':
		    case 'traduzioni':
		        that.normalizeBreadCrumbs();
			that.createTableFromList();
			that.setImageWidth();
			that.createFooterWrapper();
			that.wrapAcronymsWithLinks();
			that.wrapSpansWithLinks();
			that.stylizeFirstParagraph();
			that.addTableStripes();
			that.hideShowContactElements();
			that.submitContactForm();
			that.removeElements('Post su Innovando');
			break;
			
		    case 'home':
		        that.setOpacityOnCooperationImage();
		        that.createFooterWrapper();
			that.wrapAcronymsWithLinks();
            that.removeElements('Post su Innovando');			
			break;
		    
		    default:
		        break;
		}
	    }
	});
    
    
    };

    


}();

function init() {
    CSSZibaldone.loader('body');
}

  

$(document).ready(function() {
    
    init();
    
});
                                                


