/* make sure this code is at bottom of grid page:
	<div id="popupHolder" class="popupHolder">
		<div class="popupContent">
			<iframe id="iframeContent" frameborder="0" width="100%" name="iframeContent">
		</div>
	</div>
*/
/* add this class to the link you'd like to pop up:
	class="blankPopupClass"
*/
/* add this query string to the end of your href location
	?bpopw=[width in px]&bpoph=[height in px]
*/
//$(document).ready(function(){

	//check for query string
	function getQuery(id, popupLoc, width, height){
		function getPopQuery(name){
			var query = window.location.search.substring(1);
			var vars = query.split("&");
			
			for (var i=0;i<vars.length;i++) {
				var pair = vars[i].split("=");
				if (pair[0] == name)
				{
					if(pair[1].indexOf("#") != -1)
					{
						var nohash = pair[1].split("#");
						return nohash[0];
					}
					return pair[1];
				}
			}
			return null;
		}
		var idIs = getPopQuery(id);
		if(getPopQuery(id) !== null){
			populateAndOpenBlank(popupLoc+idIs, width, height);
		}
	}
	
	//IAdvantage News Portlet Function, July 2010
	getQuery('id', '/NewsPortlet/viewDetail.jsp?id=', 700, 700);	
	
	/*
	$('a.blankPopupClass').click(function(){
		openBlankPopup($(this));
		console.log('correct');
		return false;
	});
	*/
	//add click function
	$('a.blankPopupClass').live('click',function(){
		openBlankPopup($(this));
		if($(this).attr('class').toString().indexOf('truckDetailLink') != -1){
			var bodyHeight = $('body').height();
			var backdropPop = '<div class="backdropPop" style="background: #000; filter: alpha(opacity=40); height: 100%; left: 0; opacity: 0.4; position: absolute; top: 0; width: 100%; z-index: 2999;"></div>';
			$('body').append(backdropPop);
			$('.backdropPop').height(bodyHeight);
			window.location = '#';
		}
		return false;
	});
	function openBlankPopup(aClass)
	{
		//get href from url
		var isHome = null;
		var modUrl = aClass.attr("href");
		if(aClass.html() == 'Read More'){
			var popHeight = 700;
			var popWidth = 720;
		}
		else if(aClass.html() == 'Edit Profile' || aClass.html() == 'Update your profile' || aClass.html() == 'Edit' || aClass.html() == 'click here'){
			var popHeight = 623;
			var popWidth = 720;
		}
		else{
			//get href from url
			var modUrl = aClass.attr("href");
			//check for query string on href link
			var url = $.query.load(modUrl);
			var popWidth = $.query.load(modUrl).get('bpopw');
			var popHeight = $.query.load(modUrl).get('bpoph');
		}
		if(popHeight == 244 && popWidth == 470){
			isHome = true;
		}
		populateAndOpenBlank(modUrl, popHeight, popWidth, isHome);
		return false;
	}
	function populateAndOpenBlank(url, height, width, isHome)
	{
		$('#popupHolder .popupContent, #iframeContent').height(height);
		$('#iframeContent').bind('load',function(){setBlankWidth(width, url, isHome)});
		$('#iframeContent').attr('src',url);
		$('#popupScreen, #popupHolder').css({'opacity':0,'display':'block'});
		$('#popupScreen').animate({opacity:0.8},500);
		$('#popupHolder').animate({opacity:1},500);
		return false;
	}
	function setBlankWidth(pxWide, url, isHome)
	{
		$('.popupContent, .popupHolder').css('background','#FFFFFF').width(pxWide);
		// This next line is unnecessary on trucks, as all content of iframe is pre-formatted. It also causes security issues.
		//$('#iframeContent').contents().find('body').css('overflow-y','auto').css('overflow-x','hidden').css('background','#FFFFFF').width(pxWide);
		$('#iframeContent').unbind('load');
		centerBlankPopup(isHome);
		return false;
	}
	function centerBlankPopup(isHome)
	{
		var windowHeight = $(document).height();
		var windowWidth = $(document).width();
		var popupWidth = $('#popupHolder').width();
		var popupLeft = (windowWidth-popupWidth)/2;
		if(isHome == true){
			var homeLeft = ($(window).width()-470)/2;
			$('#popupHolder').css({'top':'130px','left':homeLeft});
		}
		else{
			var newTop = ($(window).height()/2 - $('#popupHolder').outerHeight()/2) + $(window).scrollTop();
			if(newTop <=0){
				$('#popupHolder').css('top', '20px');
			}
			else{
				$('#popupHolder').css('top', newTop);
			}
			$('#popupHolder').css('left',popupLeft);
		}
		$('#popupScreen').css('height', windowHeight);
		return false;
	}
//});
