//a few globals
var navTimerId = 0;

var querystring = "";


function navOn(id) {
    $('#' + id).src = 'images/menu/' + id + 'on.png';
    alert('on:' + id);
}

function navOff(id) {
    $('#' + id).src = 'images/menu/' + id + '.png';
}


//nav image rollover and show subnav - runs on doc ready
$(function() {

    //reset each menu so you can turn the right one on
    hideSubMenus();
    //show the currently selected subnav item
    $('.subMenuDiv.selected').show();

    /*
    var n = 0;
    $("div.enterleave").bind("mouseenter", function() {
    $("p:first", this).text("mouse enter");
    $("p:last", this).text(++n);
    }).bind("mouseleave", function() {
    $("p:first", this).text("mouse leave");
    });
    */

    $('.navImage').mouseenter(function() {navDelay();});
    $('.subnav_menu li').mouseenter(function() {navDelay();});
    $('.navImage').mouseleave(function() {navDelay();});
    $('.subnav_menu li').mouseleave(function() {navDelay();});

    $('.navImage').hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
        swapSubNav($(this).attr('id'));
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
        //swapSubNav($(this).attr('id'));
    });


    $('.subNavImage').hover(function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    }, function() {
        var currentImg = $(this).attr('src');
        $(this).attr('src', $(this).attr('hover'));
        $(this).attr('hover', currentImg);
    });

   fixPaginationLinks();


    floatThumbs();
    floatSmallThumbs();
    

});

function swapSubNav(id) {
    //reset them all
    $('.subMenuDiv').hide();

    $('#sub' + id).show();
    //alert('sub' + id);
}

function hideSubMenus() {$('.subMenuDiv').hide();}

//this is the pause that happens after mouseovers on nav items
//call revert nav if nothing else happens
function navDelay() {
    //init above in globals
    //clear it if it is already running
    clearTimeout(navTimerId);
    //restart it
    navTimerId = setTimeout("revertNav()", 2000);
}

function revertNav() {
    hideSubMenus();
    $('.subMenuDiv.selected').show();
    //clear timeout after reverting
    clearTimeout(navTimerId);
}

function downloadClick(selectId){
     var url = $('#' + selectId).val()
     //alert(url);
     window.open(url);
     pageTracker._trackPageview('/download_clicks' + url);
     return false;
}

function toggleViewMore(eleId) {

     //vertically position image so that it is not below the fold  
     var targetEl = $('#' + eleId);
     var targetImg = $('#' + eleId + " > img");

     var win_height = $(window).height();
     var scrollTop = $(window).scrollTop();
     var win_bottom_pos = scrollTop + win_height;

//reset to original position
targetImg.css({"position" : "relative" , "top" :  "0px"  });

     //original offset is -120px;
     var pos_offset = "120";

     targetEl.toggle();

     var imgOffset = targetImg.offset();
     var el_top = imgOffset.top;
     //alert(el_top);
     var el_height = targetImg.height();  
     var el_bottom = el_top + el_height;

    //check to see if the image would scroll past bottom by default
    if (el_bottom > win_bottom_pos) {
     //alert('img should move');
       var offset = (el_bottom - win_bottom_pos) + 40;
       targetImg.css({"position" : "relative" , "top" :  "-" + offset + "px" }); 
    } else {
         //alert('img is fine');
//       targetImg.css({"position" : "relative" , "top" :  -120px  }); 
    }


    // targetImg.css({"position" : "relative" , "top" :  ((win_height/2 + scrollTop) - el_height/2)  }); 

     //alert(win_height + ":" + el_height + "::" + scrollTop + ":::" + (win_height/2 + scrollTop));

      //targetEl.toggle();
      //$('#' + eleId).toggle();

}

function submitFilters(){$("#filtersForm").submit();}

function submitRegistration(){
    //alert('submit reg'); 
    $("#register_member_form").submit();
}

function submitLogin(){
     //alert('submit login');      
     $("#login_form").submit();
}

function submitPwdReset(){
     //alert('submit pwd'); 
     $("#forgot_password_form").submit();
}

function showContact(eleId, top, left) {
     hideAllContacts();
     var element = $('#contact_' + eleId)
     element.show();
     element.css("color", "#fff");
     element.css("top", top + "px");
     element.css("left", left + "px");
}

function hideAllContacts() {$('.contactCard').hide();}

function swapImage(img) {
   var lgImg = $('#largeImg');
   lgImg.attr('src', img.src);
}

function fixPaginationLinks(){
  var links =   $(".pagination_wrap a");
  for (var i = 0; i < links.length; i++) {
      link = links[i];
      $(link).attr("href", $(link).attr("href") + "?" + querystring);
  }
}

function floatThumbs(){
   var thumbs = $(".floatedThumb");
   //alert("thumbs length = " + thumbs.length);
  
   for (var i = 0; i < thumbs.length; i++) {floatThumb(thumbs[i]);}
}

function floatThumb(img) {

  //container is 261 w x 131 h = aspect ratio of 1.993 w/h)
  //if the aspect of the image is larger than that the image is wider than long

  var aspect = img.width / img.height;
  var newHeight = 0;
  var newWidth = 0;

  //figure out if image is wider or taller to start
  //and make sure that the changing doesn't make the other dimension smaller than
  //the container

  var taller = false;
  if (img.width <= img.height) {

	img.width = 261;
	newHeight = img.width / aspect;
	
	if (newHeight >= 131) {
		taller = true;
                img.height=131;
	} else {
		taller = true;
	}

  } else {
	
	img.height = 131;
	newWidth = img.height * aspect;

	if (newWidth >= 261) {
	    img.width = 261;
            taller = false;
	} else {
		taller = true;
	}
  }

 if (taller != false) {
   
    img.width = 261;
    img.height = img.width / aspect;
    var offset = (img.height - 131) / 2;
    img.style.top = "-" + offset + "px";   
 
  } else { 
  
     img.height = 131;
     img.width = img.height * aspect;
     var offset = (img.width - 261) / 2;
     img.style.left = "-" + offset + "px";

  }
return;
}


function floatSmallThumbs(){
   var thumbs = $(".floatedSmallThumb");
   //alert("thumbs length = " + thumbs.length);
  
   for (var i = 0; i < thumbs.length; i++) {floatSmallThumb(thumbs[i]);}
}

function floatSmallThumb(img) {

  var aspect = img.width / img.height;
  var newHeight = 0;
  var newWidth = 0;

  var taller = false;
  if (img.width <= img.height) {

	img.width = 193;
	newHeight = img.width / aspect;
	
	if (newHeight >= 130) {
		taller = true;
                img.height=130;
	} else {
		taller = true;
	}

  } else {
	
	img.height = 130;
	newWidth = img.height * aspect;

	if (newWidth >= 193) {
	    img.width = 193;
            taller = false;
	} else {
		taller = true;
	}
  }

 if (taller != false) {
   
    img.width = 193;
    img.height = img.width / aspect;
    var offset = (img.height - 130) / 2;
    img.style.top = "-" + offset + "px";   
 
  } else { 
  
     img.height = 130;
     img.width = img.height * aspect;
     var offset = (img.width - 193) / 2;
     img.style.left = "-" + offset + "px";

  }
return;
}


