/**
 * check if we visited this page before.  if not set cookie so the next call returns false
 *
 */
function is_first_load() 
{
	var has_cookie = $.cookie('DNAInfo_visited');
	if(has_cookie) {
		obj_resize('collapse');
		return false;
	}
		
	obj_resize('expand');
	$.cookie('DNAInfo_visited', 1);
	return true;
}


/**
 * Expand or collapse 'expanding flash' ad
 * @parameter	stringt	action	Set to the action, either expand or collapse
 */
function obj_resize(action) {
	if(action == "expand") {
		$('#flash_expand', window.parent.document).height(330);
	}
	else if(action == "collapse") {
		$('#flash_expand', window.parent.document).height(30);
	}
}


/**
 * resize the iframe
 * @parameter	object	frame	Object or Object's ID
 */
            function resize(frame)
            {

	//if frame is an string (obj id) get the object
	if(frame instanceof String) {
		frame = $('#'.frame);
	}

                //find the width of the internal page
	var width = frame.contentWindow.document.body.scrollWidth;
                //find the height of the internal page
	var height = frame.contentWindow.document.body.scrollHeight;
				
                //If the New Ad's Width is different change the Width of the iframe
	frame.width = width;

                //If the New Ad's Height is different change the Height of the iframe
	frame.height = height;
            }


            function GetFrames()
            {
                var Frames = new Array();
				var count=0;
                $("body").find("iframe").each(function(){
					if( $(this).attr("id").search("doubleclick")!=-1){
                    	Frames[count++] = $(this).attr("id");
					}});
	
                return Frames;
            }
        

function changeAd(refresh_ad) {
	if(typeof(googletag) == 'object') {
    	if((typeof(refresh_ad) != undefined) && (refresh_ad != null)) {
    		googletag.pubads().refresh([refresh_ad]);
    	}
    	else {
    	    //refresh all ads
		googletag.pubads().refresh();
						}
					}
}

if($('#stickyPanel').length) {
	var topPosition = $('#stickyPanel').offset().top;
	var heightLessFooter = $(document).height() - 410;
	$(window).scroll(function() {
		var adPosition = $(document).scrollTop() + $('#stickyPanel').height();
		if($(document).scrollTop() + 5 > topPosition) {
			$('#stickyPanel').css('top', '5px');
			$('#stickyPanel').css('position', 'fixed');
		} else if ($(document).scrollTop() + 5 < topPosition) {
			$('#stickyPanel').css('top', '0');
			$('#stickyPanel').css('position', 'static');
		} 
		if (adPosition > heightLessFooter) {
			var adjustedTop = adPosition - heightLessFooter;
			$('#stickyPanel').css('top', '-'+adjustedTop+'px');
		}
	});
}

