/*
 * jQuery FlyOut
 *
 * version 0.21 (July 21, 2008)
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

/**
 * The flyout() method provides an alternate means of loading and display sub-content
 * with a nifty flyout animation technique.
 * Currently, flyout only supports img sub-content.
 *
 * flyout() takes a single object argument:  $().flyout({param:setting, etc..})
 *
 * Settings:
 *
 *			outSpeed:	speed in milliseconds for the flyout animation - default: 1000
 *
 *			inSpeed:	speed in milliseconds for the flyback animation - default: 500
 *
 *			outEase:	the easing method to use for the flyout animation - default: swing
 *
 *			inEase:		the easing method to use for the flyback animation - default: swing
 *			
 *			loadingSrc: the image file to use while an image is being loaded prior to flyout
 *						default: none
 *						
 *			loader: 	the ID for the created flyout div that contains the sub-content
 *						this is currently only useful for multiple skinnings via CSS
 *						default: 'loader'
 *
 *			loaderZIndex: the CSS z-index for the flyout
 *						default: 500
 *
 *			widthMargin: the left and right margin space for the final flyout
 *						this value is effectively divided between the left and right margin
 *						default: 40
 *			
 *			heightMargin: the top and bottom margin space for the final flyout
 *						this value is effectively divided between the top and bottom margin
 *						default: 40
 *
 * For more details see: http://nixbox.com/demos/jquery.flyout.php
 *
 * @example $('.thumb').flyout();
 * @desc standard flyouts applied to all elements the 'thumbs' class. 
 * 
 * @example $('.thumb').flyout({loadingSrc:'/images/thumb-loading.gif',
								outEase:'easeOutCirc',
								inEase:'easeOutBounce'});
 * @desc flyouts created with different ease in and ease out and a loading animation image is specified
 *
 * @name flyout
 * @type jQuery
 * @param Object options Options which control the flyout animation and content
 * @cat Plugins/Flyout
 * @return jQuery
 * @author Jolyon Terwilliger (jolyon@nixbox.com)
 */


