﻿function popRules() {
    openWin("SweepsRules.aspx", 670, 520, true);
}
var popUpWin;
var popDefWidth = 500;
var popDefHeight = 500;
var lastWidth;
var lastHeight;

//Open URL in new window with fixed dimensions
function openWin(url, pixWidth, pixHeight, canScroll) {
    var winFeatures = "width=" + pixWidth + ",height=" + pixHeight + "," + getCentered(pixWidth, pixHeight) + ",resizable=no,scrollbars=";
    if (canScroll) {
        winFeatures += "yes";
    } else {
        winFeatures += "no";
    }
    if (popUpWin != null) {
        if (popUpWin.closed) {
            popUpWin = window.open(url, "popwin", winFeatures);
            popUpWin.focus();
        } else if ((lastWidth == pixWidth) && (lastHeight == pixHeight)) {
            popUpWin.location.href = url;
            popUpWin.focus();
        } else {
            popUpWin.close();
            var timeDelay = 0;
            if (navigator.userAgent.indexOf("Safari") != -1) {
                timeDelay = 1000;
            }
            var winWait = window.setTimeout('popUpWin = window.open("' + url + '", "popwin", "' + winFeatures + '");popUpWin.focus();', timeDelay);
        }
    } else {
        popUpWin = window.open(url, "popwin", winFeatures);
        if (!popUpWin) {
            var swfMovie = getObject("lufthansa");
            swfMovie.openRules();
        } else {
            popUpWin.focus();
        }
    }
    lastWidth = pixWidth;
    lastHeight = pixHeight;
}

//Get coordinates for centering window
function getCentered(popupWidth, popupHeight) {
    var indentNetscapeWidth = 'screenX=' + getIndent(popupWidth, false);
    var indentNetscapeHeight = 'screenY=' + getIndent(popupHeight, true);
    var indentMSIEWidth = 'left=' + getIndent(popupWidth, false);
    var indentMSIEHeight = 'top=' + getIndent(popupHeight, true);
    var centerCoordinates = indentNetscapeWidth + ',' + indentNetscapeHeight + ',' + indentMSIEWidth + ',' + indentMSIEHeight;
    return centerCoordinates;
}

//Determine top and left margins for window
function getIndent(popupDimension, isHeight) {
    if (isHeight) {
        return ((screen.availHeight - popupDimension) / 2);
    } else {
        return ((screen.availWidth - popupDimension) / 2);
    }
}

/* Browser Detection */
var isMac = false;
var isSafari = false;
var isFirefox = false;
var isMSIE = false;
var isMSIE6 = false;

if (navigator.userAgent.indexOf("Macintosh") != -1){
    isMac = true;
}

if (navigator.userAgent.indexOf("Safari") != -1){
	isSafari = true;
} else if (navigator.userAgent.indexOf("MSIE") != -1){
    isMSIE = true;
    if (navigator.userAgent.indexOf("MSIE 6.0") != -1){
	    isMSIE6 = true;
    }
} else if (navigator.userAgent.indexOf("Firefox") != -1){
	isFirefox = true;
}

/* Preloading and Rollovers */
var rolloverOn;
var rolloverOff;
var rolloverImages;
var topNav;
var topNavImages;
var topNavSection = -1;

function tnavSelect(currSection) {
    topNavSection = currSection;
    addLoadEvent(tnavSet);
}

function tnavSet() {
   /*topNav = document.getElementById("upper_nav");*/
    topNavImages = getElementsByTagClass("img", "rollover");
   if(topNavImages.length != 0){
        if (topNavSection != -1) {
            currNavImage = topNavImages[topNavSection];
            rolloverLight(currNavImage.id, currNavImage.navid);
            removeMouseListeners(currNavImage);
        }
    }
}


function preloadRollovers(roImages) {
    if (document.images) {
        var currMax = 0;
        if (roImages == null) {
            rolloverImages = getElementsByTagClass("img", "rollover").concat(getElementsByTagClass("input", "rollover"));
            rolloverOn = new Array(rolloverImages.length);
            rolloverOff = new Array(rolloverImages.length);
        } else {
            rolloverImages = roImages;
            currMax = rolloverOff.length;
        }
        for (var i = 0; i < rolloverImages.length; i++) {
            rolloverOff[i + currMax] = new Image();
            rolloverOn[i + currMax] = new Image();
            
            if (isMSIE6 && isPNG(rolloverImages[i])) {
                rolloverOff[i + currMax].src = getPNGSrc(rolloverImages[i].runtimeStyle.filter);
            } else {
                rolloverOff[i + currMax].src = rolloverImages[i].src;
            }

            rolloverOn[i + currMax].src = getRolloverSrc(rolloverOff[i + currMax].src);
            rolloverImages[i].navid = i + currMax;
            if ((rolloverImages[i].name.indexOf("$") == -1) && (rolloverImages[i].id.indexOf("slide") == -1)){
                rolloverImages[i].name = "rollover" + (i + currMax);
                rolloverImages[i].id = "rollover" + (i + currMax);
            }
            
            setMouseListeners(rolloverImages[i]);
        }
    }
}

