/*************************************************
make sure this code is at bottom of your page:
	<div id="popupHolder" class="popupHolder">
		<div class="popupContent">
        	<div class="roundWrapper">
                <div class="topLeftRound">&nbsp;</div>
                <div class="roundMid">&nbsp;</div>
                <div class="topRightRound">&nbsp;</div>
			</div>
			<iframe id="iframeContent" frameborder="0" width="100%" name="iframeContent"></iframe>
        	<div class="roundWrapper">
                <div class="bottomLeftRound">&nbsp;</div>
                <div class="roundMid">&nbsp;</div>
                <div class="bottomRightRound">&nbsp;</div>
			</div>
		</div>
	</div>
	<div id="popupScreen"></div>

**************************************************
	
include these files with popup:
	queryPlugin.js
	genericPopup.css
	http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js [or native jquery file]

**************************************************
	
add this class to the anchor tag you'd like to pop up:
	class="blankPopupClass"
	
**************************************************
	
add this query string to the end of your href location to determine height and width
	?bpopw=[width in px]&bpoph=[height in px]

**************************************************

add this class to the close button anchor tag on your iframe page
	class="closeBtn"
	
**************************************************

add this script at the top of your iframe page
	<script type="text/javascript">
		$(document).ready(function() {
			$('a.closeBtn').click(function(){
				$('#popupHolder',window.parent.document).animate({opacity:0},500,function(){
					$('#popupHolder',window.parent.document).hide();
				});
				$('#popupScreen',window.parent.document).hide();
			});
		});
	</script>
	
**************************************************
	
this plugin interacts between a page and a frame; make sure pages have the same document.domain and are using the same SSL config

*************************************************/

//$(document).ready(function(){
	//check for special query string on page
	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);
		}
	}	
	//add click function
	$('a.blankPopupClass').live('click',function(){
		openBlankPopup($(this));
		
		//genOmn.init("events:eventVal1, campaign:campaignVal, pageName:new page name, eVar33:newEvar");

		return false;
	});
	function openBlankPopup(aClass){
		//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');
		//check for truck detail
		var truckDetail = false;
		if(popHeight == 2600 || popHeight == '2000'){
			truckDetail = true;
		}
		var externalFrame = $.query.load(modUrl).get('external');
		if(externalFrame){
			appendCloseButton();
		}
		populateAndOpenBlank(modUrl, popHeight, popWidth, truckDetail);
		return false;
	}
	function populateAndOpenBlank(url, height, width, truckDetail){
		$('#popupHolder').css('margin-top','0px');
		$('#popupHolder .popupContent').height(height+20);
		$('#popupHolder .popupContent #iframeContent').height(height);
		$('#iframeContent').bind('load',function(){setBlankWidth(width, truckDetail)});
		$('#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, truckDetail){
		$('.roundWrapper .roundMid').width(pxWide-20);
		$('.popupContent, .popupHolder')
			.css('overflow','hidden')
			.width(pxWide);
		//iframe special commands
		/*
		$('#iframeContent').contents()
			.find('body')
			.css('overflow','hidden')
			.css('margin','0')
			.css('padding','0')
			.css('background','#FFFFFF')
			.width(pxWide);
		*/
		$('iframeContent').css('overflow','hidden');
		$('#iframeContent').unbind('load');
		centerBlankPopup(truckDetail);
		return false;
	}
	function centerBlankPopup(truckDetail){
		var windowScrollTop = $(window).scrollTop();
		var windowHeight = $(window).height();
		var halfWindowHeight = windowHeight/2;
		var holderOuterHeight = $('#popupHolder').outerHeight();
		var halfHolderOutHeight = holderOuterHeight/2;
		var holderTopPos = halfWindowHeight - halfHolderOutHeight + windowScrollTop;
		$('#popupScreen').css('top', windowScrollTop);
		$('#popupHolder').css('top', holderTopPos);
		if($('#popupHolder').position().top < 0){
			if(truckDetail){
				$('#popupHolder').css('top','100px');
			}
			else{
				$('#popupHolder').css('margin-top','195px');
			}
		}
		var windowWidth = $(window).width();
		var halfWindowWidth = windowWidth/2;
		var holderOuterWidth = $('#popupHolder').outerWidth();
		var halfHolderOutWidth = holderOuterWidth/2;
		var holderLeftPos = halfWindowWidth - halfHolderOutWidth;
		$('#popupHolder').css('left', holderLeftPos);
		
		var _window = $(window);
		var popupScreen = $('#popupScreen');
		var popupHolder = $('#popupHolder');
		_window.scroll(function(){
			popupScreen.css('top',$(document).scrollTop());
			//popupHolder.css('top',holderTopPos + $(window).scrollTop());
			/*
			if(popupHolder.position().top < 0){
				popupHolder.css('margin-top','150px');
			}
			*/
		});
		return false;
	}
	function appendCloseButton(){
		$('div.roundMid:first').append('<a class="closeBtn2" href="#" style="position: absolute; right: 59px; top: 17px; color: #0B395F;">Close</a>');
		$('a.closeBtn2').click(function(){
			$('#popupHolder').animate({opacity:0},500,function(){
				$('#popupHolder').hide();
			});
			$('#popupScreen').hide();
			$('#iframeContent').attr('src','');
			$(this).remove();
		});
	}
//});
