var g_ie6 = 0;
var g_clog = new Array();
g_clog[0] = 'images/provenPix_clog_0static_img.jpg';
g_clog[1] = 'images/provenPix_clog_1enhance_img.jpg';
g_clog[2] = 'images/provenPix_clog_2fatigue_img.jpg';
g_clog[3] = 'images/provenPix_clog_3pressure_img.jpg';
g_clog[4] = 'images/provenPix_clog_4footbed_img.jpg';
g_clog[5] = 'images/provenPix_clog_5arch_img.jpg';
g_clog[6] = 'images/provenPix_clog_6strap_img.jpg';

var g_flip = new Array();
g_flip[0] = 'images/provenPix_flip_0static_img.jpg';
g_flip[1] = 'images/provenPix_flip_1enhance_img.jpg';
g_flip[2] = 'images/provenPix_flip_2fatigue_img.jpg';
g_flip[3] = 'images/provenPix_flip_3pressure_img.jpg';
g_flip[4] = 'images/provenPix_flip_4footbed_img.jpg';
g_flip[5] = 'images/provenPix_flip_5arch_img.jpg';

var g_slide = new Array();
g_slide[0] = 'images/provenPix_slide_0static_img.jpg';
g_slide[1] = 'images/provenPix_slide_1enhance_img.jpg';
g_slide[2] = 'images/provenPix_slide_2fatigue_img.jpg';
g_slide[3] = 'images/provenPix_slide_3pressure_img.jpg';
g_slide[4] = 'images/provenPix_slide_4footbed_img.jpg';
g_slide[5] = 'images/provenPix_slide_5arch_img.jpg';

var g_prod = '';

