function openFlashWin(winurl,features)
{
	window.open(winurl,'',features+',location=no,status=no,status=0,titlebar=no,resizeable=yes,left=0,top=0');
}

window.onbeforeunload = function() { //covering everything prevents errors from user events (because jquery unloads but the elements still have the events)
    $('body').loading_layer();
};

$(function() {//on doc ready
	FD.init();
});

var FD = new function() {
	this.curr_loc = window.location + '';
	this.curr_loc = this.curr_loc.substring( this.curr_loc.indexOf('.com')+4 );
	this.featured_text = "Graphwise is the world's first data search engine and visualization tool, designed to locate, extract and visualize data.  It allows users to quickly find, compare and extract facts, figures and statistics and view them in powerful 3D graphs and charts.<br /><br />Developed first as a web application then as an enterprise solution, Graphwise was on the bleeding-edge, developing features not seen before.  I had a blast working with the talented team there; my duties included GUI design and development using PHP, AJAX, JQuery and custom JavaScript in a Unix environment.";
	this.featured_text_short = this.featured_text.substring(0, 256) + '...';
        
	this.init = function() {
        $('body').loading_layer();
        
        //preload images:
	    var bg1 = new Image();
	    bg1.src = "/images/blue_foil_bg.jpg";
	    var feat = new Image();
	    feat.src = "/images/graphwise_zoom.gif";
	    
		$("#menu div[@lnk]").each( function() {
			if ( (FD.curr_loc.indexOf( $(this).attr('lnk') ) != -1 && $(this).attr('lnk') != "/") || FD.curr_loc == $(this).attr('lnk') ) {
				$(this).addClass('hover');
			}
			else {
				$(this).addClass('hand')
				.mouseover(function() {
					$(this).addClass('hover');
				})
				.mouseout(function() {
					$(this).removeClass('hover');
				})
				.click(function(){
					$(this).addClass('hover');
					window.location = $(this).attr('lnk');
				})
			}
		} );

		$("#menu .form_link")
			.addClass('hand')
			.mouseover(function() {
				$(this).addClass('hover');
			})
			.mouseout(function() {
				if ( $(this).attr("class").indexOf('open') == -1 ) {
					$(this).removeClass('hover');
				}
			})
			.click( FD.toggleMenu )

		 $('#main').click( function(){
			FD.closeOpenMenus();
		 } )

		 $('.form .submit').click( FD.checkForm );
		 		 
		 $('.form input').keypress(function(event){ 
			if ( event.keyCode == 13 ) {
				return false;
			}
		 });
		 
		 $('.footer_link, .body_link').click( function(){
		     parid = $(this).attr('parid');
		     $('#menu #' + parid).removeClass('hover').removeClass('open');
		     $(this).TransferTo({to:'#'+parid+'_form',className:'transfer', duration: 600});
		     $('#'+parid).trigger('click');
		     return false;
		 } );
		 
		 $('#featured_project .content p').html( FD.featured_text_short );
		 
		 $('#featured_project').click( FD.openFeatured )
		 
		 $(window).resize(function(){
		 	if ( $('#featured_project').get(0) && $('#featured_project').attr("class").indexOf('open') != -1 ) {
			  	$('#featured_project').bind('click', FD.openFeatured ).trigger('click');
		 	}
		 	if ( $('#master_port').get(0) && $('#master_port').attr("class").indexOf('open') != -1 ) {
				FD.openPortfolio();
		 	}
 		 });

		 $(window).scroll(function(){
		 	if ( $('#featured_project').get(0) && $('#featured_project').attr("class").indexOf('open') != -1 ) {
			  	$('#featured_project').bind('click', FD.openFeatured ).trigger('click');
		 	}
		 	if ( $('#master_port').get(0) && $('#master_port').attr("class").indexOf('open') != -1 ) {
				FD.openPortfolio();
		 	}
 		 });
		 
		 //FAQ:
		 $('.question').click(function(){
		     par_p = $(this).parents('.QA')
		     $('.answer', par_p).BlindToggleVertically(200);
		     $(this).blur();
		     return false;
		 })

		$(':input[@def_val]').each(function(){
            var this_val = $(this).attr('def_val');
            $(this).bind('focus', function(){
                if ( $(this).val() == this_val ) {
                    $(this).val('');
                }
            })
            
            $(this).bind('blur', function(){
                if ( $(this).val() == '' ) {
                    $(this).val(this_val);
                }
            })            
        })
		 
		 //Portfolio:
		 $('.port_thumb_wrapper')
		 	.mouseover( function(){
		 		$('.port_title', this).addClass('hover')
		 	} )
		 	.mouseout( function(){
		 		$('.port_title', this).removeClass('hover')
		 	} )
		 	.click( FD.openPortfolio )		 	
		 			 
		 remove_loading_layer();
	}//end init
	
	this.openPortfolio = function(){
		remove_loading_layer();
		elem = this;
		thumb_id = $(elem).attr('thumb_id');
		var dims = findWindowCenter(-370, -120);
		if ( $('#master_port').attr("class").indexOf('open') == -1 ) {
			master_content = '';
			master_content += '<div class="portfolio_thumb">';
			master_content += '<img src="/images/'+thumb_id+'_full.gif" />'
			master_content += '</div>';
			master_content += '<div class="bold">';
			master_content += '<img src="/images/close.gif" align="right" class="close hand" onClick="FD.closePortfolio()" />';
			master_content += '<a href="' + eval(thumb_id+"_link") + '" target="_blank">' + eval(thumb_id+"_title").replace(' ', '&#160;') + '</a>';
			master_content += '</div><br />';
			master_content += eval(thumb_id+"_desc");
			$('#master_port').html(master_content)
		}
		
		$('#master_port')
			.css('left', dims[0]+'px')
			.css('top', dims[1]+'px')

		if ( $('#master_port').attr("class").indexOf('open') == -1 ) {
			$('#master_port')
				.fadeIn(500, function(){
					$('body').loading_layer2();
				}).addClass('open')				
		}
		else {
			$('body').loading_layer();
		}
	}
	
	this.closePortfolio = function(){
		$('#master_port').fadeOut(300, function(){
			if ( navigator.userAgent.indexOf("MSIE") == -1 ) {			
				$('#loading_dialog').fadeOut(300)
			}
			else {
				remove_loading_layer();
			}
		});
		$('#master_port').removeClass('open');
	}
	
	this.openFeatured = function(){
		FD.closeOpenMenus();
       	$('#fp_td').css('background','none');
		$('#featured_project .title .close').show();
	 	remove_loading_layer();
	 	
	 	//get the position of the column, so we know where to put it back
		//var coors = findPos( $(this).get(0) );
		var coors = findPos( $('#fp_td').get(0) );
		FD.featured_x = coors[0];
		FD.featured_y = coors[1];
		
		//if it's not already open, move it absolutely so it appears to be in the same position as it was relatively
	 	if ( $(this).attr("class").indexOf('open') == -1 ) {
    		$(this).css('left', coors[0]).css('top', coors[1])
	 	}
	 	else {
	 		$('body').loading_layer();
	 	}
        $('#featured_project .project_img').attr('src','/images/graphwise_zoom.gif').css('float', 'left').animate( { className: 'featured_lg_img'}, 500);
	    var dims = findWindowCenter(-370, -165);	    			
		//var new_left = (dims[0]/2)-370;
		//var new_top = (dims[1]/2)-165;
		var new_left = dims[0];
		var new_top = dims[1];

		if ( $(this).attr("class").indexOf('open') != -1 ) {
			$(this).css('left', new_left).css('top', new_top);			
		}
		else {
	        $(this).animate( { className: 'featured_large', left: new_left + "px", top: new_top + "px"}, 500, function(){
		        //$('#featured_project .project_name').html('<a href="http://www.graphwise.com"  target="_blank">www.graphwise.com</a>');
				$('#featured_project .project_name').html('Graphwise');
		        if ( $(this).attr("class").indexOf('open') == -1 ) {
	        		$('body').loading_layer2();
	        		$(this).addClass('open');
		        }
	        } );
		}
		$('#featured_project .content p').html( FD.featured_text );        
        $('#featured_project .title').animate( { className: 'featured_lg_title'}, 500);
        $(this).unbind('click');
        $('#featured_project p, #featured_project .content, #featured_project .title').removeClass('hand');
        $('#featured_project .close').bind('click', FD.closeFeatured)
        return false;
	}
	
	this.closeFeatured  = function() {
		//$('#featured_project .project_name').html('www.graphwise.com');
		$('#featured_project .project_name').html('Graphwise');
		$('#featured_project .content p').html( FD.featured_text_short );
		$('#featured_project .title .close').hide();
		$('#featured_project .title').animate( { className: 'title'}, 500);
		$('#featured_project .project_img').animate( { className: 'project_img'}, 400).attr('src','/images/graphwise_featured.gif').css('float', 'none');
		if ( navigator.userAgent.indexOf("Safari") != -1 ) {
            $('#featured_project').removeAttr('style').css('display','inline')
		}
        $('#featured_project').animate( { 
        	left: FD.featured_x + 'px',
        	top: FD.featured_y + 'px',
		    width: '220px'
        	}, 400, function(){
		        $('#featured_project').bind('click', FD.openFeatured ).css('position','static');
            	$('#fp_td').css('background-color','#e0e0e0');
        	} )
        $('#featured_project p, #featured_project .content, #featured_project .title').addClass('hand');
		if ( navigator.userAgent.indexOf("MSIE") == -1 ) {//IE makes layer opaque before making it transparent
			$('#loading_dialog').fadeOut(500);
		}
		else {
			remove_loading_layer();
		}
		$('#featured_project').removeClass('open');		
        return false;
	}

	this.checkForm = function() {
		var err_msg = '';
		var err_fld = '';
		var form = $(this).parents('.form');
		var realname1 = $('#realname',form).val().trim();
		var email1 = $('#email',form).val().trim();
		var femail1 = $('#femail',form).val().trim();
		var comments1 = $('#comments',form).val().trim();
		
		if (realname1=='') {
			err_msg = 'Please enter your name.';
			err_fld = 'realname';
		}
		else if ( email1=='' || !email1.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\..{2,2}))$)\b/gi) ) {
			err_msg = 'Please enter your email address.';
			err_fld = 'email';
		}
		else if ( femail1=='' || !femail1.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\..{2,2}))$)\b/gi) ) {
			err_msg = 'Please enter a recepient email address.';
			err_fld = 'femail';
		}
		else if (comments1=='') {
			err_msg = 'Please enter your comments.';
			err_fld = 'comment';
		}
		
		if ( err_fld != '' ) {
			$( '.loading',form ).html(err_msg).slideDown(200);
			$( '#'+err_fld,form ).focus();
			return false;
		}
		else {
			$( '.loading',form ).loading_small_wait().slideDown(200);
			$.post('/tellafriend.php', { realname:realname1, email:email1, comments:comments1, femail:femail1 }, function(resp){
				if ( resp == 'ok' ) {
				    if ( femail1 == 'danny@freire-design.com' ) {
				        $( '.loading',form ).html('Thanks for your message.  I\'ll get back to you soon!');
				    }
				    else {
					   $( '.loading',form ).html('Thanks for spreading the word!');
				    }
				}
			})
		}
	}

	this.toggleMenu = function() {
		menu_id = $(this).attr("id")
		if ( $(this).attr("class").indexOf('open') == -1 ) {
			$("#menu .form_link").each(function(){
				if ( $(this).attr("class").indexOf('open') != -1 && $(this).attr("id")!=menu_id ) {
					FD.closeMenuForm( $(this).attr('id') );
				}		 			
		 	})	
			$(this).addClass('hover').addClass('open');
			$('#'+menu_id+'_form').fadeIn(300, function(){
				$('#'+menu_id+'_form input:first').focus();
			});
		}
		else {
			FD.closeMenuForm(menu_id);
		}
	}
	 
	this.closeMenuForm  = function(ident) {
		$("#menu #"+ident).removeClass('hover').removeClass('open');
		$('#'+ident+'_form').fadeOut(300);
	}
		
	this.closeOpenMenus = function() {
		$("#menu .form_link").each(function(){
			if ( $(this).attr("class").indexOf('open') != -1 ) {
				FD.closeMenuForm( $(this).attr('id') );
			}		 			
	 	})		
	}
}

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

