/**
 * @author Oliver D'Alton
 */
 
var lastInput= "";
var isIE = false;
var isIE_LT8 = false;
var isTestPage = false;
 
$(document).ready(function(){
	
	if($.browser.msie){
	    isIE = true;
	    isIE_LT8 = (parseFloat($.browser.version) < 8) ? true : false ;
	}
	
    $("input").click(function(){
     var inputID = $(this).attr("id");
     
     if(inputID!=lastInput){
        lastInput = inputID;
        $(this).focus();
        $(this).select();
     }
    })
    
    if(location.hash == "#testpage"){isTestPage = true;}
        
    fnFancyBox();

    //fnRollOvers();
    
    fnAddSubNavClicks();
    
    fnEnsureSideNavOpenIE();
    
    if($.browser.msie){
        fnNavFix();
    }
    
    var FFrameWidth = 660;
    var FFrameHeight = 580;
    
    if(isIE_LT8){ 
        FFrameWidth = 665;
        FFrameHeight = 585;
    }
    
    $(".CustomFacOpener").fancybox({padding: 0,frameWidth: FFrameWidth, frameHeight: FFrameHeight, hideOnContentClick: false});
    
    //$("#page-wrapper").show();
    
    fnSetHeights();
    
    //setTimeout('fnFormatDisqus();',1000);
    
    
});

function fnFancyNoScroll(){
 $("#fancy_frame").attr("scrolling", "no");
}

function fnNavFix(){
    $("ul.main-navigation li").each(function(){
    
    $(this).hover(
        function(){
            $(this).find("ul").css("display","block");
            //alert($(this).find("li ul").length);
        },
        function(){
            $(this).find("ul").css("display","none");
        }
    );
    
    });
}


function fnAddSubNavClicks(){
//get rid of empty pluses
$("li.sub-nav.vertical.outer").each(function(){
 if($(this).find("ul.sub-nav.vertical.inner li").length == 0){
  $(this).find("div.sub-nav.vertical.toggle.plus")
     .removeClass("plus")
     .addClass("empty")
  }
 });

 $("div.sub-nav.vertical.toggle").each(function(){
    $(this).click(function(){
    
     if($(this).hasClass("plus")){
          $(this).removeClass("plus")
                   .addClass("minus");
                  
    }else if($(this).hasClass("minus")){
         $(this).removeClass("minus")
                    .addClass("plus");
                    
    }
    
    fnEnsureSideNavOpenIE();
    //fnSetSubNavHt();
     
    });
 });
}
/*
var iSubItmsHt;
function fnSetSubNavHt(){
  iSubItmsHt = 0;
  $("li.sub-nav.vertical.outer").each(function(){
    iSubItmsHt = iSubItmsHt + ($(this).height());
    iSubItmsHt = iSubItmsHt + parseFloat($(this).css("padding-top"));
    iSubItmsHt = iSubItmsHt + parseFloat($(this).css("padding-bottom"));
  });
  $("div.sub-nav.container").height(iSubItmsHt);
  fnHeightFix("div.sub-nav.container");
}
*/
function fnEnsureSideNavOpenIE(){
    if($.browser.msie && parseFloat($.browser.version) < 7){
        $("div.sub-nav.minus+h1+ul").show();
        $("div.sub-nav.plus+h1+ul").hide();
    }
}

//Adds rollover images to all difr anchors tags
//Do not use a # infront of the hex value
function fnRollOvers(){
    $("a.difr").each(function(){

        var fWidth = parseFloat($(this).width());
        var fHeight = parseFloat($(this).height());
        
        $(this).width(fWidth);
        $(this).height(fHeight);
        
        $(this).hover(
        function(){
            $(this).css("background-position", "left bottom");
        },
        function(){
            $(this).css("background-position", "left top");
        });
    });
}

function fnFancyBox(){

    $("a.fancy-box").fancybox({callbackOnShow: fnFixFancyPaddingIE});
}