$.fn.extend({flyout : function(options) {
		var shown = false;
		var animating = false;
		var holder;
		var thumb;
		var tloc;
		var th;
		var tw;
		var bigimg = new Image();
		var subType = 'img';
		var offset;
	
		this.click(function() {
			if (animating == true) { return false; }
	
			if (shown) { putAway(this); }
			else { flyOut(this); }
	
			return false;
		});			
		
		var o = jQuery.extend({
			outSpeed : 500,
			inSpeed : 200,
			outEase : 'swing',
			inEase : 'swing',
			loadingSrc: null,
			loader: 'loader',
			loaderZIndex: 500,
			widthMargin: 40,
			heightMargin: 40
		}, options);
	
		function flyOut(it) {
			animating = true;
			
			holder = $(it);
			thumb = $('img',it);
			bigimg = new Image(); 
			sL = $(window).scrollLeft();
			sT = $(window).scrollTop();
			tloc = thumb.offset();
			th = thumb.height();
			tw = thumb.width();
			$('<div></div>').attr('id',o.loader)
							.appendTo('body')
							.css({'position':'absolute',
								'top':tloc.top,
								'left':tloc.left,
								'height':th,
								'width':tw,
								'opacity':.5,
								'display':'block',
								'z-index':o.loaderZIndex});
			if(o.loadingSrc){
				$('#'+o.loader).append($('<img/>').load(function() {
												//alert(this.width+'X'+this.height);
												$(this)
													.attr('alt','Loading...Please wait')
													.css({'position':'relative',
														 'top':th/2-(this.height/2),
														 'left':tw/2-(this.width/2)});
											})
											.attr('src',o.loadingSrc)
						);
			}else{
				$('#'+o.loader).css('background-color','#FFAF5B')
								.append($('<span></span>').text('loading')
															.css({'position':'relative',
																 'top':'2px',
																 'left':'2px',
																 'color':'#FFAF5B',
																 'font-size':'9px'})
									 	);
			}
			$(bigimg).load(function() {
				imgtag = $('<img/>').attr('src',holder.attr('href')).attr('title',holder.attr('title')+" click to minimize the picture").height(th).width(tw);
	
				max_x = $(window).width()-o.widthMargin;
				max_y = $(window).height()-o.heightMargin;
				
				width = bigimg.width;
				height = bigimg.height;                                                      
	
				x_dim = max_x / width;
				y_dim = max_y / height;
	
				if (x_dim <=y_dim) {
					y_dim = x_dim;
				} else {
					x_dim = y_dim;
				}
				
				dw = Math.round(width  * x_dim);
				dh = Math.round(height * y_dim);
				if (dw>width) {dw = width}
				if (dh>height) {dh = height}
				dl = Math.round(($(window).width()/2)-(dw/2)+sL);
				dt = Math.round(($(window).height()/2)-(dh/2)+sT);
	
				$('#'+o.loader).empty().css('opacity',1).append(imgtag).width('auto').height('auto').animate({top:dt, left:dl},{duration:o.outSpeed, queue:false, easing:o.outEase});
				$('#'+o.loader+' '+subType).animate({height:dh, width:dw}, o.outSpeed, o.outEase, function() { 	
																					shown = true;
																					animating=false;
																					$('#'+o.loader+' '+subType).click(function(){putAway(null)})
																				});
			});
			bigimg.src = holder.attr('href');
		}
	
	
		function putAway(next){
			// not necessary right now, but jic.
			if (animating==true || shown==false){return false;}
			animating=true;
			$('#'+o.loader).animate({top:tloc.top, left:tloc.left},{duration:o.inSpeed, queue:false, easing:o.inEase});
			$('#'+o.loader+' '+subType).animate({height:th, width:tw}, o.inSpeed, o.inEase, function(){
			                                                                                  $('#'+o.loader).css('display','none').remove();
                                                                                                 shown=false;
                                                                                                 animating=false;
                                                                                                 bigimg=null;
                                                                                                 if(next){
                                                                                                      flyOut(next);
                                                                                                 }
                                                                                               }
                                                  );
		}
		return this;
	}
});



