jQuery(function() {
 endTrim();
 modalWindow();
 selectItem();
 bubbleLogin();
 truncateText();
});

// END TRIM OFFS --------------------------------------------
function endTrim() {
 $('nav#global ul').find('li:last').css('padding-right', '0').css('border-right', 'none');
 $('nav#global ul').find('li:first').css('padding-left', '0');
 $('ul.controls').find('li:first').css('margin-left', '0');
};

// MODAL POPUP WINDOWS --------------------------------------------
function modalWindow() {
 //When you click on a link with class of poplight and the href starts with a # 
	$('a.modal[href^=#]').click(function(e) {
  e.preventDefault(); // Cancel the link behavior

		var popID = $(this).attr('rel'); //Get Popup Name
  var popURL = $(this).attr('href'); //Get Popup href to define popup location (eg. ?w=300&amp;t=100&amp;=100)

  //Pull Query & Variables from href URL
  var query = popURL.split('?');
		var dim = query[1].split('&');
  var popWidth = dim[0].split('=')[1]; // Get the first query string value
  var popHeight = dim[1].split('=')[1];
		var popTop = dim[2].split('=')[1];
  var popLeft = dim[3].split('=')[1];
  var popOpacity = dim[4].split('=')[1];
  var popCenter = dim[5].split('=')[1];

  //Center or not to center the popup
  if (popCenter == 'no') {
		 //Fade in the Popup and add close button
   $('#' + popID).fadeIn().css({ 'width' : Number(popWidth), 'height' : Number(popHeight) }).prepend('<a href="#" class="close"><img src="img/icon_close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
   $('.window').fadeIn().css({ 'width' : Number(popWidth), 'height' : Number(popHeight) });
		
   //Apply window positioning
   $('#' + popID).css({ 'left': Number(popLeft) });
   $('.modal_container').css({ 'top' : Number(popTop) });
  } else if (popCenter == 'yes') {
   //Fade in the Popup and add close button
   $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="img/icon_close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
		
   //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
		 var popMargTop = ($('#' + popID).height() + 10) / 2;
		 var popMargLeft = ($('#' + popID).width() + 10) / 2;
		
		 //Apply Margin to Popup
		 $('#' + popID).css({ 
			 'margin-top' : -popMargTop,
			 'margin-left' : -popMargLeft,
    'position' : 'fixed','top' : '50%','left' : '50%'
		 });
  }

		//Fade in Background
		$('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
  if (popOpacity == 'on') {
   $('#fade').css({'filter' : 'alpha(opacity=60)'}).css({opacity: 0.6}).fadeIn(); //Fade in the fade layer 
  } else if (popOpacity == 'off') {
		 $('#fade').css({'filter' : 'alpha(opacity=0)'}).css({opacity: 0}).fadeIn(); //Fade in the fade layer 
		}
		return false;
	});
	
	//Close Popups and Fade Layer
	$('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
	 $('#fade, .window_container').fadeOut(function() {	$('#fade, a.close').remove();	}); //fade them both out

  if ($("#popvideo").hasClass("video")) {
   location.reload();
  };

		return false;
	});
};

// HIGHLIGHT SELECTED ITEMS --------------------------------------------
function selectItem() {
 $('nav#global ul').click(function(e) {
  // unhighlight the previous selection
  $('nav#global ul li.active').removeClass('active');
  // highlight the selection
  $(e.target).closest('li').addClass('active');
 });

 $('div.side_column ul.list').click(function(e) {
  // unhighlight the previous selection
  $('div.side_column ul.list li.active').removeClass('active');
  // highlight the selection
  $(e.target).closest('li').addClass('active');
 });
};

// FLYOUT LOGIN BOX --------------------------------------------
function bubbleLogin() {
 $('#bubbleLogin').click(function(e) {
  e.preventDefault(); // Cancel the link behavior
  $(e.target).closest('li').addClass('active');
  $('.bubble_login').toggleClass('show');
  $('#basecontent, footer').toggleClass('sendback');
  $('body').append('<div id="fade"></div>');
  $('#fade').toggleClass('sendback');
  $('#fade').css({'filter' : 'alpha(opacity=0)'}).css({opacity: 0}).fadeIn(); //Fade in the fade layer
  $('#fade').live('click', function() { //When clicking on fade layer...
   $(e.target).closest('li').removeClass('active');
   $('#fade').fadeOut(function() {	$('#fade').remove();	}); //fade out
   $('.bubble_login').removeClass('show');
   $('#basecontent, footer').removeClass('sendback');
   return false;
  });
 });

 return false;
};

// TEXT TRUNCATOR --------------------------------------------
function truncateText() {
 $('ul.albums li div.details h3').shorten({width: 90});
};