function setMouseListeners(roImage){
    roImage.onmouseover = function() {
    rolloverLight(this.id, this.navid);
    };
    roImage.onmouseout = function() {
        rolloverDim(this.id, this.navid);
    };
}

function removeMouseListeners(roImage){
    roImage.onmouseover = function() {};
    roImage.onmouseout = function() {};
}

function getElementsByTagClass(tagName, className) {
    var matches = new Array();
    var matchesByTag = document.getElementsByTagName(tagName);
    for (var i = 0; i < matchesByTag.length; i++) {
        if (matchesByTag[i].className.indexOf(className) != -1) {
            matches.push(matchesByTag[i]);
        }
    }
    return matches;
}

function getSelectedSrc(offSrc) {
    var regEx;
    if (offSrc.indexOf("_on.png") == -1){
        regEx = new RegExp("_off.png");
    } else {
        regEx = new RegExp("_on.png");
    }
    return offSrc.replace(regEx, "_down.png");
}

function getRolloverSrc(offSrc) {
    var regEx = new RegExp("_off.png");
    return offSrc.replace(regEx, "_on.png");
}

function getRolloutSrc(offSrc) {
    var regEx = new RegExp("_on.png");
    return offSrc.replace(regEx, "_off.png");
}

function rolloverLight(imgID, navID) {
   if (document.images) {
		if (isMSIE6 && isPNG(document.getElementById(imgID))){
			document.getElementById(imgID).runtimeStyle.filter = getRolloverSrc(document.getElementById(imgID).runtimeStyle.filter);
		} else {
            document.getElementById(imgID).src = rolloverOn[navID].src;
		}
    }
}

function rolloverDim(imgID, navID) {
    if (document.images) {
		if (isMSIE6 && isPNG(document.getElementById(imgID))){
			document.getElementById(imgID).runtimeStyle.filter = getRolloutSrc(document.getElementById(imgID).runtimeStyle.filter);
		} else {
            document.getElementById(imgID).src = rolloverOff[navID].src;
        }
    }
}

function isPNG(img){
    return ((img.src.toLowerCase().indexOf(".png") != -1) || (img.runtimeStyle.filter.toLowerCase().indexOf(".png") != -1));
}

function getPNGSrc(pngFilter) {
    var startSearch = "AlphaImageLoader(src='";
    var start = pngFilter.indexOf(startSearch) + startSearch.length;
    var end = pngFilter.indexOf("'", start);
    return pngFilter.substring(start, end);
}

//Gets Parameter Values from QueryString
function getParamValue(paramName){
	paramName += "=";
	var paramLength=paramName.length;
	var start = -1;
	if (location.search.indexOf("?" + paramName) != -1){
		start = location.search.indexOf("?" + paramName) + 1;
	} else if (location.search.indexOf("&" + paramName) != -1){
		start = location.search.indexOf("&" + paramName) + 1;
	}
	if (start != -1){
		if (location.search.indexOf("&",start + 1) != -1){
			tempValue = location.search.substring(start + paramLength, location.search.indexOf("&",start + 1));
		}
		else {
			tempValue = location.search.substring(start + paramLength);
		}
		return tempValue;
	}
	else {
		return null;
	}
}

/* AddThis Configuration */
//var addthis_offset_top = -150;
var addthis_offset_left = 50;
var addthis_options = 'facebook, twitter, myspace, digg'; // List of Social Media Outlets
var addthis_header_color = "#FFFFFF"; // AddThis Popup Header Text Color
var addthis_header_background = "#7da1d6"; // AddThis Popup Header Background Color
var addthis_localize = {
    share_caption: "<strong>Share</strong>"
} // AddThis Popup Header Text
var addthis_share = {
} // AddThis URL to Share
var addthis_config = {
   data_ga_tracker: "pageTracker"
} // AddThis Google Analytics Integration

