var minSiteWidth = 978;
var minSiteHeight = 658;

var maxSiteWidth = 1920;
var maxSiteHeight = 1200;

var newSiteWidth;
var newSiteHeight;

var viewWidth;
var viewHeight;

var currentNavColor;
var navPos;



jQuery.fn.disableSelection = function() {
    $(this).attr('unselectable', 'on')
           .css('-moz-user-select', 'none')
           .each(function() { 
               this.onselectstart = function() { return false; };
               this.onmousedown = function() { return false; };
            });
};



jQuery.fn.exists = function(){return jQuery(this).length>0;}

$(document).ready ( onBasicDomReady );

var defaultBGLoaded = false;

function onDefaultBGLoaded ( ) {
  
  defaultBGLoaded = true;
  
}


function onBasicDomReady ( ) {
  
  $(window).resize ( function () { onBasicResize ( true ); } );
  onBasicResize ( true );
  
  if ( currentNavColor != undefined && currentNavColor != "white" ) {
  
    $(".mainNavLink").css ( "color", "#000000" );
  
  }
  
  Cufon.set('fontFamily', 'Calibri').replace('#metaNav a', { hover: true })('.metaSepterator');
  Cufon.set('fontFamily', 'BlairMdITC TT').replace('#navLinks a', { hover: true })('#navBack a', { hover: true })('.metaSepterator');
  
  $('#logoIMG').supersleight();
  //$(".dottetLineWhite").supersleight();
  
  if ( $("#navActiveState").exists () ) setActiveNav ( );
  
  if ( $("#defaultBGImage").exists () ) fadeBackground ( );
  
  var is_chrome = /chrome/.test( navigator.userAgent.toLowerCase() );
  
  if ( is_chrome ) {
    
    if ( $("#navActiveState").exists () ) $("#navActiveState").css ( "margin-top", 93 );
    if ( $("#navSlideState").exists () ) $("#navSlideState").css ( "margin-top", 87 );
    $(".selectionBoxLink").css ( "padding-top", 4 );
    $(".selectionBoxLink").css ( "padding-bottom", 6 );
    
  } else if ( $.browser.safari ) {
    if ( $("#navActiveState").exists () ) $("#navActiveState").css ( "margin-top", 85 );
    if ( $("#navSlideState").exists () ) $("#navSlideState").css ( "margin-top", 85 );
    $(".selectionBoxLink").css ( "padding-top", 4 );
    $(".selectionBoxLink").css ( "padding-bottom", 6 );
  } else if ( $.browser.msie ) {
    if ( $("#navActiveState").exists () ) $("#navActiveState").css ( "margin-top", 93 );
    if ( $("#navSlideState").exists () ) $("#navSlideState").css ( "margin-top", 93 );
  }

}

function fadeBackground ( ) {
  
  if ( defaultBGLoaded ) {
    
    $("#defaultBGImage").css ( "display", "none" );
    onBGLoaded ( );
    
  } else {
  
    $("#defaultBGImage").css ( "display", "none" );
    $("#defaultBGImage").load ( onBGLoaded );
    
  }
  
}

function onBGLoaded ( ) {
  
  onBasicResize ( true );
  $("#defaultBGImage").fadeIn ( 500 );

}


function setActiveNav ( ) {
  
  $("#navActiveState").css ( "margin-left", 0 );
  
  var num = currentMainnavActive;
  var cLinePos = $("#navActiveState").offset().left;
  var cNavPointPos = $("#mainNavItem_" + num).offset().left;
  var newlineMargin = cNavPointPos-cLinePos;
  $("#navActiveState").css ( "margin-left", newlineMargin );
  $("#navActiveState").css ( "height", 1 );
  $("#navActiveState").css ( "width", $("#mainNavItem_" + num).width()-1 );

}



