
$(function() {

    //NO JS - prevent user from registering (register button is hidden by default)
    $('#noJavascript').hide();
    
    //COOKIES DISABLED TEST
    document.cookie = "test";
    if(document.cookie === "" && !$('#noCookieContainer').size()) {
        window.location = BASE_URL + '/index/no-cookies';
    } 

    $('#registerPage').show();
    
    // Track PDF Downloads
    $('a[rel~=pdf]').live('click', function() {
        var url = $(this).attr('href');

        // Create the Google Analytics queue if it doesn't exist.
        if ('undefined' == typeof _gaq) {
            _gaq = []; // Intentional omission of 'var'. This need to be in window scope.
        }

        _gaq.push(['_trackEvent', 'PDF Downloads', url]);
        $(this).attr('target', '_blank'); // Open in a new window.
    });


/*
 * Code Entry
 */
    $('.sweepstakesRulesFirst').fancybox({});
	$('.sweepstakesRulesSecond').fancybox({});

	// Fancybox
	$('#codeEntryForm #codeSubmit').fancybox({
		width: 797,
		height: 520,
		padding: 0,
		margin: 0,
		scrolling: 'no',
		onClosed: function() {
			$('#codeModal #codeContent').html('Looking-Up Code<br/>Please Wait');
		}
	});

	// Submit Code and Load Response
	$('#codeEntryForm #codeSubmit').click(function() {
		var code = $('#codeEntryForm #codeInput').val();
			var langMod = "";
			if (LANGUAGE != "") {
				langMod = "/" + LANGUAGE;
			}
                        
		$.post(BASE_URL + langMod + '/codes/add/', { codeInput: code }, function(response) {

			$('#codeModal #codeContent').html(response);

		});
	});



/*
 * Albertsons
 */
	$('#bannerSelectOpen').fancybox({
		autoDimensions: false,
		height: 187,
		width: 480,
		onStart: function() {
			_gaq.push(['_trackPageview','/choose-your-store']);
		},
		onClosed: function() {
			$('#bannerSelectModel #bannerSelectContent').html('Loading<br />Please Wait');
		}
	});


    $('.dialogPopupTrigger').fancybox({
                    autoDimensions: false,
                    height: 45,
                    width: 500
    });
     $(".dialogPopupTrigger").trigger('click');


/*
 * Refer Friend
 */
         var referFriendWidth = 672;
         var referFriendHeight = 497;

        if($('.referFriendOpen').attr('data-width')) {
            referFriendWidth = parseInt($('.referFriendOpen').attr('data-width'));
            $('#referModal #referContent').css('width', (referFriendWidth - 40));
            $('#referModal #referContent').css('background-image', 'none');
        }
        if($('.referFriendOpen').attr('data-height')) {
            referFriendHeight = parseInt($('.referFriendOpen').attr('data-height'));
            $('#referModal #referContent').css('height', (referFriendHeight - 40));
            $('#referModal #referContent').css('background-image', 'none');
        }

	// Fancybox
	$('.referFriendOpen').fancybox({
		autoDimensions: false,
		height: referFriendHeight,
		width: referFriendWidth,
		onStart: function() {
			var langMod = "";
			if (LANGUAGE != "") {
				langMod = "/" + LANGUAGE;
			}
			$('#referModal #referContent').load(BASE_URL + langMod + '/register/refer-modal/');
		},
		onClosed: function() {
			$('#referModal #referContent').html('Loading<br />Please Wait');
		}
	});

        //have popup automatically appear when #refer is in the url
	var url = window.location.href;
	if(url.indexOf('#refer') > -1) {
		  $('.referFriendOpen').trigger('click');
	}


	// Fancybox
	$('#fbRules').fancybox({
		autoDimensions: false,
		height: 497,
		width: 672,
		onStart: function() {
			$('#rulesModal #rulesContent').load(BASE_URL + '/index/fb-rules/');
		}
		,
		onClosed: function() {
			$('#rulesModal #rulesContent').html('Loading<br />Please Wait');
		}
	});

	$('#esTermsRules').fancybox({
		autoDimensions: false,
		height: 497,
		width: 672,
		onStart: function() {
			$('#esTermsModal #esTermsContent').load(BASE_URL + '/index/es-terms/');
		}
		,
		onClosed: function() {
			$('#esTermsModal #esTermsContent').html('Loading<br />Please Wait');
		}
	});

	$('#esPrivacyRules').fancybox({
		autoDimensions: false,
		height: 497,
		width: 672,
		onStart: function() {
			$('#esPrivacyModal #esPrivacyContent').load(BASE_URL + '/index/es-privacy/');
		}
		,
		onClosed: function() {
			$('#esPrivacyModal #esPrivacyContent').html('Loading<br />Please Wait');
		}
	});

	$('#esEnglishWarningRules').fancybox({
		autoDimensions: false,
		height: 118,
		width: 668,
		onStart: function() {
			$('#esEnglishWarningModal #esEnglishWarningContent').load(BASE_URL + '/index/es-english-warning/');
		}
		,
		onClosed: function() {
			$('#esEnglishWarningModal #esEnglishWarningContent').html('Loading<br />Please Wait');
		}
	});

	$('#sizzleLink').fancybox();

	//$('#rulesModal #rulesContent').show();



/*
 * Where do I find codes
 */
	// Open
	$('#codeEntryForm #findCodes').click(function() {
		if(!$.browser.msie)
			$('#codeEntryForm #findCodesModal').fadeIn();
		else
			$('#codeEntryForm #findCodesModal').show();
	});

	// Close
	$('#findCodesModal .close').click(function() {
		if(!$.browser.msie)
			$('#codeEntryForm #findCodesModal').fadeOut();
		else
			$('#codeEntryForm #findCodesModal').hide();
	});


	// Select Banner
	$('#selectBannerOpen').fancybox();

                    $('.pBanner').click(function(event) {
                            
                            var tokens = $(this).attr('href').split( '/' );
                       
                            // First parse the URL to find the bID parameter.
                            for ( var i=0; i < tokens.length; i++ ) {

                                    if ( 'bannerID' == tokens[i] || 'bID' == tokens[i]) {

                                            // The banner id is the url segment after the 'bID'
                                            // key name.
                                            var id = tokens[i+1];

                                            // If there is a comma in the id, we have multiple
                                            // banners.  Open the selector modal so we can
                                            // choose one.
                                            if ( -1 != id.indexOf(',') ) {
                                                    $('#bannerSelectOpen').attr('href', BASE_URL + '/index/select-banner/bannerID/' + id ).click();
                                                    event.preventDefault();
                                            }

                                            break;
                                    }
                            }
                    });

});