function bgSize(){
    if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {      
        yScroll = window.innerHeight + window.scrollMaxY;
        xScroll = window.innerWidth + window.scrollMaxX;
        var deff = document.documentElement;
        var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
        xScroll -= (window.innerWidth - wff);        
        /*
        var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
        yScroll -= (window.innerHeight - hff);
        */
    } else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
        yScroll = document.body.scrollHeight;
        if ( navigator.userAgent.indexOf("MSIE") != -1 ) {
            xScroll = document.body.offsetWidth;       
        }
        else {
            xScroll = document.body.scrollWidth;       
        }
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight;
        xScroll = document.body.offsetWidth;
    }
    return [xScroll, yScroll];
}

function findWindowCenter(widthCorrect, heightCorrect){ 
    if (window.innerHeight || window.innerWidth) {      
        yScroll = window.innerHeight/2 + window.scrollY;
        xScroll = window.innerWidth/2 + window.scrollX;
    } else if (document.body.offsetHeight || document.body.offsetWidth){ // all but Explorer Mac
        yScroll = document.body.offsetHeight/2 + document.body.scrollTop;
		xScroll = document.body.offsetWidth/2
    } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
        yScroll = document.body.offsetHeight/2;
        xScroll = document.body.offsetWidth/2;
    }
    yScroll += heightCorrect;
    xScroll += widthCorrect;
    return [xScroll, yScroll];
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}