function onBasicResize ( redo ) {
  
  var centerX = false;
  var centerY = false;
  var diffX;
  var diffY;
  
  viewWidth = $(window).width();
  viewHeight = $(window).height();
  
  newSiteWidth = viewWidth;
  newSiteHeight = viewHeight;
  
  if ( newSiteWidth < minSiteWidth ) {
  
    newSiteWidth = minSiteWidth;
  
  } else if ( newSiteWidth > maxSiteWidth ) {
    
    centerX = true;
    newSiteWidth = maxSiteWidth;
  
  }
  
  if ( newSiteHeight < minSiteHeight ) {
  
    newSiteHeight = minSiteHeight;
  
  } else if ( newSiteHeight > maxSiteHeight ) {
    
    centerY = true;
    newSiteHeight = maxSiteHeight;
  
  }
  
  $("#main").css ( "width", newSiteWidth );
  $("#main").css ( "height", newSiteHeight );
  
  if ( $("#defaultBGImage").exists () && $("#defaultBGImage").width() > 0 ) {
    
    resizeMinCenter ( "#defaultBGImage", newSiteWidth, newSiteHeight, $("#defaultBGImage").width(), $("#defaultBGImage").height() );
  
  }
  
  if ( centerX ) {
    diffX = viewWidth-newSiteWidth;
    $("#main").css ( "margin-left", diffX/2 );
  } else {
    $("#main").css ( "margin-left", 0 );
  }
  
  if ( centerY ) {
    diffY = viewHeight-newSiteHeight;
    $("#main").css ( "margin-top", diffY/2 );
  } else {
    $("#main").css ( "margin-top", 0 );
  }
  
  if ( $("#viewArea") != undefined ) {
    
    var viewPos = ( newSiteHeight-minSiteHeight ) / 2;
    $("#viewArea").css ( "margin-top", viewPos );
    
    if ( $("#navArea") != undefined ) {
    
      navPos = ( newSiteWidth-minSiteWidth ) / 2;
      $("#navArea").css ( "margin-left", navPos );
      
      if ( $("#metaArea") != undefined ) {
        
        $("#metaArea").css ( "margin-left", navPos );
        $("#metaArea").css ( "margin-top", newSiteHeight-37 );
        
      }
  
    }
  
  }
  
  if ( !redo && typeof onSiteResize == 'function' ) onSiteResize ( );
  if ( redo == true ) onBasicResize ( false );
  
}






function resizeMinCenter ( target, width, height, orgWidth, orgHeight ) {

  var newWidth;
  var newHeight;
  
  var posX;
  var posY;
  
  
  newHeight = height;
  newWidth = newHeight*orgWidth/orgHeight;
  
  if ( newWidth < width ) {
  
    newWidth = width;
    newHeight = newWidth*orgHeight/orgWidth;
  
  }
  
  posX = (width-newWidth)/2;
  posY = (height-newHeight)/2;
  
  var tmpScale;
 
  if ( typeof(scaleMode) !== 'undefined' ) {
    tmpScale = scaleMode;
  }
 
  if ( tmpScale != undefined && tmpScale != "" ) {
    
    var tmpScaleData = tmpScale.split ( " " );
    var xAlign = tmpScaleData[1];
    var yAlign = tmpScaleData[0];
   
    if ( xAlign == "Left" ) {
      posX = 0;
    } else if ( xAlign == "Center" ) {
      posX = (width-newWidth)/2;
    } else if ( xAlign == "Right" ) {
      posX = width-newWidth;
    }
    
    if ( yAlign == "Top" ) {
      posY = 0;
    } else if ( yAlign == "Center" ) {
      posY = (height-newHeight)/2;
    } else if ( yAlign == "Bottom" ) {
      posY = height-newHeight;
    }
   
  }
  
  $(target).width ( newWidth );
  $(target).height ( newHeight );
  $(target).css ( "margin-left", posX );
  $(target).css ( "margin-top", posY );

}

function resizeMaxCenter ( target, width, height, orgWidth, orgHeight ) {

  var newWidth;
  var newHeight;
  
  var posX;
  var posY;
  
  
  newHeight = height;
  newWidth = newHeight*orgWidth/orgHeight;
  
  if ( newWidth > width ) {
  
    newWidth = width;
    newHeight = newWidth*orgHeight/orgWidth;
  
  }
  
  posX = (width-newWidth)/2;
  posY = (height-newHeight)/2;
  
  $(target).width ( newWidth );
  $(target).height ( newHeight );
  $(target).css ( "margin-left", posX );
  $(target).css ( "margin-top", posY );
  
  //alert ( newHeight + " // " + orgWidth + " // " + orgHeight );

}


function in_array (needle, haystack, argStrict) {
    
    
    var key = '',
    strict = !! argStrict;

    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }

    return false;
}

 


