$(document).ready(function() {

    // Optional message for form fields
    // --------------------------------
    $(".OptionalInput").each(function() {
        $(this).val($(this).attr("title"));
        $(this).css({ color: "#cccccc" });
    });

    $(".OptionalInput").focus(function() {
        if ($(this).val() == $(this).attr("title")) {
            $(this).val("");
            $(this).css({ color: "#666666" });
        }

    }).blur(function() {
        if ($(this).val() == "") {
            $(this).val($(this).attr("title"));
            $(this).css({ color: "#cccccc" });
        }
    });

    // Podcasts & Articles
    // --------------------------------
    $("a#ShowMorePodcasts").click(function() {
        var SpanValue = $("#viewMoreLess").html();      
        $("#MorePodcasts").slideToggle("slow");

        if (SpanValue == "more") {
            $("#viewMoreLess").html("fewer");
        } else {
            $("#viewMoreLess").html("more");
        }        

    });

    $("a.ArticleTitle").click(function() {
        var CurrentClass = $(this).attr("class")

        if (CurrentClass == "ArticleTitle") {
            $(this).removeClass("ArticleTitle");
            $(this).addClass("ArticleTitleOff");
        } else {
            $(this).removeClass("ArticleTitleOff");
            $(this).addClass("ArticleTitle");
        };

        $(this).parent().next(".ArticleBody").slideToggle("slow");
    });

	
	
	// Tooltips
	// --------------------------------
	$(".ToolTip span").hover(function() {
		$(this).next("div").stop(true, true).animate({opacity: "show", bottom: "25"}, "fast");
		}, function() {
		$(this).next("div").animate({opacity: "hide", bottom: "10"}, "fast");
	});
	//---
	
	

	// Responsive JS Check
    // --------------------------------
    function setResponsiveJS() {
        var zNum = $("#Responsive").height();

        if (zNum == "1") {
            // Desktop
            
            // Hide/Show Flash/NonFlash
            if (swfobject.hasFlashPlayerVersion("1")) {
			 $('.NoFlash').hide();
			 $('.Flash').show();
			} 
			else {
			 $('.NoFlash').show();
			 $('.Flash').hide();
			}
        }
        else if (zNum == "2") {
            // Tablet
            
            // Hide/Show Flash/NonFlash
            if (swfobject.hasFlashPlayerVersion("1")) {
			 $('.NoFlash').hide();
			 $('.Flash').show();
			} 
			else {
			 $('.NoFlash').show();
			 $('.Flash').hide();
			}
        }
        else if (zNum == "3") {
            // Landscape
            
            // Hide/Show Flash/NonFlash
            $('.NoFlash').show();
			$('.Flash').hide();
            
            // Home
        	$('body .StoreLocatorMobile p').replaceWith('<p>You can always find your closest store by calling <strong><a href="tel:+1-800-378-2337">800-378-BEDS (2337)</a></strong></p>');
        	$('body.SC .StoreLocatorMobile p').replaceWith('<p>You can always find your closest store by calling <strong><a href="tel:+1-888-887-5337">888-88-SLEEP (75337)</a></strong></p>')
        	
        	// Slider image change
		    $("#HomePromo .flexslider img").each(function(){
        		var imagepath = $(this).attr('src');
	            imagepath = imagepath.replace("-mobile.jpg",".jpg");
	            $(this).attr('src', imagepath);
      		});
        }
        else if (zNum == "4") {
            // Portrait
            
            // Hide/Show Flash/NonFlash
            $('.NoFlash').show();
			$('.Flash').hide();
            
            // Home
            $('body .StoreLocatorMobile p').replaceWith('<p>Find your closest store <strong><a href="tel:+1-800-378-2337">800-378-BEDS (2337)</a></strong></p>');
            $('body.SC .StoreLocatorMobile p').replaceWith('<p>Find your closest store <strong><a href="tel:+1-888-887-5337">888-88-SLEEP (75337)</a></strong></p>');
            
            // Slider image change
		    $("#HomePromo .flexslider img").each(function(){
        		var imagepath = $(this).attr('src');
        		if (imagepath.toLowerCase().indexOf("-mobile.jpg") <= 0){
        			imagepath = imagepath.replace(".jpg","-mobile.jpg");
		            $(this).attr('src', imagepath);
        		}
      		});
            
                        
        }
    }

    $(window).resize(function() {
        setResponsiveJS();
    });

    $(window).load(function() {
        setResponsiveJS();
    });
	//---
	
	
	
	// Mobile Navigation
	// --------------------------------
	$("#NavButton").click(function() {
		$("#NavButton a").toggleClass("Active");
		$("#Nav").slideToggle("fast");
	});

	$("#FooterNavMore").click(function() {
		$("#FooterNavLinks").slideToggle('fast');
	});
	
	if (!navigator.userAgent.match(/Android/i) && !navigator.userAgent.match(/webOS/i) && !navigator.userAgent.match(/iPhone/i) && !navigator.userAgent.match(/iPod/i) && !navigator.userAgent.match(/MSIE/i)) {
    	
	 	$(window).resize(function() {
	    	if ($(window).width() > 767) {
	    		$("#Nav").show();
	    	}
	    });
	};
	//---
	



});  // END Doc

if (typeof jQuery != 'undefined') {
    jQuery(document).ready(function($) {
        var filetypes = /\.(zip|exe|pdf|doc*|xls*|ppt*|mp3)$/i;
        $('a').each(function() {
            var href = $(this).attr('href');
            if (href && (href.match(/^https?\:/i)) && (!href.match(document.domain))) {
                $(this).click(function() {
                    var extLink = href.replace(/^https?\:\/\//i, '');
                    _gaq.push(['_trackEvent', 'External', 'Click', extLink]);
                    if ($(this).attr('target') != '_blank') {
                        setTimeout(function() { location.href = href; }, 200);
                        return false;
                    }
                });
            }
            else if (href && href.match(/^mailto\:/i)) {
                $(this).click(function() {
                    var mailLink = href.replace(/^mailto\:/i, '');
                    _gaq.push(['_trackEvent', 'Email', 'Click', mailLink]);
                });
            }
            else if (href && href.match(filetypes)) {
                $(this).click(function() {
                    var extension = (/[.]/.exec(href)) ? /[^.]+$/.exec(href) : undefined;
                    var filePath = href;
                    _gaq.push(['_trackEvent', 'Download', 'Click-' + extension, filePath]);
                    if ($(this).attr('target') != '_blank') {
                        setTimeout(function() { location.href = href; }, 200);
                        return false;
                    }
                });
            }
        });
    });
}

var sourceParam = 'utm_source';
var gaCustomSlot = 1;

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function checkIfNewGASession() {
    //check for existence of a __utmc cookie or an expired __utmb cookie
    if (readCookie('__utmc') == null || readCookie('__utmb') == null)
        return true;
    else
        return false;
}

function checkForCampaignParams() {
    //check for querystring or hash that matches this sourceParam if referrer is not empty
    if (document.referrer != '') {
        if (document.location.href.indexOf('?' + sourceParam + '=') != -1)
            return true;
        else if (document.location.href.indexOf('&' + sourceParam + '=') != -1)
            return true;
        else if (document.location.href.indexOf('#' + sourceParam + '=') != -1)
            return true;
        else
            return false;
    }
    else
        return false;
}

function getReferrerData() {
    //grab and truncate referrer data. GA limits custom variable to 64 bytes
    return document.referrer.replace(/^https?\:\/\//i,'').replace('/','!').substr(0, 61);
}