// jcarousellite_1.0.1.min.js
//
(
function($){
	$.fn.jCarouselLite=function(o){
          o=$.extend({btnPrev:null,btnNext:null,btnGo:null,mouseWheel:false,auto:null,speed:100,easing:null,vertical:false,circular:false,visible:4,start:0,scroll:1,beforeStart:null,afterEnd:null},o||{});
          return this.each(
               function(){
                    var b=false,animCss=o.vertical?"top":"left",sizeCss=o.vertical?"height":"width";
                    var c=$(this),ul=$("ul",c),tLi=$("li",ul),tl=tLi.size(),v=o.visible;
                    if(o.circular){
                         ul.prepend(tLi.slice(tl-v-1+1).clone()).append(tLi.slice(0,v).clone());o.start+=v
                    }
                    var f=$("li",ul),itemLength=f.size(),curr=o.start;c.css("visibility","visible");
                    f.css({overflow:"hidden",float:o.vertical?"none":"left"});
                    ul.css({margin:"0",padding:"0",position:"relative","list-style-type":"none","z-index":"1"});
                    c.css({overflow:"hidden",position:"relative","z-index":"2",left:"0px"});
                    var g=o.vertical?height(f):width(f);
                    var h=g*itemLength;
                    var j=g*v;f.css({width:f.width(),height:f.height()});
                    ul.css(sizeCss,h+"px").css(animCss,-(curr*g));c.css(sizeCss,j+"px");
                    if(o.btnPrev)$(o.btnPrev).click(
                         function(){
                              return go(curr-o.scroll)
                         }
                    );
                    if(o.btnNext)$(o.btnNext).click(
                         function(){
                              return go(curr+o.scroll)
                         }
                    );
                    if(o.btnGo)$.each(
                         o.btnGo,function(i,a){
                              $(a).click(
                                   function(){
                                        return go(o.circular?o.visible+i:i)
                                   }
                              )
                         }
                    );
                    if(o.mouseWheel&&c.mousewheel)c.mousewheel(
                         function(e,d){
                              return d>0?go(curr-o.scroll):go(curr+o.scroll)
                         }
                    );
                    if(o.auto)setInterval(
                         function(){
                              go(curr+o.scroll)
                         },o.auto+o.speed
                    );
                    function vis(){
                         return f.slice(curr).slice(0,v)
                    };
                    function go(a){
                         if(!b){
                              if(o.beforeStart)o.beforeStart.call(this,vis());
                              if(o.circular){
                                   if(a<=o.start-v-1){
                                        ul.css(animCss,-((itemLength-(v*2))*g)+"px");curr=a==o.start-v-1?itemLength-(v*2)-1:itemLength-(v*2)-o.scroll
                                   }else if(a>=itemLength-v+1){
                                        ul.css(animCss,-((v)*g)+"px");curr=a==itemLength-v+1?v+1:v+o.scroll
                                   }else curr=a
                              }else{
                                   if(a<0||a>itemLength-v)
                                        return;
                                   else curr=a
                              }b=true;
                              ul.animate(animCss=="left"?{left:-(curr*g)}:{top:-(curr*g)},o.speed,o.easing,function(){if(o.afterEnd)o.afterEnd.call(this,vis());b=false});
                              if(!o.circular){
                                   $(o.btnPrev+","+o.btnNext).removeClass("disabled");
                                   $((curr-o.scroll<0&&o.btnPrev)||(curr+o.scroll>itemLength-v&&o.btnNext)||[]).addClass("disabled")
                              }
                         }return false
                    }
               }
          )
     };
     
     function css(a,b){  
          return parseInt($.css(a[0],b))||0
     };
     
     function width(a){
          return a[0].offsetWidth+css(a,'marginLeft')+css(a,'marginRight')
     };
     
     function height(a){
          return a[0].offsetHeight+css(a,'marginTop')+css(a,'marginBottom')
     }
}
)
(jQuery);




// common.js
// This function here for legacy support. Shouldn't be used going forward
function MM_openBrWindow(theURL,winName,features) { //v2.0
    window.open(theURL,winName,features);
}

function parseQuery (query) {
   var Params = {};
   if ( ! query ) {return Params;}// return empty object
   var Pairs = query.split(/[;&]/);
   for ( var i = 0; i < Pairs.length; i++ ) {
      var KeyVal = Pairs[i].split('=');
      if ( ! KeyVal || KeyVal.length != 2 ) {continue;}
      var key = unescape( KeyVal[0] );
      var val = unescape( KeyVal[1] );
      val = val.replace(/\+/g, ' ');
      Params[key] = val;
   }
   return Params;
}

$(document).ready(function(){
    $('#frontlines a, #positions a, #information a:not(.nopop), a.pop').click(function () { 
        var linkURL = this.href;
        var baseURL;
        if(linkURL.indexOf("?")!==-1){ // if there is a query string involved
            baseURL = linkURL.substr(0, linkURL.indexOf("?"));
        } else { 
            baseURL = linkURL;
        }
    
        var queryString = linkURL.replace(/^[^\?]+\??/,'');
        var params = parseQuery(queryString);
        if (params) {
            var popWidth = (params['width']*1);
            var popHeight = (params['height']*1);
        }
    
        window.open(baseURL, 'pop', 'width='+popWidth+',height='+popHeight+',scrollbars=yes,resizable=yes');
        return false;
    })

    $('.mt-enclosure-image a').flyout();
    $('.thumbs').jCarouselLite({
        btnPrev: '.prev',
        btnNext: '.next',
        visible: 4
    });          

});

