/**
 * Window Load
 *
 * These functions will take place after the images are completely loaded.
 */

var yellowPageAdsLoaded = false;
var loadYellowPageAds = function() {

	//alert("window loaded");
	var Constants = new CONSTANTS();
	var yellowPageAdList = new YellowPageAdList();
	var couponList = new CouponList();

	// Giving the logo div height allows the east and west sides of
	// the compass to expand to 100% height in IE6. This also
	// inadvertantly fixes the whitespace that appears at the bottom
	// of the image in FF, chrome and safari (so you don't have to
	// crunch all the tags together on one line).
	$("#proAdHeader .headerBox.logo").css({
		"height" : $("#proAdHeader .headerBox.logo img").height()
	});

	// Build Yellow Page Ad List
	$("#adletYellowPageAds .adPreview .adHolder img").each(function() {
		var yellowPageAd = new YellowPageAd();

		yellowPageAd.setUrl($(this).attr("src"));
		yellowPageAd.setProAdMediaForFilmstripId($(this).attr("proAdMediaForFilmstripId"));
		yellowPageAd.setHeight($(this).height());
		yellowPageAd.setWidth($(this).width());

		yellowPageAd.setAdjustedWidth( yellowPageAd.getWidth() > Constants.MAX_YELLOW_PAGE_AD_DISPLAY_WIDTH() ? Constants.MAX_YELLOW_PAGE_AD_DISPLAY_WIDTH() : yellowPageAd.getWidth() );
		yellowPageAd.setAdjustedHeight( $(this).height() / (yellowPageAd.getWidth() / yellowPageAd.getAdjustedWidth()) );

		yellowPageAd.setPopUpHeight( yellowPageAd.getHeight() + Constants.POPUP_YELLOW_PAGE_AD_ADDITIONAL_HEIGHT() );
		yellowPageAd.setPopUpWidth( yellowPageAd.getWidth() + Constants.POPUP_YELLOW_PAGE_AD_ADDITIONAL_WIDTH() );

		yellowPageAdList.add(yellowPageAd);
	});

	$(".proAdYellowPageAds .adPreview .adHolder img").each(function() {
		var yellowPageAd = yellowPageAdList.getYellowPageAdFromUrl( $(this).attr("src") );

		$(this).hover(function() {
			$(this).parent().parent().parent().find(".fullAd img").attr("src", yellowPageAd.getUrl());
			$(this).parent().parent().parent().find(".fullAd img").css({"width" : yellowPageAd.getAdjustedWidth()});
		});

		// Set width of preview
		if (yellowPageAd.getWidth() > Constants.MAX_YELLOW_PAGE_AD_PREVIEW_WIDTH()) {
			$(this).css({"width" : Constants.MAX_YELLOW_PAGE_AD_PREVIEW_WIDTH() + "px"});
		}
	});

	// Set the height of the display to that of the largest ad and show the previews.
	$(".proAdYellowPageAds .fullAd").css({"height" : yellowPageAdList.getLargestHeight()});
	$(".proAdYellowPageAds .adPreview .adHolder img").slideDown(Constants.TOGGLE_TIME());

	// Set Default Yellow Page Ad.
	if ( yellowPageAdList.size() > 0 ) {
		$(".proAdYellowPageAds .fullAd img").attr("src", yellowPageAdList.getDefaultAd().getUrl());
		$(".proAdYellowPageAds .fullAd img").css({"width" : yellowPageAdList.getDefaultAd().getAdjustedWidth()});
		$(".proAdYellowPageAds .fullAd img").show(Constants.TOGGLE_TIME());
	}

	// Show Ad in pop up.
	$(".proAdYellowPageAds .fullAd img, .proAdYellowPageAds .adPreview .adHolder img").click(function() {
		var yellowPageAd = yellowPageAdList.getYellowPageAdFromUrl( $(this).attr("src") );
		window.open(
			Constants.PROAD_MEDIA_DISPLAY_URL() + "?proAdMediaForFilmstripId=" + yellowPageAd.getProAdMediaForFilmstripId(),
			"lolwindow",
			"status=1, width=" + yellowPageAd.getPopUpWidth() + ", height=" + yellowPageAd.getPopUpHeight()
		);
	});

	// Build Coupon List
	$("#adletCoupon .adPreview .adHolder img").each(function() {

		var coupon = new Coupon();

		coupon.setUrl($(this).attr("src"));
		coupon.setProAdMediaForFilmstripId($(this).attr("proAdMediaForFilmstripId"));
		coupon.setHeight($(this).height());
		coupon.setWidth($(this).width());

		coupon.setAdjustedWidth( coupon.getWidth() > Constants.MAX_COUPON_DISPLAY_WIDTH() ? Constants.MAX_COUPON_DISPLAY_WIDTH() : coupon.getWidth() );
		coupon.setAdjustedHeight( $(this).height() / (coupon.getWidth() / coupon.getAdjustedWidth()) );

		coupon.setPopUpHeight( coupon.getHeight() + Constants.POPUP_COUPON_ADDITIONAL_HEIGHT() );
		coupon.setPopUpWidth( coupon.getWidth() + Constants.POPUP_COUPON_ADDITIONAL_WIDTH() );

		couponList.add(coupon);
	});

	// Set coupon previews.
	$(".proAdCoupon .adPreview .adHolder img").each(function() {

		var coupon = couponList.getCouponFromUrl( $(this).attr("src") );

		// Add hover event.
		$(this).hover(function() {
			$(this).parent().parent().parent().find(".fullAd img").attr("src", coupon.getUrl());
			$(this).parent().parent().parent().find(".fullAd img").css({"width" : coupon.getAdjustedWidth()});
		});

		// Set width of preview
		if (coupon.getWidth() > Constants.MAX_COUPON_PREVIEW_WIDTH()) {
			$(this).css({"width" : Constants.MAX_COUPON_PREVIEW_WIDTH() + "px"});
		}

	});

	// Set the height of the display to that of the largest ad and show the previews.
	$(".proAdCoupon .fullAd").css({"height" : couponList.getLargestHeight()});
	$(".proAdCoupon .adPreview .adHolder img").slideDown(Constants.TOGGLE_TIME());

	// Set Default Coupon.
	if ( $(".proAdCoupon .fullAd img").length > 0 ) {
		$(".proAdCoupon .fullAd img").attr("src", couponList.getDefaultCoupon().getUrl());
		$(".proAdCoupon .fullAd img").css({"width" : couponList.getDefaultCoupon().getAdjustedWidth()});
		$(".proAdCoupon .fullAd img").show(Constants.TOGGLE_TIME());
	}

	// Show Coupon in pop up.
	$(".proAdCoupon .fullAd img, .proAdCoupon .adPreview .adHolder img").click(function() {
		var coupon = couponList.getCouponFromUrl( $(this).attr("src") );
		window.open(
			Constants.PROAD_MEDIA_DISPLAY_URL() + "?proAdMediaForFilmstripId=" + coupon.getProAdMediaForFilmstripId(),
			"lolwindow",
			"status=1, width=" + coupon.getPopUpWidth() + ", height=" + coupon.getPopUpHeight()
		);
	});

	yellowPageAdsLoaded = true;
};

$(window).load(function() {

	if ( $("#adletYellowPageAds").is(":visible") ) {
		loadYellowPageAds();
	}
});