/*
 * Login Notification
 */
function loginNotificationShow() {

	hideNotifications();
	if(!$.browser.msie)
		$('#loginNotification').fadeIn();
	else
		$('#loginNotification').show();

}

/*
 * Login Notification
 */
function couponNotificationShow() {
	hideNotifications();
	if(!$.browser.msie)
		$('#couponNotification').fadeIn();
	else
		$('#couponNotification').show();
}

function hideNotifications() {
	loginNotificationHide();
	couponNotificationHide();

}

function loginNotificationHide() {
	if(!$.browser.msie)
		$('#loginNotification').fadeOut();
	else
		$('#loginNotification').hide();
}

function couponNotificationHide() {
	if(!$.browser.msie)
		$('#couponNotification').fadeOut();
	else
		$('#couponNotification').hide();
}

/*
 * Go To Offers
 */
function goToOffers() {

	// Check out bannerID Global (would be better if we had a GLL object or something)
	if (bannerID == null) {
		openBannerSelector();
		return false;
	}

	// Banner ID in session, OK to send to the regular link
	return true;

}


/*
 * Open the banner selector modal
 */
function openBannerSelector() {
	$('#selectBannerOpen').trigger('click');
}


/*
 * Submit Enter Functionality
 */
	function submitCode() {
		$('#codeSubmit').trigger('click');
	}