function fnFixFancyPaddingIE(){
//fixes image size with padding for fancybox in IE 7 and less - Oliver
     if(isIE_LT8){
           var padding = parseFloat($("#fancy_content").css("top"));
           var curWidth = parseFloat($("#fancy_content img").width());
           var curHeight = parseFloat($("#fancy_content img").height());
           var iShrnkBy = padding * 2 -20;
          $("#fancy_content img").width(curWidth -iShrnkBy)
                                 .height(curHeight -iShrnkBy)
                                 .show();
    }
}

function fnSetHeights(){

  //fnHeightFix("#page-wrapper");
  $("div.flickr-gallery.preview").each(function(){fnHeightFix(this)});
  $("div.box-module div.mod-content").each(function(){fnHeightFix(this)});
  $("div.box-module.mod-html-block").each(function(){fnHeightFix(this)});
  fnHeightFix("div.mod-top");
}

function fnHeightFix(ele){
    
    var iHeight = parseFloat($(ele).height());
    var iProdEight = fnUpToProdEight(iHeight);
    if(iHeight != iProdEight){
        $(ele).height(iProdEight);
    }
 }

function fnUpToProdEight(i){
  var bReturn = false;
  var iFailsafe = 0;
  while(!bReturn && iFailsafe<8){
    iFailsafe ++;
    (i % 8 == 0) ? bReturn=true : bReturn=false;
    if(bReturn != true){ i = i + 1; }
  }
  return i;
}
/*function setContentPositions(){
	
	$('#content-wrapper').show();
	setXPosition($('#content-wrapper'));

    // massage the footer
	setXPosition($('#mod-footer'));

}

 
function setXPosition(element){
	var offset = $(element).offset();

	var current = $(element).offset().left;
	$(element).css({'position':'relative','left': (current - roundIt(current))});
}


function setYPosition(element){
	var offset = $(element).offset();

	var current = $(element).offset().top	
	$(element).css({'position':'relative','top': (current - roundIt(current))});

}


function setHeight(element){
	var current = $(element).height();
	$(element).css({'height':(roundIt(current))});
}

function roundIt(num){
	var roundtoNearest = 8
    if( num !="" )
    {
		num = Math.round(num/roundtoNearest) * roundtoNearest;
    }
	return num
}

window.onresize = setContentPositions;*/

/*-------------------DISQUS------------------*/
var disqus_url = addSlash(location.href.replace(location.hash, ''));
var disqus_developer = 1;

if(location.hash=='#testpage'){
alert(disqus_url);
}

function addSlash(inStr){
var lastChar = inStr.substr(inStr.length -1, 1);
var outStr = (lastChar == '/') ? inStr : (inStr.indexOf('?')>0) ? inStr.replace('?','/?').replace('//?','/?') : inStr + '/' ;


return outStr;
}

function openFacWindow()
{
    $(".CustomFacOpener").click();
}

/*
function fnFormatDisqus(){

if($("#disqus_thread").length > 0){
  $("div.dsq-comment-footer").append('<div class="dsq-foot-left">&nbsp;</div><div class="dsq-foot-right">&nbsp;</div>').addClass("clear");
  $("li.dsq-report a").text("Flag");
  var sCommCount = $("h3.dsq-h3-commentcount").text();
  var iSlicePos = sCommCount.search('of') + 2;
  var iSliceEnd = sCommCount.length - 2;
  sCommCount = sCommCount.slice(iSlicePos, iSliceEnd);
  $("h3.dsq-h3-commentcount").html(sCommCount + "&nbsp;");
  
  /*re-arrange disqus header items
  $("#disqus_thread .dsq-comment").each(function(){
    
    var headHtml = $(this).find("div.dsq-comment-header").html();
    $(this).find("div.dsq-comment-body").prepend(headHtml);
    $(this).find("div.dsq-comment-header").html('<div class="dsq-head-left"></div><div class="dsq-head-right"></div>');
  
  });
}
   $("#disqus_thread").css("display","block");

}
*/