/*
 * Code is licensed under a Creative Commons Attribution-Share Alike License:
 * http://creativecommons.org/licenses/by-sa/3.0/
 * version: 1.0.0
 */

$(document).ready(function(){

    // We add all the stylesheets
    $('head').append('<link rel="stylesheet" href="stylesheets/reset.css" type="text/css" media="screen" />');
    $('head').append('<link rel="stylesheet" href="stylesheets/base.css" type="text/css" media="screen" />');
    $('head').append('<link rel="stylesheet" href="stylesheets/style.css" type="text/css" media="screen" />');

    // We remove internal link of projects
    for(i=1;i<11;i++) {
        $('#projects-'+i).remove();
    }

    // We wait 1 second before initialization to allow browser to load CSS stylesheet.
    setTimeout("init()",1000);

});


function init() {

    var full_hash = '';
    var hash = '';
    var hash_next = '';

    // If direct link, we redirect to about part.
    if( document.location.hash == "" ) document.location.hash = "#about";

    bubble = '';
    show_about = true;

    // Hide the div. Default behaviour.
    full_hash = document.location.hash;
    hash = full_hash.split('-')[0];
    hash_next = full_hash.split('-')[1];

    //if(hash == "#projects") document.location.hash = "#projects";

    // Get every internal link - Need to map each navigation link to appropriate part.
    $("#navigation a[href^='#']").each(function(){

        // Get element's ID.
        href = $(this).attr('href');
        el = $(href);

        if( ( hash != href ) && ( hash != '#' + href.substr( 1 ) ) ) el.hide();
        else {
            bubble = el;
            show_about = false;
        }

        // Set position.
        left = $(this).offset().left - ($(el).width()/2) + ($(this).width()/2);
        $(el).css('left', left);

		// Add click event.
		$(this).click(function(){

			// Get element's ID.
			href = $(this).attr('href');
			el = $('#'+href.substr(1));

			// Toggle previously opened bubble.
			if(bubble != el){
				toggleFade(bubble);
				bubble = el;
			}

			if(href == '#projects') hide_project();

			toggleFade(bubble);

		});
	});

	// Get every project link.
	$(".gallery a[href^='#']").each(function(){
        //Split hash by /
        var href_project = $(this).attr('href');
        var idp = href_project.split('-')[1];
        
        if( hash_next == idp ) {
            show_project(idp);
        }

        // Add click event.
    		$(this).click(function(){
                var href_project = $(this).attr('href');
                var idp = href_project.split('-')[1];
                show_project(idp);
    		});
	});

	//Get every #previous link
	$(".projects a[id^='previousproject']").each(function(){
        var id = $(this).attr('id');
        id = id.replace('previousproject','');
        $(this).attr('href', '#projects-'+(parseInt(id)-1));
        // Add click event.
		$(this).click(function(){
			previous_project("");
		});
	});

    // Init of all internal link on project panel.
    // Get every #next link
	$(".projects a[id^='nextproject']").each(function(){
        var id = $(this).attr('id');
        id = id.replace('nextproject','');
        var total = $(".project_detail").length;

        if(id == total) $(this).attr('href', '#projects');
        else $(this).attr('href', '#projects-'+(parseInt(id)+1));

        // Add click event.
		$(this).click(function(){
			next_project();
		});
	});

	// Add resize event.
	$(window).resize( function() { event_resize(); } );
	
	event_resize();


}

function next_project() {
  var left = $('#project_slider').css('left');
  left = left.replace('px', '');
  var value = parseInt(left)+673;
  $('#project_slider').animate({ left: value }, { queue:true, duration:1500 } );
  
  if(value == 0) $('#back_projects_link').attr('innerHTML','');
  else {
    $('#back_projects_link').attr('innerHTML','<a href="#projects">Back to projects list<a/>');
    $('#back_projects_link').click(function(){
		
			hide_project();
			
		});
  }
}