// Enables Assignment of Multiple window.onload Functions
function addLoadEvent(func) {
    var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
} else {
        window.onload = function() {   
			if (oldonload) {   
				oldonload();   
			}   
			func();   
		}   
	}   
}   

/* Begin Google Tracking Code */

function onclickFix(theLink, trackName) {
    pageTracker._trackPageview(trackName);
    if (theLink.target.toLowerCase() == "_blank") {
        return true;
    } else {
        setTimeout("location.href = '" + theLink.href + "'", 500);
        return false;
    }
}


addLoadEvent(preloadRollovers);

function isAutoTrackPage() {
    var excludedPages = [""];
    var pageURL = location.pathname.toLowerCase();
    for (var i = 0; i < excludedPages.length; i++) {
        if (pageURL.indexOf(excludedPages[i].toLowerCase()) != -1) {
            return false;
        }
    }
    return true;
}

/* Store Coupons Code */

function getStoreURL(listName) {
    
    var storeList = document.getElementById(listName);
    var strStoreName = "";
    var storeURL = "";

    if (storeList != null) {
        if (storeList.selectedIndex == 0) {
            var choiceText = storeList.style.color = '#ff0000';
        }
        else {

            strStoreName = storeList.options[storeList.selectedIndex].value;

            switch (strStoreName) {
                case "Kroger":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Kroger/Source/PURINA";
                    break;
                case "Ralphs":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Ralphs/Source/PURINA";
                    break;
                case "Fred Meyer":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Fred_Meyer/Source/PURINA";
                    break;
                case "King Soopers":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/King_Soopers/Source/PURINA";
                    break;
                case "City Market":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/City_Market/Source/PURINA";
                    break;
                case "Frys":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Frys/Source/PURINA";
                    break;
                case "Smiths":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Smiths/Source/PURINA";
                    break;
                case "Quality Food Centers":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/QFC/Source/PURINA";
                    break;
                case "Dillons":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Dillons/Source/PURINA";
                    break;
                case "Bakers":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Bakers/Source/PURINA";
                    break;
                case "Gerbes":
                    storeURL = "http://www.softcoin.com/Sites/Kroger_ECoupons/Page/HomePage/Retailer/Gerbes/Source/PURINA";
                    break;
            }

            window.open(storeURL, 'coupons', 'scrollbars=yes');
        }
    }
}


/* Home Page Slide Display Functions */
var slideshow_navID = 0;

var counter = 1;

function slideSwitch() {
    var $active = $('#slideshow .jj.active');
    if ($active.length == 0) $active = $('#slideshow .jj:last');

    // use this to pull the divs in the order they appear in the markup
    var $next = $active.next().length ? $active.next()
        : $('#slideshow .jj:first');

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });


    // Set nav button to on state if we are on that slide, set other nav itesm to off state
    counter = counter + 1;
	hpSlideNavSet(counter);
    if (counter >= 3) {
        counter = 0;
    }
}

function startHPSlides() {
	if (document.getElementById("slideshow") != null){
		var currNavImage = document.getElementById('slideImg1');
		rolloverLight(currNavImage.id, currNavImage.navid);
		removeMouseListeners(currNavImage);
		slideshow_navID = setInterval("slideSwitch()", 5000);
	}
};

function stopHPSlides() {
    window.clearInterval(slideshow_navID);
    
}

function hpSlideSet(currSection) {
   
    // turn off automatic slide sequence since we selected a nav item
    // and want to navigate manually
    window.clearInterval(slideshow_navID)
    hpSlideNavSet(currSection);

    // now bring the correct slide to the front depending on what nav item was clicked
    // and hide the others
    var slideToSet = 'slide' + currSection;
    slide = document.getElementById(slideToSet);

    var $active = $('#slideshow .jj.active');

    if ($active.length == 0) $active = $('#slideshow .jj:last');

    var $next = $('#' + slideToSet);

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
    .addClass('active')
    .animate({ opacity: 1.0 }, 1000, function() {
        $active.removeClass('active last-active');
    });
}

function hpSlideNavSet(currSection){
    for (var i = 1; i <= 3; i++) {
        // if it is not the one we are on, turn it off
        if (i != currSection) {
			var currNavImage = document.getElementById('slideImg' + i);
			rolloverDim(currNavImage.id, currNavImage.navid);
			setMouseListeners(currNavImage);
        }
        // otherwise, turn it on
        else {
			currNavImage = document.getElementById('slideImg' + i);
            rolloverLight(currNavImage.id, currNavImage.navid);
            removeMouseListeners(currNavImage);
         }
    }
}

/*addLoadEvent(startHPSlides);*/