$(function() {
    if (typeof document.body.style.maxHeight === "undefined") {
        g_ie6 = 1;
    }
    // email updates
    $('#emailUpsForm').validate({
        errorLabelContainer: '#emailUpsVal',
        submitHandler: function(form) {
            $('#emailUpsReturn').fadeIn('fast');
            $(form).ajaxSubmit({
                target: '#emailUpsReturn'
            });
        }
    });
    $('#sendToFriendForm').validate({
        errorLabelContainer: '#sendToFriendVal',
        submitHandler: function(form) {
            $('#sendToFriendReturn').fadeIn('fast');
            $(form).ajaxSubmit({
                target: '#sendToFriendReturn'
            });
        }
    });
    $('#testimonialForm').validate({
        errorLabelContainer: '#testimonialVal',
        submitHandler: function(form) {
            $('#testimonialReturn').fadeIn('fast');
            $(form).ajaxSubmit({
                target: '#testimonialReturn'
            });
        }
    });
    // background video
    $('#video').flash({
        src: 'video_adam.swf',
        width: 448,
        height: 284,
        wmode: 'opaque'
    },
	{ version: 8 }
	);
    $('img.v2, img.v3').fadeTo('fast', 0.5);
    $('#bgVid img').each(function(i) {
        $(this).click(function() {
            if ($(this).hasClass('v1') == true && $(this).hasClass('active') != true) {
                $('img.v2').removeClass('active').fadeTo('fast', 0.5);
                $('img.v3').removeClass('active').fadeTo('fast', 0.5);
                $('img.v1').addClass('active').fadeTo('fast', 0.99);
                $('#video').empty().flash({
                    src: 'video_adam.swf',
                    width: 448,
                    height: 284,
                    wmode: 'opaque'
                },
				{ version: 8 }
				);
            } else if ($(this).hasClass('v2') == true && $(this).hasClass('active') != true) {
                $('img.v1').removeClass('active').fadeTo('fast', 0.5);
                $('img.v3').removeClass('active').fadeTo('fast', 0.5);
                $('img.v2').addClass('active').fadeTo('fast', 0.99);
                $('#video').empty().flash({
                    src: 'video_matty.swf',
                    width: 448,
                    height: 284,
                    wmode: 'opaque'
                },
				{ version: 8 }
				);
            } else if ($(this).hasClass('v3') == true && $(this).hasClass('active') != true) {
                $('img.v1').removeClass('active').fadeTo('fast', 0.5);
                $('img.v2').removeClass('active').fadeTo('fast', 0.5);
                $('img.v3').addClass('active').fadeTo('fast', 0.99);
                $('#video').empty().flash({
                    src: 'video_testim.swf',
                    width: 448,
                    height: 284,
                    wmode: 'opaque'
                },
				{ version: 8 }
				);
            } else {
            }
        });
    });
    // proven pix
    $('#mapClog area').each(function(i) {
        $(this).hover(function() {
            num = $(this).attr('class');
            prepImage(g_clog, 'clog', num);

        }, function() {
            hideImage();
        });
    });
    $('#mapFlip area').each(function(i) {
        $(this).hover(function() {
            num = $(this).attr('class');
            prepImage(g_flip, 'flip', num);
        }, function() {
            hideImage();
        });
    });
    $('#mapSlide area').each(function(i) {
        $(this).hover(function() {
            num = $(this).attr('class');
            prepImage(g_slide, 'slide', num);
        }, function() {
            hideImage();
        });
    });

    $('#controls li.c1 a').click(function() {
        if ($(this).hasClass('active')) {

        } else {
            $('#provenPix div.active').fadeOut('fast', function() {
                $(this).removeClass('active');
                $('#controls li a.active').removeClass('active');
            });
            $('#clog').fadeIn('fast', function() {
                $(this).addClass('active');
                $('#controls li.c1 a').addClass('active');
            });
        }
    });

    $('#controls li.c2 a').click(function() {
        if ($(this).hasClass('active')) {

        } else {
            $('#provenPix div.active').fadeOut('fast', function() {
                $(this).removeClass('active');
                $('#controls li a.active').removeClass('active');
            });
            $('#flip').fadeIn('fast', function() {
                $(this).addClass('active');
                $('#controls li.c2 a').addClass('active');
            });
        }
    });

    $('#controls li.c3 a').click(function() {
        if ($(this).hasClass('active')) {

        } else {
            $('#provenPix div.active').fadeOut('fast', function() {
                $(this).removeClass('active');
                $('#controls li a.active').removeClass('active');
            });
            $('#slide').fadeIn('fast', function() {
                $(this).addClass('active');
                $('#controls li.c3 a').addClass('active');
            });
        }
    });

    $('map *').tooltip({
        delay: 0,
        track: true,
        showURL: false,
        fixPNG: true,
        top: 0,
        left: 20
        /*bodyHandler: function() {
        c = 'Size ';
        c = c + $(this).siblings('span').text();
        c = c + ' is currently out of stock. You can get an email notice when it becomes available - just select this size and fill in your email address.';
        return c; 
        }*/
    });

    //sweepstakes
    $.getFeed({
        url: '/prepair/process/load/blogPREPAIR_RSS.php',
        success: function(feed) {
            var html = '';
            for (var i = 0; i < feed.items.length && i < 7; i++) {
                var item = feed.items[i];
                html += '<div class="blogitem">';
                html += '<div class="blogtitle">'
			+ '<a href="' + item.link + '" target="_blank">' + item.title + '</a>'
			+ '</div>';
                html += '<div class="blogtext">'
			+ item.description
			+ '</div>';
                html += '</div>';
            }
            $('#sweepNews div.content').append(html);
        }
    });

});

function prepImage(array, prod, num) {
    g_prod = prod;
    target = '#' + prod;
    $('<img>').appendTo(target).attr('src', array[num]).addClass('layer');
    $(target + ' img.layer').onImagesLoad({
        itemCallback: showImage
    });
}

function showImage() {
    $('#' + g_prod + ' img.layer').fadeIn('fast');
}

function hideImage() {
    $('#' + g_prod + ' img.layer').fadeOut('fast', function() { $(this).remove(); });
}