function previous_project(value) {
  if(value != "") {
    $('#project_slider').animate({ left: value }, { queue:true, duration:1500 } );
    if(value == 0) $('#back_projects_link').attr('innerHTML','');
    else {
      $('#back_projects_link').attr('innerHTML','<a href="#projects">Back to projects list<a/>');
      $('#back_projects_link').click(function(){
  		
  			hide_project();
  			
  		});
    }
  }
  else {
    var left = $('#project_slider').css('left');
    left = left.replace('px', '');
    var newvalue = parseInt(left)-673;
    $('#project_slider').animate({ left: newvalue }, { queue:true, duration:1500 } );
    
    if(newvalue == 0) $('#back_projects_link').attr('innerHTML','');
    else {
      $('#back_projects_link').attr('innerHTML','<a href="#projects">Back to projects list<a/>');
      $('#back_projects_link').click(function(){
  		
  			hide_project();
  			
  		});
    }
  }
}

function hide_project() {
    $('#project_slider').animate({ left: 0 }, { queue:true, duration:1500 } );
    $('#back_projects_link').attr('innerHTML','');
}

function show_project(id) {
    var total = $(".project_detail").length;
    var id_int = parseInt(id.replace('',''));
    var final_id = (total - id_int) + 1;
    
    previous_project((final_id*673)*(-1));
}

function toggleFade(el){
	if(el!=''){
		if($(el).css('display') == 'none'){
			$(el).fadeIn('slow', function () {
				resize(el);
            });
		}
		else{
			$(el).fadeOut('slow');
			bubble = '';
		}
		
	}
}

function resize(el){
	if(el!=''){
		if($(el).css('display') != 'none'){
			
			id = $(el).attr('id');
			linkel = $('#'+id+'-link');
			
			href = linkel.attr('href');
			
			height = ($(document).height()-65-70);
			
			if( $(window).width() < 700 ) width = (700-140);
			else width = ($(window).width()-140);
			
			//Apply each size to each bloc
			if( $(el).attr('id') == "about" ) {
				width = 255;
				height = 510;
			}
			if( $(el).attr('id') == "cv" ) {
				width = 120;
				height = 275;
			}
			if( $(el).attr('id') == "contact" ) {
				width = 300;
				height = 460;
			}
			if( $(el).attr('id') == "login" ) {
				width = 275;
				height = 245;
			}
			if( $(el).attr('id') == "projects" ) {
				width = 673;
				height = 438;
			}
			
			if( id == "lifestream" ) {
				left = 70;
				//$(el).css('left', left);
				
				linkwidth = (linkel.width()/2)
				leftT = linkel.offset().left + (linkwidth - 8);
				$('#'+id+'-t').css('background', 'transparent url(images/triangle.gif) no-repeat scroll '+(leftT-70)+'px center');
			}
			else {
				left = linkel.offset().left - (width/2) + (linkel.width()/2);
				
				if( left < 0) {
					left = 0;
					$(el).css('left', left);
					
					linkwidth = (linkel.width()/2)
					leftT = linkel.offset().left + (linkwidth - 8);
					
					$('#'+id+'-t').css('background', 'transparent url(images/triangle.gif) no-repeat scroll '+(leftT)+'px center');
				} else {
					
					// Check if too right
					if( $('#page').width() < (left + width) ) {
						left = $('#page').width() - width - 10;
						$(el).css('left', left);
					
						linkwidth = (linkel.width()/2)
						leftT = linkel.offset().left + (linkwidth - 8);
					
						$('#'+id+'-t').css('background', 'transparent url(images/triangle.gif) no-repeat scroll '+(leftT-left)+'px center');
					} else {
						left = linkel.offset().left - (width/2) + (linkel.width()/2);
						$(el).css('left', left);
						$('#'+id+'-t').css('background', 'transparent url(images/triangle.gif) no-repeat scroll center center');
					}
				}
			}
			
			if( id == "lifestream" ) {
				$(el).animate({"left": left+"px", "height": height+"px", "width": width+"px"}, "slow", function () {
				  $(el).css('overflow', 'visible');
				  $(el).css('border', '2px solid #262722');
				});
			} else {
				$(el).animate({"height": height+"px", "width": width+"px"}, "slow", function () {
				  $(el).css('overflow', 'visible');
				  $(el).css('border', '2px solid #262722');
				});
				
			}
		}
	} 
}

function event_resize() {
    var full_hash = '';
    var hash = '';
	if( document.location.hash != "" ) {
	  full_hash = document.location.hash;
		hash = full_hash.split('-')[0];
		resize($(hash));
	}	
}
