function externalLinks() 
{ 
    if (!document.getElementsByTagName) return; 
    
    var anchors = document.getElementsByTagName("a"); 

    for (var i=0; i<anchors.length; i++) 
    { 
        var anchor = anchors[i]; 
        
        if (anchor.getAttribute("href") && 
            anchor.getAttribute("rel") == "external") 
            anchor.target = "_blank"; 
    } 
}

window.addEvent('domready', externalLinks);





function showSingleItemFromSet(set, id)
{
    set.each(function(item) {
        if (item.id==id)
            item.setStyle('display', 'block');
        else
            item.setStyle('display', 'none');
    });
}

/* SFX for "solutions" */

var solutions;
var solutionsNavigation;

function showSolution(id)
{
    showSingleItemFromSet(solutions, id);
}

function animateSolutions()
{
    window.addEvent('domready', function() {

        solutions=$$('#solutions-content li');
        solutionsNavigation=$$('#solutions-navigation a');
        
        solutionsNavigation.each(function(solutionLink) {
            var href=solutionLink.href;
            var id=href.substring(href.lastIndexOf('#')+1);
            
            solutionLink.addEvent('mouseenter', function (event, id) {
                showSolution(id);
                //window.location.replace('#'+id);
            }.bindWithEvent(this, id));
        });
        
        /* if the user has selected an anchor link, show the appropriate solution */
        
        var selectedSolution=location.hash.substring(1);
        
        if (selectedSolution!='')
            showSolution(selectedSolution);
        else
            showSolution("solutions-instructions");
    });
}




/* SFX for "the profiles" */

var profiles;
var profilesNavigation;

function showProfile(id)
{
    showSingleItemFromSet(profiles, id);
}

function animateProfiles()
{
    window.addEvent('domready', function() {

        profiles=$$('#profiles-content li');
        profilesNavigation=$$('#profiles-navigation a');
        
        profilesNavigation.each(function(profileLink) {
            var href=profileLink.href;
            var id=href.substring(href.lastIndexOf('#')+1);
            
            profileLink.addEvent('mouseenter', function (event, id) {
                showProfile(id);
                //window.location.replace('#'+id);
            }.bindWithEvent(this, id));
        });
        
        /* if the user has selected an anchor link, show the appropriate profile */
        
        var selectedProfile=location.hash.substring(1);
        
        if (selectedProfile!='')
            showProfile(selectedProfile);
        else
            showProfile("profiles-instructions");
    });
}


/* SFX for Projects */

Fx.Morph = Fx.Styles.extend({
 
	start: function(className){
 
		var to = {};
 
		$each(document.styleSheets, function(style){
			var rules = style.rules || style.cssRules;
			$each(rules, function(rule){
				if (!rule.selectorText.test('\.' + className + '$')) return;
				Fx.CSS.Styles.each(function(style){
					if (!rule.style || !rule.style[style]) return;
					var ruleStyle = rule.style[style];
					to[style] = (style.test(/color/i) && ruleStyle.test(/^rgb/)) ? ruleStyle.rgbToHex() : ruleStyle;
				});
			});
		});
		return this.parent(to);
	}
 
});

//Fx.CSS.Styles = ["backgroundColor", "backgroundPosition", "color", "width", "height", "left", "top", "bottom", "right", "fontSize", "letterSpacing", "lineHeight", "textIndent", "opacity"];
Fx.CSS.Styles = ["width", "height"];
 
Fx.CSS.Styles.extend(Element.Styles.padding);
Fx.CSS.Styles.extend(Element.Styles.margin);
 
 
//Element.Styles.border.each(function(border){
//	['Width', 'Color'].each(function(property){
//		Fx.CSS.Styles.push(border + property);
//	});
//});





var projectsCategoryNavigation;
var projectsProjectCategories;
var projectsProjectNavigation;
var projectsContent;


function showCategory(id)
{
    showSingleItemFromSet(projectsProjectCategories, id);
}

function showProject(id)
{
    showSingleItemFromSet(projectsContent, id);
}

function morphInDescription (item, duration) {
	var myMorph = new Fx.Morph(item, {wait: false, duration: duration});
	
	myMorph.start('project-description-hover');
	
	item.getElement('h3').setStyle('margin-bottom', '3em');
}


function morphOutDescription (item, duration) {
	var myMorph = new Fx.Morph(item, {wait: false, duration: duration});
	
	myMorph.start('project-description');
	
	item.getElement('h3').setStyle('margin-bottom', '1em');
}



function animateProjects()
{
    window.addEvent('domready', function() {

        projectsCategoryNavigation=$$('#projects-category-navigation a');
        projectsProjectCategories=$$('#projects-project-navigation ul');
		
        projectsProjectNavigation=$$('#projects-project-navigation a');
		projectsContent=$$('#projects-content li');
		
        projectsCategoryNavigation.each(function(link) {
            var href=link.href;
            var id=href.substring(href.lastIndexOf('#')+1);
            
            link.addEvent('click', function (event, id) {
				event = new Event(event).stop();
                showCategory(id);
            }.bindWithEvent(this, id));
        });
		
        projectsProjectNavigation.each(function(link) {
            var href=link.href;
            var id=href.substring(href.lastIndexOf('#')+1);
            
            link.addEvent('click', function (event, id) {
				event = new Event(event).stop();
                // location.replace("#"+id);
                showProject(id);
                
            }.bindWithEvent(this, id));
        });		
        
        /* if the user has selected an anchor link, show the appropriate profile */
        
        var selection=location.hash.substring(1);
        
        if (selection!='')
		{
            showCategory(selection);
			showProject(selection);
		}
        else
		{
			showCategory('empty-category');
			showProject('empty-project');
		}
		
		
		/* add animation to the project display */
		
		$$('#projects-content li').each(function(item) {
			var projectDescription=item.getElement('.project-description');
			var projectScreenshot=item.getElement('.project-screenshot');
			
			projectDescription.setStyle("overflow", "hidden");
			
			projectDescription.addEvent('mouseenter', function (event, item) {
				event = new Event(event).stop();
				morphInDescription(item, 300);
			}.bindWithEvent(this, projectDescription));
			
			
			var outFunction = function (event, item) {
				event = new Event(event).stop();
				morphOutDescription(item, 0);
			}.bindWithEvent(this, projectDescription);
			
			projectDescription.addEvent('mouseleave', outFunction);
			
			if (projectScreenshot)
				projectScreenshot.addEvent('mouseenter', outFunction);
						
			// this overrides the :hover style in CSS
			morphInDescription(projectDescription, 0);
			morphOutDescription(projectDescription, 0);
		});
		
    });
}

