$(function() {
  // Add Mouseover functions
  $( "div.enterleave" ).mouseenter( function() {
    $(this).addClass( 'over' );
  }).mouseleave(function() {
    $(this).removeClass( 'over' );
  });

  // Create buttons
  $( ".g10Button" ).button();
  
  // Create the Dialogs
  $( '.dialog' ).dialog({
    autoOpen: false,
    show: 'blind',
    hide: 'blind',
    draggable: false,
    resizable: false,
    width: 300,
    dialogClass: 'g10'
  });
  
  $( '.opener' ).click( function() {
	var id = $( this ).attr( 'id' );
	id = id.substring( 0, id.indexOf( '-', 0 ));
	var anchorID = '#' + id + '-anchor';
    var myDialogX = $( anchorID ).offset().left
    if( $( anchorID ).hasClass( "right" )) {
      myDialogX = myDialogX - 140;
    } else {
      myDialogX = myDialogX - 6;
    }
    var myDialogY = $( anchorID ).offset().top - $( window ).scrollTop() + 7;
    var dialogID = '#dialog-' + id;
    $( dialogID ).dialog( 'option', 'position', [myDialogX, myDialogY] );
    var opened = false;
    var openingID = '#dialog-' + id;
    $( '.g10dialog' ).each( function() {
      if( $( this ).dialog( 'isOpen' ) ) {
        var closingID = '#' + $( this ).attr( 'id' );
        opened = true;
        $( closingID ).bind( 'dialogclose', function( event, ui ) {
          $( openingID ).dialog( 'open' );
        });
        $( openingID ).bind( 'dialogopen', function( event, ui ) {
          $( closingID ).unbind( 'dialogclose' );
        });
        $( closingID ).dialog( 'close' );
      }
    });
    if( opened == false ) {
      $( openingID ).dialog( 'open' );
    }
    return false;
  });

  $( '.panel' ).dialog({
    autoOpen: false,
    show: 'blind',
    hide: 'blind',
    draggable: true,
    resizable: true,
    width: 600,
    dialogClass: 'g10Panel'
  });
  $( '.panelOpener' ).click( function() {
    var opened = false;
    var openingID = '#panel-' + $( this ).attr( 'id' );
    $( '.g10dialog' ).each( function() {
      if( $( this ).dialog( 'isOpen' ) ) {
        var closingID = '#' + $( this ).attr( 'id' );
        opened = true;
        $( closingID ).bind( 'dialogclose', function( event, ui ) {
          $( openingID ).dialog( 'open' );
        });
        $( openingID ).bind( 'dialogopen', function( event, ui ) {
          $( closingID ).unbind( 'dialogclose' );
        });
        $( closingID ).dialog( 'close' );
      }
    });
    if( opened == false ) {
      $( openingID ).dialog( 'open' );
    }
    return false;
  });
  
  
  $( '.btnDialog' ).dialog({
    autoOpen: false,
    show: 'blind',
    hide: 'blind',
    draggable: true,
    resizable: true,
    width: 600,
    dialogClass: 'g10'
  });
  $( '.btnOpener' ).click( function() {
    var opened = false;
    var openingID = '#dialog-' + $( this ).attr( 'id' );
    $( '.g10dialog' ).each( function() {
      if( $( this ).dialog( 'isOpen' ) ) {
        var closingID = '#' + $( this ).attr( 'id' );
        opened = true;
        $( closingID ).bind( 'dialogclose', function( event, ui ) {
          $( openingID ).dialog( 'open' );
        });
        $( openingID ).bind( 'dialogopen', function( event, ui ) {
          $( closingID ).unbind( 'dialogclose' );
        });
        $( closingID ).dialog( 'close' );
      }
    });
    if( opened == false ) {
      $( openingID ).dialog( 'open' );
    }
    return false;
  });
});

var viaLP = false;
var viaCP = false;

function lpVisible() {
  return !$("div#loginPanel").is( ":hidden" );
}

function cpVisible() {
  return !$( "div#contactPanel" ).is( ":hidden" );
}

function toggleLoginPanel( viaLink ) {
  if( viaLink ) { 
    viaLP = true; 
  }
  $( "div#loginPanel" ).slideToggle( "slow" );
}

function toggleContactPanel( viaLink ) {
  if( viaLink ) {
    viaCP = true; 
  }
  $( "div#contactPanel" ).slideToggle( "slow" );
}

function lpClick( e ) {
  viaLP = true;
}

function cpClick( e ) {
  viaCP = true;
}
 
function bodyClick( e ) {
  if( lpVisible() && !viaLP ) {
    toggleLoginPanel( false );
  } else {
    viaLP = false;  
  }
  if( cpVisible() && !viaCP ) {
    toggleContactPanel( false );
  } else {
    viaCP = false;  
  }
}

function login() {
  document.forms['loginForm'].submit();
  document.forms['loginForm'].reset();
  toggleLoginPanel( false );
}
