
var isIE6 = $.browser.msie && $.browser.version=="6.0" ? true : false;
var isIE8 = $.browser.msie && $.browser.version=="7.0" ? true : false;
var isIE7 = $.browser.msie && $.browser.version=="8.0" ? true : false;

// I translate the coordiantes from a global context to
// a local context.
$.globalToLocal = function( context, globalX, globalY ){
	// Get the position of the context element.
	var position = context.offset();
	// Return the X/Y in the local context.
	return({
		x: Math.floor( globalX - position.left ),
		y: Math.floor( globalY - position.top )
	});
};
 

// I am the FN version of the global to local function.
$.fn.globalToLocal = function( globalX, globalY ){
	return( $.globalToLocal(
			this.first(),
			globalX,
			globalY
		)
	);
};

function goToByScroll(id)
{	
	var target = id == undefined ? 'html' : "#"+id;
	
	//$('html,body').animate({scrollTop: $( target ).offset().top},'slow');
	$('html,body').animate( {scrollTop:'0'},'slow');
}

function position_background( w ) {
	var pos = w < 990 ? -320 : w / 2 - 990 + 160; 
	return  pos;
}

$(document).ready(function() {
	
	/*** inner fade inner page area***/
	var tFade = 500;
	
	if( ! isIE6 && ! isIE7 && ! isIE8  ){
		$("#fade-in").css( "opacity", "0.1");
		$("#fade-in").fadeTo( tFade, 1);
	}

	/*** project-page scroll top ***/
	$('#project-images .scroll-top' ).click(function(e) {
		
		e.preventDefault();
		goToByScroll();
	});
	$('footer .scroll-top' ).click(function(e) {
		
		e.preventDefault();
		goToByScroll();
	});


	/*** position background ***/
	$('body').css("background-position", position_background( $('body').width()) + "px 0px");

	// resize window
	// reposition background
	$(window).resize(function(){
		$('body').css("background-position", position_background( $('body').width()) + "px 0px");
	});

	/*** sitemap mouse over link color ***/

	$("#sitemap-info li a").hover(function(){
			
			var attrColor = $(this).attr("class").split('-')[1];
			if (attrColor != undefined ){
				$(this).css("background-color", attrColor);
			}
		}, function() {
			if (  $(this).attr("class").split('-')[1] != undefined ){
				$(this).css("background", "None");
			}
		});


	/*** projects mouse over link color ***/

	$("#project a.related_project").hover(function(){

			var attrColor =  $(this).attr("class").split(' ')[0].split('-')[1]; 

			if (attrColor != undefined ){
				$(this).css("background-color", attrColor);
			}
		}, function() {
			
			var attrColor =  $(this).attr("class").split(' ')[0].split('-')[1]; 
			
			if (attrColor != undefined ){
				$(this).css("background", "None");
			}
		});

	/*** mouse over top nav ***/
	
	// var localContext;

	if( ! isIE6 ){

		$("#project nav ul img").removeAttr("title");
		$("header ul#sub-nav li a img").removeAttr("title");
		
		$("header ul#sub-nav li a").hover(function(){
			var x;
			tip = $(this).next();
			x =  tip.width() / 2 * (-1);
			$("header ul#sub-nav li .arrow").css( "left", (tip.width() / 2) + "px");
			tip.css( "top", "35px" );
			tip.css( "left", x + "px" );
			tip.slideDown("fast");
		}, function() {
			tip.fadeOut("fast");
		});
	}
	
	/*** mouse over project nav ***/

	if( ! isIE6 ){

		$("#project nav ul li a").hover(function(){
			var x;
			tip = $(this).next();
			x =  tip.width() / 2 * (-1);
			$("#project nav ul li .arrow").css( "left", (tip.width() / 2) + "px");
			tip.css( "top", "35px" );
			tip.css( "left", x + "px" );
			tip.slideDown("fast");
		}, function() {
			tip.fadeOut("fast");
		});
	}

	//localContext = $("#galery figure div");
	/***  mouse over gallery figure nav ***/
	$(".galery figure").hover(function(){
		
		var h = 196;

	    tip = $('p', this);
		tip.css("top", ( h - tip.height()  ) + "px").slideDown("fast");	
		
	}, function() {

		tip.fadeOut("fast");
	
    });
});
