$(document).ready(function(){
	var animatedLine = new AnimatedLine();
	animatedLine.run();
	
	var lastParentId = null;
	var width = null;
	
	setTimeout(function(){
		
		$('#workGalleryTitle .cufon-canvas').each(function(){
			
			if($(this).attr('alt') == " ")
				$(this).css('width','0px');
			
		});
			
		$('.homePageProductTitle > .cufon-canvas').each(function(){
			
			if($(this).attr('alt') == " "){
				$(this).css('width','0px');
			}else{
				
				if (lastParentId === $(this).parent().attr('id')){
					width += $(this).width();
				}else{
					width = $(this).width();
				}
				
				lastParentId = $(this).parent().attr('id');
				
				$(this).parent().width(width);
				
			}
			
		});
			
		$('.homePageNewsTitle .cufon-canvas').each(function(){
				
			if($(this).attr('alt') == " "){
				$(this).css('width','0px');
			}else{
				
				$(this).parent().parent().css('width',$(this).width());
				
			}
			
			
		});
	
	},100);
	
	
});

function AnimatedLine() {
	
	var currentlySelectedElement;
	var pathArray = window.location.pathname.split( '/' );
	
	this.run = function (){
		
		$('body').css({ opacity: 0.5 }).animate({opacity:1.0},"fast");

		sendLineBackToCorrectPageTitle(false);
		sendLineBackToCorrectPageTitle(true);
		makeLineBlueIfNecessary();
		
		$("#gallery").mouseover(function(){sendLineBackToCorrectPageTitle(true);});
		
		$("#services").live('mouseover',moveLine);
		$("#clients").live('mouseover',moveLine);
		$("#work").live('mouseover',moveLine);
		$("#about").live('mouseover',moveLine);
		$("#contact").live('mouseover',moveLine);
		$("#blog").live('mouseover',moveLine);
		$("#products").live('mouseover',moveLine);
		//$("#logo").live('mouseover',moveLine);		
		
		$(window).resize(function(){moveLineToId(currentlySelectedElement,false);});
		
		sendLineBackToCorrectPageTitle(false);
		sendLineBackToCorrectPageTitle(true);
		
		setTimeout(function(){sendLineBackToCorrectPageTitle(false);},100);
		
	};
	
	function sendLineBackToCorrectPageTitle(animated){
		
		if (pathArray[1] != null && pathArray[1] != "home" && pathArray[1] != ""){
			moveLineToId("#"+pathArray[1],animated);
		}
		
		else{
			moveLineToId("blank",animated);	
		}
		
	}

	function makeLineBlueIfNecessary(){

		if ($(currentlySelectedElement).attr("id") == "products"){
			
			$("#animatedLine").css('background-color','#000000');
		//	$("#leftArrow").attr("src",'/static/images/header/dbsite_header_bar_blue_left.png');
		//	$("#rightArrow").attr("src",'/static/images/header/dbsite_header_bar_blue_right.png');			
			
			}
		else {
			
			$("#animatedLine").css('background-color','#de0120');
			//$("#leftArrow").attr("src",'/static/images/header/dbsite_header_highlight_leftcorner.png');
			//$("#rightArrow").attr("src",'/static/images/header/dbsite_header_highlight_rightcorner.png');
			
		}
		
	}
	
	function moveLineToId(idToMoveTo,animated){
		
		var marginLeft = ($(window).width() - $('#header').width()) / 2;
		
		if (marginLeft < 0){marginLeft = 0;}

		$('#logo').css('left',(marginLeft-120)+'px');
		$('.nycmateVideoContainer').width($(window).width());
		
		if (idToMoveTo === "blank" || idToMoveTo == null){
			$('#animatedLine').fadeOut();
			return;
		}
		
		$("#footer").width($(window).width());
		
		if (idToMoveTo == currentlySelectedElement && animated == true){return;}
		
		$("#animatedLine").stop();	
		//$("#rightArrow").stop();
		//$("#leftArrow").stop();
		
		var padding = $(idToMoveTo).css("padding-left").replace('px','');
		
		if (animated && $('#animatedLine').is(":visible")){
			$('#animatedLine').fadeTo(0,1.0);

			if(idToMoveTo == "#logo"||idToMoveTo.id == "logo"){
				/*$("#animatedLine").animate({left: 0,width: 0}, "fast",makeLineBlueIfNecessary);
				$("#rightArrow").animate({left: $(idToMoveTo).position().left + 1.05*padding + 89}, "fast",makeLineBlueIfNecessary);
				$("#leftArrow").animate({left: $(idToMoveTo).position().left + 70}, "fast",makeLineBlueIfNecessary);*/
				
				$('#animatedLine').fadeOut();
			}
			
			else{
				
				$('#animatedLine').fadeIn();
				$("#animatedLine").animate({left: $(idToMoveTo).position().left + 1.05*padding,width: $(idToMoveTo).width()}, "fast");
				//$("#rightArrow").animate({left: $(idToMoveTo).position().left + 1.05*padding -11 + $(idToMoveTo).width()}, "fast");
				//$("#leftArrow").animate({left: $(idToMoveTo).position().left +26.5}, "fast");
			}
		}
		
		else{
			
			if(idToMoveTo == "#logo"||idToMoveTo.id == "logo"){
				$("#animatedLine").css( 'left', '75%' );
				$("#animatedLine").css( 'width', 0 );
				//$("#rightArrow").css( 'left', $(idToMoveTo).position().left + 1.05*padding + 89 );
				//$("#leftArrow").css('left', $(idToMoveTo).position().left + 70);
				$('#animatedLine').hide();

			}
			else{
				$('#animatedLine').fadeIn();
				$("#animatedLine").css( 'left', $(idToMoveTo).position().left + 1.05*padding);
				$("#animatedLine").css( 'width', $(idToMoveTo).width() );
				//$("#rightArrow").css( 'left', $(idToMoveTo).position().left + 1.05*padding -11 + $(idToMoveTo).width());
				//$("#leftArrow").css( 'left', $(idToMoveTo).position().left +26.5);

			}
		}
		
		currentlySelectedElement = idToMoveTo;
		
	}
	
	function moveLine(){
		moveLineToId(this,true);
	}
	
}



