var duration = 300;
var sliding = false;

$(document).ready(function () {
    if (hasFlash()) {
        $('#connectivityToolbar').css('display', '');
    } else {
        $('#contactLink').css('display', 'block');
        $('#facebookLink').css('display', 'block');
    }

    $("ul.topnav li").hover(function () {
        $(this).addClass('over');

        //Following events are applied to the subnav itself (moving subnav up and down)
        $(this).find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click

        $(this).hover(function () { //notice the first function passed is the "onhover function" and the second is the "hoverout function"
        }, function () {
            $(this).removeClass('over');
            $(this).find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
        });
    });

    $("ul.subnav li").hover(function () {
        $(this).addClass('over');
    }, function () {
        $(this).removeClass('over');
    });

    $("ul.subnav a").hover(function () {
        $(this).stop().animate({ paddingLeft: '14px' }, 300, function () { });
    }, function () {
        $(this).stop().animate({ paddingLeft: '8px' }, 300, function () { });
    });

    $('#slideshow')
	.cycle({
	    fx: 'fade',
	    activePagerClass: 'activeSlide',
	    pager: '#pager',
	    pagerAnchorBuilder: function (idx, slide) {
	        return "<span class='pageSlide'></span>";
	    }
	});


    $('#slideshow .slide').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        fade: 350
    });

    var thumbnails = $('div.results img');

    var options = {
        opacity: 70,
        closeHTML: '<a href="#" title="Close" class="modal-close">Close</a>',
        onOpen: openModal,
        onClose: closeModal
    };

    thumbnails.click(function () {
        $.modal('<div><img src="' + this.src + '" title="' + this.title + '" /></div>', options);
    });

    var trainerImages = $('div.trainer img');
    //    trainerImages.click(function() {
    //        $.modal('<div><img src="ImageHandler.ashx?Thumb=0&ID=' + this.id + '" /></div>', options);
    //    });
});

function showCreditCardMessage() {
    var cookieName = 'messageDisplayed';

    if (readCookie(cookieName) != 'true') {
        createCookie(cookieName, 'true', 365);
        newmessage = $('#newmessage');
        setTimeout(showMessage, 800);
    }
}

var newmessage;

function showMessage() {
    newmessage.show();
    newmessage.animate({ top: 55 }, function () {
        setTimeout(hideMessage, 6000);
    });
}

function hideMessage() {
    if (isIE()) {
        newmessage.hide();
    } else {
        newmessage.animate({ top: 100, opacity: 0 });
    }
}

function isIE() {
    return (navigator.appName == 'Microsoft Internet Explorer');
}

function hasFlash() {
    if ((navigator.appName == "Microsoft Internet Explorer" &&
        navigator.appVersion.indexOf("Mac") == -1 &&
        navigator.appVersion.indexOf("3.1") == -1) ||
        (navigator.plugins && navigator.plugins["Shockwave Flash"]) ||
        navigator.plugins["Shockwave Flash 2.0"]) {
        return true;
    }
    return false;
}

function contactClick() {
    window.location = 'Contact.aspx?Type=1';
}

function facebookClick() {
	window.open('http://www.facebook.com/Mr.Ms.NaturalMinnesota', '_blank');
}

function openModal(dialog) {
    dialog.overlay.fadeIn(duration, function() {
        dialog.container.show(duration, function() {
            dialog.data.slideDown(duration);
        });
    });
}

function closeModal(dialog) {    
    dialog.data.slideUp(duration, function() {
        dialog.container.hide(duration, function() {
            dialog.overlay.fadeOut(duration, function() {
                $.modal.close();
            });
        });
    });
}

function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = '; expires=' + date.toGMTString();
    } else { var expires = ''; }

    document.cookie = name + '=' + value + expires + '; path=/';
}

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;
}
