var 
tin=0,tout=0,
underline='2px',
defaultTitle=' Illustration & Design ',defaultDescription=' Philipp Dornbierer ',
inMain=true,currentsub;

/*


infos


*/

function replaceText(title,desc){
	if(title!=''){
		$('#title').text('').append(title.replace(/\|/g,'<br />'));
	}
	if(desc!=''){
		$('#description').text('').append(desc.replace(/\|/g,'<br />'));
	}
}

function replaceNav(abt,cntct){
	if(abt!=''){
		$('#about').text('').append(abt);
	}
	if(cntct!=''){
		$('#contact').text('').append(cntct);
	}
}

function changeSubpage(nsub){
	currentsub.stop(true).fadeOut(tout);
	/*
	var currentsubtxt=nsub.children().eq(0).attr('src').split('-')[1];
	if(currentsubtxt.indexOf('1')>0) currentsubtxt=currentsubtxt.substring(0,currentsub.length-1);
	*/
	replaceText(nsub.attr('data-title'),nsub.attr('data-description'));
	nsub.stop(true).fadeIn(tin);
	currentsub=nsub;
    var txt=$(location).attr('href');
    if(txt.indexOf('#')>0){
        txt=txt.split('#')[0];
    }
    window.location=txt+"#"+currentsub.children().eq(0).attr('src').split('-')[1];
}

function toSubpage(){
	if(!currentsub.length) return;
    if(inMain){
	if($('#about').data('activated')) underl($('#about'),true);
	if($('#contact').data('activated')) underl($('#contact'),true);
	$('#content').stop(true).fadeOut(tout);
	currentsub.stop(true).fadeIn(tin);
	$('#body').css('backgroundColor','#fff');
	replaceNav('<span id="goleft" class="hand border">Previous</span><span> / </span><span id="goright" class="hand border">Next</span>',' Back ');
	replaceText(currentsub.attr('data-title'),currentsub.attr('data-description'));
    }
    $('#goleft').click(function(){
        var nsub;
        if(currentsub.prev().length) nsub=currentsub.prev();
        else nsub=$('#subpage>*:last');
        changeSubpage(nsub);
    });
    $('#goright').click(function(){
        var nsub;
        if(currentsub.next().length) nsub=currentsub.next();
        else nsub=$('#subpage>*:first');
        changeSubpage(nsub);
    });
    $('#yehteh').addClass('hand');
    $('#about').removeClass('hand');
    $('#goleft,#goright').hover(function(){
    	$(this).css('borderBottomWidth',underline);
    },function(){
    	$(this).css('borderBottomWidth','0px');
    });
    var txt=$(location).attr('href');
    if(txt.indexOf('#')>0){
        txt=txt.split('#')[0];
    }
    window.location=txt+"#"+currentsub.children().eq(0).attr('src').split('-')[1];
	inMain=false;
}

function toIndex(){
	currentsub.stop(true).fadeOut(tout);
	$('#content').stop(true).fadeIn(tin);
	$('#body').css('backgroundColor','#fff');
	$('#goleft,#goright').unbind().css('borderBottomWidth','0px');
	replaceNav(' About ',' Contact ');
	replaceText(defaultTitle,defaultDescription);
	$('#yehteh').removeClass('hand');
	$('#about').addClass('hand');
	//$('#contact').unbind('mouseenter').unbind('mouseleave')
    //$('#contact').css('borderBottomWidth','0px');
	var txt=$(location).attr('href');
    if(txt.indexOf('#')>0){
        txt=txt.split('#')[0];
    }
    window.location=txt;
    inMain=true;
}

function underl(node,ext){//alert(node.data('activated'));
    if(!node.data('activated')){
        //node.css('borderBottomWidth',underline);
        replaceText(node.attr('data-title'),node.attr('data-description'));
        node.data('activated',true);
    }else{
        //node.css('borderBottomWidth','0px');
        replaceText(defaultTitle,defaultDescription);
        node.data('activated',false);
        if(ext) node.css('borderBottomWidth','0px');
    }
}

$(document).ready(function(){
//console.log($(location).attr('href').indexOf('#'));
if($(location).attr('href').indexOf('#')>0){
	currentsub=$('#subpage [data-'+$(location).attr('href').split('#')[1]+']')
	if(currentsub.length>0){
        toSubpage();
	}
}
if(inMain){
	$('#content').fadeIn(tin);
}

var imgs=$('#content>*>*');
imgs.wrap($('<span class="imgwrap hand" style="z-index:100;"/>'));
for(i=0;i<imgs.length;i++){
	//imgs.eq(i).parent().addClass(imgs.eq(i).attr('class'));
}
imgs.addClass('abs');

$('#subpage>*>*')
    .addClass('hand')
    .click(function(){
		toIndex();
	});

$('.imgwrap').click(function(){
	currentsub=$('#subpage [data-'+$(this).children().eq(0).attr('src').split('-')[1]+']');
	toSubpage()
	})
	.hover(function(){
		var img=$(this).children().eq(0);
		$(this)
			.append($('<img id="rollovrimg" class="'+img.attr('class')+'" src="'+img.attr('data-rolloverimage')+'"/>'))
			.append($('<span id="rollovrtxt" class="'+img.attr('class')+'" style="width:'+(img.attr('width')-20)+'px;height:20px;color:#999">'+img.attr('data-rollovertext')+'</span>'));
	},function(){
		$('#rollovrimg').remove();
		$('#rollovrtxt').remove();
	});

$('#contact')
	.click(function(){
    if(inMain){
		if($('#about').data('activated')){
            underl($('#about'),true);
        }
        underl($(this),false);
    }else{
		toIndex();
    }
	})
    .hover(function(){
        //if(!inMain) return;
        $(this).css('borderBottomWidth',underline);
    },function(){
        if(inMain && $(this).data('activated')) return;
        $(this).css('borderBottomWidth','0px');
    });

$('#yehteh')
	.click(function(){
		if(inMain) return;
		toIndex();
	});

$('#about')
    .click(function(){
    if(!inMain) return;
    if($('#contact').data('activated')){
        underl($('#contact'),true);
    }
    underl($(this),false);
    })
    .hover(function(){
        if(!inMain) return;
        $(this).css('borderBottomWidth',underline);
    },function(){ 
        if(!inMain || $(this).data('activated')) return;
        $(this).css('borderBottomWidth','0px');
    });

$('.selectable').css('user-select','none').css('-moz-user-select','none').css('-webkit-user-select','none');
$('#yehteh').css('user-select','none').css('-moz-user-select','none').css('-webkit-user-select','none');
});

